Skip to content

Commit

Permalink
Add more unit tests and fix existing test
Browse files Browse the repository at this point in the history
  • Loading branch information
JackPlowman committed Apr 4, 2024
1 parent 140b03b commit f61f771
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
11 changes: 11 additions & 0 deletions sandbox/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions sandbox/api/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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()
52 changes: 41 additions & 11 deletions sandbox/api/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions sandbox/api/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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"}

0 comments on commit f61f771

Please sign in to comment.