|
| 1 | +"""Tests for the process_row module""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +from unittest.mock import patch |
| 5 | +from copy import deepcopy |
| 6 | +from boto3 import client as boto3_client |
| 7 | +from moto import mock_s3 |
| 8 | +from decimal import Decimal |
| 9 | + |
| 10 | + |
| 11 | +from tests.utils_for_recordprocessor_tests.values_for_recordprocessor_tests import ( |
| 12 | + MockFieldDictionaries, |
| 13 | +) |
| 14 | + |
| 15 | +from tests.utils_for_recordprocessor_tests.utils_for_recordprocessor_tests import ( |
| 16 | + GenericSetUp, |
| 17 | + GenericTearDown, |
| 18 | +) |
| 19 | +from tests.utils_for_recordprocessor_tests.mock_environment_variables import MOCK_ENVIRONMENT_DICT |
| 20 | + |
| 21 | +with patch("os.environ", MOCK_ENVIRONMENT_DICT): |
| 22 | + # Do not attempt 'from src.mappings import Vaccine' as this imports a different instance of Vaccine |
| 23 | + # and tests will break |
| 24 | + from clients import REGION_NAME |
| 25 | + from src.process_row import process_row |
| 26 | + |
| 27 | +s3_client = boto3_client("s3", region_name=REGION_NAME) |
| 28 | +ROW_DETAILS = MockFieldDictionaries.all_fields |
| 29 | +Allowed_Operations = {"CREATE", "UPDATE", "DELETE"} |
| 30 | + |
| 31 | + |
| 32 | +@mock_s3 |
| 33 | +@patch.dict("os.environ", MOCK_ENVIRONMENT_DICT) |
| 34 | +class TestProcessRow(unittest.TestCase): |
| 35 | + """Tests for process_row""" |
| 36 | + |
| 37 | + def setUp(self) -> None: |
| 38 | + GenericSetUp(s3_client) |
| 39 | + |
| 40 | + def tearDown(self) -> None: |
| 41 | + GenericTearDown(s3_client) |
| 42 | + |
| 43 | + def test_process_row_success(self): |
| 44 | + """ |
| 45 | + Test that process_row gives the expected output. |
| 46 | + These tests check that the row is valid and matches the expected output. |
| 47 | + """ |
| 48 | + # set the expected output from 'process_row' in case of success |
| 49 | + expected_result = { |
| 50 | + "resourceType": "Immunization", |
| 51 | + "status": "completed", |
| 52 | + "protocolApplied": [{"targetDisease": [], "doseNumberPositiveInt": 1}], |
| 53 | + "reasonCode": [{"coding": [{"system": "http://snomed.info/sct", "code": "1037351000000105"}]}], |
| 54 | + "recorded": "2024-09-04", |
| 55 | + "identifier": [{"value": "RSV_002", "system": "https://www.ravs.england.nhs.uk/"}], |
| 56 | + "patient": {"reference": "#Patient1"}, |
| 57 | + "contained": [ |
| 58 | + { |
| 59 | + "id": "Patient1", |
| 60 | + "resourceType": "Patient", |
| 61 | + "birthDate": "2008-02-17", |
| 62 | + "gender": "male", |
| 63 | + "address": [{"postalCode": "WD25 0DZ"}], |
| 64 | + "identifier": [{"system": "https://fhir.nhs.uk/Id/nhs-number", "value": "9732928395"}], |
| 65 | + "name": [{"family": "PEEL", "given": ["PHYLIS"]}], |
| 66 | + }, |
| 67 | + { |
| 68 | + "resourceType": "Practitioner", |
| 69 | + "id": "Practitioner1", |
| 70 | + "name": [{"family": "O'Reilly", "given": ["Ellena"]}], |
| 71 | + }, |
| 72 | + ], |
| 73 | + "vaccineCode": { |
| 74 | + "coding": [ |
| 75 | + { |
| 76 | + "system": "http://snomed.info/sct", |
| 77 | + "code": "42223111000001107", |
| 78 | + "display": "Quadrivalent influenza vaccine (split virion, inactivated)", |
| 79 | + } |
| 80 | + ] |
| 81 | + }, |
| 82 | + "manufacturer": {"display": "Sanofi Pasteur"}, |
| 83 | + "expirationDate": "2024-09-15", |
| 84 | + "lotNumber": "BN92478105653", |
| 85 | + "extension": [ |
| 86 | + { |
| 87 | + "url": "https://fhir.hl7.org.uk/StructureDefinition/Extension-UKCore-VaccinationProcedure", |
| 88 | + "valueCodeableConcept": { |
| 89 | + "coding": [ |
| 90 | + { |
| 91 | + "system": "http://snomed.info/sct", |
| 92 | + "code": "956951000000104", |
| 93 | + "display": "RSV vaccination in pregnancy (procedure)", |
| 94 | + } |
| 95 | + ] |
| 96 | + }, |
| 97 | + } |
| 98 | + ], |
| 99 | + "occurrenceDateTime": "2024-09-04T18:33:25+00:00", |
| 100 | + "primarySource": True, |
| 101 | + "site": {"coding": [{"system": "http://snomed.info/sct", "code": "368209003", "display": "Right arm"}]}, |
| 102 | + "route": { |
| 103 | + "coding": [{"system": "http://snomed.info/sct", "code": "1210999013", "display": "Intradermal use"}] |
| 104 | + }, |
| 105 | + "doseQuantity": { |
| 106 | + "value": Decimal("0.3"), |
| 107 | + "unit": "Inhalation - unit of product usage", |
| 108 | + "system": "http://snomed.info/sct", |
| 109 | + "code": "2622896019", |
| 110 | + }, |
| 111 | + "performer": [ |
| 112 | + { |
| 113 | + "actor": { |
| 114 | + "type": "Organization", |
| 115 | + "identifier": {"system": "https://fhir.nhs.uk/Id/ods-organization-code", "value": "RVVKC"}, |
| 116 | + } |
| 117 | + }, |
| 118 | + {"actor": {"reference": "#Practitioner1"}}, |
| 119 | + ], |
| 120 | + "location": {"identifier": {"value": "RJC02", "system": "https://fhir.nhs.uk/Id/ods-organization-code"}}, |
| 121 | + } |
| 122 | + |
| 123 | + # call 'process_row' with required details |
| 124 | + imms_fhir_resource = process_row("EMIS", Allowed_Operations, ROW_DETAILS) |
| 125 | + # validate if the response with expected result |
| 126 | + self.assertDictEqual(imms_fhir_resource["fhir_json"], expected_result) |
| 127 | + |
| 128 | + def test_process_row_invalid_action_flag(self): |
| 129 | + """ |
| 130 | + Test that process_row gives the expected output. |
| 131 | + These tests check that the row is valid and matches the expected output. |
| 132 | + """ |
| 133 | + Mock_Row = deepcopy(ROW_DETAILS) |
| 134 | + # setting up the invalid action flag other than 'NEW', 'UPDATE' or 'DELETE' |
| 135 | + Mock_Row["ACTION_FLAG"] = "Invalid" |
| 136 | + |
| 137 | + # call 'process_row' with required details |
| 138 | + response = process_row("EMIS", Allowed_Operations, Mock_Row) |
| 139 | + |
| 140 | + # validate if we got INVALID_ACTION_FLAG in response |
| 141 | + self.assertEqual(response["diagnostics"]["error_type"], "INVALID_ACTION_FLAG") |
| 142 | + |
| 143 | + def test_process_row_missing_action_flag(self): |
| 144 | + """ |
| 145 | + Test that process_row gives the expected output. |
| 146 | + These tests check that the row is valid and matches the expected output. |
| 147 | + """ |
| 148 | + |
| 149 | + Mock_Row = deepcopy(ROW_DETAILS) |
| 150 | + # removing action flag from row |
| 151 | + Mock_Row.pop("ACTION_FLAG") |
| 152 | + |
| 153 | + # call 'process_row' with required details |
| 154 | + response = process_row("EMIS", Allowed_Operations, Mock_Row) |
| 155 | + # validate if we got INVALID_ACTION_FLAG in response |
| 156 | + self.assertEqual(response["diagnostics"]["error_type"], "INVALID_ACTION_FLAG") |
| 157 | + |
| 158 | + def test_process_row_missing_permission(self): |
| 159 | + """ |
| 160 | + Test that process_row gives the expected output. |
| 161 | + These tests check that the row is valid and matches the expected output. |
| 162 | + """ |
| 163 | + # only create and delete permission. Missing update |
| 164 | + allowed_operation = {"CREATE", "DELETE"} |
| 165 | + # copy row data with Action_Flag = 'Update' |
| 166 | + Mock_Row = deepcopy(ROW_DETAILS) |
| 167 | + |
| 168 | + # call 'process_row' with required details |
| 169 | + response = process_row("EMIS", allowed_operation, Mock_Row) |
| 170 | + self.assertEqual(response["diagnostics"]["error_type"], "NO_PERMISSIONS") |
| 171 | + self.assertEqual(response["diagnostics"]["statusCode"], 403) |
| 172 | + |
| 173 | + def test_process_row_missing_unique_id(self): |
| 174 | + """ |
| 175 | + Test that process_row gives the expected output. |
| 176 | + These tests check that the row is valid and matches the expected output. |
| 177 | + """ |
| 178 | + # copy row data and remove 'UNIQUE_ID' |
| 179 | + Mock_Row = deepcopy(ROW_DETAILS) |
| 180 | + Mock_Row.pop("UNIQUE_ID") |
| 181 | + # call 'process_row' with required details |
| 182 | + response = process_row("EMIS", Allowed_Operations, Mock_Row) |
| 183 | + |
| 184 | + self.assertEqual(response["diagnostics"]["error_type"], "MISSING_UNIQUE_ID") |
| 185 | + self.assertEqual(response["diagnostics"]["statusCode"], 400) |
| 186 | + |
| 187 | + def test_process_row_missing_unique_id_uri(self): |
| 188 | + """ |
| 189 | + Test that process_row gives the expected output. |
| 190 | + These tests check that the row is valid and matches the expected output. |
| 191 | + """ |
| 192 | + # copy row data and remove 'UNIQUE_ID_URI' |
| 193 | + Mock_Row = deepcopy(ROW_DETAILS) |
| 194 | + Mock_Row.pop("UNIQUE_ID_URI") |
| 195 | + # call 'process_row' with required details |
| 196 | + response = process_row("EMIS", Allowed_Operations, Mock_Row) |
| 197 | + |
| 198 | + self.assertEqual(response["diagnostics"]["error_message"], "UNIQUE_ID or UNIQUE_ID_URI is missing") |
| 199 | + self.assertEqual(response["diagnostics"]["statusCode"], 400) |
| 200 | + |
| 201 | + |
| 202 | +if __name__ == "__main__": |
| 203 | + unittest.main() |
0 commit comments