diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 64cb615..a2e5c5b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,21 +9,20 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.7", "3.8", "3.9", "3.10"] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + datasette-version: ["<=1.0a0", ">=1.0a0", "https://codeload.github.com/simonw/datasette/zip/7a5adb592ae6674a2058639c66e85eb1b49448fb"] steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} - if: hashFiles('setup.py') - uses: actions/setup-python@v2 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} cache: pip - cache-dependency-path: '**/setup.py' + cache-dependency-path: '**/pyproject.toml' - name: Install dependencies - if: hashFiles('setup.py') run: | pip install -e '.[test]' + pip install 'datasette${{ matrix.datasette-version }}' - name: Run tests - if: hashFiles('setup.py') run: | pytest diff --git a/datasette_test/__init__.py b/datasette_test/__init__.py index 923334e..9fb78ac 100644 --- a/datasette_test/__init__.py +++ b/datasette_test/__init__.py @@ -1,2 +1,31 @@ -def example_function(): - return 1 + 1 +from datasette.app import Datasette as _Datasette + +try: + from datasette.utils import fail_if_plugins_in_metadata + + plugin_config_should_be_in_metadata = False +except ImportError: + plugin_config_should_be_in_metadata = True + + +class Datasette(_Datasette): + def __init__(self, *args, **kwargs): + plugin_config = kwargs.pop("plugin_config", None) + if plugin_config is not None: + if plugin_config_should_be_in_metadata: + metadata = kwargs.pop("metadata", None) or {} + metadata["plugins"] = plugin_config + kwargs["metadata"] = metadata + else: + config = kwargs.pop("config", None) or {} + config["plugins"] = plugin_config + kwargs["config"] = config + elif ( + "plugins" in (kwargs.get("metadata") or {}) + and not plugin_config_should_be_in_metadata + ): + # Move plugins to config anyway + plugin_config = kwargs["metadata"].pop("plugins") + kwargs["config"] = kwargs.get("config") or {} + kwargs["config"]["plugins"] = plugin_config + super().__init__(*args, **kwargs) diff --git a/pyproject.toml b/pyproject.toml index 2d34330..3792ba2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ classifiers = [ "License :: OSI Approved :: Apache Software License" ] dependencies = [ - + "datasette" ] [project.urls] @@ -19,6 +19,5 @@ Changelog = "https://github.com/datasette/datasette-test/releases" Issues = "https://github.com/datasette/datasette-test/issues" CI = "https://github.com/datasette/datasette-test/actions" - [project.optional-dependencies] -test = ["pytest"] +test = ["pytest", "pytest-asyncio"] diff --git a/tests/test_datasette_test.py b/tests/test_datasette_test.py index 75ee6af..526321d 100644 --- a/tests/test_datasette_test.py +++ b/tests/test_datasette_test.py @@ -1,5 +1,16 @@ -from datasette_test import example_function +from datasette_test import Datasette, plugin_config_should_be_in_metadata +import pytest -def test_example_function(): - assert example_function() == 2 +@pytest.mark.asyncio +async def test_datasette_plugin_config(): + ds = Datasette() + response = await ds.client.get("/-/metadata.json") + assert response.json() == {} + + ds = Datasette(plugin_config={"foo": "bar"}) + response2 = await ds.client.get("/-/metadata.json") + if plugin_config_should_be_in_metadata: + assert response2.json() == {"plugins": {"foo": "bar"}} + else: + assert response2.json() == {}