Skip to content

Commit

Permalink
test: fix failing count with isPublic condition tests #312
Browse files Browse the repository at this point in the history
  • Loading branch information
VKTB committed Feb 8, 2022
1 parent 1021c80 commit 20e3cb1
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions test/search_api/endpoints/test_count_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from unittest.mock import patch

import pytest

from datagateway_api.src.common.config import Config
from datagateway_api.src.common.date_handler import DateHandler


class TestSearchAPICountEndpoint:
Expand Down Expand Up @@ -67,6 +70,28 @@ class TestSearchAPICountEndpoint:
{"count": 13},
id="Instrument count with where (operator specified)",
),
pytest.param(
"instruments",
'{"facility": {"like": "LILS"}}',
{"count": 14},
id="Instrument count with where using related ICAT mapping",
),
],
)
def test_valid_count_endpoint(
self, flask_test_app_search_api, endpoint_name, request_filter, expected_json,
):
test_response = flask_test_app_search_api.get(
f"{Config.config.search_api.extension}/{endpoint_name}/count?where="
f"{request_filter}",
)

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

@pytest.mark.parametrize(
"endpoint_name, request_filter, expected_json",
[
pytest.param(
"datasets",
'{"isPublic": true}',
Expand All @@ -79,17 +104,29 @@ class TestSearchAPICountEndpoint:
{"count": 76},
id="Document count with isPublic condition",
),
pytest.param(
"instruments",
'{"facility": {"like": "LILS"}}',
{"count": 14},
id="Instrument count with where using related ICAT mapping",
),
],
)
def test_valid_count_endpoint(
self, flask_test_app_search_api, endpoint_name, request_filter, expected_json,
@patch("datagateway_api.src.search_api.query_filter_factory.datetime")
def test_valid_count_endpoint_is_public_field(
self,
datetime_mock,
flask_test_app_search_api,
endpoint_name,
request_filter,
expected_json,
):
"""
The datetime must be mocked here to prevent tests from failing as time passes.
A dataset or document that was created or released 2 years and 364 ago would be
fall in the not public category, however that same dataset or document would
fall in the public category (in the case of ISIS) a few days later because it
will be 3 years old. As a result of this, the tests will fail because the actual
count will be different to that of the expected. Mocking datetime takes care of
this issue because it sets the time to the one provided in this test method.
"""
datetime_mock.now.return_value = DateHandler.str_to_datetime_object(
"2022-02-06 00:00:01+00:00",
)
test_response = flask_test_app_search_api.get(
f"{Config.config.search_api.extension}/{endpoint_name}/count?where="
f"{request_filter}",
Expand Down

0 comments on commit 20e3cb1

Please sign in to comment.