Refactored URL routing to add tests, closes #1666

Refs #1660
This commit is contained in:
Simon Willison 2022-03-18 21:03:08 -07:00
commit 711767bcd3
3 changed files with 72 additions and 24 deletions

34
tests/test_routes.py Normal file
View file

@ -0,0 +1,34 @@
from datasette.app import Datasette
from datasette.utils import resolve_routes
import pytest
@pytest.fixture(scope="session")
def routes():
ds = Datasette()
return ds._routes()
@pytest.mark.parametrize(
"path,expected",
(
("/", "IndexView"),
("/foo", "DatabaseView"),
("/foo.csv", "DatabaseView"),
("/foo.json", "DatabaseView"),
("/foo.humbug", "DatabaseView"),
("/foo/humbug", "TableView"),
("/foo/humbug.json", "TableView"),
("/foo/humbug.blah", "TableView"),
("/foo/humbug/1", "RowView"),
("/foo/humbug/1.json", "RowView"),
("/-/metadata.json", "JsonDataView"),
("/-/metadata", "JsonDataView"),
),
)
def test_routes(routes, path, expected):
match, view = resolve_routes(routes, path)
if expected is None:
assert match is None
else:
assert view.view_class.__name__ == expected