Skip to content

Commit 1000d50

Browse files
committed
1 parent d333069 commit 1000d50

10 files changed

Lines changed: 545 additions & 407 deletions

File tree

datasette/fixtures.py

Lines changed: 415 additions & 0 deletions
Large diffs are not rendered by default.

datasette/utils/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,15 @@ async def all(self):
155155
functions_marked_as_documented = []
156156

157157

158-
def documented(fn):
159-
functions_marked_as_documented.append(fn)
160-
return fn
158+
def documented(fn=None, *, label=None):
159+
def decorate(fn):
160+
fn._datasette_docs_label = label or "internals_utils_{}".format(fn.__name__)
161+
functions_marked_as_documented.append(fn)
162+
return fn
163+
164+
if fn is None:
165+
return decorate
166+
return decorate(fn)
161167

162168

163169
@documented

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Unreleased
1313
- Fixed a bug where stale tables and other related resources were not removed from ``catalog_*`` tables when a database was removed. (:issue:`2723`)
1414
- Fixed a Safari bug with the table search mechanism triggered by pressing ``/``. (:issue:`2724`)
1515
- New "Jump to..." menu item, always visible, for triggering the previously undocumented ``/`` menu. (:issue:`2725`)
16+
- New documented :ref:`datasette.fixtures.populate_fixture_database(conn) <datasette_fixtures_populate_fixture_database>` helper for creating the fixture database tables used by Datasette's own tests, intended for plugin test suites.
1617

1718
.. _v1_0_a29:
1819

docs/testing_plugins.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,48 @@ This method registers any :ref:`plugin_hook_startup` or :ref:`plugin_hook_prepar
8282

8383
If you are using ``await datasette.client.get()`` and similar methods then you don't need to worry about this - Datasette automatically calls ``invoke_startup()`` the first time it handles a request.
8484

85+
.. _testing_plugins_datasette_fixtures_database:
86+
87+
Using Datasette's fixtures database
88+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89+
90+
Datasette's own test suite uses a SQLite database with tables that exercise features such as compound primary keys, foreign keys, sortable columns, facets, full-text search and binary data.
91+
92+
You can use those same tables in your plugin tests using the ``populate_fixture_database(conn)`` helper in ``datasette.fixtures``:
93+
94+
Be aware that future Datasette releases may change details of these tables, so try not to rely on their exact structure in your own tests.
95+
96+
.. _datasette_fixtures_populate_fixture_database:
97+
98+
``populate_fixture_database(conn)``
99+
Populates an existing SQLite connection with the fixture tables.
100+
101+
For an in-memory test database:
102+
103+
.. code-block:: python
104+
105+
from datasette.app import Datasette
106+
from datasette.fixtures import populate_fixture_database
107+
import pytest
108+
import pytest_asyncio
109+
110+
111+
@pytest_asyncio.fixture
112+
async def datasette():
113+
datasette = Datasette()
114+
db = datasette.add_memory_database("fixtures")
115+
await db.execute_write_fn(populate_fixture_database)
116+
await datasette.invoke_startup()
117+
return datasette
118+
119+
120+
@pytest.mark.asyncio
121+
async def test_facetable(datasette):
122+
response = await datasette.client.get(
123+
"/fixtures/facetable.json?_shape=array"
124+
)
125+
assert response.status_code == 200
126+
85127
.. _testing_plugins_autoclose:
86128

87129
Automatic cleanup of Datasette instances

tests/conftest.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async def ds_client():
7272
"num_sql_threads": 1,
7373
},
7474
)
75-
from .fixtures import TABLES, TABLE_PARAMETERIZED_SQL
75+
from datasette.fixtures import populate_fixture_database
7676

7777
# Use a unique memory_name to avoid collisions between different
7878
# Datasette instances in the same process, but use "fixtures" for routing
@@ -82,10 +82,7 @@ async def ds_client():
8282

8383
def prepare(conn):
8484
if not conn.execute("select count(*) from sqlite_master").fetchone()[0]:
85-
conn.executescript(TABLES)
86-
for sql, params in TABLE_PARAMETERIZED_SQL:
87-
with conn:
88-
conn.execute(sql, params)
85+
populate_fixture_database(conn)
8986

9087
await db.execute_write_fn(prepare)
9188
await ds.invoke_startup()
@@ -264,8 +261,6 @@ def ds_unix_domain_socket_server(tmp_path_factory):
264261
app_client_with_cors,
265262
app_client_with_dot,
266263
app_client_with_trace,
267-
generate_compound_rows,
268-
generate_sortable_rows,
269264
make_app_client,
270265
TEMP_PLUGIN_SECRET_FILE,
271266
)

0 commit comments

Comments
 (0)