Skip to content

Commit

Permalink
Merge pull request #111 from aiondemand/feature/print_response_assert…
Browse files Browse the repository at this point in the history
…_error

Print response if assert fails
  • Loading branch information
Taniya-Das authored Jul 28, 2023
2 parents cb20e3b + 9006b5c commit 6cf700e
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 85 deletions.
20 changes: 10 additions & 10 deletions src/tests/routers/generic/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ def test_get_all_unauthenticated(
):
"""You don't need authentication for GET"""
response = client_test_resource.get("/test_resources/v0")
assert response.status_code == 200
assert response.status_code == 200, response.json()
assert len(response.json()) == 1


def test_get_unauthenticated(client_test_resource: TestClient, engine_test_resource_filled: Engine):
"""You don't need authentication for GET"""
response = client_test_resource.get("/test_resources/v0/1")
assert response.status_code == 200
assert response.status_code == 200, response.json()


def test_platform_get_all_unauthenticated(
client_test_resource: TestClient, engine_test_resource_filled: Engine
):
"""You don't need authentication for GET"""
response = client_test_resource.get("/platforms/example/test_resources/v0")
assert response.status_code == 200
assert response.status_code == 200, response.json()
assert len(response.json()) == 1


Expand All @@ -35,15 +35,15 @@ def test_platform_get_unauthenticated(
):
"""You don't need authentication for GET"""
response = client_test_resource.get("/platforms/example/test_resources/v0")
assert response.status_code == 200
assert response.status_code == 200, response.json()
assert len(response.json()) == 1


def test_delete_unauthenticated(
client_test_resource: TestClient, engine_test_resource_filled: Engine
):
response = client_test_resource.delete("/test_resources/v0/1")
assert response.status_code == 401
assert response.status_code == 401, response.json()


def test_delete_unauthorized(client_test_resource: TestClient, mocked_token: Mock):
Expand All @@ -52,7 +52,7 @@ def test_delete_unauthorized(client_test_resource: TestClient, mocked_token: Moc
"/test_resources/v0/1",
headers={"Authorization": "fake-token"},
)
assert response.status_code == 403
assert response.status_code == 403, response.json()
response_json = response.json()
assert response_json["detail"] == "You do not have permission to edit Aiod resources."

Expand All @@ -64,14 +64,14 @@ def test_post_unauthorized(client_test_resource: TestClient, mocked_token: Mock)
json={"title": "example"},
headers={"Authorization": "fake-token"},
)
assert response.status_code == 403
assert response.status_code == 403, response.json()
response_json = response.json()
assert response_json["detail"] == "You do not have permission to edit Aiod resources."


def test_post_unauthenticated(client_test_resource: TestClient):
response = client_test_resource.post("/test_resources/v0", json={"title": "example"})
assert response.status_code == 401
assert response.status_code == 401, response.json()
response_json = response.json()
assert (
response_json["detail"] == "This endpoint requires authorization. You need to be logged in."
Expand All @@ -85,14 +85,14 @@ def test_put_unauthorized(client_test_resource: TestClient, mocked_token: Mock):
json={"title": "example"},
headers={"Authorization": "fake-token"},
)
assert response.status_code == 403
assert response.status_code == 403, response.json()
response_json = response.json()
assert response_json["detail"] == "You do not have permission to edit Aiod resources."


