Debian Codegraph @

tests/test_routes.py

raw
1 from datasette.app import Datasette, Database
2 from datasette.utils import resolve_routes
3 import pytest
4 import pytest_asyncio
5
6
7 @pytest.fixture(scope="session")
8 def routes():
9 ds = Datasette()
10 return ds._routes()
11
12
13 @pytest.mark.parametrize(
14 "path,expected_class,expected_matches",
15 (
16 ("/", "IndexView", {"format": None}),
17 ("/foo", "DatabaseView", {"format": None, "database": "foo"}),
18 ("/foo.csv", "DatabaseView", {"format": "csv", "database": "foo"}),
19 ("/foo.json", "DatabaseView", {"format": "json", "database": "foo"}),
20 ("/foo.humbug", "DatabaseView", {"format": "humbug", "database": "foo"}),
21 (
22 "/foo/humbug",
23 "TableView",
24 {"database": "foo", "table": "humbug", "format": None},
25 ),
26 (
27 "/foo/humbug.json",
28 "TableView",
29 {"database": "foo", "table": "humbug", "format": "json"},
30 ),
31 (
32 "/foo/humbug.blah",
33 "TableView",
34 {"database": "foo", "table": "humbug", "format": "blah"},
35 ),
36 (
37 "/foo/humbug/1",
38 "RowView",
39 {"format": None, "database": "foo", "pks": "1", "table": "humbug"},
40 ),
41 (
42 "/foo/humbug/1.json",
43 "RowView",
44 {"format": "json", "database": "foo", "pks": "1", "table": "humbug"},
45 ),
46 ("/-/metadata.json", "JsonDataView", {"format": "json"}),
47 ("/-/metadata", "JsonDataView", {"format": None}),
48 ),
49 )
50 def test_routes(routes, path, expected_class, expected_matches):
51 match, view = resolve_routes(routes, path)
52 if expected_class is None:
53 assert match is None
54 else:
55 assert view.view_class.__name__ == expected_class
56 assert match.groupdict() == expected_matches
57
58
59 @pytest_asyncio.fixture
60 async def ds_with_route():
61 ds = Datasette()
62 await ds.invoke_startup()
63 ds.remove_database("_memory")
64 db = Database(ds, is_memory=True, memory_name="route-name-db")
65 ds.add_database(db, name="original-name", route="custom-route-name")
66 await db.execute_write_script(
67 """
68 create table if not exists t (id integer primary key);
69 insert or replace into t (id) values (1);
70 """
71 )
72 return ds
73
74
75 @pytest.mark.asyncio
76 async def test_db_with_route_databases(ds_with_route):
77 response = await ds_with_route.client.get("/-/databases.json")
78 assert response.json()[0] == {
79 "name": "original-name",
80 "route": "custom-route-name",
81 "path": None,
82 "size": 0,
83 "is_mutable": True,
84 "is_memory": True,
85 "hash": None,
86 }
87
88
89 @pytest.mark.asyncio
90 @pytest.mark.parametrize(
91 "path,expected_status",
92 (
93 ("/", 200),
94 ("/original-name", 404),
95 ("/original-name/t", 404),
96 ("/original-name/t/1", 404),
97 ("/custom-route-name", 200),
98 ("/custom-route-name?sql=select+id+from+t", 200),
99 ("/custom-route-name/t", 200),
100 ("/custom-route-name/t/1", 200),
101 ),
102 )
103 async def test_db_with_route_that_does_not_match_name(
104 ds_with_route, path, expected_status
105 ):
106 response = await ds_with_route.client.get(path)
107 assert response.status_code == expected_status
108 # There should be links to custom-route-name but none to original-name
109 if response.status_code == 200:
110 assert "/custom-route-name" in response.text
111 assert "/original-name" not in response.text