2017-12-15 12:04:17 +00:00
|
|
|
|
from bs4 import BeautifulSoup as Soup
|
2019-05-04 02:15:14 +00:00
|
|
|
|
from .fixtures import ( # noqa
|
2018-05-31 13:40:30 +00:00
|
|
|
|
app_client,
|
|
|
|
|
app_client_shorter_time_limit,
|
2019-05-14 15:46:57 +00:00
|
|
|
|
app_client_two_attached_databases,
|
2019-03-17 22:55:04 +00:00
|
|
|
|
app_client_with_hash,
|
2018-11-20 17:16:31 +00:00
|
|
|
|
make_app_client,
|
2019-05-03 20:40:24 +00:00
|
|
|
|
METADATA,
|
2018-05-31 13:40:30 +00:00
|
|
|
|
)
|
2019-05-03 20:40:24 +00:00
|
|
|
|
import json
|
2019-06-24 03:13:09 +00:00
|
|
|
|
import pathlib
|
2017-12-15 12:04:17 +00:00
|
|
|
|
import pytest
|
|
|
|
|
import re
|
2020-04-26 18:46:43 +00:00
|
|
|
|
import textwrap
|
2017-12-15 12:04:17 +00:00
|
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
|
|
|
2019-05-14 15:46:57 +00:00
|
|
|
|
def test_homepage(app_client_two_attached_databases):
|
|
|
|
|
response = app_client_two_attached_databases.get("/")
|
2017-12-15 12:04:17 +00:00
|
|
|
|
assert response.status == 200
|
2019-06-24 03:13:09 +00:00
|
|
|
|
assert "text/html; charset=utf-8" == response.headers["content-type"]
|
2019-05-14 15:46:57 +00:00
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
assert "Datasette Fixtures" == soup.find("h1").text
|
|
|
|
|
assert (
|
|
|
|
|
"An example SQLite database demonstrating Datasette"
|
|
|
|
|
== soup.select(".metadata-description")[0].text.strip()
|
|
|
|
|
)
|
|
|
|
|
# Should be two attached databases
|
|
|
|
|
assert [
|
2019-05-16 00:40:10 +00:00
|
|
|
|
{"href": "/fixtures", "text": "fixtures"},
|
2019-11-04 23:16:30 +00:00
|
|
|
|
{"href": "/extra database", "text": "extra database"},
|
2019-05-14 15:46:57 +00:00
|
|
|
|
] == [{"href": a["href"], "text": a.text.strip()} for a in soup.select("h2 a")]
|
2019-05-16 00:40:10 +00:00
|
|
|
|
# The first attached database should show count text and attached tables
|
2019-10-14 03:52:33 +00:00
|
|
|
|
h2 = soup.select("h2")[1]
|
2019-11-04 23:16:30 +00:00
|
|
|
|
assert "extra database" == h2.text.strip()
|
2019-05-16 00:40:10 +00:00
|
|
|
|
counts_p, links_p = h2.find_all_next("p")[:2]
|
2019-05-16 00:28:07 +00:00
|
|
|
|
assert (
|
2019-05-16 00:40:10 +00:00
|
|
|
|
"2 rows in 1 table, 5 rows in 4 hidden tables, 1 view" == counts_p.text.strip()
|
2019-05-14 15:46:57 +00:00
|
|
|
|
)
|
|
|
|
|
# We should only show visible, not hidden tables here:
|
|
|
|
|
table_links = [
|
|
|
|
|
{"href": a["href"], "text": a.text.strip()} for a in links_p.findAll("a")
|
|
|
|
|
]
|
2019-05-16 00:28:07 +00:00
|
|
|
|
assert [
|
2019-11-04 23:16:30 +00:00
|
|
|
|
{"href": "/extra database/searchable", "text": "searchable"},
|
|
|
|
|
{"href": "/extra database/searchable_view", "text": "searchable_view"},
|
2019-05-16 00:28:07 +00:00
|
|
|
|
] == table_links
|
2017-12-15 12:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
2019-06-24 03:13:09 +00:00
|
|
|
|
def test_http_head(app_client):
|
|
|
|
|
response = app_client.get("/", method="HEAD")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
|
|
|
2020-06-29 04:27:11 +00:00
|
|
|
|
def test_favicon(app_client):
|
|
|
|
|
response = app_client.get("/favicon.ico")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
assert "" == response.text
|
|
|
|
|
|
|
|
|
|
|
2019-06-24 03:13:09 +00:00
|
|
|
|
def test_static(app_client):
|
|
|
|
|
response = app_client.get("/-/static/app2.css")
|
|
|
|
|
assert response.status == 404
|
|
|
|
|
response = app_client.get("/-/static/app.css")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
assert "text/css" == response.headers["content-type"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_static_mounts():
|
2020-06-07 21:14:10 +00:00
|
|
|
|
with make_app_client(
|
2019-06-24 03:13:09 +00:00
|
|
|
|
static_mounts=[("custom-static", str(pathlib.Path(__file__).parent))]
|
2020-06-07 21:14:10 +00:00
|
|
|
|
) as client:
|
2019-06-24 03:13:09 +00:00
|
|
|
|
response = client.get("/custom-static/test_html.py")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
response = client.get("/custom-static/not_exists.py")
|
|
|
|
|
assert response.status == 404
|
2019-07-11 16:13:19 +00:00
|
|
|
|
response = client.get("/custom-static/../LICENSE")
|
|
|
|
|
assert response.status == 404
|
2019-06-24 03:13:09 +00:00
|
|
|
|
|
|
|
|
|
|
2019-05-19 20:41:09 +00:00
|
|
|
|
def test_memory_database_page():
|
2020-06-07 21:14:10 +00:00
|
|
|
|
with make_app_client(memory=True) as client:
|
2019-05-19 20:41:09 +00:00
|
|
|
|
response = client.get("/:memory:")
|
|
|
|
|
assert response.status == 200
|
2019-05-05 18:01:14 +00:00
|
|
|
|
|
|
|
|
|
|
2019-03-17 22:55:04 +00:00
|
|
|
|
def test_database_page_redirects_with_url_hash(app_client_with_hash):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client_with_hash.get("/fixtures", allow_redirects=False)
|
2017-12-15 12:04:17 +00:00
|
|
|
|
assert response.status == 302
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client_with_hash.get("/fixtures")
|
|
|
|
|
assert "fixtures" in response.text
|
2017-12-15 12:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
2020-05-27 21:52:03 +00:00
|
|
|
|
def test_database_page(app_client):
|
|
|
|
|
response = app_client.get("/fixtures")
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
queries_ul = soup.find("h2", text="Queries").find_next_sibling("ul")
|
|
|
|
|
assert queries_ul is not None
|
|
|
|
|
assert [
|
|
|
|
|
(
|
|
|
|
|
"/fixtures/%F0%9D%90%9C%F0%9D%90%A2%F0%9D%90%AD%F0%9D%90%A2%F0%9D%90%9E%F0%9D%90%AC",
|
|
|
|
|
"𝐜𝐢𝐭𝐢𝐞𝐬",
|
|
|
|
|
),
|
2020-06-18 23:22:33 +00:00
|
|
|
|
("/fixtures/from_async_hook", "from_async_hook"),
|
|
|
|
|
("/fixtures/from_hook", "from_hook"),
|
2020-06-28 03:22:49 +00:00
|
|
|
|
("/fixtures/magic_parameters", "magic_parameters"),
|
2020-06-18 23:52:06 +00:00
|
|
|
|
("/fixtures/neighborhood_search#fragment-goes-here", "Search neighborhoods"),
|
|
|
|
|
("/fixtures/pragma_cache_size", "pragma_cache_size"),
|
|
|
|
|
] == sorted(
|
|
|
|
|
[(a["href"], a.text) for a in queries_ul.find_all("a")], key=lambda p: p[0]
|
|
|
|
|
)
|
2020-05-27 21:52:03 +00:00
|
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
|
def test_invalid_custom_sql(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures?sql=.schema")
|
2017-12-15 12:04:17 +00:00
|
|
|
|
assert response.status == 400
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert "Statement must be a SELECT" in response.text
|
2017-12-15 12:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
2018-05-28 21:24:19 +00:00
|
|
|
|
def test_sql_time_limit(app_client_shorter_time_limit):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client_shorter_time_limit.get("/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
|
|
|
|
|
|
|
|
|
|
|
2019-03-17 22:55:04 +00:00
|
|
|
|
def test_row_redirects_with_url_hash(app_client_with_hash):
|
|
|
|
|
response = app_client_with_hash.get(
|
2019-05-04 02:15:14 +00:00
|
|
|
|
"/fixtures/simple_primary_key/1", allow_redirects=False
|
2017-12-15 12:04:17 +00:00
|
|
|
|
)
|
|
|
|
|
assert response.status == 302
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert response.headers["Location"].endswith("/1")
|
|
|
|
|
response = app_client_with_hash.get("/fixtures/simple_primary_key/1")
|
2017-12-15 12:04:17 +00:00
|
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
|
|
|
2019-03-17 22:55:04 +00:00
|
|
|
|
def test_row_strange_table_name_with_url_hash(app_client_with_hash):
|
|
|
|
|
response = app_client_with_hash.get(
|
2019-05-04 02:15:14 +00:00
|
|
|
|
"/fixtures/table%2Fwith%2Fslashes.csv/3", allow_redirects=False
|
2018-07-08 05:21:51 +00:00
|
|
|
|
)
|
|
|
|
|
assert response.status == 302
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert response.headers["Location"].endswith("/table%2Fwith%2Fslashes.csv/3")
|
|
|
|
|
response = app_client_with_hash.get("/fixtures/table%2Fwith%2Fslashes.csv/3")
|
2018-07-08 05:21:51 +00:00
|
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
|
|
|
2019-11-10 01:29:36 +00:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"path,expected_definition_sql",
|
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
"/fixtures/facet_cities",
|
|
|
|
|
"""
|
|
|
|
|
CREATE TABLE facet_cities (
|
|
|
|
|
id integer primary key,
|
|
|
|
|
name text
|
|
|
|
|
);
|
|
|
|
|
""".strip(),
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"/fixtures/compound_three_primary_keys",
|
|
|
|
|
"""
|
|
|
|
|
CREATE TABLE compound_three_primary_keys (
|
|
|
|
|
pk1 varchar(30),
|
|
|
|
|
pk2 varchar(30),
|
|
|
|
|
pk3 varchar(30),
|
|
|
|
|
content text,
|
|
|
|
|
PRIMARY KEY (pk1, pk2, pk3)
|
|
|
|
|
);
|
|
|
|
|
CREATE INDEX idx_compound_three_primary_keys_content ON compound_three_primary_keys(content);
|
|
|
|
|
""".strip(),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
def test_definition_sql(path, expected_definition_sql, app_client):
|
|
|
|
|
response = app_client.get(path)
|
|
|
|
|
pre = Soup(response.body, "html.parser").select_one("pre.wrapped-sql")
|
|
|
|
|
assert expected_definition_sql == pre.string
|
|
|
|
|
|
|
|
|
|
|
2018-07-10 16:20:41 +00:00
|
|
|
|
def test_table_cell_truncation():
|
2020-06-07 21:14:10 +00:00
|
|
|
|
with make_app_client(config={"truncate_cells_html": 5}) as client:
|
2018-07-10 16:20:41 +00:00
|
|
|
|
response = client.get("/fixtures/facetable")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
assert table["class"] == ["rows-and-columns"]
|
|
|
|
|
assert [
|
2019-05-04 02:15:14 +00:00
|
|
|
|
"Missi…",
|
|
|
|
|
"Dogpa…",
|
|
|
|
|
"SOMA",
|
|
|
|
|
"Tende…",
|
|
|
|
|
"Berna…",
|
|
|
|
|
"Hayes…",
|
|
|
|
|
"Holly…",
|
|
|
|
|
"Downt…",
|
|
|
|
|
"Los F…",
|
|
|
|
|
"Korea…",
|
|
|
|
|
"Downt…",
|
|
|
|
|
"Greek…",
|
|
|
|
|
"Corkt…",
|
|
|
|
|
"Mexic…",
|
|
|
|
|
"Arcad…",
|
|
|
|
|
] == [td.string for td in table.findAll("td", {"class": "col-neighborhood"})]
|
2018-07-10 16:20:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_row_page_does_not_truncate():
|
2020-06-07 21:14:10 +00:00
|
|
|
|
with make_app_client(config={"truncate_cells_html": 5}) as client:
|
2018-07-10 16:20:41 +00:00
|
|
|
|
response = client.get("/fixtures/facetable/1")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
assert table["class"] == ["rows-and-columns"]
|
|
|
|
|
assert ["Mission"] == [
|
2019-05-04 02:15:14 +00:00
|
|
|
|
td.string for td in table.findAll("td", {"class": "col-neighborhood"})
|
2018-07-10 16:20:41 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
|
def test_add_filter_redirects(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
filter_args = urllib.parse.urlencode(
|
|
|
|
|
{"_filter_column": "content", "_filter_op": "startswith", "_filter_value": "x"}
|
|
|
|
|
)
|
|
|
|
|
path_base = "/fixtures/simple_primary_key"
|
|
|
|
|
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
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert response.headers["Location"].endswith("?content__startswith=x")
|
2017-12-15 12:04:17 +00:00
|
|
|
|
|
|
|
|
|
# Adding a redirect to an existing querystring:
|
2019-05-04 02:15:14 +00:00
|
|
|
|
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
|
2019-05-04 02:15:14 +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
|
2019-05-04 02:15:14 +00:00
|
|
|
|
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
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert response.headers["Location"].endswith("?content__isnull=5")
|
2017-12-15 12:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_existing_filter_redirects(app_client):
|
|
|
|
|
filter_args = {
|
2019-05-04 02:15:14 +00:00
|
|
|
|
"_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",
|
2017-12-15 12:04:17 +00:00
|
|
|
|
}
|
2019-05-04 02:15:14 +00:00
|
|
|
|
path_base = "/fixtures/simple_primary_key"
|
|
|
|
|
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(
|
2019-05-04 02:15:14 +00:00
|
|
|
|
"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
|
2019-05-04 02:15:14 +00:00
|
|
|
|
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(
|
2019-05-04 02:15:14 +00:00
|
|
|
|
"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
|
2019-05-04 02:15:14 +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
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert "?" not in response.headers["Location"]
|
2017-12-15 12:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_empty_search_parameter_gets_removed(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
path_base = "/fixtures/simple_primary_key"
|
|
|
|
|
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
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert response.headers["Location"].endswith("?name__exact=chidi")
|
2017-12-15 12:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
2019-04-12 04:21:17 +00:00
|
|
|
|
def test_searchable_view_persists_fts_table(app_client):
|
|
|
|
|
# The search form should persist ?_fts_table as a hidden field
|
|
|
|
|
response = app_client.get(
|
|
|
|
|
"/fixtures/searchable_view?_fts_table=searchable_fts&_fts_pk=pk"
|
|
|
|
|
)
|
|
|
|
|
inputs = Soup(response.body, "html.parser").find("form").findAll("input")
|
|
|
|
|
hiddens = [i for i in inputs if i["type"] == "hidden"]
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert [("_fts_table", "searchable_fts"), ("_fts_pk", "pk")] == [
|
|
|
|
|
(hidden["name"], hidden["value"]) for hidden in hiddens
|
2019-04-12 04:21:17 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2018-04-10 00:30:44 +00:00
|
|
|
|
def test_sort_by_desc_redirects(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
path_base = "/fixtures/sortable"
|
|
|
|
|
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
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert response.headers["Location"].endswith("?_sort_desc=sortable")
|
2018-04-10 00:30:44 +00:00
|
|
|
|
|
|
|
|
|
|
2018-05-15 09:34:45 +00:00
|
|
|
|
def test_sort_links(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/sortable?_sort=sortable")
|
2018-05-15 09:34:45 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
ths = Soup(response.body, "html.parser").findAll("th")
|
|
|
|
|
attrs_and_link_attrs = [
|
2018-05-15 09:34:45 +00:00
|
|
|
|
{
|
2019-05-04 02:15:14 +00:00
|
|
|
|
"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},
|
2018-05-15 09:34:45 +00:00
|
|
|
|
{
|
|
|
|
|
"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")
|
2019-05-04 02:15:14 +00:00
|
|
|
|
divs = soup.find("div", {"class": "facet-results"}).findAll("div")
|
2018-06-21 04:30:13 +00:00
|
|
|
|
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):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/facetable?_facet=planet_int&_facet=city_id")
|
2018-05-15 10:28:48 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
inputs = Soup(response.body, "html.parser").find("form").findAll("input")
|
|
|
|
|
hiddens = [i for i in inputs if i["type"] == "hidden"]
|
|
|
|
|
assert [("_facet", "city_id"), ("_facet", "planet_int")] == [
|
|
|
|
|
(hidden["name"], hidden["value"]) for hidden in hiddens
|
2018-05-15 10:28:48 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2019-05-04 02:15:14 +00:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"path,expected_classes",
|
|
|
|
|
[
|
|
|
|
|
("/", ["index"]),
|
|
|
|
|
("/fixtures", ["db", "db-fixtures"]),
|
2020-04-15 21:06:12 +00:00
|
|
|
|
("/fixtures?sql=select+1", ["query", "db-fixtures"]),
|
2019-05-04 02:15:14 +00:00
|
|
|
|
(
|
|
|
|
|
"/fixtures/simple_primary_key",
|
|
|
|
|
["table", "db-fixtures", "table-simple_primary_key"],
|
|
|
|
|
),
|
2020-04-15 21:06:12 +00:00
|
|
|
|
(
|
|
|
|
|
"/fixtures/neighborhood_search",
|
|
|
|
|
["query", "db-fixtures", "query-neighborhood_search"],
|
|
|
|
|
),
|
2019-05-04 02:15:14 +00:00
|
|
|
|
(
|
|
|
|
|
"/fixtures/table%2Fwith%2Fslashes.csv",
|
|
|
|
|
["table", "db-fixtures", "table-tablewithslashescsv-fa7563"],
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"/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
|
|
|
|
|
|
|
|
|
|
|
2020-04-05 19:38:33 +00:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"path,expected_considered",
|
|
|
|
|
[
|
|
|
|
|
("/", "*index.html"),
|
|
|
|
|
("/fixtures", "database-fixtures.html, *database.html"),
|
|
|
|
|
(
|
|
|
|
|
"/fixtures/simple_primary_key",
|
|
|
|
|
"table-fixtures-simple_primary_key.html, *table.html",
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"/fixtures/table%2Fwith%2Fslashes.csv",
|
|
|
|
|
"table-fixtures-tablewithslashescsv-fa7563.html, *table.html",
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"/fixtures/simple_primary_key/1",
|
|
|
|
|
"row-fixtures-simple_primary_key.html, *row.html",
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
def test_templates_considered(app_client, path, expected_considered):
|
|
|
|
|
response = app_client.get(path)
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
assert (
|
|
|
|
|
"<!-- Templates considered: {} -->".format(expected_considered) in response.text
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
|
def test_table_html_simple_primary_key(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/simple_primary_key?_size=3")
|
2018-04-16 20:22:04 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
assert table["class"] == ["rows-and-columns"]
|
|
|
|
|
ths = table.findAll("th")
|
2020-03-21 23:57:37 +00:00
|
|
|
|
assert "id\xa0▼" == ths[0].find("a").string.strip()
|
2019-05-04 02:15:14 +00:00
|
|
|
|
for expected_col, th in zip(("content",), ths[1:]):
|
|
|
|
|
a = th.find("a")
|
2018-04-09 02:08:28 +00:00
|
|
|
|
assert expected_col == a.string
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert a["href"].endswith(
|
|
|
|
|
"/simple_primary_key?_size=3&_sort={}".format(expected_col)
|
|
|
|
|
)
|
|
|
|
|
assert ["nofollow"] == a["rel"]
|
2017-12-15 12:04:17 +00:00
|
|
|
|
assert [
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-id type-pk"><a href="/fixtures/simple_primary_key/1">1</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-content type-str">hello</td>',
|
2019-05-04 02:15:14 +00:00
|
|
|
|
],
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-id type-pk"><a href="/fixtures/simple_primary_key/2">2</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-content type-str">world</td>',
|
2019-05-04 02:15:14 +00:00
|
|
|
|
],
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-id type-pk"><a href="/fixtures/simple_primary_key/3">3</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-content type-str">\xa0</td>',
|
2019-05-04 02:15:14 +00:00
|
|
|
|
],
|
|
|
|
|
] == [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
|
2017-12-15 12:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
2018-06-18 06:03:22 +00:00
|
|
|
|
def test_table_csv_json_export_interface(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/simple_primary_key?id__gt=2")
|
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
|
2019-05-04 02:15:14 +00:00
|
|
|
|
links = (
|
|
|
|
|
Soup(response.body, "html.parser")
|
|
|
|
|
.find("p", {"class": "export-links"})
|
|
|
|
|
.findAll("a")
|
|
|
|
|
)
|
2018-06-15 06:51:23 +00:00
|
|
|
|
actual = [l["href"].split("/")[-1] for l in links]
|
|
|
|
|
expected = [
|
2019-01-03 02:43:56 +00:00
|
|
|
|
"simple_primary_key.json?id__gt=2",
|
2020-05-28 02:21:41 +00:00
|
|
|
|
"simple_primary_key.testall?id__gt=2",
|
|
|
|
|
"simple_primary_key.testnone?id__gt=2",
|
2019-01-03 02:43:56 +00:00
|
|
|
|
"simple_primary_key.csv?id__gt=2&_size=max",
|
2019-05-04 02:15:14 +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:
|
2019-05-04 02:15:14 +00:00
|
|
|
|
div = Soup(response.body, "html.parser").find("div", {"class": "advanced-export"})
|
2018-06-18 06:03:22 +00:00
|
|
|
|
json_links = [a["href"].split("/")[-1] for a in div.find("p").findAll("a")]
|
|
|
|
|
assert [
|
2019-01-03 02:43:56 +00:00
|
|
|
|
"simple_primary_key.json?id__gt=2",
|
|
|
|
|
"simple_primary_key.json?id__gt=2&_shape=array",
|
2019-01-28 01:40:23 +00:00
|
|
|
|
"simple_primary_key.json?id__gt=2&_shape=array&_nl=on",
|
2019-05-04 02:15:14 +00:00
|
|
|
|
"simple_primary_key.json?id__gt=2&_shape=object",
|
2018-06-18 06:03:22 +00:00
|
|
|
|
] == 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"/>',
|
2019-01-03 02:43:56 +00:00
|
|
|
|
'<input name="id__gt" type="hidden" value="2"/>',
|
2019-05-04 02:15:14 +00:00
|
|
|
|
'<input name="_size" type="hidden" value="max"/>',
|
2018-06-18 06:03:22 +00:00
|
|
|
|
] == 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):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/facetable")
|
2018-06-17 22:56:55 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
links = (
|
|
|
|
|
Soup(response.body, "html.parser")
|
|
|
|
|
.find("p", {"class": "export-links"})
|
|
|
|
|
.findAll("a")
|
|
|
|
|
)
|
2018-06-17 22:56:55 +00:00
|
|
|
|
actual = [l["href"].split("/")[-1] for l in links]
|
|
|
|
|
expected = [
|
|
|
|
|
"facetable.json?_labels=on",
|
2020-05-28 02:21:41 +00:00
|
|
|
|
"facetable.testall?_labels=on",
|
|
|
|
|
"facetable.testnone?_labels=on",
|
2018-06-17 22:56:55 +00:00
|
|
|
|
"facetable.csv?_labels=on&_size=max",
|
2019-05-04 02:15:14 +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):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/simple_primary_key/1")
|
2018-04-16 20:22:04 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
assert ["id", "content"] == [th.string.strip() for th in table.select("thead th")]
|
2020-05-15 05:51:39 +00:00
|
|
|
|
assert [
|
|
|
|
|
[
|
|
|
|
|
'<td class="col-id type-str">1</td>',
|
|
|
|
|
'<td class="col-content type-str">hello</td>',
|
|
|
|
|
]
|
|
|
|
|
] == [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
|
2017-12-15 12:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
2018-04-13 18:17:22 +00:00
|
|
|
|
def test_table_not_exists(app_client):
|
2020-06-02 21:29:12 +00:00
|
|
|
|
assert "Table not found: blah" in app_client.get("/fixtures/blah").text
|
2018-04-13 18:17:22 +00:00
|
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
|
def test_table_html_no_primary_key(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/no_primary_key")
|
2018-04-16 20:22:04 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +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
|
2019-05-04 02:15:14 +00:00
|
|
|
|
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 = [
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-Link type-pk"><a href="/fixtures/no_primary_key/{}">{}</a></td>'.format(
|
2019-05-04 02:15:14 +00:00
|
|
|
|
i, i
|
|
|
|
|
),
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-rowid type-int">{}</td>'.format(i),
|
|
|
|
|
'<td class="col-content type-str">{}</td>'.format(i),
|
|
|
|
|
'<td class="col-a type-str">a{}</td>'.format(i),
|
|
|
|
|
'<td class="col-b type-str">b{}</td>'.format(i),
|
|
|
|
|
'<td class="col-c type-str">c{}</td>'.format(i),
|
2019-05-04 02:15:14 +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")
|
2017-12-15 12:04:17 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2020-03-21 23:57:37 +00:00
|
|
|
|
def test_rowid_sortable_no_primary_key(app_client):
|
|
|
|
|
response = app_client.get("/fixtures/no_primary_key")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
assert table["class"] == ["rows-and-columns"]
|
|
|
|
|
ths = table.findAll("th")
|
|
|
|
|
assert "rowid\xa0▼" == ths[1].find("a").string.strip()
|
|
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
|
def test_row_html_no_primary_key(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/no_primary_key/1")
|
2018-04-16 20:22:04 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
assert ["rowid", "content", "a", "b", "c"] == [
|
|
|
|
|
th.string.strip() for th in table.select("thead th")
|
|
|
|
|
]
|
2017-12-15 12:04:17 +00:00
|
|
|
|
expected = [
|
|
|
|
|
[
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-rowid type-int">1</td>',
|
|
|
|
|
'<td class="col-content type-str">1</td>',
|
|
|
|
|
'<td class="col-a type-str">a1</td>',
|
|
|
|
|
'<td class="col-b type-str">b1</td>',
|
|
|
|
|
'<td class="col-c type-str">c1</td>',
|
2017-12-15 12:04:17 +00:00
|
|
|
|
]
|
|
|
|
|
]
|
2019-05-04 02:15:14 +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_table_html_compound_primary_key(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/compound_primary_key")
|
2018-04-16 20:22:04 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
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")
|
2018-04-09 02:08:28 +00:00
|
|
|
|
assert expected_col == a.string
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert th["class"] == ["col-{}".format(expected_col)]
|
|
|
|
|
assert a["href"].endswith("/compound_primary_key?_sort={}".format(expected_col))
|
2017-12-15 12:04:17 +00:00
|
|
|
|
expected = [
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-Link type-pk"><a href="/fixtures/compound_primary_key/a,b">a,b</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-pk1 type-str">a</td>',
|
|
|
|
|
'<td class="col-pk2 type-str">b</td>',
|
|
|
|
|
'<td class="col-content type-str">c</td>',
|
2017-12-15 12:04:17 +00:00
|
|
|
|
]
|
|
|
|
|
]
|
2019-05-04 02:15:14 +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
|
|
|
|
|
|
|
|
|
|
2018-04-14 14:55:27 +00:00
|
|
|
|
def test_table_html_foreign_key_links(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/foreign_key_references")
|
2018-04-16 20:22:04 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
2018-04-14 14:55:27 +00:00
|
|
|
|
expected = [
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-pk type-pk"><a href="/fixtures/foreign_key_references/1">1</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-foreign_key_with_label type-str"><a href="/fixtures/simple_primary_key/1">hello</a>\xa0<em>1</em></td>',
|
|
|
|
|
'<td class="col-foreign_key_with_no_label type-str"><a href="/fixtures/primary_key_multiple_columns/1">1</a></td>',
|
2019-11-02 22:29:40 +00:00
|
|
|
|
],
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-pk type-pk"><a href="/fixtures/foreign_key_references/2">2</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-foreign_key_with_label type-none">\xa0</td>',
|
|
|
|
|
'<td class="col-foreign_key_with_no_label type-none">\xa0</td>',
|
2019-11-02 22:29:40 +00:00
|
|
|
|
],
|
2018-04-14 14:55:27 +00:00
|
|
|
|
]
|
2019-05-04 02:15:14 +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
|
|
|
|
|
|
|
|
|
|
?_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):
|
2019-11-02 22:29:40 +00:00
|
|
|
|
response = app_client.get("/fixtures/foreign_key_references?_labels=off&_size=1")
|
?_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
|
2019-05-04 02:15:14 +00:00
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
?_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
|
|
|
|
expected = [
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-pk type-pk"><a href="/fixtures/foreign_key_references/1">1</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-foreign_key_with_label type-str">1</td>',
|
|
|
|
|
'<td class="col-foreign_key_with_no_label type-str">1</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
|
|
|
|
]
|
|
|
|
|
]
|
2019-05-04 02:15:14 +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
|
|
|
|
|
|
|
|
|
|
2018-04-22 17:51:43 +00:00
|
|
|
|
def test_table_html_foreign_key_custom_label_column(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/custom_foreign_key_label")
|
2018-04-22 17:51:43 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
2018-04-22 17:51:43 +00:00
|
|
|
|
expected = [
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-pk type-pk"><a href="/fixtures/custom_foreign_key_label/1">1</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-foreign_key_with_custom_label type-str"><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
|
|
|
|
]
|
|
|
|
|
]
|
2019-05-04 02:15:14 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
2019-11-19 23:01:10 +00:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"path,expected_column_options",
|
|
|
|
|
[
|
|
|
|
|
("/fixtures/infinity", ["- column -", "rowid", "value"]),
|
|
|
|
|
(
|
|
|
|
|
"/fixtures/primary_key_multiple_columns",
|
|
|
|
|
["- column -", "id", "content", "content2"],
|
|
|
|
|
),
|
|
|
|
|
("/fixtures/compound_primary_key", ["- column -", "pk1", "pk2", "content"]),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
def test_table_html_filter_form_column_options(
|
|
|
|
|
path, expected_column_options, app_client
|
|
|
|
|
):
|
|
|
|
|
response = app_client.get(path)
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
form = Soup(response.body, "html.parser").find("form")
|
|
|
|
|
column_options = [
|
|
|
|
|
o.attrs.get("value") or o.string
|
|
|
|
|
for o in form.select("select[name=_filter_column] option")
|
|
|
|
|
]
|
|
|
|
|
assert expected_column_options == column_options
|
|
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
|
def test_row_html_compound_primary_key(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/compound_primary_key/a,b")
|
2018-04-16 20:22:04 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
assert ["pk1", "pk2", "content"] == [
|
|
|
|
|
th.string.strip() for th in table.select("thead th")
|
|
|
|
|
]
|
2017-12-15 12:04:17 +00:00
|
|
|
|
expected = [
|
|
|
|
|
[
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-pk1 type-str">a</td>',
|
|
|
|
|
'<td class="col-pk2 type-str">b</td>',
|
|
|
|
|
'<td class="col-content type-str">c</td>',
|
2017-12-15 12:04:17 +00:00
|
|
|
|
]
|
|
|
|
|
]
|
2019-05-04 02:15:14 +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
|
|
|
|
|
|
|
|
|
|
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
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/searchable_tags")
|
2018-06-21 14:56:28 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
2018-06-21 14:56:28 +00:00
|
|
|
|
expected = [
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-Link type-pk"><a href="/fixtures/searchable_tags/1,feline">1,feline</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-searchable_id type-int"><a href="/fixtures/searchable/1">1</a>\xa0<em>1</em></td>',
|
|
|
|
|
'<td class="col-tag type-str"><a href="/fixtures/tags/feline">feline</a></td>',
|
2018-06-21 14:56:28 +00:00
|
|
|
|
],
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-Link type-pk"><a href="/fixtures/searchable_tags/2,canine">2,canine</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-searchable_id type-int"><a href="/fixtures/searchable/2">2</a>\xa0<em>2</em></td>',
|
|
|
|
|
'<td class="col-tag type-str"><a href="/fixtures/tags/canine">canine</a></td>',
|
2018-06-21 14:56:28 +00:00
|
|
|
|
],
|
|
|
|
|
]
|
2019-05-04 02:15:14 +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
|
|
|
|
|
|
|
|
|
|
2017-12-15 12:04:17 +00:00
|
|
|
|
def test_view_html(app_client):
|
2018-08-28 10:03:01 +00:00
|
|
|
|
response = app_client.get("/fixtures/simple_view?_size=3")
|
2018-04-16 20:22:04 +00:00
|
|
|
|
assert response.status == 200
|
2018-08-06 00:29:23 +00:00
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
ths = table.select("thead th")
|
|
|
|
|
assert 2 == len(ths)
|
|
|
|
|
assert ths[0].find("a") is not None
|
2018-08-28 10:03:01 +00:00
|
|
|
|
assert ths[0].find("a")["href"].endswith("/simple_view?_size=3&_sort=content")
|
2018-08-06 00:29:23 +00:00
|
|
|
|
assert ths[0].find("a").string.strip() == "content"
|
|
|
|
|
assert ths[1].find("a") is None
|
|
|
|
|
assert ths[1].string.strip() == "upper_content"
|
2017-12-15 12:04:17 +00:00
|
|
|
|
expected = [
|
|
|
|
|
[
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-content type-str">hello</td>',
|
|
|
|
|
'<td class="col-upper_content type-str">HELLO</td>',
|
2018-08-06 00:29:23 +00:00
|
|
|
|
],
|
|
|
|
|
[
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-content type-str">world</td>',
|
|
|
|
|
'<td class="col-upper_content type-str">WORLD</td>',
|
2018-08-06 00:29:23 +00:00
|
|
|
|
],
|
|
|
|
|
[
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-content type-str">\xa0</td>',
|
|
|
|
|
'<td class="col-upper_content type-str">\xa0</td>',
|
2018-08-06 00:29:23 +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
|
|
|
|
]
|
2018-03-27 16:18:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_index_metadata(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/")
|
2018-04-16 20:22:04 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
assert "Datasette Fixtures" == soup.find("h1").text
|
|
|
|
|
assert "An example SQLite database demonstrating Datasette" == inner_html(
|
|
|
|
|
soup.find("div", {"class": "metadata-description"})
|
2018-03-27 16:18:32 +00:00
|
|
|
|
)
|
|
|
|
|
assert_footer_links(soup)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_database_metadata(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures")
|
2018-04-16 20:22:04 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
soup = Soup(response.body, "html.parser")
|
2018-03-27 16:18:32 +00:00
|
|
|
|
# Page title should be the default
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert "fixtures" == soup.find("h1").text
|
2018-03-27 16:18:32 +00:00
|
|
|
|
# Description should be custom
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert "Test tables description" == inner_html(
|
|
|
|
|
soup.find("div", {"class": "metadata-description"})
|
2018-03-27 16:18:32 +00:00
|
|
|
|
)
|
|
|
|
|
# The source/license should be inherited
|
|
|
|
|
assert_footer_links(soup)
|
|
|
|
|
|
|
|
|
|
|
2019-10-14 03:53:21 +00:00
|
|
|
|
def test_database_metadata_with_custom_sql(app_client):
|
|
|
|
|
response = app_client.get("/fixtures?sql=select+*+from+simple_primary_key")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
# Page title should be the default
|
|
|
|
|
assert "fixtures" == soup.find("h1").text
|
|
|
|
|
# Description should be custom
|
|
|
|
|
assert "Custom SQL query returning" in soup.find("h3").text
|
|
|
|
|
# The source/license should be inherited
|
|
|
|
|
assert_footer_links(soup)
|
|
|
|
|
|
|
|
|
|
|
2018-03-27 16:18:32 +00:00
|
|
|
|
def test_table_metadata(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/simple_primary_key")
|
2018-04-16 20:22:04 +00:00
|
|
|
|
assert response.status == 200
|
2019-05-04 02:15:14 +00:00
|
|
|
|
soup = Soup(response.body, "html.parser")
|
2018-03-27 16:18:32 +00:00
|
|
|
|
# Page title should be custom and should be HTML escaped
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert "This <em>HTML</em> is escaped" == inner_html(soup.find("h1"))
|
2018-03-27 16:18:32 +00:00
|
|
|
|
# Description should be custom and NOT escaped (we used description_html)
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert "Simple <em>primary</em> key" == inner_html(
|
|
|
|
|
soup.find("div", {"class": "metadata-description"})
|
2018-03-27 16:18:32 +00:00
|
|
|
|
)
|
|
|
|
|
# The source/license should be inherited
|
|
|
|
|
assert_footer_links(soup)
|
|
|
|
|
|
|
|
|
|
|
2019-05-19 20:41:09 +00:00
|
|
|
|
def test_database_download_allowed_for_immutable():
|
2020-06-07 21:14:10 +00:00
|
|
|
|
with make_app_client(is_immutable=True) as client:
|
2019-05-19 20:41:09 +00:00
|
|
|
|
assert not client.ds.databases["fixtures"].is_mutable
|
|
|
|
|
# Regular page should have a download link
|
|
|
|
|
response = client.get("/fixtures")
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
assert len(soup.findAll("a", {"href": re.compile(r"\.db$")}))
|
|
|
|
|
# Check we can actually download it
|
|
|
|
|
assert 200 == client.get("/fixtures.db").status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_database_download_disallowed_for_mutable(app_client):
|
|
|
|
|
response = app_client.get("/fixtures")
|
2019-05-04 02:15:14 +00:00
|
|
|
|
soup = Soup(response.body, "html.parser")
|
2019-05-19 20:41:09 +00:00
|
|
|
|
assert 0 == len(soup.findAll("a", {"href": re.compile(r"\.db$")}))
|
|
|
|
|
assert 403 == app_client.get("/fixtures.db").status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_database_download_disallowed_for_memory():
|
2020-06-07 21:14:10 +00:00
|
|
|
|
with make_app_client(memory=True) as client:
|
2019-05-19 20:41:09 +00:00
|
|
|
|
# Memory page should NOT have a download link
|
|
|
|
|
response = client.get("/:memory:")
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
assert 0 == len(soup.findAll("a", {"href": re.compile(r"\.db$")}))
|
|
|
|
|
assert 404 == client.get("/:memory:.db").status
|
2018-05-25 01:12:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_allow_download_off():
|
2020-06-07 21:14:10 +00:00
|
|
|
|
with make_app_client(is_immutable=True, config={"allow_download": False}) as client:
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = client.get("/fixtures")
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
assert not len(soup.findAll("a", {"href": re.compile(r"\.db$")}))
|
2018-05-25 01:12:27 +00:00
|
|
|
|
# Accessing URL directly should 403
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = client.get("/fixtures.db")
|
2018-05-25 01:12:27 +00:00
|
|
|
|
assert 403 == response.status
|
|
|
|
|
|
|
|
|
|
|
2018-05-25 05:50:50 +00:00
|
|
|
|
def test_allow_sql_off():
|
2020-06-09 00:05:44 +00:00
|
|
|
|
with make_app_client(metadata={"allow_sql": {}}) as client:
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = client.get("/fixtures")
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
assert not len(soup.findAll("textarea", {"name": "sql"}))
|
2018-08-06 03:17:17 +00:00
|
|
|
|
# The table page should no longer show "View and edit SQL"
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = client.get("/fixtures/sortable")
|
2018-08-06 03:17:17 +00:00
|
|
|
|
assert b"View and edit SQL" not in response.body
|
2018-05-25 05:50:50 +00:00
|
|
|
|
|
|
|
|
|
|
2018-05-12 22:49:37 +00:00
|
|
|
|
def assert_querystring_equal(expected, actual):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert sorted(expected.split("&")) == sorted(actual.split("&"))
|
2018-05-12 22:49:37 +00:00
|
|
|
|
|
|
|
|
|
|
2018-03-27 16:18:32 +00:00
|
|
|
|
def assert_footer_links(soup):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
footer_links = soup.find("div", {"class": "ft"}).findAll("a")
|
2019-03-10 21:37:11 +00:00
|
|
|
|
assert 4 == len(footer_links)
|
|
|
|
|
datasette_link, license_link, source_link, about_link = footer_links
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert "Datasette" == datasette_link.text.strip()
|
|
|
|
|
assert "tests/fixtures.py" == source_link.text.strip()
|
|
|
|
|
assert "Apache License 2.0" == license_link.text.strip()
|
|
|
|
|
assert "About Datasette" == about_link.text.strip()
|
|
|
|
|
assert "https://github.com/simonw/datasette" == datasette_link["href"]
|
|
|
|
|
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"]
|
|
|
|
|
)
|
|
|
|
|
assert "https://github.com/simonw/datasette" == about_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
|
2019-05-04 02:15:14 +00:00
|
|
|
|
inner_html = html.split(">", 1)[1].rsplit("<", 1)[0]
|
2018-03-27 16:18:32 +00:00
|
|
|
|
return inner_html.strip()
|
2018-06-21 15:13:07 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-09 00:35:23 +00:00
|
|
|
|
@pytest.mark.parametrize("path", ["/404", "/fixtures/404"])
|
|
|
|
|
def test_404(app_client, path):
|
|
|
|
|
response = app_client.get(path)
|
|
|
|
|
assert 404 == response.status
|
|
|
|
|
assert (
|
|
|
|
|
'<link rel="stylesheet" href="/-/static/app.css?{}'.format(
|
|
|
|
|
app_client.ds.app_css_hash()
|
|
|
|
|
)
|
|
|
|
|
in response.text
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2019-05-04 02:15:14 +00:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"path,expected_redirect",
|
|
|
|
|
[("/fixtures/", "/fixtures"), ("/fixtures/simple_view/", "/fixtures/simple_view")],
|
|
|
|
|
)
|
2018-06-21 15:13:07 +00:00
|
|
|
|
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"]
|
2018-07-16 02:33:30 +00:00
|
|
|
|
|
|
|
|
|
|
2020-05-04 17:41:58 +00:00
|
|
|
|
def test_404_content_type(app_client):
|
|
|
|
|
response = app_client.get("/404")
|
|
|
|
|
assert 404 == response.status
|
|
|
|
|
assert "text/html; charset=utf-8" == response.headers["content-type"]
|
|
|
|
|
|
|
|
|
|
|
2020-07-02 00:23:37 +00:00
|
|
|
|
def test_canned_query_default_title(app_client):
|
|
|
|
|
response = app_client.get("/fixtures/magic_parameters")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
assert "fixtures: magic_parameters" == soup.find("h1").text
|
|
|
|
|
|
|
|
|
|
|
2018-07-16 02:33:30 +00:00
|
|
|
|
def test_canned_query_with_custom_metadata(app_client):
|
|
|
|
|
response = app_client.get("/fixtures/neighborhood_search?text=town")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
assert "Search neighborhoods" == soup.find("h1").text
|
|
|
|
|
assert (
|
|
|
|
|
"""
|
|
|
|
|
<div class="metadata-description">
|
|
|
|
|
<b>
|
|
|
|
|
Demonstrating
|
|
|
|
|
</b>
|
|
|
|
|
simple like search
|
|
|
|
|
</div>""".strip()
|
|
|
|
|
== soup.find("div", {"class": "metadata-description"}).prettify().strip()
|
|
|
|
|
)
|
2018-07-24 03:38:31 +00:00
|
|
|
|
|
|
|
|
|
|
2019-05-04 02:15:14 +00:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"path,has_object,has_stream,has_expand",
|
|
|
|
|
[
|
|
|
|
|
("/fixtures/no_primary_key", False, True, False),
|
|
|
|
|
("/fixtures/complex_foreign_keys", True, False, True),
|
|
|
|
|
],
|
|
|
|
|
)
|
2018-07-24 03:38:31 +00:00
|
|
|
|
def test_advanced_export_box(app_client, path, has_object, has_stream, has_expand):
|
|
|
|
|
response = app_client.get(path)
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
# JSON shape options
|
2019-01-28 01:40:23 +00:00
|
|
|
|
expected_json_shapes = ["default", "array", "newline-delimited"]
|
2018-07-24 03:38:31 +00:00
|
|
|
|
if has_object:
|
|
|
|
|
expected_json_shapes.append("object")
|
|
|
|
|
div = soup.find("div", {"class": "advanced-export"})
|
|
|
|
|
assert expected_json_shapes == [a.text for a in div.find("p").findAll("a")]
|
|
|
|
|
# "stream all rows" option
|
|
|
|
|
if has_stream:
|
|
|
|
|
assert "stream all rows" in str(div)
|
|
|
|
|
# "expand labels" option
|
|
|
|
|
if has_expand:
|
|
|
|
|
assert "expand labels" in str(div)
|
2018-07-24 03:56:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_urlify_custom_queries(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
path = "/fixtures?" + urllib.parse.urlencode(
|
|
|
|
|
{"sql": "select ('https://twitter.com/' || 'simonw') as user_url;"}
|
|
|
|
|
)
|
2018-07-24 03:56:32 +00:00
|
|
|
|
response = app_client.get(path)
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert (
|
|
|
|
|
"""<td class="col-user_url">
|
2018-07-24 03:56:32 +00:00
|
|
|
|
<a href="https://twitter.com/simonw">
|
|
|
|
|
https://twitter.com/simonw
|
|
|
|
|
</a>
|
2019-05-04 02:15:14 +00:00
|
|
|
|
</td>"""
|
|
|
|
|
== soup.find("td", {"class": "col-user_url"}).prettify().strip()
|
|
|
|
|
)
|
2019-03-15 05:22:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_show_hide_sql_query(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
path = "/fixtures?" + urllib.parse.urlencode(
|
|
|
|
|
{"sql": "select ('https://twitter.com/' || 'simonw') as user_url;"}
|
|
|
|
|
)
|
2019-03-15 05:22:24 +00:00
|
|
|
|
response = app_client.get(path)
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
span = soup.select(".show-hide-sql")[0]
|
|
|
|
|
assert span.find("a")["href"].endswith("&_hide_sql=1")
|
|
|
|
|
assert "(hide)" == span.getText()
|
|
|
|
|
assert soup.find("textarea") is not None
|
|
|
|
|
# Now follow the link to hide it
|
|
|
|
|
response = app_client.get(span.find("a")["href"])
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
span = soup.select(".show-hide-sql")[0]
|
|
|
|
|
assert not span.find("a")["href"].endswith("&_hide_sql=1")
|
|
|
|
|
assert "(show)" == span.getText()
|
|
|
|
|
assert soup.find("textarea") is None
|
2019-04-12 05:00:47 +00:00
|
|
|
|
# The SQL should still be there in a hidden form field
|
|
|
|
|
hiddens = soup.find("form").select("input[type=hidden]")
|
|
|
|
|
assert [
|
2019-05-04 02:15:14 +00:00
|
|
|
|
("sql", "select ('https://twitter.com/' || 'simonw') as user_url;"),
|
|
|
|
|
("_hide_sql", "1"),
|
|
|
|
|
] == [(hidden["name"], hidden["value"]) for hidden in hiddens]
|
2019-04-13 01:37:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_extra_where_clauses(app_client):
|
|
|
|
|
response = app_client.get(
|
|
|
|
|
"/fixtures/facetable?_where=neighborhood='Dogpatch'&_where=city_id=1"
|
|
|
|
|
)
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
div = soup.select(".extra-wheres")[0]
|
|
|
|
|
assert "2 extra where clauses" == div.find("h3").text
|
|
|
|
|
hrefs = [a["href"] for a in div.findAll("a")]
|
|
|
|
|
assert [
|
|
|
|
|
"/fixtures/facetable?_where=city_id%3D1",
|
2019-05-04 02:15:14 +00:00
|
|
|
|
"/fixtures/facetable?_where=neighborhood%3D%27Dogpatch%27",
|
2019-04-13 01:37:22 +00:00
|
|
|
|
] == hrefs
|
2019-10-30 18:49:26 +00:00
|
|
|
|
# These should also be persisted as hidden fields
|
|
|
|
|
inputs = soup.find("form").findAll("input")
|
|
|
|
|
hiddens = [i for i in inputs if i["type"] == "hidden"]
|
|
|
|
|
assert [("_where", "neighborhood='Dogpatch'"), ("_where", "city_id=1")] == [
|
|
|
|
|
(hidden["name"], hidden["value"]) for hidden in hiddens
|
|
|
|
|
]
|
2019-05-03 16:43:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_binary_data_display(app_client):
|
|
|
|
|
response = app_client.get("/fixtures/binary_data")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
expected_tds = [
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-Link type-pk"><a href="/fixtures/binary_data/1">1</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-rowid type-int">1</td>',
|
|
|
|
|
'<td class="col-data type-bytes"><Binary\xa0data:\xa019\xa0bytes></td>',
|
2019-05-03 16:43:59 +00:00
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
assert expected_tds == [
|
|
|
|
|
[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")
|
|
|
|
|
]
|
2019-05-03 20:40:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_metadata_json_html(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/-/metadata")
|
2019-05-03 20:40:24 +00:00
|
|
|
|
assert response.status == 200
|
|
|
|
|
pre = Soup(response.body, "html.parser").find("pre")
|
|
|
|
|
assert METADATA == json.loads(pre.text)
|
2019-07-03 03:06:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_custom_table_include():
|
2020-06-07 21:14:10 +00:00
|
|
|
|
with make_app_client(
|
2019-07-03 03:06:22 +00:00
|
|
|
|
template_dir=str(pathlib.Path(__file__).parent / "test_templates")
|
2020-06-07 21:14:10 +00:00
|
|
|
|
) as client:
|
2019-07-03 03:06:22 +00:00
|
|
|
|
response = client.get("/fixtures/complex_foreign_keys")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
assert (
|
|
|
|
|
'<div class="custom-table-row">'
|
|
|
|
|
'1 - 2 - <a href="/fixtures/simple_primary_key/1">hello</a> <em>1</em>'
|
|
|
|
|
"</div>"
|
|
|
|
|
) == str(Soup(response.text, "html.parser").select_one("div.custom-table-row"))
|
2019-11-23 06:07:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"path",
|
|
|
|
|
[
|
|
|
|
|
"/fixtures?sql=select+*+from+[123_starts_with_digits]",
|
|
|
|
|
"/fixtures/123_starts_with_digits",
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
def test_zero_results(app_client, path):
|
|
|
|
|
response = app_client.get(path)
|
|
|
|
|
soup = Soup(response.text, "html.parser")
|
|
|
|
|
assert 0 == len(soup.select("table"))
|
|
|
|
|
assert 1 == len(soup.select("p.zero-results"))
|
2019-12-22 16:04:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_template_debug_on():
|
2020-06-07 21:14:10 +00:00
|
|
|
|
with make_app_client(config={"template_debug": True}) as client:
|
2019-12-22 16:04:45 +00:00
|
|
|
|
response = client.get("/fixtures/facetable?_context=1")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
assert response.text.startswith("<pre>{")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_template_debug_off(app_client):
|
|
|
|
|
response = app_client.get("/fixtures/facetable?_context=1")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
assert not response.text.startswith("<pre>{")
|
2020-03-22 02:28:35 +00:00
|
|
|
|
|
|
|
|
|
|
2020-04-05 18:49:15 +00:00
|
|
|
|
def test_debug_context_includes_extra_template_vars():
|
|
|
|
|
# https://github.com/simonw/datasette/issues/693
|
2020-06-07 21:14:10 +00:00
|
|
|
|
with make_app_client(config={"template_debug": True}) as client:
|
2020-04-05 18:49:15 +00:00
|
|
|
|
response = client.get("/fixtures/facetable?_context=1")
|
|
|
|
|
# scope_path is added by PLUGIN1
|
|
|
|
|
assert "scope_path" in response.text
|
|
|
|
|
|
|
|
|
|
|
2020-03-22 02:28:35 +00:00
|
|
|
|
def test_metadata_sort(app_client):
|
|
|
|
|
response = app_client.get("/fixtures/facet_cities")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
assert table["class"] == ["rows-and-columns"]
|
|
|
|
|
ths = table.findAll("th")
|
|
|
|
|
assert ["id", "name\xa0▼"] == [th.find("a").string.strip() for th in ths]
|
|
|
|
|
rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
|
2020-03-22 02:40:29 +00:00
|
|
|
|
expected = [
|
2020-03-22 02:28:35 +00:00
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-id type-pk"><a href="/fixtures/facet_cities/3">3</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-name type-str">Detroit</td>',
|
2020-03-22 02:28:35 +00:00
|
|
|
|
],
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-id type-pk"><a href="/fixtures/facet_cities/2">2</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-name type-str">Los Angeles</td>',
|
2020-03-22 02:28:35 +00:00
|
|
|
|
],
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-id type-pk"><a href="/fixtures/facet_cities/4">4</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-name type-str">Memnonia</td>',
|
2020-03-22 02:28:35 +00:00
|
|
|
|
],
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-id type-pk"><a href="/fixtures/facet_cities/1">1</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-name type-str">San Francisco</td>',
|
2020-03-22 02:28:35 +00:00
|
|
|
|
],
|
2020-03-22 02:40:29 +00:00
|
|
|
|
]
|
|
|
|
|
assert expected == rows
|
|
|
|
|
# Make sure you can reverse that sort order
|
|
|
|
|
response = app_client.get("/fixtures/facet_cities?_sort_desc=name")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
|
|
|
|
|
assert list(reversed(expected)) == rows
|
2020-03-22 02:28:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_metadata_sort_desc(app_client):
|
|
|
|
|
response = app_client.get("/fixtures/attraction_characteristic")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
assert table["class"] == ["rows-and-columns"]
|
|
|
|
|
ths = table.findAll("th")
|
|
|
|
|
assert ["pk\xa0▲", "name"] == [th.find("a").string.strip() for th in ths]
|
|
|
|
|
rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
|
2020-03-22 02:40:29 +00:00
|
|
|
|
expected = [
|
2020-03-22 02:28:35 +00:00
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-pk type-pk"><a href="/fixtures/attraction_characteristic/2">2</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-name type-str">Paranormal</td>',
|
2020-03-22 02:28:35 +00:00
|
|
|
|
],
|
|
|
|
|
[
|
2020-05-15 05:55:20 +00:00
|
|
|
|
'<td class="col-pk type-pk"><a href="/fixtures/attraction_characteristic/1">1</a></td>',
|
2020-05-15 05:51:39 +00:00
|
|
|
|
'<td class="col-name type-str">Museum</td>',
|
2020-03-22 02:28:35 +00:00
|
|
|
|
],
|
2020-03-22 02:40:29 +00:00
|
|
|
|
]
|
|
|
|
|
assert expected == rows
|
|
|
|
|
# Make sure you can reverse that sort order
|
|
|
|
|
response = app_client.get("/fixtures/attraction_characteristic?_sort=pk")
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
table = Soup(response.body, "html.parser").find("table")
|
|
|
|
|
rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
|
|
|
|
|
assert list(reversed(expected)) == rows
|
2020-03-25 00:18:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("base_url", ["/prefix/", "https://example.com/"])
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"path",
|
|
|
|
|
[
|
|
|
|
|
"/",
|
|
|
|
|
"/fixtures",
|
|
|
|
|
"/fixtures/compound_three_primary_keys",
|
|
|
|
|
"/fixtures/compound_three_primary_keys/a,a,a",
|
|
|
|
|
"/fixtures/paginated_view",
|
2020-03-26 02:31:22 +00:00
|
|
|
|
"/fixtures/facetable",
|
2020-03-25 00:18:43 +00:00
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
def test_base_url_config(base_url, path):
|
2020-06-07 21:14:10 +00:00
|
|
|
|
with make_app_client(config={"base_url": base_url}) as client:
|
2020-03-25 00:18:43 +00:00
|
|
|
|
response = client.get(base_url + path.lstrip("/"))
|
|
|
|
|
soup = Soup(response.body, "html.parser")
|
|
|
|
|
for el in soup.findAll(["a", "link", "script"]):
|
|
|
|
|
if "href" in el.attrs:
|
|
|
|
|
href = el["href"]
|
|
|
|
|
elif "src" in el.attrs:
|
|
|
|
|
href = el["src"]
|
|
|
|
|
else:
|
|
|
|
|
continue # Could be a <script>...</script>
|
|
|
|
|
if (
|
|
|
|
|
not href.startswith("#")
|
|
|
|
|
and href
|
|
|
|
|
not in {
|
|
|
|
|
"https://github.com/simonw/datasette",
|
|
|
|
|
"https://github.com/simonw/datasette/blob/master/LICENSE",
|
|
|
|
|
"https://github.com/simonw/datasette/blob/master/tests/fixtures.py",
|
|
|
|
|
}
|
|
|
|
|
and not href.startswith("https://plugin-example.com/")
|
|
|
|
|
):
|
|
|
|
|
# If this has been made absolute it may start http://localhost/
|
|
|
|
|
if href.startswith("http://localhost/"):
|
|
|
|
|
href = href[len("http://localost/") :]
|
|
|
|
|
assert href.startswith(base_url), {
|
|
|
|
|
"base_url": base_url,
|
|
|
|
|
"path": path,
|
|
|
|
|
"href_or_src": href,
|
|
|
|
|
"element_parent": str(el.parent),
|
|
|
|
|
}
|