From f61f7710f405d7a1790a4b58799dd5aae56a1d33 Mon Sep 17 00:00:00 2001 From: JackPlowman <62281988+JackPlowman@users.noreply.github.com> Date: Thu, 4 Apr 2024 10:10:51 +0100 Subject: [PATCH] Add more unit tests and fix existing test --- sandbox/api/app.py | 11 +++++++ sandbox/api/tests/conftest.py | 9 ++++++ sandbox/api/tests/test_app.py | 52 ++++++++++++++++++++++++++------- sandbox/api/tests/test_utils.py | 19 ++++++++++++ 4 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 sandbox/api/tests/conftest.py create mode 100644 sandbox/api/tests/test_utils.py diff --git a/sandbox/api/app.py b/sandbox/api/app.py index 4a6acaa..61acdf8 100644 --- a/sandbox/api/app.py +++ b/sandbox/api/app.py @@ -10,6 +10,17 @@ logger = getLogger(__name__) +@app.route("/_status", methods=["GET"]) +@app.route("/_ping", methods=["GET"]) +@app.route("/health", methods=["GET"]) +def health() -> dict: + """Health check endpoint""" + return { + "status": "online", + "message": "Validated Relationships Service Sandbox is running", + } + + @app.route("/RelatedPerson", methods=["GET"]) def related_persons() -> Union[dict, tuple]: """Sandbox API for GET /RelatedPerson diff --git a/sandbox/api/tests/conftest.py b/sandbox/api/tests/conftest.py new file mode 100644 index 0000000..57798b8 --- /dev/null +++ b/sandbox/api/tests/conftest.py @@ -0,0 +1,9 @@ +import pytest + +from ..app import app + + +@pytest.fixture() +def client() -> object: + """Create a test client for the app.""" + return app.test_client() diff --git a/sandbox/api/tests/test_app.py b/sandbox/api/tests/test_app.py index 330567d..9e6c051 100644 --- a/sandbox/api/tests/test_app.py +++ b/sandbox/api/tests/test_app.py @@ -2,21 +2,51 @@ import pytest -from ..app import app +FILE_PATH = "sandbox.api.app" -@pytest.fixture() -def client() -> object: - """Create a test client for the app.""" - return app.test_client() +@pytest.mark.parametrize("endpoint", ["/_status", "/_ping", "/health"]) +def test_health_check(client: object, endpoint: str) -> None: + """Test health check endpoints.""" + # Act + response = client.get(endpoint) + # Assert + assert response.status_code == 200 + assert response.json == { + "status": "online", + "message": "Validated Relationships Service Sandbox is running", + } + + +@patch(f"{FILE_PATH}.get_response") +def test_related_person__identifier_only( + mock_get_response: MagicMock, client: object +) -> None: + """Test related_persons endpoint with identifier only.""" + # Arrange + mock_get_response.return_value = expected_body = {"data": "mocked"} + # Act + response = client.get("/RelatedPerson?identifier=1234567890") + # Assert + mock_get_response.assert_called_once_with( + "./api/responses/GET_RelatedPerson/identifier.json" + ) + assert response.status_code == 200 + assert response.json == expected_body -@patch("sandbox.api.app.get_response") -def test_related_person(mock_get_response: MagicMock, client: object) -> None: - """""" +@patch(f"{FILE_PATH}.get_response") +def test_related_person__identifier_and_patient( + mock_get_response: MagicMock, client: object +) -> None: + """Test related_persons endpoint with identifier and patient.""" # Arrange - mock_get_response.return_value = {"data": "mocked"} + mock_get_response.return_value = expected_body = {"data": "mocked"} # Act - client.get("/RelatedPerson?identifier=1234") + response = client.get("/RelatedPerson?identifier=1234567890&patient=0987654321") # Assert - mock_get_response.assert_called_once_with("./api/responses/RelatedPerson.json") + mock_get_response.assert_called_once_with( + "./api/responses/GET_RelatedPerson/identifier_and_patient.json" + ) + assert response.status_code == 200 + assert response.json == expected_body diff --git a/sandbox/api/tests/test_utils.py b/sandbox/api/tests/test_utils.py new file mode 100644 index 0000000..f8b7a97 --- /dev/null +++ b/sandbox/api/tests/test_utils.py @@ -0,0 +1,19 @@ +from unittest.mock import MagicMock, patch + +from ..utils import get_response + +FILE_PATH = "sandbox.api.utils" + + +@patch(f"{FILE_PATH}.open") +def test_get_response(mock_open: MagicMock) -> None: + # Arrange + mock_open.return_value.__enter__.return_value.read.return_value = ( + '{"data": "mocked"}' + ) + file_name = "./api/responses/GET_RelatedPerson/identifier.json" + # Act + response = get_response(file_name) + # Assert + mock_open.assert_called_once_with(file_name, "r") + assert response == {"data": "mocked"}