Skip to content

Commit

Permalink
Datasette() with plugin_config option
Browse files Browse the repository at this point in the history
Tests against multiple Datasette versions, refs #1
  • Loading branch information
simonw committed Jan 16, 2024
1 parent 8d5f826 commit da59895
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 31 additions & 2 deletions datasette_test/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ classifiers = [
"License :: OSI Approved :: Apache Software License"
]
dependencies = [

"datasette"
]

[project.urls]
Expand All @@ -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"]
17 changes: 14 additions & 3 deletions tests/test_datasette_test.py
Original file line number Diff line number Diff line change
@@ -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() == {}

0 comments on commit da59895

Please sign in to comment.