mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
One more blacken-docs test, refs #1718
This commit is contained in:
parent
92b26673d8
commit
498e1536f5
1 changed files with 27 additions and 18 deletions
|
|
@ -19,7 +19,10 @@ If you use the template described in :ref:`writing_plugins_cookiecutter` your pl
|
||||||
response = await datasette.client.get("/-/plugins.json")
|
response = await datasette.client.get("/-/plugins.json")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
installed_plugins = {p["name"] for p in response.json()}
|
installed_plugins = {p["name"] for p in response.json()}
|
||||||
assert "datasette-plugin-template-demo" in installed_plugins
|
assert (
|
||||||
|
"datasette-plugin-template-demo"
|
||||||
|
in installed_plugins
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
This test uses the :ref:`internals_datasette_client` object to exercise a test instance of Datasette. ``datasette.client`` is a wrapper around the `HTTPX <https://www.python-httpx.org/>`__ Python library which can imitate HTTP requests using ASGI. This is the recommended way to write tests against a Datasette instance.
|
This test uses the :ref:`internals_datasette_client` object to exercise a test instance of Datasette. ``datasette.client`` is a wrapper around the `HTTPX <https://www.python-httpx.org/>`__ Python library which can imitate HTTP requests using ASGI. This is the recommended way to write tests against a Datasette instance.
|
||||||
|
|
@ -37,9 +40,7 @@ If you are building an installable package you can add them as test dependencies
|
||||||
setup(
|
setup(
|
||||||
name="datasette-my-plugin",
|
name="datasette-my-plugin",
|
||||||
# ...
|
# ...
|
||||||
extras_require={
|
extras_require={"test": ["pytest", "pytest-asyncio"]},
|
||||||
"test": ["pytest", "pytest-asyncio"]
|
|
||||||
},
|
|
||||||
tests_require=["datasette-my-plugin[test]"],
|
tests_require=["datasette-my-plugin[test]"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -87,31 +88,34 @@ Here's an example that uses the `sqlite-utils library <https://sqlite-utils.data
|
||||||
import pytest
|
import pytest
|
||||||
import sqlite_utils
|
import sqlite_utils
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def datasette(tmp_path_factory):
|
def datasette(tmp_path_factory):
|
||||||
db_directory = tmp_path_factory.mktemp("dbs")
|
db_directory = tmp_path_factory.mktemp("dbs")
|
||||||
db_path = db_directory / "test.db"
|
db_path = db_directory / "test.db"
|
||||||
db = sqlite_utils.Database(db_path)
|
db = sqlite_utils.Database(db_path)
|
||||||
db["dogs"].insert_all([
|
db["dogs"].insert_all(
|
||||||
{"id": 1, "name": "Cleo", "age": 5},
|
[
|
||||||
{"id": 2, "name": "Pancakes", "age": 4}
|
{"id": 1, "name": "Cleo", "age": 5},
|
||||||
], pk="id")
|
{"id": 2, "name": "Pancakes", "age": 4},
|
||||||
|
],
|
||||||
|
pk="id",
|
||||||
|
)
|
||||||
datasette = Datasette(
|
datasette = Datasette(
|
||||||
[db_path],
|
[db_path],
|
||||||
metadata={
|
metadata={
|
||||||
"databases": {
|
"databases": {
|
||||||
"test": {
|
"test": {
|
||||||
"tables": {
|
"tables": {
|
||||||
"dogs": {
|
"dogs": {"title": "Some dogs"}
|
||||||
"title": "Some dogs"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
return datasette
|
return datasette
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_example_table_json(datasette):
|
async def test_example_table_json(datasette):
|
||||||
response = await datasette.client.get("/test/dogs.json?_shape=array")
|
response = await datasette.client.get("/test/dogs.json?_shape=array")
|
||||||
|
|
@ -121,6 +125,7 @@ Here's an example that uses the `sqlite-utils library <https://sqlite-utils.data
|
||||||
{"id": 2, "name": "Pancakes", "age": 4},
|
{"id": 2, "name": "Pancakes", "age": 4},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_example_table_html(datasette):
|
async def test_example_table_html(datasette):
|
||||||
response = await datasette.client.get("/test/dogs")
|
response = await datasette.client.get("/test/dogs")
|
||||||
|
|
@ -137,6 +142,7 @@ If you want to create that test database repeatedly for every individual test fu
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def datasette(tmp_path_factory):
|
def datasette(tmp_path_factory):
|
||||||
# This fixture will be executed repeatedly for every test
|
# This fixture will be executed repeatedly for every test
|
||||||
|
...
|
||||||
|
|
||||||
.. _testing_plugins_pytest_httpx:
|
.. _testing_plugins_pytest_httpx:
|
||||||
|
|
||||||
|
|
@ -197,14 +203,17 @@ Here's a test for that plugin that mocks the HTTPX outbound request:
|
||||||
|
|
||||||
async def test_outbound_http_call(httpx_mock):
|
async def test_outbound_http_call(httpx_mock):
|
||||||
httpx_mock.add_response(
|
httpx_mock.add_response(
|
||||||
url='https://www.example.com/',
|
url="https://www.example.com/",
|
||||||
text='Hello world',
|
text="Hello world",
|
||||||
)
|
)
|
||||||
datasette = Datasette([], memory=True)
|
datasette = Datasette([], memory=True)
|
||||||
response = await datasette.client.post("/-/fetch-url", data={
|
response = await datasette.client.post(
|
||||||
"url": "https://www.example.com/"
|
"/-/fetch-url",
|
||||||
})
|
data={"url": "https://www.example.com/"},
|
||||||
|
)
|
||||||
assert response.text == "Hello world"
|
assert response.text == "Hello world"
|
||||||
|
|
||||||
outbound_request = httpx_mock.get_request()
|
outbound_request = httpx_mock.get_request()
|
||||||
assert outbound_request.url == "https://www.example.com/"
|
assert (
|
||||||
|
outbound_request.url == "https://www.example.com/"
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue