|
| 1 | +import json |
1 | 2 | from json import dumps |
2 | 3 | from unittest.mock import MagicMock, patch |
3 | 4 |
|
@@ -30,16 +31,6 @@ def test_health_check(client: object, endpoint: str) -> None: |
30 | 31 | @pytest.mark.parametrize( |
31 | 32 | "request_args,response_file_name,status_code", |
32 | 33 | [ |
33 | | - ( |
34 | | - "identifier=9000000041", |
35 | | - "./api/responses/not_found.json", |
36 | | - 404, |
37 | | - ), |
38 | | - ( |
39 | | - "identifier=9000000017&patient:identifier=9000000041", |
40 | | - "./api/responses/not_found.json", |
41 | | - 404, |
42 | | - ), |
43 | 34 | ( |
44 | 35 | "identifier=9000000033", |
45 | 36 | "./api/responses/GET_RelatedPerson/empty_response_9000000033.json", |
@@ -111,6 +102,46 @@ def test_related_person( |
111 | 102 | assert response.json == expected_body |
112 | 103 |
|
113 | 104 |
|
| 105 | +@pytest.mark.parametrize( |
| 106 | + "request_args,response_file_name,status_code", |
| 107 | + [ |
| 108 | + ( |
| 109 | + "identifier=9000000041", |
| 110 | + "./api/examples/errors/not-found.yaml", |
| 111 | + 404, |
| 112 | + ), |
| 113 | + ( |
| 114 | + "identifier=9000000017&patient:identifier=9000000041", |
| 115 | + "./api/examples/errors/not-found.yaml", |
| 116 | + 404, |
| 117 | + ) |
| 118 | + ], |
| 119 | +) |
| 120 | +@patch(f"{UTILS_FILE_PATH}.generate_response_from_example") |
| 121 | +def test_related_person__not_found( |
| 122 | + mock_generate_response_from_example: MagicMock, |
| 123 | + request_args: str, |
| 124 | + response_file_name: str, |
| 125 | + status_code: int, |
| 126 | + client: object |
| 127 | +) -> None: |
| 128 | + """Test related_persons endpoint.""" |
| 129 | + # Arrange |
| 130 | + mock_generate_response_from_example.return_value = mocked_response = ( |
| 131 | + Response( |
| 132 | + dumps({"data": "mocked"}), |
| 133 | + status=status_code, |
| 134 | + content_type="application/json" |
| 135 | + ) |
| 136 | + ) |
| 137 | + # Act |
| 138 | + response = client.get(f"{RELATED_PERSON_API_ENDPOINT}?{request_args}") |
| 139 | + # Assert |
| 140 | + mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code) |
| 141 | + assert response.status_code == status_code |
| 142 | + assert response.json == json.loads(mocked_response.get_data(as_text=True)) |
| 143 | + |
| 144 | + |
114 | 145 | @pytest.mark.parametrize( |
115 | 146 | "url_path,response_file_name,status_code", |
116 | 147 | [ |
@@ -144,54 +175,106 @@ def test_questionnaire_response( |
144 | 175 | ("request_args,response_file_name,status_code"), |
145 | 176 | [ |
146 | 177 | ( |
147 | | - "performer:identifier=9000000010&status=active&_include=Consent:performer", |
148 | | - "./api/examples/GET_Consent/adults-consenting-active-include-both.yaml", |
| 178 | + "performer:identifier=9000000017&status=active", |
| 179 | + "./api/examples/GET_Consent/filtered-relationships-status-active.yaml", |
149 | 180 | 200, |
150 | 181 | ), |
151 | 182 | ( |
152 | | - "performer:identifier=9000000017&status=active&_include=Consent:performer", |
153 | | - "./api/examples/GET_Consent/mixed.yaml", |
| 183 | + "performer:identifier=9000000017&status=inactive", |
| 184 | + "./api/examples/GET_Consent/filtered-relationships-status-inactive.yaml", |
154 | 185 | 200, |
155 | 186 | ), |
156 | 187 | ( |
157 | | - "performer:identifier=9000000019&status=active&_include=Consent:performer", |
158 | | - "./api/examples/GET_Consent/mother-child.yaml", |
| 188 | + "performer:identifier=9000000017&status=proposed,active", |
| 189 | + "./api/examples/GET_Consent/filtered-relationships-status-proposed-active.yaml", |
159 | 190 | 200, |
160 | 191 | ), |
161 | | - ], |
| 192 | + ( |
| 193 | + "performer:identifier=9000000022", |
| 194 | + "./api/examples/GET_Consent/multiple-relationships.yaml", |
| 195 | + 200, |
| 196 | + ), |
| 197 | + ( |
| 198 | + "performer:identifier=9000000022&_include=Consent:patient", |
| 199 | + "./api/examples/GET_Consent/multiple-relationships-include-patient.yaml", |
| 200 | + 200, |
| 201 | + ), |
| 202 | + ( |
| 203 | + "performer:identifier=9000000022&_include=Consent:performer", |
| 204 | + "./api/examples/GET_Consent/multiple-relationships-include-performer.yaml", |
| 205 | + 200, |
| 206 | + ), |
| 207 | + ( |
| 208 | + "performer:identifier=9000000022&_include=Consent:performer,Consent:patient", |
| 209 | + "./api/examples/GET_Consent/multiple-relationships-include-performer-patient.yaml", |
| 210 | + 200, |
| 211 | + ), |
| 212 | + ( |
| 213 | + "performer:identifier=9000000025", |
| 214 | + "./api/examples/GET_Consent/no-relationships.yaml", |
| 215 | + 200, |
| 216 | + ), |
| 217 | + ( |
| 218 | + "performer:identifier=9000000010", |
| 219 | + "./api/examples/GET_Consent/single-consenting-adult-relationship.yaml", |
| 220 | + 200, |
| 221 | + ), |
| 222 | + ( |
| 223 | + "performer:identifier=9000000010&_include=Consent:performer,Consent:patient", |
| 224 | + "./api/examples/GET_Consent/single-consenting-adult-relationship-include-performer-patient.yaml", |
| 225 | + 200, |
| 226 | + ), |
| 227 | + ( |
| 228 | + "performer:identifier=9000000019", |
| 229 | + "./api/examples/GET_Consent/single-mother-child-relationship.yaml", |
| 230 | + 200, |
| 231 | + ), |
| 232 | + ( |
| 233 | + "performer:identifier=9000000019&_include=Consent:performer,Consent:patient", |
| 234 | + "./api/examples/GET_Consent/single-mother-child-relationship-include-performer-patient.yaml", |
| 235 | + 200 |
| 236 | + ) |
| 237 | + ] |
162 | 238 | ) |
163 | 239 | @patch(f"{APP_FILE_PATH}.generate_response_from_example") |
164 | 240 | def test_consent( |
165 | 241 | mock_generate_response_from_example: MagicMock, |
166 | 242 | request_args: str, |
167 | 243 | response_file_name: str, |
168 | | - client: object, |
169 | 244 | status_code: int, |
| 245 | + client: object |
170 | 246 | ) -> None: |
171 | 247 | """Test Consent endpoint.""" |
172 | | - mock_generate_response_from_example.return_value = mock_response = Response( |
173 | | - dumps({"data": "abc"}), status_code |
| 248 | + mock_generate_response_from_example.return_value = mocked_response = ( |
| 249 | + Response( |
| 250 | + dumps({"data": "mocked"}), |
| 251 | + status=status_code, |
| 252 | + content_type="application/json" |
| 253 | + ) |
174 | 254 | ) |
175 | 255 | # Act |
176 | 256 | response = client.get(f"{CONSENT_API_ENDPOINT}?{request_args}") |
177 | 257 | # Assert |
178 | | - mock_generate_response_from_example.assert_called_once_with(response_file_name, 200) |
| 258 | + mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code) |
179 | 259 | assert response.status_code == status_code |
180 | | - assert response.json == mock_response.json |
| 260 | + assert response.json == json.loads(mocked_response.get_data(as_text=True)) |
181 | 261 |
|
182 | 262 |
|
183 | | -@patch(f"{APP_FILE_PATH}.load_json_file") |
| 263 | +@patch(f"{APP_FILE_PATH}.generate_response_from_example") |
184 | 264 | def test_consent__400_bad_request( |
185 | | - mock_load_json_file: MagicMock, client: object |
| 265 | + mock_generate_response_from_example: MagicMock, |
| 266 | + client: object, |
186 | 267 | ) -> None: |
187 | 268 | """Test Consent endpoint.""" |
188 | | - mock_load_json_file.return_value = {"data": "mocked"} |
| 269 | + mock_generate_response_from_example.return_value = {"data": "mocked"} |
189 | 270 | # Act |
190 | 271 | client.get( |
191 | | - f"{CONSENT_API_ENDPOINT}?performer:identifier=9000000012&status=active&_include=Consent:performer" |
| 272 | + f"{CONSENT_API_ENDPOINT}?performer:identifier=9000000999" |
192 | 273 | ) |
193 | 274 | # Assert |
194 | | - mock_load_json_file.assert_called_once_with("./api/responses/not_found.json") |
| 275 | + mock_generate_response_from_example.assert_called_once_with( |
| 276 | + "./api/examples/errors/not-found.yaml" |
| 277 | + ) |
195 | 278 |
|
196 | 279 |
|
197 | 280 | @patch(f"{APP_FILE_PATH}.remove_system") |
|
0 commit comments