diff --git a/tests/test_table_html.py b/tests/test_table_html.py index f8e7c295..c2f512c9 100644 --- a/tests/test_table_html.py +++ b/tests/test_table_html.py @@ -10,6 +10,8 @@ import urllib.parse from .utils import assert_footer_links, inner_html +@pytest.mark.ds_client +@pytest.mark.asyncio @pytest.mark.parametrize( "path,expected_definition_sql", [ @@ -37,9 +39,9 @@ CREATE INDEX idx_compound_three_primary_keys_content ON compound_three_primary_k ), ], ) -def test_table_definition_sql(path, expected_definition_sql, app_client): - response = app_client.get(path) - pre = Soup(response.body, "html.parser").select_one("pre.wrapped-sql") +async def test_table_definition_sql(path, expected_definition_sql, ds_client): + response = await ds_client.get(path) + pre = Soup(response.text, "html.parser").select_one("pre.wrapped-sql") assert expected_definition_sql == pre.string @@ -82,20 +84,22 @@ def test_table_cell_truncation(): ] -def test_add_filter_redirects(app_client): +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_add_filter_redirects(ds_client): 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 - response = app_client.get(path) - assert response.status == 302 + response = await ds_client.get(path) + assert response.status_code == 302 assert response.headers["Location"].endswith("?content__startswith=x") # Adding a redirect to an existing query string: path = path_base + "?foo=bar&" + filter_args - response = app_client.get(path) - assert response.status == 302 + response = await ds_client.get(path) + assert response.status_code == 302 assert response.headers["Location"].endswith("?foo=bar&content__startswith=x") # Test that op with a __x suffix overrides the filter value @@ -110,12 +114,14 @@ def test_add_filter_redirects(app_client): } ) ) - response = app_client.get(path) - assert response.status == 302 + response = await ds_client.get(path) + assert response.status_code == 302 assert response.headers["Location"].endswith("?content__isnull=5") -def test_existing_filter_redirects(app_client): +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_existing_filter_redirects(ds_client): filter_args = { "_filter_column_1": "name", "_filter_op_1": "contains", @@ -132,8 +138,8 @@ def test_existing_filter_redirects(app_client): } path_base = "/fixtures/simple_primary_key" path = path_base + "?" + urllib.parse.urlencode(filter_args) - response = app_client.get(path) - assert response.status == 302 + response = await ds_client.get(path) + assert response.status_code == 302 assert_querystring_equal( "name__contains=hello&age__gte=22&age__lt=30&name__contains=world", response.headers["Location"].split("?")[1], @@ -142,19 +148,21 @@ def test_existing_filter_redirects(app_client): # Setting _filter_column_3 to empty string should remove *_3 entirely filter_args["_filter_column_3"] = "" path = path_base + "?" + urllib.parse.urlencode(filter_args) - response = app_client.get(path) - assert response.status == 302 + response = await ds_client.get(path) + assert response.status_code == 302 assert_querystring_equal( "name__contains=hello&age__gte=22&name__contains=world", response.headers["Location"].split("?")[1], ) # ?_filter_op=exact should be removed if unaccompanied by _fiter_column - response = app_client.get(path_base + "?_filter_op=exact") - assert response.status == 302 + response = await ds_client.get(path_base + "?_filter_op=exact") + assert response.status_code == 302 assert "?" not in response.headers["Location"] +@pytest.mark.ds_client +@pytest.mark.asyncio @pytest.mark.parametrize( "qs,expected_hidden", ( @@ -169,18 +177,20 @@ def test_existing_filter_redirects(app_client): ("_facet=_neighborhood&_city_id__gt=1", {"_facet": "_neighborhood"}), ), ) -def test_reflected_hidden_form_fields(app_client, qs, expected_hidden): +async def test_reflected_hidden_form_fields(ds_client, qs, expected_hidden): # https://github.com/simonw/datasette/issues/1527 - response = app_client.get("/fixtures/facetable?{}".format(qs)) + response = await ds_client.get("/fixtures/facetable?{}".format(qs)) # In this case we should NOT have a hidden _neighborhood__exact=Downtown field - form = Soup(response.body, "html.parser").find("form") + form = Soup(response.text, "html.parser").find("form") hidden_inputs = { input["name"]: input["value"] for input in form.select("input[type=hidden]") } assert hidden_inputs == expected_hidden -def test_empty_search_parameter_gets_removed(app_client): +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_empty_search_parameter_gets_removed(ds_client): path_base = "/fixtures/simple_primary_key" path = ( path_base @@ -194,39 +204,45 @@ def test_empty_search_parameter_gets_removed(app_client): } ) ) - response = app_client.get(path) - assert response.status == 302 + response = await ds_client.get(path) + assert response.status_code == 302 assert response.headers["Location"].endswith("?name__exact=chidi") -def test_searchable_view_persists_fts_table(app_client): +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_searchable_view_persists_fts_table(ds_client): # The search form should persist ?_fts_table as a hidden field - response = app_client.get( + response = await ds_client.get( "/fixtures/searchable_view?_fts_table=searchable_fts&_fts_pk=pk" ) - inputs = Soup(response.body, "html.parser").find("form").findAll("input") + inputs = Soup(response.text, "html.parser").find("form").findAll("input") hiddens = [i for i in inputs if i["type"] == "hidden"] assert [("_fts_table", "searchable_fts"), ("_fts_pk", "pk")] == [ (hidden["name"], hidden["value"]) for hidden in hiddens ] -def test_sort_by_desc_redirects(app_client): +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_sort_by_desc_redirects(ds_client): path_base = "/fixtures/sortable" path = ( path_base + "?" + urllib.parse.urlencode({"_sort": "sortable", "_sort_by_desc": "1"}) ) - response = app_client.get(path) - assert response.status == 302 + response = await ds_client.get(path) + assert response.status_code == 302 assert response.headers["Location"].endswith("?_sort_desc=sortable") -def test_sort_links(app_client): - response = app_client.get("/fixtures/sortable?_sort=sortable") - assert response.status == 200 - ths = Soup(response.body, "html.parser").findAll("th") +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_sort_links(ds_client): + response = await ds_client.get("/fixtures/sortable?_sort=sortable") + assert response.status_code == 200 + ths = Soup(response.text, "html.parser").findAll("th") attrs_and_link_attrs = [ { "attrs": th.attrs, @@ -326,12 +342,14 @@ def test_sort_links(app_client): ] -def test_facet_display(app_client): - response = app_client.get( +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_facet_display(ds_client): + response = await ds_client.get( "/fixtures/facetable?_facet=planet_int&_facet=_city_id&_facet=on_earth" ) - assert response.status == 200 - soup = Soup(response.body, "html.parser") + assert response.status_code == 200 + soup = Soup(response.text, "html.parser") divs = soup.find("div", {"class": "facet-results"}).findAll("div") actual = [] for div in divs: @@ -407,12 +425,14 @@ def test_facet_display(app_client): ] -def test_facets_persist_through_filter_form(app_client): - response = app_client.get( +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_facets_persist_through_filter_form(ds_client): + response = await ds_client.get( "/fixtures/facetable?_facet=planet_int&_facet=_city_id&_facet_array=tags" ) - assert response.status == 200 - inputs = Soup(response.body, "html.parser").find("form").findAll("input") + assert response.status_code == 200 + inputs = Soup(response.text, "html.parser").find("form").findAll("input") hiddens = [i for i in inputs if i["type"] == "hidden"] assert [(hidden["name"], hidden["value"]) for hidden in hiddens] == [ ("_facet", "planet_int"), @@ -421,20 +441,24 @@ def test_facets_persist_through_filter_form(app_client): ] -def test_next_does_not_persist_in_hidden_field(app_client): - response = app_client.get("/fixtures/searchable?_size=1&_next=1") - assert response.status == 200 - inputs = Soup(response.body, "html.parser").find("form").findAll("input") +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_next_does_not_persist_in_hidden_field(ds_client): + response = await ds_client.get("/fixtures/searchable?_size=1&_next=1") + assert response.status_code == 200 + inputs = Soup(response.text, "html.parser").find("form").findAll("input") hiddens = [i for i in inputs if i["type"] == "hidden"] assert [(hidden["name"], hidden["value"]) for hidden in hiddens] == [ ("_size", "1"), ] -def test_table_html_simple_primary_key(app_client): - response = app_client.get("/fixtures/simple_primary_key?_size=3") - assert response.status == 200 - table = Soup(response.body, "html.parser").find("table") +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_table_html_simple_primary_key(ds_client): + response = await ds_client.get("/fixtures/simple_primary_key?_size=3") + assert response.status_code == 200 + table = Soup(response.text, "html.parser").find("table") assert table["class"] == ["rows-and-columns"] ths = table.findAll("th") assert "id\xa0▼" == ths[0].find("a").string.strip() @@ -459,12 +483,14 @@ def test_table_html_simple_primary_key(app_client): ] == [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")] -def test_table_csv_json_export_interface(app_client): - response = app_client.get("/fixtures/simple_primary_key?id__gt=2") - assert response.status == 200 +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_table_csv_json_export_interface(ds_client): + response = await ds_client.get("/fixtures/simple_primary_key?id__gt=2") + assert response.status_code == 200 # The links at the top of the page links = ( - Soup(response.body, "html.parser") + Soup(response.text, "html.parser") .find("p", {"class": "export-links"}) .findAll("a") ) @@ -478,8 +504,8 @@ def test_table_csv_json_export_interface(app_client): "#export", ] assert expected == actual - # And the advaced export box at the bottom: - div = Soup(response.body, "html.parser").find("div", {"class": "advanced-export"}) + # And the advanced export box at the bottom: + div = Soup(response.text, "html.parser").find("div", {"class": "advanced-export"}) json_links = [a["href"] for a in div.find("p").findAll("a")] assert [ "/fixtures/simple_primary_key.json?id__gt=2", @@ -499,11 +525,13 @@ def test_table_csv_json_export_interface(app_client): ] == inputs -def test_csv_json_export_links_include_labels_if_foreign_keys(app_client): - response = app_client.get("/fixtures/facetable") - assert response.status == 200 +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_csv_json_export_links_include_labels_if_foreign_keys(ds_client): + response = await ds_client.get("/fixtures/facetable") + assert response.status_code == 200 links = ( - Soup(response.body, "html.parser") + Soup(response.text, "html.parser") .find("p", {"class": "export-links"}) .findAll("a") ) @@ -519,14 +547,18 @@ def test_csv_json_export_links_include_labels_if_foreign_keys(app_client): assert expected == actual -def test_table_not_exists(app_client): - assert "Table not found: blah" in app_client.get("/fixtures/blah").text +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_table_not_exists(ds_client): + assert "Table not found: blah" in (await ds_client.get("/fixtures/blah")).text -def test_table_html_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") +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_table_html_no_primary_key(ds_client): + response = await ds_client.get("/fixtures/no_primary_key") + assert response.status_code == 200 + table = Soup(response.text, "html.parser").find("table") # 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:] @@ -549,19 +581,23 @@ def test_table_html_no_primary_key(app_client): ] -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") +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_rowid_sortable_no_primary_key(ds_client): + response = await ds_client.get("/fixtures/no_primary_key") + assert response.status_code == 200 + table = Soup(response.text, "html.parser").find("table") assert table["class"] == ["rows-and-columns"] ths = table.findAll("th") assert "rowid\xa0▼" == ths[1].find("a").string.strip() -def test_table_html_compound_primary_key(app_client): - response = app_client.get("/fixtures/compound_primary_key") - assert response.status == 200 - table = Soup(response.body, "html.parser").find("table") +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_table_html_compound_primary_key(ds_client): + response = await ds_client.get("/fixtures/compound_primary_key") + assert response.status_code == 200 + table = Soup(response.text, "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:]): @@ -588,10 +624,12 @@ def test_table_html_compound_primary_key(app_client): ] == expected -def test_table_html_foreign_key_links(app_client): - response = app_client.get("/fixtures/foreign_key_references") - assert response.status == 200 - table = Soup(response.body, "html.parser").find("table") +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_table_html_foreign_key_links(ds_client): + response = await ds_client.get("/fixtures/foreign_key_references") + assert response.status_code == 200 + table = Soup(response.text, "html.parser").find("table") actual = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")] assert actual == [ [ @@ -613,21 +651,27 @@ def test_table_html_foreign_key_links(app_client): ] -def test_table_html_foreign_key_facets(app_client): - response = app_client.get( +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_table_html_foreign_key_facets(ds_client): + response = await ds_client.get( "/fixtures/foreign_key_references?_facet=foreign_key_with_blank_label" ) - assert response.status == 200 + assert response.status_code == 200 assert ( '