-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_search_imms.py
More file actions
165 lines (127 loc) · 6.54 KB
/
test_search_imms.py
File metadata and controls
165 lines (127 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import json
import unittest
from unittest.mock import create_autospec, patch
from fhir_controller import FhirController
from models.errors import Severity, Code, create_operation_outcome
from search_imms_handler import search_imms
from pathlib import Path
from constants import GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE
script_location = Path(__file__).absolute().parent
class TestSearchImmunizations(unittest.TestCase):
def setUp(self):
self.controller = create_autospec(FhirController)
self.logger_exception_patcher = patch("logging.Logger.exception")
self.mock_logger_exception = self.logger_exception_patcher.start()
self.logger_info_patcher = patch("logging.Logger.info")
self.mock_logger_info = self.logger_info_patcher.start()
self.logger_exception_patcher = patch("logging.Logger.exception")
self.mock_logger_exception = self.logger_exception_patcher.start()
def tearDown(self):
patch.stopall()
def test_search_immunizations(self):
"""it should return a list of Immunizations"""
lambda_event = {"pathParameters": {"id": "an-id"}, "body": None}
exp_res = {"a-key": "a-value"}
self.controller.search_immunizations.return_value = exp_res
# When
act_res = search_imms(lambda_event, self.controller)
# Then
self.controller.search_immunizations.assert_called_once_with(lambda_event)
self.assertDictEqual(exp_res, act_res)
def test_search_immunizations_to_get_imms_id(self):
"""it should return a list of Immunizations"""
lambda_event = {
"pathParameters": {"id": "an-id"},
"queryStringParameters": {
"identifier": "https://supplierABC/identifiers/vacc|f10b59b3-fc73-4616-99c9-9e882ab31184",
"_elements": "id,meta",
},
"body": None,
}
exp_res = {"a-key": "a-value"}
self.controller.get_immunization_by_identifier.return_value = exp_res
# When
act_res = search_imms(lambda_event, self.controller)
# Then
self.controller.get_immunization_by_identifier.assert_called_once_with(lambda_event)
self.assertDictEqual(exp_res, act_res)
def test_search_immunizations_get_id_from_body(self):
"""it should return a list of Immunizations"""
lambda_event = {
"pathParameters": {"id": "an-id"},
"body": "cGF0aWVudC5pZGVudGlmaWVyPWh0dHBzJTNBJTJGJTJGZmhpci5uaHMudWslMkZJZCUyRm5ocy1udW1iZXIlN0M5NjkzNjMyMTA5Ji1pbW11bml6YXRpb24udGFyZ2V0PUNPVklEMTkmX2luY2x1ZGU9SW1tdW5pemF0aW9uJTNBcGF0aWVudCZpZGVudGlmaWVyPWh0dHBzJTNBJTJGJTJGc3VwcGxpZXJBQkMlMkZpZGVudGlmaWVycyUyRnZhY2MlN0NmMTBiNTliMy1mYzczLTQ2MTYtOTljOS05ZTg4MmFiMzExODQmX2VsZW1lbnRzPWlkJTJDbWV0YSZpZD1z",
"queryStringParameters": None,
}
exp_res = {"a-key": "a-value"}
self.controller.get_immunization_by_identifier.return_value = exp_res
# When
act_res = search_imms(lambda_event, self.controller)
# Then
self.controller.get_immunization_by_identifier.assert_called_once_with(lambda_event)
self.assertDictEqual(exp_res, act_res)
def test_search_immunizations_get_id_from_body_passing_none(self):
"""it should enter search_immunizations as both the request params are none"""
lambda_event = {"pathParameters": {"id": "an-id"}, "body": None, "queryStringParameters": None}
exp_res = {"a-key": "a-value"}
self.controller.search_immunizations.return_value = exp_res
# When
act_res = search_imms(lambda_event, self.controller)
# Then
self.controller.search_immunizations.assert_called_once_with(lambda_event)
self.assertDictEqual(exp_res, act_res)
def test_search_immunizations_get_id_from_body_element(self):
"""it should enter into get_immunization_by_identifier only _element paramter is present"""
lambda_event = {
"pathParameters": {"id": "an-id"},
"body": "X2VsZW1lbnRzPWlkJTJDbWV0YQ==",
"queryStringParameters": None,
}
exp_res = {"a-key": "a-value"}
self.controller.get_immunization_by_identifier.return_value = exp_res
# When
act_res = search_imms(lambda_event, self.controller)
# Then
self.controller.get_immunization_by_identifier.assert_called_once_with(lambda_event)
self.assertDictEqual(exp_res, act_res)
def test_search_immunizations_get_id_from_body_imms_identifer(self):
"""it should enter into get_immunization_by_identifier only identifier paramter is present"""
lambda_event = {
"pathParameters": {"id": "an-id"},
"body": "aWRlbnRpZmllcj1pZCUyQ21ldGE=",
"queryStringParameters": None,
}
exp_res = {"a-key": "a-value"}
self.controller.get_immunization_by_identifier.return_value = exp_res
# When
act_res = search_imms(lambda_event, self.controller)
# Then
self.controller.get_immunization_by_identifier.assert_called_once_with(lambda_event)
self.assertDictEqual(exp_res, act_res)
@patch("search_imms_handler.MAX_RESPONSE_SIZE_BYTES", 10)
def test_search_immunizations_lambda_size_limit(self):
"""it should return 400 as search returned too many results."""
lambda_event = {"pathParameters": {"id": "an-id"}, "body": None}
self.controller.search_immunizations.return_value = {"response": "size is larger than lambda limit"}
# When
act_res = search_imms(lambda_event, self.controller)
# Then
self.controller.search_immunizations.assert_called_once_with(lambda_event)
self.assertEqual(act_res["statusCode"], 400)
def test_search_handle_exception(self):
"""unhandled exceptions should result in 500"""
lambda_event = {"pathParameters": {"id": "an-id"}}
error_msg = "an unhandled error"
self.controller.search_immunizations.side_effect = Exception(error_msg)
exp_error = create_operation_outcome(
resource_id=None,
severity=Severity.error,
code=Code.server_error,
diagnostics=GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE,
)
# When
act_res = search_imms(lambda_event, self.controller)
# Then
act_body = json.loads(act_res["body"])
self.assertEqual(exp_error["issue"][0]["code"], act_body["issue"][0]["code"])
self.assertEqual(exp_error["issue"][0]["severity"], act_body["issue"][0]["severity"])
self.assertEqual(act_res["statusCode"], 500)