Skip to content

Commit

Permalink
Docs on datasette.client for tests, closes #1830
Browse files Browse the repository at this point in the history
Also covers ds.client.actor_cookie() helper
  • Loading branch information
simonw committed Jan 10, 2024
1 parent 48148e6 commit 7506a89
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
28 changes: 28 additions & 0 deletions docs/testing_plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,34 @@ This method registers any :ref:`plugin_hook_startup` or :ref:`plugin_hook_prepar

If you are using ``await datasette.client.get()`` and similar methods then you don't need to worry about this - Datasette automatically calls ``invoke_startup()`` the first time it handles a request.

.. _testing_datasette_client:

Using datasette.client in tests
-------------------------------

The :ref:`internals_datasette_client` mechanism is designed for use in tests. It provides access to a pre-configured `HTTPX async client <https://www.python-httpx.org/async/>`__ instance that can make GET, POST and other HTTP requests against a Datasette instance from inside a test.

I simple test looks like this:

.. literalinclude:: ../tests/test_docs.py
:language: python
:start-after: # -- start test_homepage --
:end-before: # -- end test_homepage --

Or for a JSON API:

.. literalinclude:: ../tests/test_docs.py
:language: python
:start-after: # -- start test_actor_is_null --
:end-before: # -- end test_actor_is_null --

To make requests as an authenticated actor, create a signed ``ds_cookie`` using the ``datasette.client.actor_cookie()`` helper function and pass it in ``cookies=`` like this:

.. literalinclude:: ../tests/test_docs.py
:language: python
:start-after: # -- start test_signed_cookie_actor --
:end-before: # -- end test_signed_cookie_actor --

.. _testing_plugins_pdb:

Using pdb for errors thrown inside Datasette
Expand Down
41 changes: 39 additions & 2 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""
Tests to ensure certain things are documented.
"""
from click.testing import CliRunner
from datasette import app, utils
from datasette.cli import cli
from datasette.app import Datasette
from datasette.filters import Filters
from pathlib import Path
import pytest
Expand Down Expand Up @@ -102,3 +101,41 @@ def documented_fns():
@pytest.mark.parametrize("fn", utils.functions_marked_as_documented)
def test_functions_marked_with_documented_are_documented(documented_fns, fn):
assert fn.__name__ in documented_fns


# Tests for testing_plugins.rst documentation


# -- start test_homepage --
@pytest.mark.asyncio
async def test_homepage():
ds = Datasette(memory=True)
response = await ds.client.get("/")
html = response.text
assert "<h1>" in html


# -- end test_homepage --


# -- start test_actor_is_null --
@pytest.mark.asyncio
async def test_actor_is_null():
ds = Datasette(memory=True)
response = await ds.client.get("/-/actor.json")
assert response.json() == {"actor": None}


# -- end test_actor_is_null --


# -- start test_signed_cookie_actor --
@pytest.mark.asyncio
async def test_signed_cookie_actor():
ds = Datasette(memory=True)
cookies = {"ds_actor": ds.client.actor_cookie({"id": "root"})}
response = await ds.client.get("/-/actor.json", cookies=cookies)
assert response.json() == {"actor": {"id": "root"}}


# -- end test_signed_cookie_actor --

0 comments on commit 7506a89

Please sign in to comment.