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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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