More consistent use of response.text/response.json in tests, closes #792

writable-canned
Simon Willison 2020-06-02 14:29:12 -07:00
rodzic 61e40e917e
commit 9dd6d1ae6d
5 zmienionych plików z 14 dodań i 25 usunięć

Wyświetl plik

@ -1762,16 +1762,10 @@ def test_common_prefix_database_names(app_client_conflicting_database_names):
# https://github.com/simonw/datasette/issues/597
assert ["fixtures", "foo", "foo-bar"] == [
d["name"]
for d in json.loads(
app_client_conflicting_database_names.get("/-/databases.json").body.decode(
"utf8"
)
)
for d in app_client_conflicting_database_names.get("/-/databases.json").json
]
for db_name, path in (("foo", "/foo.json"), ("foo-bar", "/foo-bar.json")):
data = json.loads(
app_client_conflicting_database_names.get(path).body.decode("utf8")
)
data = app_client_conflicting_database_names.get(path).json
assert db_name == data["database"]

Wyświetl plik

@ -84,21 +84,20 @@ def config_dir_client(tmp_path_factory):
def test_metadata(config_dir_client):
response = config_dir_client.get("/-/metadata.json")
assert 200 == response.status
assert METADATA == json.loads(response.text)
assert METADATA == response.json
def test_config(config_dir_client):
response = config_dir_client.get("/-/config.json")
assert 200 == response.status
config = json.loads(response.text)
assert 60 == config["default_cache_ttl"]
assert not config["allow_sql"]
assert 60 == response.json["default_cache_ttl"]
assert not response.json["allow_sql"]
def test_plugins(config_dir_client):
response = config_dir_client.get("/-/plugins.json")
assert 200 == response.status
assert "hooray.py" in {p["name"] for p in json.loads(response.text)}
assert "hooray.py" in {p["name"] for p in response.json}
def test_templates_and_plugin(config_dir_client):
@ -123,7 +122,7 @@ def test_static_directory_browsing_not_allowed(config_dir_client):
def test_databases(config_dir_client):
response = config_dir_client.get("/-/databases.json")
assert 200 == response.status
databases = json.loads(response.text)
databases = response.json
assert 2 == len(databases)
databases.sort(key=lambda d: d["name"])
assert "demo" == databases[0]["name"]
@ -141,4 +140,4 @@ def test_metadata_yaml(tmp_path_factory, filename):
client.ds = ds
response = client.get("/-/metadata.json")
assert 200 == response.status
assert {"title": "Title from metadata"} == json.loads(response.text)
assert {"title": "Title from metadata"} == response.json

Wyświetl plik

@ -101,7 +101,7 @@ def test_csv_with_non_ascii_characters(app_client):
)
assert response.status == 200
assert "text/plain; charset=utf-8" == response.headers["content-type"]
assert "text,number\r\n𝐜𝐢𝐭𝐢𝐞𝐬,1\r\nbob,2\r\n" == response.body.decode("utf8")
assert "text,number\r\n𝐜𝐢𝐭𝐢𝐞𝐬,1\r\nbob,2\r\n" == response.text
def test_max_csv_mb(app_client_csv_max_mb_one):

Wyświetl plik

@ -606,9 +606,7 @@ def test_row_html_simple_primary_key(app_client):
def test_table_not_exists(app_client):
assert "Table not found: blah" in app_client.get("/fixtures/blah").body.decode(
"utf8"
)
assert "Table not found: blah" in app_client.get("/fixtures/blah").text
def test_table_html_no_primary_key(app_client):

Wyświetl plik

@ -218,7 +218,7 @@ def test_plugin_config_file(app_client):
)
def test_plugins_extra_body_script(app_client, path, expected_extra_body_script):
r = re.compile(r"<script>var extra_body_script = (.*?);</script>")
json_data = r.search(app_client.get(path).body.decode("utf8")).group(1)
json_data = r.search(app_client.get(path).text).group(1)
actual_data = json.loads(json_data)
assert expected_extra_body_script == actual_data
@ -331,7 +331,7 @@ def view_names_client(tmp_path_factory):
def test_view_names(view_names_client, path, view_name):
response = view_names_client.get(path)
assert response.status == 200
assert "view_name:{}".format(view_name) == response.body.decode("utf8")
assert "view_name:{}".format(view_name) == response.text
def test_register_output_renderer_no_parameters(app_client):
@ -345,8 +345,7 @@ def test_register_output_renderer_all_parameters(app_client):
assert 200 == response.status
# Lots of 'at 0x103a4a690' in here - replace those so we can do
# an easy comparison
body = response.body.decode("utf-8")
body = at_memory_re.sub(" at 0xXXX", body)
body = at_memory_re.sub(" at 0xXXX", response.text)
assert {
"1+1": 2,
"datasette": "<datasette.app.Datasette object at 0xXXX>",
@ -468,7 +467,6 @@ def test_register_facet_classes(app_client):
response = app_client.get(
"/fixtures/compound_three_primary_keys.json?_dummy_facet=1"
)
data = json.loads(response.body)
assert [
{
"name": "pk1",
@ -502,7 +500,7 @@ def test_register_facet_classes(app_client):
"name": "pk3",
"toggle_url": "http://localhost/fixtures/compound_three_primary_keys.json?_dummy_facet=1&_facet=pk3",
},
] == data["suggested_facets"]
] == response.json["suggested_facets"]
def test_actor_from_request(app_client):