mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Initial sketch of custom URL routing, refs #306
This commit is contained in:
parent
a246f476b4
commit
018af454f2
2 changed files with 143 additions and 0 deletions
65
tests/test_routes.py
Normal file
65
tests/test_routes.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
from datasette import routes
|
||||
from datasette.views.database import DatabaseView
|
||||
from datasette.views.special import JsonDataView
|
||||
from datasette.views.table import TableView
|
||||
import pytest
|
||||
|
||||
MOCK_DATABASES = {
|
||||
# database: set-of-tables
|
||||
'foo': {'bar'},
|
||||
'foo-bar': {'baz'}
|
||||
}
|
||||
MOCK_DATABASE_HASHES = {
|
||||
'foo': 'foohash',
|
||||
'foo-bar': 'foobarhash',
|
||||
}
|
||||
|
||||
|
||||
def database_exists(database):
|
||||
return database in MOCK_DATABASES
|
||||
|
||||
|
||||
def table_exists(database, table):
|
||||
print('table_exists: ', database, table)
|
||||
return table in MOCK_DATABASES.get(database, set())
|
||||
|
||||
|
||||
def database_hash(database):
|
||||
return MOCK_DATABASE_HASHES[database]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('path,expected', [
|
||||
('/does-not-exist', None),
|
||||
# This should redirect
|
||||
('/foo', routes.RouteResult(
|
||||
None, None, '/foo-foohash'
|
||||
)),
|
||||
('/foo-bar-badhash', routes.RouteResult(
|
||||
None, None, '/foo-bar-foobarhash'
|
||||
)),
|
||||
('/foo-foohash', routes.RouteResult(
|
||||
DatabaseView, {'database': 'foo'}, None
|
||||
)),
|
||||
# Table views
|
||||
('/foo/bar', routes.RouteResult(
|
||||
None, None, '/foo-foohash/bar'
|
||||
)),
|
||||
('/foo/bad', routes.RouteResult(
|
||||
None, None, '/foo-foohash/bad'
|
||||
)),
|
||||
('/foo-foohash/bad', None),
|
||||
('/foo-foohash/bar', routes.RouteResult(
|
||||
TableView, {'database': 'foo', 'table': 'bar'}, None
|
||||
)),
|
||||
] + [
|
||||
('/-/{}'.format(filename), routes.RouteResult(
|
||||
JsonDataView, {'filename': filename}, None
|
||||
)) for filename in ('inspect', 'metadata', 'versions', 'plugins', 'config')
|
||||
] + [
|
||||
('/-/{}.json'.format(filename), routes.RouteResult(
|
||||
JsonDataView, {'filename': filename, 'format': 'json'}, None
|
||||
)) for filename in ('inspect', 'metadata', 'versions', 'plugins', 'config')
|
||||
])
|
||||
def test_routes(path, expected):
|
||||
actual = routes.resolve(path, database_exists, table_exists, database_hash)
|
||||
assert actual == expected
|
||||
Loading…
Add table
Add a link
Reference in a new issue