Skip to content

Commit 9a72960

Browse files
committed
tests
1 parent 10337fc commit 9a72960

File tree

4 files changed

+71
-19
lines changed

4 files changed

+71
-19
lines changed

backend/src/models/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ class Constants:
3939
}
4040

4141
ALLOWED_CONTAINED_RESOURCES = {"Practitioner", "Patient"}
42+
43+
SUPPLIER_PERMISSIONS_KEY = "supplier_permissions"
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from clients import redis_client
2+
from models.constants import Constants
23
import json
34

45
def get_supplier_permissions(supplier: str) -> list[str]:
5-
permissions_data = redis_client.hget("supplier_permissions", supplier)
6+
permissions_data = redis_client.hget(Constants.SUPPLIER_PERMISSIONS_KEY, supplier)
67
if not permissions_data:
78
return []
89
return json.loads(permissions_data)

backend/tests/test_update_imms.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import unittest
3-
from unittest.mock import create_autospec
3+
from unittest.mock import create_autospec, patch
44

55
from fhir_controller import FhirController
66
from models.errors import Severity, Code, create_operation_outcome
@@ -10,7 +10,18 @@
1010

1111
class TestUpdateImmunizations(unittest.TestCase):
1212
def setUp(self):
13+
# self.controller = create_autospec(FhirController)
14+
self.logger_exception_patcher = patch("logging.Logger.exception")
15+
self.mock_logger_exception = self.logger_exception_patcher.start()
16+
# patch FhirController
1317
self.controller = create_autospec(FhirController)
18+
self.controller.update_immunization = FhirController.update_immunization
19+
20+
21+
22+
def tearDown(self):
23+
self.logger_exception_patcher.stop()
24+
1425

1526
def test_update_immunization(self):
1627
"""it should call service update method"""
@@ -26,7 +37,8 @@ def test_update_immunization(self):
2637
self.controller.update_immunization.assert_called_once_with(lambda_event)
2738
self.assertDictEqual(exp_res, act_res)
2839

29-
def test_handle_exception(self):
40+
@patch("update_imms_handler.FhirController.create_response")
41+
def test_update_imms_exception(self, mock_create_response):
3042
"""unhandled exceptions should result in 500"""
3143
lambda_event = {"pathParameters": {"id": "an-id"}}
3244
error_msg = "an unhandled error"
@@ -38,16 +50,26 @@ def test_handle_exception(self):
3850
code=Code.server_error,
3951
diagnostics=GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE,
4052
)
53+
mock_response = "controller-response-error"
54+
mock_create_response.return_value = mock_response
4155

4256
# When
4357
act_res = update_imms(lambda_event, self.controller)
4458

4559
# Then
46-
act_body = json.loads(act_res["body"])
47-
act_body["id"] = None
60+
# check parameters used to call create_response
61+
args, kwargs = mock_create_response.call_args
62+
self.assertEqual(args[0], 500)
63+
issue = args[1]["issue"][0]
64+
severity = issue["severity"]
65+
code = issue["code"]
66+
diagnostics = issue["diagnostics"]
67+
self.assertEqual(severity, "error")
68+
self.assertEqual(code, "exception")
69+
self.assertEqual(diagnostics, GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE)
70+
self.assertEqual(act_res, mock_response)
4871

49-
self.assertDictEqual(act_body, exp_error)
50-
self.assertEqual(act_res["statusCode"], 500)
72+
5173

5274
def test_update_imms_with_duplicated_identifier_returns_error(self):
5375
"""Should return an IdentifierDuplication error"""
Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
11
import unittest
22
from unittest.mock import patch
3-
from src.models.utils.permissions import get_supplier_permissions
3+
from models.constants import Constants
44

5-
class TestPermissions(unittest.TestCase):
5+
from models.utils.permissions import get_supplier_permissions
66

7-
@patch("clients.redis_client.hget")
8-
def test_returns_list_if_permissions_exist(self, mock_hget):
9-
mock_hget.return_value = '["COVID19_FULL", "FLU_CREATE"]'
10-
result = get_supplier_permissions("DPSFULL")
11-
self.assertEqual(result, ["COVID19_FULL", "FLU_CREATE"])
7+
class TestGetSupplierPermissions(unittest.TestCase):
8+
9+
def setUp(self):
10+
self.redis_client_patcher = patch("models.utils.permissions.redis_client")
11+
self.mock_redis_client = self.redis_client_patcher.start()
12+
13+
def tearDown(self):
14+
patch.stopall()
15+
16+
def test_returns_permissions_list(self):
17+
# Arrange
18+
supplier = "supplier-1"
19+
self.mock_redis_client.hget.return_value = '["perm1", "perm2"]'
20+
# Act
21+
result = get_supplier_permissions(supplier)
22+
# Assert
23+
self.assertEqual(result, ["perm1", "perm2"])
24+
self.mock_redis_client.hget.assert_called_once_with(Constants.SUPPLIER_PERMISSIONS_KEY, supplier)
1225

13-
@patch("clients.redis_client.hget")
14-
def test_returns_empty_list_if_no_permissions(self, mock_hget):
15-
mock_hget.return_value = None
16-
result = get_supplier_permissions("UNKNOWN")
17-
self.assertEqual(result, [])
26+
def test_returns_empty_list_when_no_permissions(self):
27+
# Arrange
28+
supplier = "supplier-2"
29+
self.mock_redis_client.hget.return_value = None
30+
# Act
31+
result = get_supplier_permissions(supplier)
32+
# Assert
33+
self.assertEqual(result, [])
34+
self.mock_redis_client.hget.assert_called_once_with(Constants.SUPPLIER_PERMISSIONS_KEY, supplier)
35+
36+
def test_returns_empty_list_when_empty_string(self):
37+
# Arrange
38+
supplier = "supplier-3"
39+
self.mock_redis_client.hget.return_value = ""
40+
# Act
41+
result = get_supplier_permissions(supplier)
42+
# Assert
43+
self.assertEqual(result, [])
44+
self.mock_redis_client.hget.assert_called_once_with(Constants.SUPPLIER_PERMISSIONS_KEY, supplier)

0 commit comments

Comments
 (0)