Debian Codegraph @

tests/test_docs.py

raw
1 """
2 Tests to ensure certain things are documented.
3 """
4
5 from click.testing import CliRunner
6 from datasette import app, utils
7 from datasette.cli import cli
8 from datasette.filters import Filters
9 from pathlib import Path
10 import pytest
11 import re
12
13 docs_path = Path(__file__).parent.parent / "docs"
14 label_re = re.compile(r"\.\. _([^\s:]+):")
15
16
17 def get_headings(content, underline="-"):
18 heading_re = re.compile(r"(\w+)(\([^)]*\))?\n\{}+\n".format(underline))
19 return {h[0] for h in heading_re.findall(content)}
20
21
22 def get_labels(filename):
23 content = (docs_path / filename).read_text()
24 return set(label_re.findall(content))
25
26
27 @pytest.fixture(scope="session")
28 def settings_headings():
29 return get_headings((docs_path / "settings.rst").read_text(), "~")
30
31
32 @pytest.mark.parametrize("setting", app.SETTINGS)
33 def test_settings_are_documented(settings_headings, setting):
34 assert setting.name in settings_headings
35
36
37 @pytest.fixture(scope="session")
38 def plugin_hooks_content():
39 return (docs_path / "plugin_hooks.rst").read_text()
40
41
42 @pytest.mark.parametrize(
43 "plugin", [name for name in dir(app.pm.hook) if not name.startswith("_")]
44 )
45 def test_plugin_hooks_are_documented(plugin, plugin_hooks_content):
46 headings = get_headings(plugin_hooks_content, "-")
47 assert plugin in headings
48 hook_caller = getattr(app.pm.hook, plugin)
49 arg_names = [a for a in hook_caller.spec.argnames if a != "__multicall__"]
50 # Check for plugin_name(arg1, arg2, arg3)
51 expected = f"{plugin}({', '.join(arg_names)})"
52 assert (
53 expected in plugin_hooks_content
54 ), f"Missing from plugin hook documentation: {expected}"
55
56
57 @pytest.fixture(scope="session")
58 def documented_views():
59 view_labels = set()
60 for filename in docs_path.glob("*.rst"):
61 for label in get_labels(filename):
62 first_word = label.split("_")[0]
63 if first_word.endswith("View"):
64 view_labels.add(first_word)
65 # We deliberately don't document these:
66 view_labels.update(("PatternPortfolioView", "AuthTokenView"))
67 return view_labels
68
69
70 @pytest.mark.parametrize("view_class", [v for v in dir(app) if v.endswith("View")])
71 def test_view_classes_are_documented(documented_views, view_class):
72 assert view_class in documented_views
73
74
75 @pytest.fixture(scope="session")
76 def documented_table_filters():
77 json_api_rst = (docs_path / "json_api.rst").read_text()
78 section = json_api_rst.split(".. _table_arguments:")[-1]
79 # Lines starting with ``?column__exact= are docs for filters
80 return {
81 line.split("__")[1].split("=")[0]
82 for line in section.split("\n")
83 if line.startswith("``?column__")
84 }
85
86
87 @pytest.mark.parametrize("filter", [f.key for f in Filters._filters])
88 def test_table_filters_are_documented(documented_table_filters, filter):
89 assert filter in documented_table_filters
90
91
92 @pytest.fixture(scope="session")
93 def documented_fns():
94 internals_rst = (docs_path / "internals.rst").read_text()
95 # Any line that starts .. _internals_utils_X
96 lines = internals_rst.split("\n")
97 prefix = ".. _internals_utils_"
98 return {
99 line.split(prefix)[1].split(":")[0] for line in lines if line.startswith(prefix)
100 }
101
102
103 @pytest.mark.parametrize("fn", utils.functions_marked_as_documented)
104 def test_functions_marked_with_documented_are_documented(documented_fns, fn):
105 assert fn.__name__ in documented_fns