Skip to content

Commit

Permalink
Add unit tests for SearchTokens resource
Browse files Browse the repository at this point in the history
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
maxachis committed May 23, 2024
1 parent e4797de commit d336701
Showing 1 changed file with 145 additions and 0 deletions.
145 changes: 145 additions & 0 deletions tests/resources/test_search_tokens.py
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

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L1 <100>

Missing docstring in public module
Raw output
./tests/resources/test_search_tokens.py:1:1: D100 Missing docstring in public module
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

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L9 <101>

Missing docstring in public class
Raw output
./tests/resources/test_search_tokens.py:9:1: D101 Missing docstring in public class

Check failure on line 9 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L9 <302>

expected 2 blank lines, found 1
Raw output
./tests/resources/test_search_tokens.py:9:1: E302 expected 2 blank lines, found 1
def cursor(self):

Check warning on line 10 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L10 <102>

Missing docstring in public method
Raw output
./tests/resources/test_search_tokens.py:10:1: D102 Missing docstring in public method
return MockCursor()

def commit(self):

Check warning on line 13 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L13 <102>

Missing docstring in public method
Raw output
./tests/resources/test_search_tokens.py:13:1: D102 Missing docstring in public method
pass

def rollback(self):

Check warning on line 16 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L16 <102>

Missing docstring in public method
Raw output
./tests/resources/test_search_tokens.py:16:1: D102 Missing docstring in public method
pass


class MockCursor:

Check warning on line 20 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L20 <101>

Missing docstring in public class
Raw output
./tests/resources/test_search_tokens.py:20:1: D101 Missing docstring in public class
def execute(self, query, params=None):

Check warning on line 21 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L21 <102>

Missing docstring in public method
Raw output
./tests/resources/test_search_tokens.py:21:1: D102 Missing docstring in public method

Check warning on line 21 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L21 <100>

Unused argument 'query'
Raw output
./tests/resources/test_search_tokens.py:21:23: U100 Unused argument 'query'

Check warning on line 21 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L21 <100>

Unused argument 'params'
Raw output
./tests/resources/test_search_tokens.py:21:30: U100 Unused argument 'params'
pass

def fetchall(self):

Check warning on line 24 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L24 <102>

Missing docstring in public method
Raw output
./tests/resources/test_search_tokens.py:24:1: D102 Missing docstring in public method
pass


@pytest.fixture
def app():

Check warning on line 29 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L29 <103>

Missing docstring in public function
Raw output
./tests/resources/test_search_tokens.py:29:1: D103 Missing docstring in public function
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

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L36 <103>

Missing docstring in public function
Raw output
./tests/resources/test_search_tokens.py:36:1: D103 Missing docstring in public function
return app.test_client()


@pytest.fixture
def mock_psycopg_connection():

Check warning on line 41 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L41 <103>

Missing docstring in public function
Raw output
./tests/resources/test_search_tokens.py:41:1: D103 Missing docstring in public function
return MockPsycopgConnection()


@pytest.fixture
def search_tokens(mock_psycopg_connection):

Check warning on line 46 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L46 <103>

Missing docstring in public function
Raw output
./tests/resources/test_search_tokens.py:46:1: D103 Missing docstring in public function
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

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L51 <103>

Missing docstring in public function
Raw output
./tests/resources/test_search_tokens.py:51:1: D103 Missing docstring in public function
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

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L76 <103>

Missing docstring in public function
Raw output
./tests/resources/test_search_tokens.py:76:1: D103 Missing docstring in public function
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

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L105 <103>

Missing docstring in public function
Raw output
./tests/resources/test_search_tokens.py:105:1: D103 Missing docstring in public function
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

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L127 <205>

1 blank line required between summary line and description
Raw output
./tests/resources/test_search_tokens.py:127:1: D205 1 blank line required between summary line and description

Check warning on line 127 in tests/resources/test_search_tokens.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/resources/test_search_tokens.py#L127 <400>

First line should end with a period
Raw output
./tests/resources/test_search_tokens.py:127:1: D400 First line should end with a period
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,
)

0 comments on commit d336701

Please sign in to comment.