|
3 | 3 | import sys |
4 | 4 | import unittest |
5 | 5 | import uuid |
6 | | -from unittest.mock import MagicMock, create_autospec |
| 6 | +from unittest.mock import create_autospec |
7 | 7 |
|
8 | 8 | sys.path.append(f"{os.path.dirname(os.path.abspath(__file__))}/../src") |
9 | 9 |
|
10 | | -from dynamodb import EventTable |
11 | | -from get_imms_handler import get_imms, create_operation_outcome |
| 10 | +from get_imms_handler import get_immunisation_by_id |
| 11 | +from fhir_controller import FhirController |
| 12 | +from models.errors import Severity, Code, create_operation_outcome |
12 | 13 |
|
13 | 14 |
|
14 | 15 | class TestGetImmunisationById(unittest.TestCase): |
15 | 16 | def setUp(self): |
16 | | - self.dynamodb_service = create_autospec(EventTable) |
| 17 | + self.controller = create_autospec(FhirController) |
17 | 18 |
|
18 | | - def test_get_imms_happy_path(self): |
19 | | - # Arrange |
20 | | - self.dynamodb_service.get_event_by_id = MagicMock(return_value={"message": "Mocked event data"}) |
21 | | - lambda_event = {"pathParameters": {"id": "sample-id"}} |
| 19 | + def test_get_immunisation_by_id(self): |
| 20 | + """it should return immunisation by id""" |
| 21 | + lambda_event = {"pathParameters": {"id": "an-id"}} |
| 22 | + exp_res = {"a-key": "a-value"} |
22 | 23 |
|
23 | | - # Act |
24 | | - result = get_imms(lambda_event, self.dynamodb_service) |
| 24 | + self.controller.get_immunisation_by_id.return_value = exp_res |
25 | 25 |
|
26 | | - # Assert |
27 | | - self.dynamodb_service.get_event_by_id.assert_called_once_with(lambda_event["pathParameters"]["id"]) |
28 | | - self.assertEqual(result['statusCode'], 200) |
29 | | - self.assertDictEqual(json.loads(result['body']), {"message": "Mocked event data"}) |
| 26 | + # When |
| 27 | + act_res = get_immunisation_by_id(lambda_event, self.controller) |
30 | 28 |
|
31 | | - def test_get_imms_handler_sad_path_400(self): |
32 | | - unformatted_event = {"pathParameters": {"id": "unexpected_id"}} |
| 29 | + # Then |
| 30 | + self.controller.get_immunisation_by_id.assert_called_once_with(lambda_event) |
| 31 | + self.assertDictEqual(exp_res, act_res) |
33 | 32 |
|
34 | | - # Act |
35 | | - result = get_imms(unformatted_event, None) |
| 33 | + def test_handle_exception(self): |
| 34 | + """unhandled exceptions should result in 500""" |
| 35 | + lambda_event = {"pathParameters": {"id": "an-id"}} |
| 36 | + error_msg = "an unhandled error" |
| 37 | + self.controller.get_immunisation_by_id.side_effect = Exception(error_msg) |
36 | 38 |
|
37 | | - # Assert |
38 | | - assert result['statusCode'] == 400 |
39 | | - act_body = json.loads(result['body']) |
40 | | - exp_body = create_operation_outcome(str(uuid.uuid4()), |
41 | | - "he provided event ID is either missing or not in the expected format.", |
42 | | - "invalid") |
43 | | - act_body["id"] = None |
44 | | - exp_body["id"] = None |
45 | | - self.assertDictEqual(act_body, exp_body) |
| 39 | + exp_error = create_operation_outcome(resource_id=str(uuid.uuid4()), severity=Severity.error, |
| 40 | + code=Code.not_found, |
| 41 | + diagnostics=error_msg) |
46 | 42 |
|
47 | | - def test_get_imms_handler_sad_path_404(self): |
48 | | - # Arrange |
49 | | - self.dynamodb_service.get_event_by_id = MagicMock(return_value=None) |
50 | | - incorrect_event = {"pathParameters": {"id": "incorrectid"}} |
| 43 | + # When |
| 44 | + act_res = get_immunisation_by_id(lambda_event, self.controller) |
51 | 45 |
|
52 | | - # Act |
53 | | - result = get_imms(incorrect_event, self.dynamodb_service) |
54 | | - |
55 | | - # Assert |
56 | | - assert result['statusCode'] == 404 |
57 | | - self.assertEqual(result['headers']['Content-Type'], "application/fhir+json") |
58 | | - act_body = json.loads(result['body']) |
59 | | - exp_body = create_operation_outcome(str(uuid.uuid4()), "The requested resource was not found.", "not-found") |
| 46 | + # Then |
| 47 | + act_body = json.loads(act_res["body"]) |
60 | 48 | act_body["id"] = None |
| 49 | + exp_body = json.loads(exp_error.json()) # to and from json so, we get from OrderedDict to Dict |
61 | 50 | exp_body["id"] = None |
| 51 | + |
62 | 52 | self.assertDictEqual(act_body, exp_body) |
| 53 | + self.assertEqual(act_res["statusCode"], 500) |
0 commit comments