Skip to main content

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

Release files for datasette-test 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for datasette-test 0.3.2
File Size Uploaded
datasette_test-0.3.2.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for datasette-test 0.3.2
File Interpreter ABI Platform
datasette_test-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 15.4 kB

Release files / datasette_test-0.3.2.tar.gz

Download URL datasette_test-0.3.2.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
69e76695e713e5900b831eb34172f89553386c0aa50b9909a33e41c490825887
BLAKE2b-256 checksum
How to use checksums
677640202239b88b9ccebe980e377df5d5c3661477497f0f42d333175524901c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / datasette_test-0.3.2-py3-none-any.whl

Download URL datasette_test-0.3.2-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
894e0267977a6cfd7aef1a930c3cdcc43e7fd25592a48c611a15380b8952f51e
BLAKE2b-256 checksum
How to use checksums
085043ed3ed434539ea47aea323d135a9f0df74df4276785431cd996f70cb0a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3

2 release files

0.2

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page