Skip to content

Commit

Permalink
Add function to remove creationDate from responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Reillyhewitson committed Feb 13, 2023
1 parent b060ffe commit cf7d8a2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
34 changes: 32 additions & 2 deletions test/integration/search_api/endpoints/test_get_dataset_files.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
from contextlib import suppress

import pytest

from datagateway_api.src.common.config import Config


def prepare_data_for_assertion(response):
"""
Remove creationDate from queries. creationDate is generated by python_icat
and is not static between data generations and therefore needs to be
removed for assertion.
:param response: JSON response from the search api.
"""

# Go through list and dicts to find dicts
for data in response:
if isinstance(response, dict):
data = response[data]

if type(data) in [dict, list]:
# Pass list and dicts back to the function to
# loop through nested lists or dictionaries
prepare_data_for_assertion(data)

# This handles dictionaries passed to the function
# Even if the dictionary doesn't contain creationDate
if isinstance(response, dict):
with suppress(KeyError):
del response["creationDate"]

return response


class TestSearchAPIGetDatasetFilesEndpoint:
@pytest.mark.parametrize(
"pid, request_filter, expected_json",
Expand Down Expand Up @@ -103,7 +132,6 @@ class TestSearchAPIGetDatasetFilesEndpoint:
"pid": "1-4978-6907-2",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2023-02-09T09:23:39.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand All @@ -129,8 +157,10 @@ def test_valid_get_dataset_files_endpoint(
print(test_response)
print(test_response.json)

response_data = prepare_data_for_assertion(test_response.json)

assert test_response.status_code == 200
assert test_response.json == expected_json
assert response_data == expected_json

@pytest.mark.parametrize(
"pid, request_filter, expected_status_code",
Expand Down
14 changes: 6 additions & 8 deletions test/integration/search_api/endpoints/test_get_entity_by_pid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

from datagateway_api.src.common.config import Config
from test.integration.search_api.endpoints.test_get_dataset_files import (
prepare_data_for_assertion,
)


class TestSearchAPIGetByPIDEndpoint:
Expand All @@ -15,7 +18,6 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "1-4978-6907-2",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2023-02-09T09:23:39.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -81,7 +83,6 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "1-4978-6907-2",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2023-02-09T09:23:39.000Z",
"size": None,
"documents": [
{
Expand Down Expand Up @@ -157,7 +158,6 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "1-4978-6907-2",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2023-02-09T09:23:39.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand All @@ -170,7 +170,6 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "0-557-36716-6",
"title": "DATASET 61",
"isPublic": True,
"creationDate": "2023-02-09T09:23:54.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -220,7 +219,6 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "1-4978-6907-2",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2023-02-09T09:23:39.000Z",
"size": None,
"documents": [
{
Expand Down Expand Up @@ -435,7 +433,6 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "1-4978-6907-2",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2023-02-09T09:23:39.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand All @@ -448,7 +445,6 @@ class TestSearchAPIGetByPIDEndpoint:
"pid": "0-557-36716-6",
"title": "DATASET 61",
"isPublic": True,
"creationDate": "2023-02-09T09:23:54.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -495,8 +491,10 @@ def test_valid_get_by_pid_endpoint(
f"{request_filter}",
)

response_data = prepare_data_for_assertion(test_response.json)

assert test_response.status_code == 200
assert test_response.json == expected_json
assert response_data == expected_json

@pytest.mark.parametrize(
"pid, request_filter, expected_status_code",
Expand Down
22 changes: 6 additions & 16 deletions test/integration/search_api/endpoints/test_search_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

from datagateway_api.src.common.config import Config
from test.integration.search_api.endpoints.test_get_dataset_files import (
prepare_data_for_assertion,
)


class TestSearchAPISearchEndpoint:
Expand All @@ -15,7 +18,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-4978-6907-2",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2023-02-09T09:23:39.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand All @@ -28,7 +30,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-01-107043-X",
"title": "DATASET 2",
"isPublic": True,
"creationDate": "2023-02-09T09:23:39.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -84,7 +85,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-77218-518-3",
"title": "DATASET 6",
"isPublic": True,
"creationDate": "2023-02-09T09:23:41.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -118,7 +118,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-4978-6907-2",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2023-02-09T09:23:39.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -175,7 +174,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-4978-6907-2",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2023-02-09T09:23:39.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand All @@ -188,7 +186,6 @@ class TestSearchAPISearchEndpoint:
"pid": "0-557-36716-6",
"title": "DATASET 61",
"isPublic": True,
"creationDate": "2023-02-09T09:23:54.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -253,7 +250,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-4978-6907-2",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2023-02-09T09:23:39.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -291,7 +287,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-85271-859-5",
"title": "DATASET 33",
"isPublic": True,
"creationDate": "2023-02-09T09:23:49.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -339,7 +334,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-71395-013-8",
"title": "DATASET 71",
"isPublic": True,
"creationDate": "2023-02-09T09:23:56.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -379,7 +373,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-71395-013-8",
"title": "DATASET 71",
"isPublic": True,
"creationDate": "2023-02-09T09:23:56.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -418,7 +411,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-85150-280-7",
"title": "DATASET 13",
"isPublic": True,
"creationDate": "2023-02-09T09:23:44.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -682,7 +674,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-4978-6907-2",
"title": "DATASET 1",
"isPublic": True,
"creationDate": "2023-02-09T09:23:39.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand All @@ -704,7 +695,6 @@ class TestSearchAPISearchEndpoint:
"pid": "0-557-36716-6",
"title": "DATASET 61",
"isPublic": True,
"creationDate": "2023-02-09T09:23:54.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -807,7 +797,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-937941-39-6",
"title": "DATASET 244",
"isPublic": True,
"creationDate": "2000-10-23T03:58:02.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand All @@ -828,7 +817,6 @@ class TestSearchAPISearchEndpoint:
"pid": "1-397-29815-4",
"title": "DATASET 4",
"isPublic": True,
"creationDate": "2016-06-09T06:36:38.000Z",
"size": None,
"documents": [],
"techniques": [],
Expand Down Expand Up @@ -866,7 +854,9 @@ def test_valid_search_endpoint(
f"{request_filter}",
)

assert test_response.json == expected_json
response_data = prepare_data_for_assertion(test_response.json)

assert response_data == expected_json

@pytest.mark.parametrize(
"request_filter, expected_status_code",
Expand Down

0 comments on commit cf7d8a2

Please sign in to comment.