|
| 1 | +from contextlib import contextmanager |
1 | 2 | from copy import deepcopy
|
2 | 3 |
|
3 | 4 | import pytest
|
|
6 | 7 | from app.services.supplementary_data import validate_supplementary_data
|
7 | 8 | from app.utilities.supplementary_data_parser import validate_supplementary_data_v1
|
8 | 9 |
|
| 10 | + |
| 11 | +@contextmanager |
| 12 | +def not_raises(exception): |
| 13 | + try: |
| 14 | + yield |
| 15 | + except exception as validation_error: |
| 16 | + raise pytest.fail(f"{validation_error} RAISED") |
| 17 | + |
| 18 | + |
9 | 19 | SUPPLEMENTARY_DATA_PAYLOAD = {
|
10 | 20 | "dataset_id": "44f1b432-9421-49e5-bd26-e63e18a30b69",
|
11 | 21 | "survey_id": "123",
|
@@ -54,6 +64,35 @@ def test_invalid_supplementary_data_payload_raises_error():
|
54 | 64 | assert str(error.value) == "Invalid supplementary data"
|
55 | 65 |
|
56 | 66 |
|
| 67 | +def test_invalid_supplementary_dataset_version_raises_error(): |
| 68 | + with pytest.raises(ValidationError) as error: |
| 69 | + validate_supplementary_data_v1( |
| 70 | + supplementary_data=SUPPLEMENTARY_DATA_PAYLOAD, |
| 71 | + dataset_id="44f1b432-9421-49e5-bd26-e63e18a30b69", |
| 72 | + identifier="12345678901", |
| 73 | + survey_id="123", |
| 74 | + sds_schema_version="v6", |
| 75 | + ) |
| 76 | + |
| 77 | + assert ( |
| 78 | + str(error.value) |
| 79 | + == "{'_schema': ['The Supplementary Dataset Schema Version does not match the version set in the Questionnaire Schema']}" |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def test_valid_supplementary_dataset_version_does_not_raise_error(): |
| 84 | + with not_raises(ValidationError): |
| 85 | + validated_payload = validate_supplementary_data_v1( |
| 86 | + supplementary_data=SUPPLEMENTARY_DATA_PAYLOAD, |
| 87 | + dataset_id="44f1b432-9421-49e5-bd26-e63e18a30b69", |
| 88 | + identifier="12345678901", |
| 89 | + survey_id="123", |
| 90 | + sds_schema_version="v1", |
| 91 | + ) |
| 92 | + |
| 93 | + assert validated_payload == SUPPLEMENTARY_DATA_PAYLOAD |
| 94 | + |
| 95 | + |
57 | 96 | def test_validate_supplementary_data_payload():
|
58 | 97 | validated_payload = validate_supplementary_data_v1(
|
59 | 98 | supplementary_data=SUPPLEMENTARY_DATA_PAYLOAD,
|
@@ -171,30 +210,6 @@ def test_validate_supplementary_data_payload_with_unknown_field():
|
171 | 210 | assert validated_payload == payload
|
172 | 211 |
|
173 | 212 |
|
174 |
| -def test_validate_supplementary_data_invalid_schema_version(): |
175 |
| - payload = { |
176 |
| - "dataset_id": "44f1b432-9421-49e5-bd26-e63e18a30b69", |
177 |
| - "survey_id": "123", |
178 |
| - "some_field": "value", |
179 |
| - "data": { |
180 |
| - "schema_version": "v3", |
181 |
| - "identifier": "12345678901", |
182 |
| - }, |
183 |
| - } |
184 |
| - |
185 |
| - with pytest.raises(ValidationError) as error: |
186 |
| - validate_supplementary_data_v1( |
187 |
| - supplementary_data=payload, |
188 |
| - dataset_id="001", |
189 |
| - identifier="12345678901", |
190 |
| - survey_id="123", |
191 |
| - ) |
192 |
| - |
193 |
| - assert ( |
194 |
| - str(error.value) == "{'data': {'schema_version': ['Must be one of: v1, v2.']}}" |
195 |
| - ) |
196 |
| - |
197 |
| - |
198 | 213 | def test_validate_supplementary_data_payload_missing_identifier_in_items():
|
199 | 214 | payload = deepcopy(SUPPLEMENTARY_DATA_PAYLOAD)
|
200 | 215 | payload["data"]["items"]["local_units"][0].pop("identifier")
|
|
0 commit comments