def test_put_unauthenticated(client_test_resource: TestClient):
response = client_test_resource.put("/test_resources/v0/1", json={"title": "example"})
assert response.status_code == 401
assert response.status_code == 401, response.json()
response_json = response.json()
assert (
response_json["detail"] == "This endpoint requires authorization. You need to be logged in."
Expand Down
6 changes: 3 additions & 3 deletions src/tests/routers/generic/test_router_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ def test_happy_path(
response = client_test_resource.delete(
f"/test_resources/v0/{identifier}", headers={"Authorization": "Fake token"}
)
assert response.status_code == 200
assert response.status_code == 200, response.json()
response = client_test_resource.get("/test_resources/v0/")
assert response.status_code == 200
assert response.status_code == 200, response.json()
response_json = response.json()
assert len(response_json) == 1
assert {r["identifier"] for r in response_json} == {1, 2} - {identifier}
Expand Down Expand Up @@ -64,5 +64,5 @@ def test_non_existent(
response = client_test_resource.delete(
f"/test_resources/v0/{identifier}", headers={"Authorization": "Fake token"}
)
assert response.status_code == 404
assert response.status_code == 404, response.json()
assert response.json()["detail"] == f"Test_resource '{identifier}' not found in the database."
2 changes: 1 addition & 1 deletion src/tests/routers/generic/test_router_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ def test_deprecated_router(
kwargs["headers"] = {"Authorization": "fake-token"}

response = getattr(client, verb)(url, **kwargs)
assert response.status_code == 200
assert response.status_code == 200, response.json()
assert "deprecated" in response.headers
assert response.headers.get("deprecated") == "Thu, 21 Apr 2022 00:00:00 GMT"
8 changes: 4 additions & 4 deletions src/tests/routers/generic/test_router_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

def test_get_happy_path(client_test_resource: TestClient, engine_test_resource_filled: Engine):
response = client_test_resource.get("/platforms/example/test_resources/v0/1")
assert response.status_code == 200
assert response.status_code == 200, response.json()
response_json = response.json()

assert response_json["title"] == "A title"
Expand All @@ -14,19 +14,19 @@ def test_get_happy_path(client_test_resource: TestClient, engine_test_resource_f

def test_not_found(client_test_resource: TestClient, engine_test_resource_filled: Engine):
response = client_test_resource.get("/platforms/example/test_resources/v0/99")
assert response.status_code == 404
assert response.status_code == 404, response.json()
assert response.json()["detail"] == "Test_resource '99' of 'example' not found in the database."


def test_wrong_platform(client_test_resource: TestClient, engine_test_resource_filled: Engine):
response = client_test_resource.get("/platforms/openml/test_resources/v0/1")
assert response.status_code == 404
assert response.status_code == 404, response.json()
assert response.json()["detail"] == "Test_resource '1' of 'openml' not found in the database."


def test_nonexistent_platform(
client_test_resource: TestClient, engine_test_resource_filled: Engine
):
response = client_test_resource.get("/platforms/nonexistent_platform/test_resources/v0/1")
assert response.status_code == 400
assert response.status_code == 400, response.json()
assert response.json()["detail"] == "platform 'nonexistent_platform' not recognized."
2 changes: 1 addition & 1 deletion src/tests/routers/generic/test_router_get_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_get_all_happy_path(client_test_resource: TestClient, engine_test_resour
)
session.commit()
response = client_test_resource.get("/test_resources/v0")
assert response.status_code == 200
assert response.status_code == 200, response.json()
response_json = response.json()

assert len(response_json) == 2
Expand Down
2 changes: 1 addition & 1 deletion src/tests/routers/generic/test_router_get_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_get_count_happy_path(client_test_resource: TestClient, engine_test_reso
)
session.commit()
response = client_test_resource.get("/counts/test_resources/v0")
assert response.status_code == 200
assert response.status_code == 200, response.json()
response_json = response.json()

assert response_json == 2
Expand Down
4 changes: 2 additions & 2 deletions src/tests/routers/generic/test_router_platform_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

def test_get_happy_path(client_test_resource: TestClient, engine_test_resource_filled: Engine):
response = client_test_resource.get("/test_resources/v0/1")
assert response.status_code == 200
assert response.status_code == 200, response.json()
response_json = response.json()

assert response_json["title"] == "A title"
Expand All @@ -14,5 +14,5 @@ def test_get_happy_path(client_test_resource: TestClient, engine_test_resource_f

def test_not_found(client_test_resource: TestClient, engine_test_resource_filled: Engine):
response = client_test_resource.get("/test_resources/v0/99")
assert response.status_code == 404
assert response.status_code == 404, response.json()
assert response.json()["detail"] == "Test_resource '99' not found in the database."
2 changes: 1 addition & 1 deletion src/tests/routers/generic/test_router_platform_get_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_get_all_happy_path(client_test_resource: TestClient, engine_test_resour
)
session.commit()
response = client_test_resource.get("/platforms/example/test_resources/v0")
assert response.status_code == 200
assert response.status_code == 200, response.json()
response_json = response.json()

assert len(response_json) == 2
Expand Down
22 changes: 11 additions & 11 deletions src/tests/routers/generic/test_router_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def test_unicode(client_test_resource: TestClient, title: str, mocked_privileged
json={"title": title, "platform": "example", "platform_identifier": "1"},
headers={"Authorization": "Fake token"},
)
assert response.status_code == 200
assert response.status_code == 200, response.json()
assert response.json() == {"identifier": 1}
response = client_test_resource.get("/test_resources/v0/1")
assert response.status_code == 200
assert response.status_code == 200, response.json()
response_json = response.json()
assert response_json["title"] == title

Expand All @@ -31,7 +31,7 @@ def test_missing_value(client_test_resource: TestClient, mocked_privileged_token
response = client_test_resource.post(
"/test_resources/v0", json=body, headers={"Authorization": "Fake token"}
)
assert response.status_code == 422
assert response.status_code == 422, response.json()
assert response.json()["detail"] == [
{"loc": ["body", "title"], "msg": "field required", "type": "value_error.missing"}
]
Expand All @@ -43,7 +43,7 @@ def test_null_value(client_test_resource: TestClient, mocked_privileged_token: M
response = client_test_resource.post(
"/test_resources/v0", json=body, headers={"Authorization": "Fake token"}
)
assert response.status_code == 422
assert response.status_code == 422, response.json()
assert response.json()["detail"] == [
{
"loc": ["body", "title"],
Expand All @@ -58,10 +58,10 @@ def test_posting_same_item_twice(client_test_resource: TestClient, mocked_privil
headers = {"Authorization": "Fake token"}
body = {"title": "title1", "platform": "example", "platform_identifier": "1"}
response = client_test_resource.post("/test_resources/v0", json=body, headers=headers)
assert response.status_code == 200
assert response.status_code == 200, response.json()
body = {"title": "title2", "platform": "example", "platform_identifier": "1"}
response = client_test_resource.post("/test_resources/v0", json=body, headers=headers)
assert response.status_code == 409
assert response.status_code == 409, response.json()
assert (
response.json()["detail"] == "There already exists a test_resource with the same "
"platform and platform_identifier, with identifier=1."
Expand All @@ -75,10 +75,10 @@ def test_no_platform_no_platform_identifier(
headers = {"Authorization": "Fake token"}
body = {"title": "title1", "platform": None, "platform_identifier": None}
response = client_test_resource.post("/test_resources/v0", json=body, headers=headers)
assert response.status_code == 200
assert response.status_code == 200, response.json()
body = {"title": "title2", "platform": None, "platform_identifier": None}
response = client_test_resource.post("/test_resources/v0", json=body, headers=headers)
assert response.status_code == 200
assert response.status_code == 200, response.json()


def test_no_platform_with_platform_identifier(
Expand All @@ -88,7 +88,7 @@ def test_no_platform_with_platform_identifier(
headers = {"Authorization": "Fake token"}
body = {"title": "title1", "platform": None, "platform_identifier": "1"}
response = client_test_resource.post("/test_resources/v0", json=body, headers=headers)
assert response.status_code == 400
assert response.status_code == 400, response.json()
assert (
response.json()["detail"] == "If platform is NULL, platform_identifier should also be "
"NULL, and vice versa."
Expand All @@ -102,7 +102,7 @@ def test_platform_with_no_platform_identifier(
headers = {"Authorization": "Fake token"}
body = {"title": "title1", "platform": "example", "platform_identifier": None}
response = client_test_resource.post("/test_resources/v0", json=body, headers=headers)
assert response.status_code == 400
assert response.status_code == 400, response.json()
assert (
response.json()["detail"] == "If platform is NULL, platform_identifier should also be "
"NULL, and vice versa."
Expand All @@ -115,7 +115,7 @@ def test_same_title_twice(client_test_resource: TestClient, mocked_privileged_to
headers = {"Authorization": "Fake token"}
body = {"title": "title1", "platform": None, "platform_identifier": None}
response = client_test_resource.post("/test_resources/v0", json=body, headers=headers)
assert response.status_code == 200
assert response.status_code == 200, response.json()
body = {"title": "title1", "platform": None, "platform_identifier": None}
response = client_test_resource.post("/test_resources/v0", json=body, headers=headers)
assert response.status_code == 409
Expand Down
10 changes: 5 additions & 5 deletions src/tests/routers/generic/test_router_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def test_unicode(
json={"title": title, "platform": "openml", "platform_identifier": "2"},
headers={"Authorization": "Fake token"},
)
assert response.status_code == 200
assert response.status_code == 200, response.json()
response = client_test_resource.get("/test_resources/v0/1")
assert response.status_code == 200
assert response.status_code == 200, response.json()
response_json = response.json()
assert response_json["title"] == title
assert response_json["platform"] == "openml"
Expand All @@ -43,7 +43,7 @@ def test_non_existent(
json={"title": "new_title", "platform": "other", "platform_identifier": "2"},
headers={"Authorization": "Fake token"},
)
assert response.status_code == 404
assert response.status_code == 404, response.json()
response_json = response.json()
assert response_json["detail"] == "Test_resource '2' not found in the database."

Expand All @@ -58,7 +58,7 @@ def test_too_long_name(
response = client_test_resource.put(
"/test_resources/v0/1", json={"title": name}, headers={"Authorization": "Fake token"}
)
assert response.status_code == 422
assert response.status_code == 422, response.json()
response_json = response.json()
assert response_json["detail"] == [
{
Expand All @@ -85,7 +85,7 @@ def test_no_platform_with_platform_identifier(
json={"title": "title", "platform": "other", "platform_identifier": None},
headers={"Authorization": "Fake token"},
)
assert response.status_code == 400
assert response.status_code == 400, response.json()
assert (
response.json()["detail"] == "If platform is NULL, platform_identifier should also be "
"NULL, and vice versa."
Expand Down
8 changes: 4 additions & 4 deletions src/tests/routers/generic/test_router_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def client_with_testobject(engine_test_resource) -> TestClient:

def test_get_happy_path(client_with_testobject: TestClient):
response = client_with_testobject.get("/test_resources/v0/1")
assert response.status_code == 200
assert response.status_code == 200, response.json()
response_json = response.json()

assert response_json["identifier"] == 1
Expand All @@ -171,7 +171,7 @@ def test_get_happy_path(client_with_testobject: TestClient):

def test_get_all_happy_path(client_with_testobject: TestClient):
response = client_with_testobject.get("/test_resources/v0")
assert response.status_code == 200
assert response.status_code == 200, response.json()
response_json = response.json()
assert "deprecated" not in response.headers

Expand Down Expand Up @@ -201,7 +201,7 @@ def test_post_happy_path(client_with_testobject: TestClient, mocked_privileged_t
},
headers={"Authorization": "Fake token"},
)
assert response.status_code == 200
assert response.status_code == 200, response.json()
objects = client_with_testobject.get("/test_resources/v0").json()
obj = objects[-1]
assert obj["identifier"] == 5
Expand Down Expand Up @@ -232,7 +232,7 @@ def test_put_happy_path(client_with_testobject: TestClient, mocked_privileged_to
},
headers={"Authorization": "Fake token"},
)
assert response.status_code == 200
assert response.status_code == 200, response.json()
changed_resource = client_with_testobject.get("/test_resources/v0/4").json()
assert changed_resource["title"] == "new title"
assert changed_resource["named_string"] == "new_string"
Expand Down
Loading

0 comments on commit 6cf700e

Please sign in to comment.