Nicer pattern for make_app_client() in tests, closes #395

This commit is contained in:
Simon Willison 2020-06-07 14:14:10 -07:00
commit abc7339124
7 changed files with 56 additions and 42 deletions

View file

@ -63,9 +63,9 @@ def test_static(app_client):
def test_static_mounts():
for client in make_app_client(
with make_app_client(
static_mounts=[("custom-static", str(pathlib.Path(__file__).parent))]
):
) as client:
response = client.get("/custom-static/test_html.py")
assert response.status == 200
response = client.get("/custom-static/not_exists.py")
@ -75,7 +75,7 @@ def test_static_mounts():
def test_memory_database_page():
for client in make_app_client(memory=True):
with make_app_client(memory=True) as client:
response = client.get("/:memory:")
assert_permissions_checked(
client.ds, ["view-instance", ("view-database", "database", ":memory:")]
@ -177,7 +177,7 @@ def test_definition_sql(path, expected_definition_sql, app_client):
def test_table_cell_truncation():
for client in make_app_client(config={"truncate_cells_html": 5}):
with make_app_client(config={"truncate_cells_html": 5}) as client:
response = client.get("/fixtures/facetable")
assert response.status == 200
table = Soup(response.body, "html.parser").find("table")
@ -202,7 +202,7 @@ def test_table_cell_truncation():
def test_row_page_does_not_truncate():
for client in make_app_client(config={"truncate_cells_html": 5}):
with make_app_client(config={"truncate_cells_html": 5}) as client:
response = client.get("/fixtures/facetable/1")
assert response.status == 200
assert_permissions_checked(
@ -925,7 +925,7 @@ def test_table_metadata(app_client):
def test_database_download_allowed_for_immutable():
for client in make_app_client(is_immutable=True):
with make_app_client(is_immutable=True) as client:
assert not client.ds.databases["fixtures"].is_mutable
# Regular page should have a download link
response = client.get("/fixtures")
@ -951,7 +951,7 @@ def test_database_download_disallowed_for_mutable(app_client):
def test_database_download_disallowed_for_memory():
for client in make_app_client(memory=True):
with make_app_client(memory=True) as client:
# Memory page should NOT have a download link
response = client.get("/:memory:")
soup = Soup(response.body, "html.parser")
@ -960,7 +960,7 @@ def test_database_download_disallowed_for_memory():
def test_allow_download_off():
for client in make_app_client(is_immutable=True, config={"allow_download": False}):
with make_app_client(is_immutable=True, config={"allow_download": False}) as client:
response = client.get("/fixtures")
soup = Soup(response.body, "html.parser")
assert not len(soup.findAll("a", {"href": re.compile(r"\.db$")}))
@ -978,7 +978,7 @@ def test_allow_sql_on(app_client):
def test_allow_sql_off():
for client in make_app_client(config={"allow_sql": False}):
with make_app_client(config={"allow_sql": False}) as client:
response = client.get("/fixtures")
soup = Soup(response.body, "html.parser")
assert not len(soup.findAll("textarea", {"name": "sql"}))
@ -1170,9 +1170,9 @@ def test_metadata_json_html(app_client):
def test_custom_table_include():
for client in make_app_client(
with make_app_client(
template_dir=str(pathlib.Path(__file__).parent / "test_templates")
):
) as client:
response = client.get("/fixtures/complex_foreign_keys")
assert response.status == 200
assert (
@ -1197,7 +1197,7 @@ def test_zero_results(app_client, path):
def test_config_template_debug_on():
for client in make_app_client(config={"template_debug": True}):
with make_app_client(config={"template_debug": True}) as client:
response = client.get("/fixtures/facetable?_context=1")
assert response.status == 200
assert response.text.startswith("<pre>{")
@ -1211,7 +1211,7 @@ def test_config_template_debug_off(app_client):
def test_debug_context_includes_extra_template_vars():
# https://github.com/simonw/datasette/issues/693
for client in make_app_client(config={"template_debug": True}):
with make_app_client(config={"template_debug": True}) as client:
response = client.get("/fixtures/facetable?_context=1")
# scope_path is added by PLUGIN1
assert "scope_path" in response.text
@ -1292,7 +1292,7 @@ def test_metadata_sort_desc(app_client):
],
)
def test_base_url_config(base_url, path):
for client in make_app_client(config={"base_url": base_url}):
with make_app_client(config={"base_url": base_url}) as client:
response = client.get(base_url + path.lstrip("/"))
soup = Soup(response.body, "html.parser")
for el in soup.findAll(["a", "link", "script"]):