Skip to content

Commit

Permalink
More consistent use of response.text/response.json in tests, closes #792
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 2, 2020
1 parent 61e40e9 commit 9dd6d1a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 25 deletions.
10 changes: 2 additions & 8 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand Down
13 changes: 6 additions & 7 deletions tests/test_config_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"]
Expand All @@ -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
2 changes: 1 addition & 1 deletion tests/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 1 addition & 3 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 4 additions & 6 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -331,7 +331,7 @@ def extra_template_vars(view_name):
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):
Expand All @@ -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>",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 9dd6d1a

Please sign in to comment.