-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Added a new file, 'test_search_tokens.py', containing unit tests for the SearchTokens resource. These tests confirm that each endpoint calls the correct wrapper function with the appropriate parameters. This helps maintain assurance that our resources behave as intended.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import unittest.mock | ||
Check warning on line 1 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L1 <100>
Raw output
|
||
from collections import namedtuple | ||
|
||
import pytest | ||
from flask import Flask | ||
|
||
from resources.SearchTokens import SearchTokens | ||
|
||
class MockPsycopgConnection: | ||
Check warning on line 9 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L9 <101>
Raw output
Check failure on line 9 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L9 <302>
Raw output
|
||
def cursor(self): | ||
Check warning on line 10 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L10 <102>
Raw output
|
||
return MockCursor() | ||
|
||
def commit(self): | ||
Check warning on line 13 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L13 <102>
Raw output
|
||
pass | ||
|
||
def rollback(self): | ||
Check warning on line 16 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L16 <102>
Raw output
|
||
pass | ||
|
||
|
||
class MockCursor: | ||
Check warning on line 20 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L20 <101>
Raw output
|
||
def execute(self, query, params=None): | ||
Check warning on line 21 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L21 <102>
Raw output
Check warning on line 21 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L21 <100>
Raw output
|
||
pass | ||
|
||
def fetchall(self): | ||
Check warning on line 24 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L24 <102>
Raw output
|
||
pass | ||
|
||
|
||
@pytest.fixture | ||
def app(): | ||
Check warning on line 29 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L29 <103>
Raw output
|
||
app = Flask(__name__) | ||
app.config.update({"TESTING": True}) | ||
return app | ||
|
||
|
||
@pytest.fixture | ||
def client(app): | ||
Check warning on line 36 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L36 <103>
Raw output
|
||
return app.test_client() | ||
|
||
|
||
@pytest.fixture | ||
def mock_psycopg_connection(): | ||
Check warning on line 41 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L41 <103>
Raw output
|
||
return MockPsycopgConnection() | ||
|
||
|
||
@pytest.fixture | ||
def search_tokens(mock_psycopg_connection): | ||
Check warning on line 46 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L46 <103>
Raw output
|
||
return SearchTokens(psycopg2_connection=mock_psycopg_connection) | ||
|
||
|
||
@pytest.fixture | ||
def mock_dependencies(mocker): | ||
Check warning on line 51 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L51 <103>
Raw output
|
||
mocks = { | ||
"insert_access_token": mocker.patch( | ||
"resources.SearchTokens.insert_access_token", return_value=None | ||
), | ||
"quick-search": mocker.patch( | ||
"resources.SearchTokens.quick_search_query_wrapper", | ||
return_value={"result": "quick_search"}, | ||
), | ||
"data-sources": mocker.patch( | ||
"resources.SearchTokens.get_approved_data_sources_wrapper", | ||
return_value={"result": "data_sources"}, | ||
), | ||
"data-sources-by-id": mocker.patch( | ||
"resources.SearchTokens.data_source_by_id_wrapper", | ||
return_value={"result": "data_source_by_id"}, | ||
), | ||
"data-sources-map": mocker.patch( | ||
"resources.SearchTokens.get_data_sources_for_map_wrapper", | ||
return_value={"result": "data_sources_map"}, | ||
), | ||
} | ||
return mocks | ||
|
||
|
||
def perform_test_search_tokens_endpoint( | ||
Check warning on line 76 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L76 <103>
Raw output
|
||
search_tokens, | ||
mocker, | ||
app, | ||
endpoint, | ||
expected_response, | ||
params=None, | ||
mocked_dependencies: dict[str, unittest.mock.MagicMock] = None, | ||
): | ||
mock_insert_access_token = mocker.patch( | ||
"resources.SearchTokens.insert_access_token" | ||
) | ||
url = generate_url(endpoint, params) | ||
|
||
with app.test_request_context(url): | ||
response = search_tokens.get() | ||
assert ( | ||
response == expected_response | ||
), f"{endpoint} endpoint should call {expected_response}, got {response}" | ||
mock_insert_access_token.assert_called_once() | ||
if endpoint in mocked_dependencies: | ||
# Check parameters properly called | ||
mock_dependency = mocked_dependencies[endpoint] | ||
call_args = tuple(params.values()) if params else () | ||
mock_dependency.assert_called_with( | ||
*call_args, search_tokens.psycopg2_connection | ||
), f"{mock_dependency._mock_name or 'mock'} was not called with the expected parameters" | ||
|
||
|
||
def generate_url(endpoint, params): | ||
Check warning on line 105 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L105 <103>
Raw output
|
||
url = f"/?endpoint={endpoint}" | ||
if params: | ||
url += "".join([f"&{key}={value}" for key, value in params.items()]) | ||
return url | ||
|
||
|
||
TestCase = namedtuple("TestCase", ["endpoint", "expected_response", "params"]) | ||
|
||
test_cases = [ | ||
TestCase( | ||
"quick-search", {"result": "quick_search"}, {"arg1": "test1", "arg2": "test2"} | ||
), | ||
TestCase("data-sources", {"result": "data_sources"}, None), | ||
TestCase("data-sources-by-id", {"result": "data_source_by_id"}, {"arg1": "1"}), | ||
TestCase("data-sources-map", {"result": "data_sources_map"}, None), | ||
TestCase("unknown", ({"message": "Unknown endpoint"}, 500), None), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("test_case", test_cases) | ||
def test_endpoints(search_tokens, mocker, app, test_case, mock_dependencies): | ||
""" | ||
Check warning on line 127 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L127 <205>
Raw output
Check warning on line 127 in tests/resources/test_search_tokens.py GitHub Actions / flake8[flake8] tests/resources/test_search_tokens.py#L127 <400>
Raw output
|
||
Perform test for endpoints, ensuring each provided endpoint calls | ||
the appropriate wrapper function with the appropriate arguments | ||
:param search_tokens: The search tokens to be used for the test. | ||
:param mocker: The mocker object. | ||
:param app: The application object. | ||
:param test_case: The test case object. | ||
:return: None | ||
""" | ||
perform_test_search_tokens_endpoint( | ||
search_tokens, | ||
mocker, | ||
app, | ||
test_case.endpoint, | ||
test_case.expected_response, | ||
test_case.params, | ||
mock_dependencies, | ||
) |