Skip to main content

Utilities to help write tests for Datasette plugins and applications

Project description

datasette-test

PyPI Tests Changelog License

Utilities to help write tests for Datasette plugins and applications

Installation

Install this library using pip:

pip install datasette-test

Tests that use plugin configuration

Datasette 1.0a8 enforced a configuration change where plugins are no longer configured in metadata, but instead use a configuration file.

This can result in test failures in projects that use the Datasette(metadata={"plugins": {"...": "..."}}) pattern to test out plugin configuration.

You can solve this using datasette_test.Datasette, a subclass that works with Datasette versions both before and after this breaking change:

from datasette_test import Datasette
import pytest

@pytest.mark.asyncio
async def test_datasette():
    ds = Datasette(plugin_config={"my-plugin": {"config": "goes here"})

This subclass detects if the underlying plugin needs configuration in metadata or config and instantiates the class correctly either way.

You can also use this class while continuing to pass metadata={"plugins": ...} - the class will move that configuration to config when necessary.

permissions= convenience argument

Datasette 1.0a introduces a more convenient way of defining permissions directly in the configuration:

ds = Datasette(config={"permissions": {"view-instance": {"id": "root"}}})

This is not supported by Datasette pre 1.0 - but you can use the permissions= argument in datasette_test.Datasette to achieve the same effect:

ds = Datasette(permissions={"view-instance": {"id": "root"}})

This will work across both major Datasette versions.

wait_until_responds(url, timeout=5.0)

Some Datasette plugin test suites launch a Datasette server and then need to wait for that server to become available before continuing.

Call this function to wait until the server becomes available, or raise an error if it takes longer than the timeout:

from datasette_test import wait_until_responds

def test_server():
    # ... start server ...
    wait_until_responds("http://localhost:8001")
    # Now run tests

actor_cookie(datasette, actor)

Equivalent to datasette.client.actor_cookie() in Datasette 1.0a+. Example usage:

@pytest.mark.asyncio
async def test_permissions():
    ds = Datasette(permissions={"view-instance": {"id": "root"}})
    response = await ds.client.get("/")
    assert response.status_code == 403
    response = await ds.client.get(
        "/", cookies={"ds_actor": actor_cookie(ds, {"id": "root"})}
    )
    assert response.status_code == 200

Development

To contribute to this library, first checkout the code. Then create a new virtual environment:

cd datasette-test
python -m venv venv
source venv/bin/activate

Now install the dependencies and test dependencies:

pip install -e '.[test]'

To run the tests:

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datasette_test-0.3.2.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

datasette_test-0.3.2-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file datasette_test-0.3.2.tar.gz.

File metadata

  • Download URL: datasette_test-0.3.2.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for datasette_test-0.3.2.tar.gz
Algorithm Hash digest
SHA256 69e76695e713e5900b831eb34172f89553386c0aa50b9909a33e41c490825887
MD5 5edfe9409b5528e99fadb4b57e331818
BLAKE2b-256 677640202239b88b9ccebe980e377df5d5c3661477497f0f42d333175524901c

See more details on using hashes here.

File details

Details for the file datasette_test-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for datasette_test-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 894e0267977a6cfd7aef1a930c3cdcc43e7fd25592a48c611a15380b8952f51e
MD5 fba9d595c570ee3d9ac082cf61469a2c
BLAKE2b-256 085043ed3ed434539ea47aea323d135a9f0df74df4276785431cd996f70cb0a4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page