Skip to content

Commit 2c19a27

Browse files
committed
Documentation for filters, plus new documentation unit test
https://simonwillison.net/2018/Jul/28/documentation-unit-tests/
1 parent 6da567d commit 2c19a27

2 files changed

Lines changed: 79 additions & 3 deletions

File tree

docs/json_api.rst

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,68 @@ querystring arguments:
176176

177177
.. _table_arguments:
178178

179-
Special table arguments
180-
-----------------------
179+
Table arguments
180+
---------------
181+
182+
The Datasette table view takes a number of special querystring arguments.
183+
184+
Column filter arguments
185+
~~~~~~~~~~~~~~~~~~~~~~~
186+
187+
You can filter the data returned by the table based on column values using a querystring argument.
188+
189+
``?column__exact=value`` or ``?_column=value``
190+
Returns rows where the specified column exactly matches the value.
191+
192+
``?column__not=value``
193+
Returns rows where the column does not match the value.
194+
195+
``?column__contains=value``
196+
Rows where the string column contains the specified value (``column like "%value%"`` in SQL).
197+
198+
``?column__endswith=value``
199+
Rows where the string column ends with the specified value (``column like "%value"`` in SQL).
200+
201+
``?column__startswith=value``
202+
Rows where the string column starts with the specified value (``column like "value%"`` in SQL).
203+
204+
``?column__gt=value``
205+
Rows which are greater than the specified value.
206+
207+
``?column__gte=value``
208+
Rows which are greater than or equal to the specified value.
181209

182-
The Datasette table view takes a number of special querystring arguments:
210+
``?column__lt=value``
211+
Rows which are less than the specified value.
212+
213+
``?column__lte=value``
214+
Rows which are less than or equal to the specified value.
215+
216+
``?column__like=value``
217+
Match rows with a LIKE clause, case insensitive and with ``%`` as the wildcard character.
218+
219+
``?column__glob=value``
220+
Similar to LIKE but uses Unix wildcard syntax and is case sensitive.
221+
222+
``?column__arraycontains=value``
223+
Works against columns that contain JSON arrays - matches if any of the values in that array match.
224+
225+
This is only available if the ``json1`` SQLite extension is enabled.
226+
227+
``?column__isnull=1``
228+
Matches rows where the column is null.
229+
230+
``?column__notnull=1``
231+
Matches rows where the column is not null.
232+
233+
``?column__isblank=1``
234+
Matches rows where the column is blank, meaning null or the empty string.
235+
236+
``?column__notblank=1``
237+
Matches rows where the column is not blank.
238+
239+
Special table arguments
240+
~~~~~~~~~~~~~~~~~~~~~~~
183241

184242
``?_labels=on/off``
185243
Expand foreign key references for every possible column. See below.

tests/test_docs.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from click.testing import CliRunner
55
from datasette import app
66
from datasette.cli import cli
7+
from datasette.filters import Filters
78
from pathlib import Path
89
import pytest
910
import re
@@ -71,3 +72,20 @@ def documented_views():
7172
@pytest.mark.parametrize("view_class", [v for v in dir(app) if v.endswith("View")])
7273
def test_view_classes_are_documented(documented_views, view_class):
7374
assert view_class in documented_views
75+
76+
77+
@pytest.fixture(scope="session")
78+
def documented_table_filters():
79+
json_api_rst = (docs_path / "json_api.rst").read_text()
80+
section = json_api_rst.split(".. _table_arguments:")[-1]
81+
# Lines starting with ``?column__exact= are docs for filters
82+
return set(
83+
line.split("__")[1].split("=")[0]
84+
for line in section.split("\n")
85+
if line.startswith("``?column__")
86+
)
87+
88+
89+
@pytest.mark.parametrize("filter", [f.key for f in Filters._filters])
90+
def test_table_filters_are_documented(documented_table_filters, filter):
91+
assert filter in documented_table_filters

0 commit comments

Comments
 (0)