2017-12-15 12:04:17 +00:00
|
|
|
from bs4 import BeautifulSoup as Soup
|
2018-05-31 13:40:30 +00:00
|
|
|
from .fixtures import ( # noqa
|
|
|
|
app_client,
|
|
|
|
app_client_shorter_time_limit,
|
|
|
|
)
|
2017-12-15 12:04:17 +00:00
|
|
|
import pytest
|
|
|
|
import re
|
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
|
|
|
|
def test_homepage(app_client):
|
2018-06-05 03:53:12 +00:00
|
|
|
response = app_client.get('/')
|
2017-12-15 12:04:17 +00:00
|
|
|
assert response.status == 200
|
2018-06-17 18:34:16 +00:00
|
|
|
assert 'fixtures' in response.text
|
2017-12-15 12:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_database_page(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures', allow_redirects=False)
|
2017-12-15 12:04:17 +00:00
|
|
|
assert response.status == 302
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures')
|
|
|
|
assert 'fixtures' in response.text
|
2017-12-15 12:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_custom_sql(app_client):
|
|
|
|
response = app_client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
'/fixtures?sql=.schema'
|
2017-12-15 12:04:17 +00:00
|
|
|
)
|
|
|
|
assert response.status == 400
|
|
|
|
assert 'Statement must be a SELECT' in response.text
|
|
|
|
|
|
|
|
|
2018-05-28 21:24:19 +00:00
|
|
|
def test_sql_time_limit(app_client_shorter_time_limit):
|
|
|
|
response = app_client_shorter_time_limit.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
'/fixtures?sql=select+sleep(0.5)'
|
2018-05-28 21:24:19 +00:00
|
|
|
)
|
|
|
|
assert 400 == response.status
|
|
|
|
expected_html_fragment = """
|
|
|
|
<a href="https://datasette.readthedocs.io/en/stable/config.html#sql-time-limit-ms">sql_time_limit_ms</a>
|
|
|
|
""".strip()
|
|
|
|
assert expected_html_fragment in response.text
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
def test_view(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/simple_view')
|
2017-12-15 12:04:17 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
|
|
|
|
def test_row(app_client):
|
|
|
|
response = app_client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
'/fixtures/simple_primary_key/1',
|
2018-06-05 03:53:12 +00:00
|
|
|
allow_redirects=False
|
2017-12-15 12:04:17 +00:00
|
|
|
)
|
|
|
|
assert response.status == 302
|
|
|
|
assert response.headers['Location'].endswith('/1')
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/simple_primary_key/1')
|
2017-12-15 12:04:17 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
|
2018-07-08 05:21:51 +00:00
|
|
|
def test_row_strange_table_name(app_client):
|
|
|
|
response = app_client.get(
|
|
|
|
'/fixtures/table%2Fwith%2Fslashes.csv/3',
|
|
|
|
allow_redirects=False
|
|
|
|
)
|
|
|
|
assert response.status == 302
|
|
|
|
assert response.headers['Location'].endswith(
|
|
|
|
'/table%2Fwith%2Fslashes.csv/3'
|
|
|
|
)
|
|
|
|
response = app_client.get('/fixtures/table%2Fwith%2Fslashes.csv/3')
|
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
def test_add_filter_redirects(app_client):
|
|
|
|
filter_args = urllib.parse.urlencode({
|
|
|
|
'_filter_column': 'content',
|
|
|
|
'_filter_op': 'startswith',
|
|
|
|
'_filter_value': 'x'
|
|
|
|
})
|
|
|
|
# First we need to resolve the correct path before testing more redirects
|
|
|
|
path_base = app_client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
'/fixtures/simple_primary_key', allow_redirects=False
|
2017-12-15 12:04:17 +00:00
|
|
|
).headers['Location']
|
|
|
|
path = path_base + '?' + filter_args
|
2018-06-05 03:53:12 +00:00
|
|
|
response = app_client.get(path, allow_redirects=False)
|
2017-12-15 12:04:17 +00:00
|
|
|
assert response.status == 302
|
|
|
|
assert response.headers['Location'].endswith('?content__startswith=x')
|
|
|
|
|
|
|
|
# Adding a redirect to an existing querystring:
|
|
|
|
path = path_base + '?foo=bar&' + filter_args
|
2018-06-05 03:53:12 +00:00
|
|
|
response = app_client.get(path, allow_redirects=False)
|
2017-12-15 12:04:17 +00:00
|
|
|
assert response.status == 302
|
2018-05-12 21:35:25 +00:00
|
|
|
assert response.headers['Location'].endswith('?foo=bar&content__startswith=x')
|
2017-12-15 12:04:17 +00:00
|
|
|
|
|
|
|
# Test that op with a __x suffix overrides the filter value
|
|
|
|
path = path_base + '?' + urllib.parse.urlencode({
|
|
|
|
'_filter_column': 'content',
|
|
|
|
'_filter_op': 'isnull__5',
|
|
|
|
'_filter_value': 'x'
|
|
|
|
})
|
2018-06-05 03:53:12 +00:00
|
|
|
response = app_client.get(path, allow_redirects=False)
|
2017-12-15 12:04:17 +00:00
|
|
|
assert response.status == 302
|
|
|
|
assert response.headers['Location'].endswith('?content__isnull=5')
|
|
|
|
|
|
|
|
|
|
|
|
def test_existing_filter_redirects(app_client):
|
|
|
|
filter_args = {
|
|
|
|
'_filter_column_1': 'name',
|
|
|
|
'_filter_op_1': 'contains',
|
|
|
|
'_filter_value_1': 'hello',
|
|
|
|
'_filter_column_2': 'age',
|
|
|
|
'_filter_op_2': 'gte',
|
|
|
|
'_filter_value_2': '22',
|
|
|
|
'_filter_column_3': 'age',
|
|
|
|
'_filter_op_3': 'lt',
|
|
|
|
'_filter_value_3': '30',
|
|
|
|
'_filter_column_4': 'name',
|
|
|
|
'_filter_op_4': 'contains',
|
|
|
|
'_filter_value_4': 'world',
|
|
|
|
}
|
|
|
|
path_base = app_client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
'/fixtures/simple_primary_key', allow_redirects=False
|
2017-12-15 12:04:17 +00:00
|
|
|
).headers['Location']
|
|
|
|
path = path_base + '?' + urllib.parse.urlencode(filter_args)
|
2018-06-05 03:53:12 +00:00
|
|
|
response = app_client.get(path, allow_redirects=False)
|
2017-12-15 12:04:17 +00:00
|
|
|
assert response.status == 302
|
2018-05-12 22:49:37 +00:00
|
|
|
assert_querystring_equal(
|
|
|
|
'name__contains=hello&age__gte=22&age__lt=30&name__contains=world',
|
|
|
|
response.headers['Location'].split('?')[1],
|
2017-12-15 12:04:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Setting _filter_column_3 to empty string should remove *_3 entirely
|
|
|
|
filter_args['_filter_column_3'] = ''
|
|
|
|
path = path_base + '?' + urllib.parse.urlencode(filter_args)
|
2018-06-05 03:53:12 +00:00
|
|
|
response = app_client.get(path, allow_redirects=False)
|
2017-12-15 12:04:17 +00:00
|
|
|
assert response.status == 302
|
2018-05-12 22:49:37 +00:00
|
|
|
assert_querystring_equal(
|
|
|
|
'name__contains=hello&age__gte=22&name__contains=world',
|
|
|
|
response.headers['Location'].split('?')[1],
|
2017-12-15 12:04:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# ?_filter_op=exact should be removed if unaccompanied by _fiter_column
|
2018-06-05 03:53:12 +00:00
|
|
|
response = app_client.get(path_base + '?_filter_op=exact', allow_redirects=False)
|
2017-12-15 12:04:17 +00:00
|
|
|
assert response.status == 302
|
|
|
|
assert '?' not in response.headers['Location']
|
|
|
|
|
|
|
|
|
|
|
|
def test_empty_search_parameter_gets_removed(app_client):
|
|
|
|
path_base = app_client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
'/fixtures/simple_primary_key', allow_redirects=False
|
2017-12-15 12:04:17 +00:00
|
|
|
).headers['Location']
|
|
|
|
path = path_base + '?' + urllib.parse.urlencode({
|
|
|
|
'_search': '',
|
|
|
|
'_filter_column': 'name',
|
|
|
|
'_filter_op': 'exact',
|
|
|
|
'_filter_value': 'chidi',
|
|
|
|
})
|
2018-06-05 03:53:12 +00:00
|
|
|
response = app_client.get(path, allow_redirects=False)
|
2017-12-15 12:04:17 +00:00
|
|
|
assert response.status == 302
|
|
|
|
assert response.headers['Location'].endswith(
|
|
|
|
'?name__exact=chidi'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-04-10 00:30:44 +00:00
|
|
|
def test_sort_by_desc_redirects(app_client):
|
|
|
|
path_base = app_client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
'/fixtures/sortable', allow_redirects=False
|
2018-04-10 00:30:44 +00:00
|
|
|
).headers['Location']
|
|
|
|
path = path_base + '?' + urllib.parse.urlencode({
|
|
|
|
'_sort': 'sortable',
|
|
|
|
'_sort_by_desc': '1',
|
|
|
|
})
|
2018-06-05 03:53:12 +00:00
|
|
|
response = app_client.get(path, allow_redirects=False)
|
2018-04-10 00:30:44 +00:00
|
|
|
assert response.status == 302
|
|
|
|
assert response.headers['Location'].endswith('?_sort_desc=sortable')
|
|
|
|
|
|
|
|
|
2018-05-15 09:34:45 +00:00
|
|
|
def test_sort_links(app_client):
|
|
|
|
response = app_client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
'/fixtures/sortable?_sort=sortable'
|
2018-06-05 03:53:12 +00:00
|
|
|
|
2018-05-15 09:34:45 +00:00
|
|
|
)
|
|
|
|
assert response.status == 200
|
|
|
|
ths = Soup(response.body, 'html.parser').findAll('th')
|
|
|
|
attrs_and_link_attrs = [{
|
|
|
|
'attrs': th.attrs,
|
|
|
|
'a_href': (
|
|
|
|
th.find('a')['href'].split('/')[-1]
|
|
|
|
if th.find('a')
|
|
|
|
else None
|
|
|
|
),
|
|
|
|
} for th in ths]
|
|
|
|
assert [
|
|
|
|
{
|
|
|
|
"attrs": {"class": ["col-Link"], "scope": "col"},
|
|
|
|
"a_href": None
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"attrs": {"class": ["col-pk1"], "scope": "col"},
|
|
|
|
"a_href": None
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"attrs": {"class": ["col-pk2"], "scope": "col"},
|
|
|
|
"a_href": None
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"attrs": {"class": ["col-content"], "scope": "col"},
|
|
|
|
"a_href": None
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"attrs": {"class": ["col-sortable"], "scope": "col"},
|
|
|
|
"a_href": "sortable?_sort_desc=sortable",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"attrs": {"class": ["col-sortable_with_nulls"], "scope": "col"},
|
|
|
|
"a_href": "sortable?_sort=sortable_with_nulls",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"attrs": {"class": ["col-sortable_with_nulls_2"], "scope": "col"},
|
|
|
|
"a_href": "sortable?_sort=sortable_with_nulls_2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"attrs": {"class": ["col-text"], "scope": "col"},
|
|
|
|
"a_href": "sortable?_sort=text",
|
|
|
|
},
|
|
|
|
] == attrs_and_link_attrs
|
|
|
|
|
|
|
|
|
2018-06-21 04:30:13 +00:00
|
|
|
def test_facet_display(app_client):
|
|
|
|
response = app_client.get(
|
|
|
|
"/fixtures/facetable?_facet=planet_int&_facet=city_id&_facet=on_earth"
|
|
|
|
)
|
|
|
|
assert response.status == 200
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
divs = soup.find(
|
|
|
|
"div", {"class": "facet-results"}
|
|
|
|
).findAll("div")
|
|
|
|
actual = []
|
|
|
|
for div in divs:
|
|
|
|
actual.append(
|
|
|
|
{
|
|
|
|
"name": div.find("strong").text,
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"name": a.text,
|
|
|
|
"qs": a["href"].split("?")[-1],
|
|
|
|
"count": int(str(a.parent).split("</a>")[1].split("<")[0]),
|
|
|
|
}
|
|
|
|
for a in div.find("ul").findAll("a")
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
assert [
|
|
|
|
{
|
|
|
|
"name": "city_id",
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"name": "San Francisco",
|
|
|
|
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&city_id=1",
|
|
|
|
"count": 6,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Los Angeles",
|
|
|
|
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&city_id=2",
|
|
|
|
"count": 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Detroit",
|
|
|
|
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&city_id=3",
|
|
|
|
"count": 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Memnonia",
|
|
|
|
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&city_id=4",
|
|
|
|
"count": 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "planet_int",
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"name": "1",
|
|
|
|
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&planet_int=1",
|
|
|
|
"count": 14,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "2",
|
|
|
|
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&planet_int=2",
|
|
|
|
"count": 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "on_earth",
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"name": "1",
|
|
|
|
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&on_earth=1",
|
|
|
|
"count": 14,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "0",
|
|
|
|
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&on_earth=0",
|
|
|
|
"count": 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
] == actual
|
|
|
|
|
|
|
|
|
2018-05-15 10:28:48 +00:00
|
|
|
def test_facets_persist_through_filter_form(app_client):
|
|
|
|
response = app_client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
'/fixtures/facetable?_facet=planet_int&_facet=city_id'
|
2018-05-15 10:28:48 +00:00
|
|
|
)
|
|
|
|
assert response.status == 200
|
|
|
|
inputs = Soup(response.body, 'html.parser').find('form').findAll('input')
|
|
|
|
hiddens = [i for i in inputs if i['type'] == 'hidden']
|
|
|
|
assert [
|
2018-05-15 15:52:02 +00:00
|
|
|
('_facet', 'city_id'),
|
2018-05-16 14:43:13 +00:00
|
|
|
('_facet', 'planet_int'),
|
2018-05-15 10:28:48 +00:00
|
|
|
] == [
|
|
|
|
(hidden['name'], hidden['value']) for hidden in hiddens
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
@pytest.mark.parametrize('path,expected_classes', [
|
|
|
|
('/', ['index']),
|
2018-06-17 18:34:16 +00:00
|
|
|
('/fixtures', ['db', 'db-fixtures']),
|
|
|
|
('/fixtures/simple_primary_key', [
|
|
|
|
'table', 'db-fixtures', 'table-simple_primary_key'
|
2017-12-15 12:04:17 +00:00
|
|
|
]),
|
2018-06-17 18:34:16 +00:00
|
|
|
('/fixtures/table%2Fwith%2Fslashes.csv', [
|
|
|
|
'table', 'db-fixtures', 'table-tablewithslashescsv-fa7563'
|
2017-12-15 12:04:17 +00:00
|
|
|
]),
|
2018-06-17 18:34:16 +00:00
|
|
|
('/fixtures/simple_primary_key/1', [
|
|
|
|
'row', 'db-fixtures', 'table-simple_primary_key'
|
2017-12-15 12:04:17 +00:00
|
|
|
]),
|
|
|
|
])
|
|
|
|
def test_css_classes_on_body(app_client, path, expected_classes):
|
2018-06-05 03:53:12 +00:00
|
|
|
response = app_client.get(path)
|
2018-04-16 20:22:04 +00:00
|
|
|
assert response.status == 200
|
2017-12-15 12:04:17 +00:00
|
|
|
classes = re.search(r'<body class="(.*)">', response.text).group(1).split()
|
|
|
|
assert classes == expected_classes
|
|
|
|
|
|
|
|
|
|
|
|
def test_table_html_simple_primary_key(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/simple_primary_key')
|
2018-04-16 20:22:04 +00:00
|
|
|
assert response.status == 200
|
2017-12-15 12:04:17 +00:00
|
|
|
table = Soup(response.body, 'html.parser').find('table')
|
2018-04-24 01:30:03 +00:00
|
|
|
assert table['class'] == ['rows-and-columns']
|
2018-04-09 02:08:28 +00:00
|
|
|
ths = table.findAll('th')
|
2018-04-16 20:22:04 +00:00
|
|
|
assert 'id' == ths[0].find('a').string.strip()
|
|
|
|
for expected_col, th in zip(('content',), ths[1:]):
|
2018-04-09 02:08:28 +00:00
|
|
|
a = th.find('a')
|
|
|
|
assert expected_col == a.string
|
|
|
|
assert a['href'].endswith('/simple_primary_key?_sort={}'.format(
|
|
|
|
expected_col
|
|
|
|
))
|
|
|
|
assert ['nofollow'] == a['rel']
|
2017-12-15 12:04:17 +00:00
|
|
|
assert [
|
|
|
|
[
|
2018-06-17 18:34:16 +00:00
|
|
|
'<td class="col-id"><a href="/fixtures/simple_primary_key/1">1</a></td>',
|
2018-04-17 08:29:48 +00:00
|
|
|
'<td class="col-content">hello</td>'
|
2017-12-15 12:04:17 +00:00
|
|
|
], [
|
2018-06-17 18:34:16 +00:00
|
|
|
'<td class="col-id"><a href="/fixtures/simple_primary_key/2">2</a></td>',
|
2018-04-17 08:29:48 +00:00
|
|
|
'<td class="col-content">world</td>'
|
2017-12-15 12:04:17 +00:00
|
|
|
], [
|
2018-06-17 18:34:16 +00:00
|
|
|
'<td class="col-id"><a href="/fixtures/simple_primary_key/3">3</a></td>',
|
2018-06-21 15:26:18 +00:00
|
|
|
'<td class="col-content">\xa0</td>'
|
2017-12-15 12:04:17 +00:00
|
|
|
]
|
|
|
|
] == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
|
|
|
|
|
|
|
|
2018-06-18 06:03:22 +00:00
|
|
|
def test_table_csv_json_export_interface(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/simple_primary_key')
|
2018-06-15 06:51:23 +00:00
|
|
|
assert response.status == 200
|
2018-06-18 06:03:22 +00:00
|
|
|
# The links at the top of the page
|
2018-06-15 06:51:23 +00:00
|
|
|
links = Soup(response.body, "html.parser").find("p", {
|
|
|
|
"class": "export-links"
|
|
|
|
}).findAll("a")
|
|
|
|
actual = [l["href"].split("/")[-1] for l in links]
|
|
|
|
expected = [
|
|
|
|
"simple_primary_key.json",
|
|
|
|
"simple_primary_key.csv?_size=max",
|
2018-06-18 06:03:22 +00:00
|
|
|
"#export"
|
2018-06-15 06:51:23 +00:00
|
|
|
]
|
|
|
|
assert expected == actual
|
2018-06-18 06:03:22 +00:00
|
|
|
# And the advaced export box at the bottom:
|
|
|
|
div = Soup(response.body, "html.parser").find("div", {
|
|
|
|
"class": "advanced-export"
|
|
|
|
})
|
|
|
|
json_links = [a["href"].split("/")[-1] for a in div.find("p").findAll("a")]
|
|
|
|
assert [
|
|
|
|
"simple_primary_key.json",
|
|
|
|
"simple_primary_key.json?_shape=array",
|
|
|
|
"simple_primary_key.json?_shape=object"
|
|
|
|
] == json_links
|
|
|
|
# And the CSV form
|
|
|
|
form = div.find("form")
|
|
|
|
assert form["action"].endswith("/simple_primary_key.csv")
|
|
|
|
inputs = [str(input) for input in form.findAll("input")]
|
|
|
|
assert [
|
|
|
|
'<input name="_dl" type="checkbox"/>',
|
|
|
|
'<input type="submit" value="Export CSV"/>',
|
|
|
|
'<input name="_size" type="hidden" value="max"/>'
|
|
|
|
] == inputs
|
2018-06-15 06:51:23 +00:00
|
|
|
|
|
|
|
|
2018-06-17 22:56:55 +00:00
|
|
|
def test_csv_json_export_links_include_labels_if_foreign_keys(app_client):
|
|
|
|
response = app_client.get('/fixtures/facetable')
|
|
|
|
assert response.status == 200
|
|
|
|
links = Soup(response.body, "html.parser").find("p", {
|
|
|
|
"class": "export-links"
|
|
|
|
}).findAll("a")
|
|
|
|
actual = [l["href"].split("/")[-1] for l in links]
|
|
|
|
expected = [
|
|
|
|
"facetable.json?_labels=on",
|
|
|
|
"facetable.csv?_labels=on&_size=max",
|
2018-06-18 06:03:22 +00:00
|
|
|
"#export"
|
2018-06-17 22:56:55 +00:00
|
|
|
]
|
|
|
|
assert expected == actual
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
def test_row_html_simple_primary_key(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/simple_primary_key/1')
|
2018-04-16 20:22:04 +00:00
|
|
|
assert response.status == 200
|
2017-12-15 12:04:17 +00:00
|
|
|
table = Soup(response.body, 'html.parser').find('table')
|
|
|
|
assert [
|
2018-04-14 14:55:27 +00:00
|
|
|
'id', 'content'
|
2018-04-09 02:08:28 +00:00
|
|
|
] == [th.string.strip() for th in table.select('thead th')]
|
2017-12-15 12:04:17 +00:00
|
|
|
assert [
|
|
|
|
[
|
2018-04-17 08:29:48 +00:00
|
|
|
'<td class="col-id">1</td>',
|
|
|
|
'<td class="col-content">hello</td>'
|
2017-12-15 12:04:17 +00:00
|
|
|
]
|
|
|
|
] == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
|
|
|
|
|
|
|
|
2018-04-13 18:17:22 +00:00
|
|
|
def test_table_not_exists(app_client):
|
|
|
|
assert 'Table not found: blah' in app_client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
'/fixtures/blah'
|
2018-04-13 18:17:22 +00:00
|
|
|
).body.decode('utf8')
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
def test_table_html_no_primary_key(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/no_primary_key')
|
2018-04-16 20:22:04 +00:00
|
|
|
assert response.status == 200
|
2017-12-15 12:04:17 +00:00
|
|
|
table = Soup(response.body, 'html.parser').find('table')
|
2018-04-09 04:58:25 +00:00
|
|
|
# We have disabled sorting for this table using metadata.json
|
|
|
|
assert [
|
|
|
|
'content', 'a', 'b', 'c'
|
|
|
|
] == [th.string.strip() for th in table.select('thead th')[2:]]
|
2017-12-15 12:04:17 +00:00
|
|
|
expected = [
|
|
|
|
[
|
2018-06-17 18:34:16 +00:00
|
|
|
'<td class="col-Link"><a href="/fixtures/no_primary_key/{}">{}</a></td>'.format(i, i),
|
2018-04-17 08:29:48 +00:00
|
|
|
'<td class="col-rowid">{}</td>'.format(i),
|
|
|
|
'<td class="col-content">{}</td>'.format(i),
|
|
|
|
'<td class="col-a">a{}</td>'.format(i),
|
|
|
|
'<td class="col-b">b{}</td>'.format(i),
|
|
|
|
'<td class="col-c">c{}</td>'.format(i),
|
2017-12-15 12:04:17 +00:00
|
|
|
] for i in range(1, 51)
|
|
|
|
]
|
|
|
|
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
|
|
|
|
|
|
|
|
|
|
|
def test_row_html_no_primary_key(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/no_primary_key/1')
|
2018-04-16 20:22:04 +00:00
|
|
|
assert response.status == 200
|
2017-12-15 12:04:17 +00:00
|
|
|
table = Soup(response.body, 'html.parser').find('table')
|
|
|
|
assert [
|
|
|
|
'rowid', 'content', 'a', 'b', 'c'
|
2018-04-09 02:08:28 +00:00
|
|
|
] == [th.string.strip() for th in table.select('thead th')]
|
2017-12-15 12:04:17 +00:00
|
|
|
expected = [
|
|
|
|
[
|
2018-04-17 08:29:48 +00:00
|
|
|
'<td class="col-rowid">1</td>',
|
|
|
|
'<td class="col-content">1</td>',
|
|
|
|
'<td class="col-a">a1</td>',
|
|
|
|
'<td class="col-b">b1</td>',
|
|
|
|
'<td class="col-c">c1</td>',
|
2017-12-15 12:04:17 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
|
|
|
|
|
|
|
|
|
|
|
def test_table_html_compound_primary_key(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/compound_primary_key')
|
2018-04-16 20:22:04 +00:00
|
|
|
assert response.status == 200
|
2017-12-15 12:04:17 +00:00
|
|
|
table = Soup(response.body, 'html.parser').find('table')
|
2018-04-09 02:08:28 +00:00
|
|
|
ths = table.findAll('th')
|
|
|
|
assert 'Link' == ths[0].string.strip()
|
|
|
|
for expected_col, th in zip(('pk1', 'pk2', 'content'), ths[1:]):
|
|
|
|
a = th.find('a')
|
|
|
|
assert expected_col == a.string
|
2018-04-18 02:11:11 +00:00
|
|
|
assert th['class'] == ['col-{}'.format(expected_col)]
|
2018-04-09 02:08:28 +00:00
|
|
|
assert a['href'].endswith('/compound_primary_key?_sort={}'.format(
|
|
|
|
expected_col
|
|
|
|
))
|
2017-12-15 12:04:17 +00:00
|
|
|
expected = [
|
|
|
|
[
|
2018-06-17 18:34:16 +00:00
|
|
|
'<td class="col-Link"><a href="/fixtures/compound_primary_key/a,b">a,b</a></td>',
|
2018-04-17 08:29:48 +00:00
|
|
|
'<td class="col-pk1">a</td>',
|
|
|
|
'<td class="col-pk2">b</td>',
|
|
|
|
'<td class="col-content">c</td>',
|
2017-12-15 12:04:17 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
|
|
|
|
|
|
|
|
2018-04-14 14:55:27 +00:00
|
|
|
def test_table_html_foreign_key_links(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/foreign_key_references')
|
2018-04-16 20:22:04 +00:00
|
|
|
assert response.status == 200
|
2018-04-14 14:55:27 +00:00
|
|
|
table = Soup(response.body, 'html.parser').find('table')
|
|
|
|
expected = [
|
|
|
|
[
|
2018-06-17 18:34:16 +00:00
|
|
|
'<td class="col-pk"><a href="/fixtures/foreign_key_references/1">1</a></td>',
|
|
|
|
'<td class="col-foreign_key_with_label"><a href="/fixtures/simple_primary_key/1">hello</a>\xa0<em>1</em></td>',
|
|
|
|
'<td class="col-foreign_key_with_no_label"><a href="/fixtures/primary_key_multiple_columns/1">1</a></td>'
|
2018-04-14 14:55:27 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
|
|
|
|
|
|
|
|
?_labels= and ?_label=COL to expand foreign keys in JSON/CSV
These new querystring arguments can be used to request expanded foreign keys
in both JSON and CSV formats.
?_labels=on turns on expansions for ALL foreign key columns
?_label=COLUMN1&_label=COLUMN2 can be used to pick specific columns to expand
e.g. `Street_Tree_List.json?_label=qSpecies&_label=qLegalStatus`
{
"rowid": 233,
"TreeID": 121240,
"qLegalStatus": {
"value" 2,
"label": "Private"
}
"qSpecies": {
"value": 16,
"label": "Sycamore"
}
"qAddress": "91 Commonwealth Ave",
...
}
The labels option also works for the HTML and CSV views.
HTML defaults to `?_labels=on`, so if you pass `?_labels=off` you can disable
foreign key expansion entirely - or you can use `?_label=COLUMN` to request
just specific columns.
If you expand labels on CSV you get additional columns in the output:
`/Street_Tree_List.csv?_label=qLegalStatus`
rowid,TreeID,qLegalStatus,qLegalStatus_label...
1,141565,1,Permitted Site...
2,232565,2,Undocumented...
I also refactored the existing foreign key expansion code.
Closes #233. Refs #266.
2018-06-16 22:18:57 +00:00
|
|
|
def test_table_html_disable_foreign_key_links_with_labels(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/foreign_key_references?_labels=off')
|
?_labels= and ?_label=COL to expand foreign keys in JSON/CSV
These new querystring arguments can be used to request expanded foreign keys
in both JSON and CSV formats.
?_labels=on turns on expansions for ALL foreign key columns
?_label=COLUMN1&_label=COLUMN2 can be used to pick specific columns to expand
e.g. `Street_Tree_List.json?_label=qSpecies&_label=qLegalStatus`
{
"rowid": 233,
"TreeID": 121240,
"qLegalStatus": {
"value" 2,
"label": "Private"
}
"qSpecies": {
"value": 16,
"label": "Sycamore"
}
"qAddress": "91 Commonwealth Ave",
...
}
The labels option also works for the HTML and CSV views.
HTML defaults to `?_labels=on`, so if you pass `?_labels=off` you can disable
foreign key expansion entirely - or you can use `?_label=COLUMN` to request
just specific columns.
If you expand labels on CSV you get additional columns in the output:
`/Street_Tree_List.csv?_label=qLegalStatus`
rowid,TreeID,qLegalStatus,qLegalStatus_label...
1,141565,1,Permitted Site...
2,232565,2,Undocumented...
I also refactored the existing foreign key expansion code.
Closes #233. Refs #266.
2018-06-16 22:18:57 +00:00
|
|
|
assert response.status == 200
|
|
|
|
table = Soup(response.body, 'html.parser').find('table')
|
|
|
|
expected = [
|
|
|
|
[
|
2018-06-17 18:34:16 +00:00
|
|
|
'<td class="col-pk"><a href="/fixtures/foreign_key_references/1">1</a></td>',
|
?_labels= and ?_label=COL to expand foreign keys in JSON/CSV
These new querystring arguments can be used to request expanded foreign keys
in both JSON and CSV formats.
?_labels=on turns on expansions for ALL foreign key columns
?_label=COLUMN1&_label=COLUMN2 can be used to pick specific columns to expand
e.g. `Street_Tree_List.json?_label=qSpecies&_label=qLegalStatus`
{
"rowid": 233,
"TreeID": 121240,
"qLegalStatus": {
"value" 2,
"label": "Private"
}
"qSpecies": {
"value": 16,
"label": "Sycamore"
}
"qAddress": "91 Commonwealth Ave",
...
}
The labels option also works for the HTML and CSV views.
HTML defaults to `?_labels=on`, so if you pass `?_labels=off` you can disable
foreign key expansion entirely - or you can use `?_label=COLUMN` to request
just specific columns.
If you expand labels on CSV you get additional columns in the output:
`/Street_Tree_List.csv?_label=qLegalStatus`
rowid,TreeID,qLegalStatus,qLegalStatus_label...
1,141565,1,Permitted Site...
2,232565,2,Undocumented...
I also refactored the existing foreign key expansion code.
Closes #233. Refs #266.
2018-06-16 22:18:57 +00:00
|
|
|
'<td class="col-foreign_key_with_label">1</td>',
|
|
|
|
'<td class="col-foreign_key_with_no_label">1</td>'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
|
|
|
|
|
|
|
|
2018-04-22 17:51:43 +00:00
|
|
|
def test_table_html_foreign_key_custom_label_column(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/custom_foreign_key_label')
|
2018-04-22 17:51:43 +00:00
|
|
|
assert response.status == 200
|
|
|
|
table = Soup(response.body, 'html.parser').find('table')
|
|
|
|
expected = [
|
|
|
|
[
|
2018-06-17 18:34:16 +00:00
|
|
|
'<td class="col-pk"><a href="/fixtures/custom_foreign_key_label/1">1</a></td>',
|
|
|
|
'<td class="col-foreign_key_with_custom_label"><a href="/fixtures/primary_key_multiple_columns_explicit_label/1">world2</a>\xa0<em>1</em></td>',
|
2018-04-22 17:51:43 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
def test_row_html_compound_primary_key(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/compound_primary_key/a,b')
|
2018-04-16 20:22:04 +00:00
|
|
|
assert response.status == 200
|
2017-12-15 12:04:17 +00:00
|
|
|
table = Soup(response.body, 'html.parser').find('table')
|
|
|
|
assert [
|
|
|
|
'pk1', 'pk2', 'content'
|
2018-04-09 02:08:28 +00:00
|
|
|
] == [th.string.strip() for th in table.select('thead th')]
|
2017-12-15 12:04:17 +00:00
|
|
|
expected = [
|
|
|
|
[
|
2018-04-17 08:29:48 +00:00
|
|
|
'<td class="col-pk1">a</td>',
|
|
|
|
'<td class="col-pk2">b</td>',
|
|
|
|
'<td class="col-content">c</td>',
|
2017-12-15 12:04:17 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
|
|
|
|
|
|
|
|
2018-06-21 14:56:28 +00:00
|
|
|
def test_compound_primary_key_with_foreign_key_references(app_client):
|
|
|
|
# e.g. a many-to-many table with a compound primary key on the two columns
|
|
|
|
response = app_client.get('/fixtures/searchable_tags')
|
|
|
|
assert response.status == 200
|
|
|
|
table = Soup(response.body, 'html.parser').find('table')
|
|
|
|
expected = [
|
|
|
|
[
|
|
|
|
'<td class="col-Link"><a href="/fixtures/searchable_tags/1,feline">1,feline</a></td>',
|
|
|
|
'<td class="col-searchable_id"><a href="/fixtures/searchable/1">1</a>\xa0<em>1</em></td>',
|
|
|
|
'<td class="col-tag"><a href="/fixtures/tags/feline">feline</a></td>',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<td class="col-Link"><a href="/fixtures/searchable_tags/2,canine">2,canine</a></td>',
|
|
|
|
'<td class="col-searchable_id"><a href="/fixtures/searchable/2">2</a>\xa0<em>2</em></td>',
|
|
|
|
'<td class="col-tag"><a href="/fixtures/tags/canine">canine</a></td>',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
def test_view_html(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/simple_view')
|
2018-04-16 20:22:04 +00:00
|
|
|
assert response.status == 200
|
2017-12-15 12:04:17 +00:00
|
|
|
table = Soup(response.body, 'html.parser').find('table')
|
|
|
|
assert [
|
|
|
|
'content', 'upper_content'
|
2018-04-09 02:08:28 +00:00
|
|
|
] == [th.string.strip() for th in table.select('thead th')]
|
2017-12-15 12:04:17 +00:00
|
|
|
expected = [
|
|
|
|
[
|
2018-04-17 08:29:48 +00:00
|
|
|
'<td class="col-content">hello</td>',
|
|
|
|
'<td class="col-upper_content">HELLO</td>'
|
2017-12-15 12:04:17 +00:00
|
|
|
], [
|
2018-04-17 08:29:48 +00:00
|
|
|
'<td class="col-content">world</td>',
|
|
|
|
'<td class="col-upper_content">WORLD</td>'
|
2017-12-15 12:04:17 +00:00
|
|
|
], [
|
2018-06-21 15:26:18 +00:00
|
|
|
'<td class="col-content">\xa0</td>',
|
|
|
|
'<td class="col-upper_content">\xa0</td>'
|
2017-12-15 12:04:17 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
2018-03-27 16:18:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_index_metadata(app_client):
|
2018-06-05 03:53:12 +00:00
|
|
|
response = app_client.get('/')
|
2018-04-16 20:22:04 +00:00
|
|
|
assert response.status == 200
|
2018-03-27 16:18:32 +00:00
|
|
|
soup = Soup(response.body, 'html.parser')
|
2018-06-17 19:16:04 +00:00
|
|
|
assert 'Datasette Fixtures' == soup.find('h1').text
|
|
|
|
assert 'An example SQLite database demonstrating Datasette' == inner_html(
|
2018-03-27 16:18:32 +00:00
|
|
|
soup.find('div', {'class': 'metadata-description'})
|
|
|
|
)
|
|
|
|
assert_footer_links(soup)
|
|
|
|
|
|
|
|
|
|
|
|
def test_database_metadata(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures')
|
2018-04-16 20:22:04 +00:00
|
|
|
assert response.status == 200
|
2018-03-27 16:18:32 +00:00
|
|
|
soup = Soup(response.body, 'html.parser')
|
|
|
|
# Page title should be the default
|
2018-06-17 18:34:16 +00:00
|
|
|
assert 'fixtures' == soup.find('h1').text
|
2018-03-27 16:18:32 +00:00
|
|
|
# Description should be custom
|
|
|
|
assert 'Test tables description' == inner_html(
|
|
|
|
soup.find('div', {'class': 'metadata-description'})
|
|
|
|
)
|
|
|
|
# The source/license should be inherited
|
|
|
|
assert_footer_links(soup)
|
|
|
|
|
|
|
|
|
|
|
|
def test_table_metadata(app_client):
|
2018-06-17 18:34:16 +00:00
|
|
|
response = app_client.get('/fixtures/simple_primary_key')
|
2018-04-16 20:22:04 +00:00
|
|
|
assert response.status == 200
|
2018-03-27 16:18:32 +00:00
|
|
|
soup = Soup(response.body, 'html.parser')
|
|
|
|
# Page title should be custom and should be HTML escaped
|
|
|
|
assert 'This <em>HTML</em> is escaped' == inner_html(soup.find('h1'))
|
|
|
|
# Description should be custom and NOT escaped (we used description_html)
|
|
|
|
assert 'Simple <em>primary</em> key' == inner_html(soup.find(
|
|
|
|
'div', {'class': 'metadata-description'})
|
|
|
|
)
|
|
|
|
# The source/license should be inherited
|
|
|
|
assert_footer_links(soup)
|
|
|
|
|
|
|
|
|
2018-05-25 01:12:27 +00:00
|
|
|
def test_allow_download_on(app_client):
|
|
|
|
response = app_client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
"/fixtures"
|
2018-05-25 01:12:27 +00:00
|
|
|
)
|
|
|
|
soup = Soup(response.body, 'html.parser')
|
|
|
|
assert len(soup.findAll('a', {'href': re.compile('\.db$')}))
|
|
|
|
|
|
|
|
|
|
|
|
def test_allow_download_off():
|
|
|
|
for client in app_client(config={
|
|
|
|
'allow_download': False,
|
|
|
|
}):
|
|
|
|
response = client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
"/fixtures",
|
2018-06-05 03:53:12 +00:00
|
|
|
|
2018-05-25 01:12:27 +00:00
|
|
|
)
|
|
|
|
soup = Soup(response.body, 'html.parser')
|
|
|
|
assert not len(soup.findAll('a', {'href': re.compile('\.db$')}))
|
|
|
|
# Accessing URL directly should 403
|
|
|
|
response = client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
"/fixtures.db",
|
2018-06-05 03:53:12 +00:00
|
|
|
|
2018-05-25 01:12:27 +00:00
|
|
|
)
|
|
|
|
assert 403 == response.status
|
|
|
|
|
|
|
|
|
2018-05-25 05:50:50 +00:00
|
|
|
def test_allow_sql_on(app_client):
|
|
|
|
response = app_client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
"/fixtures"
|
2018-05-25 05:50:50 +00:00
|
|
|
)
|
|
|
|
soup = Soup(response.body, 'html.parser')
|
|
|
|
assert len(soup.findAll('textarea', {'name': 'sql'}))
|
|
|
|
|
|
|
|
|
|
|
|
def test_allow_sql_off():
|
|
|
|
for client in app_client(config={
|
|
|
|
'allow_sql': False,
|
|
|
|
}):
|
|
|
|
response = client.get(
|
2018-06-17 18:34:16 +00:00
|
|
|
"/fixtures"
|
2018-05-25 05:50:50 +00:00
|
|
|
)
|
|
|
|
soup = Soup(response.body, 'html.parser')
|
|
|
|
assert not len(soup.findAll('textarea', {'name': 'sql'}))
|
|
|
|
|
|
|
|
|
2018-05-12 22:49:37 +00:00
|
|
|
def assert_querystring_equal(expected, actual):
|
|
|
|
assert sorted(expected.split('&')) == sorted(actual.split('&'))
|
|
|
|
|
|
|
|
|
2018-03-27 16:18:32 +00:00
|
|
|
def assert_footer_links(soup):
|
|
|
|
footer_links = soup.find('div', {'class': 'ft'}).findAll('a')
|
|
|
|
assert 3 == len(footer_links)
|
|
|
|
datasette_link, license_link, source_link = footer_links
|
|
|
|
assert 'Datasette' == datasette_link.text.strip()
|
2018-06-17 19:16:04 +00:00
|
|
|
assert 'tests/fixtures.py' == source_link.text.strip()
|
|
|
|
assert 'Apache License 2.0' == license_link.text.strip()
|
2018-03-27 16:18:32 +00:00
|
|
|
assert 'https://github.com/simonw/datasette' == datasette_link['href']
|
2018-06-17 19:16:04 +00:00
|
|
|
assert 'https://github.com/simonw/datasette/blob/master/tests/fixtures.py' == source_link['href']
|
|
|
|
assert 'https://github.com/simonw/datasette/blob/master/LICENSE' == license_link['href']
|
2018-03-27 16:18:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def inner_html(soup):
|
|
|
|
html = str(soup)
|
|
|
|
# This includes the parent tag - so remove that
|
|
|
|
inner_html = html.split('>', 1)[1].rsplit('<', 1)[0]
|
|
|
|
return inner_html.strip()
|
2018-06-21 15:13:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('path,expected_redirect', [
|
|
|
|
('/fixtures/', '/fixtures'),
|
|
|
|
('/fixtures/simple_view/', '/fixtures/simple_view'),
|
|
|
|
])
|
|
|
|
def test_404_trailing_slash_redirect(app_client, path, expected_redirect):
|
|
|
|
response = app_client.get(path, allow_redirects=False)
|
|
|
|
assert 302 == response.status
|
|
|
|
assert expected_redirect == response.headers["Location"]
|