|
| 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 | + MockFileRows, |
| 14 | +) |
| 15 | + |
| 16 | +from tests.utils_for_recordprocessor_tests.utils_for_recordprocessor_tests import ( |
| 17 | + GenericSetUp, |
| 18 | + GenericTearDown, |
| 19 | +) |
| 20 | +from tests.utils_for_recordprocessor_tests.mock_environment_variables import MOCK_ENVIRONMENT_DICT |
| 21 | + |
| 22 | +with patch("os.environ", MOCK_ENVIRONMENT_DICT): |
| 23 | + # Do not attempt 'from src.mappings import Vaccine' as this imports a different instance of Vaccine |
| 24 | + # and tests will break |
| 25 | + from mappings import Vaccine |
| 26 | + from clients import REGION_NAME |
| 27 | + from src.process_row import process_row |
| 28 | + |
| 29 | +s3_client = boto3_client("s3", region_name=REGION_NAME) |
| 30 | +ROW_DETAILS = MockFieldDictionaries.all_fields |
| 31 | +Allowed_Operations = {'CREATE', 'UPDATE', 'DELETE'} |
| 32 | + |
| 33 | +@mock_s3 |
| 34 | +@patch.dict("os.environ", MOCK_ENVIRONMENT_DICT) |
| 35 | +class TestProcessRow(unittest.TestCase): |
| 36 | + """Tests for process_row""" |
| 37 | + |
| 38 | + def setUp(self) -> None: |
| 39 | + GenericSetUp(s3_client) |
| 40 | + |
| 41 | + def tearDown(self) -> None: |
| 42 | + GenericTearDown(s3_client) |
| 43 | + |
| 44 | + def test_process_row_success(self): |
| 45 | + """ |
| 46 | + Test that process_row gives the expected output. 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 = {'resourceType': 'Immunization', 'status': 'completed', 'protocolApplied': [{'targetDisease': [], 'doseNumberPositiveInt': 1}], 'reasonCode': [{'coding': [{'system': 'http://snomed.info/sct', 'code': '1037351000000105'}]}], 'recorded': '2024-09-04', 'identifier': [{'value': 'RSV_002', 'system': 'https://www.ravs.england.nhs.uk/'}], 'patient': {'reference': '#Patient1'}, 'contained': [{'id': 'Patient1', 'resourceType': 'Patient', 'birthDate': '2008-02-17', 'gender': 'male', 'address': [{'postalCode': 'WD25 0DZ'}], 'identifier': [{'system': 'https://fhir.nhs.uk/Id/nhs-number', 'value': '9732928395'}], 'name': [{'family': 'PEEL', 'given': ['PHYLIS']}]}, {'resourceType': 'Practitioner', 'id': 'Practitioner1', 'name': [{'family': "O'Reilly", 'given': ['Ellena']}]}], 'vaccineCode': {'coding': [{'system': 'http://snomed.info/sct', 'code': '42223111000001107', 'display': 'Quadrivalent influenza vaccine (split virion, inactivated)'}]}, 'manufacturer': {'display': 'Sanofi Pasteur'}, 'expirationDate': '2024-09-15', 'lotNumber': 'BN92478105653', 'extension': [{'url': 'https://fhir.hl7.org.uk/StructureDefinition/Extension-UKCore-VaccinationProcedure', 'valueCodeableConcept': {'coding': [{'system': 'http://snomed.info/sct', 'code': '956951000000104', 'display': 'RSV vaccination in pregnancy (procedure)'}]}}], 'occurrenceDateTime': '2024-09-04T18:33:25+00:00', 'primarySource': True, 'site': {'coding': [{'system': 'http://snomed.info/sct', 'code': '368209003', 'display': 'Right arm'}]}, 'route': {'coding': [{'system': 'http://snomed.info/sct', 'code': '1210999013', 'display': 'Intradermal use'}]}, 'doseQuantity': {'value': Decimal('0.3'), 'unit': 'Inhalation - unit of product usage', 'system': 'http://snomed.info/sct', 'code': '2622896019'}, 'performer': [{'actor': {'type': 'Organization', 'identifier': {'system': 'https://fhir.nhs.uk/Id/ods-organization-code', 'value': 'RVVKC'}}}, {'actor': {'reference': '#Practitioner1'}}], 'location': {'identifier': {'value': 'RJC02', 'system': 'https://fhir.nhs.uk/Id/ods-organization-code'}}} |
| 50 | + |
| 51 | + # call 'process_row' with required details |
| 52 | + imms_fhir_resource = process_row("EMIS", Allowed_Operations, ROW_DETAILS) |
| 53 | + # validate if the response with expected result |
| 54 | + self.assertDictEqual(imms_fhir_resource["fhir_json"], expected_result) |
| 55 | + |
| 56 | + def test_process_row_invalid_action_flag(self): |
| 57 | + """ |
| 58 | + Test that process_row gives the expected output. These tests check that the row is valid and matches the expected output. |
| 59 | + """ |
| 60 | + Mock_Row = deepcopy(ROW_DETAILS) |
| 61 | + # setting up the invalid action flag other than 'NEW', 'UPDATE' or 'DELETE' |
| 62 | + Mock_Row['ACTION_FLAG'] = 'Invalid' |
| 63 | + |
| 64 | + # call 'process_row' with required details |
| 65 | + response = process_row("EMIS", Allowed_Operations, Mock_Row) |
| 66 | + |
| 67 | + # validate if we got INVALID_ACTION_FLAG in response |
| 68 | + self.assertEqual(response['diagnostics']['error_type'], 'INVALID_ACTION_FLAG') |
| 69 | + |
| 70 | + def test_process_row_missing_action_flag(self): |
| 71 | + """ |
| 72 | + Test that process_row gives the expected output. These tests check that the row is valid and matches the expected output. |
| 73 | + """ |
| 74 | + |
| 75 | + Mock_Row = deepcopy(ROW_DETAILS) |
| 76 | + # removing action flag from row |
| 77 | + Mock_Row.pop('ACTION_FLAG') |
| 78 | + |
| 79 | + # call 'process_row' with required details |
| 80 | + response = process_row("EMIS", Allowed_Operations, Mock_Row) |
| 81 | + # validate if we got INVALID_ACTION_FLAG in response |
| 82 | + self.assertEqual(response['diagnostics']['error_type'], 'INVALID_ACTION_FLAG') |
| 83 | + |
| 84 | + def test_process_row_missing_permission(self): |
| 85 | + """ |
| 86 | + Test that process_row gives the expected output. These tests check that the row is valid and matches the expected output. |
| 87 | + """ |
| 88 | + # only create and delete permission. Missing update |
| 89 | + allowed_operation = {'CREATE', 'DELETE'} |
| 90 | + # copy row data with Action_Flag = 'Update' |
| 91 | + Mock_Row = deepcopy(ROW_DETAILS) |
| 92 | + |
| 93 | + # call 'process_row' with required details |
| 94 | + response = process_row("EMIS", allowed_operation, Mock_Row) |
| 95 | + |
| 96 | + self.assertEqual(response['diagnostics']['error_type'], 'NO_PERMISSIONS') |
| 97 | + self.assertEqual(response['diagnostics']['statusCode'], 403) |
| 98 | + |
| 99 | + def test_process_row_missing_unique_id(self): |
| 100 | + """ |
| 101 | + Test that process_row gives the expected output. These tests check that the row is valid and matches the expected output. |
| 102 | + """ |
| 103 | + # copy row data and remove 'UNIQUE_ID' |
| 104 | + Mock_Row = deepcopy(ROW_DETAILS) |
| 105 | + Mock_Row.pop('UNIQUE_ID') |
| 106 | + # call 'process_row' with required details |
| 107 | + response = process_row("EMIS", Allowed_Operations, Mock_Row) |
| 108 | + |
| 109 | + self.assertEqual(response['diagnostics']['error_type'], 'MISSING_UNIQUE_ID') |
| 110 | + self.assertEqual(response['diagnostics']['statusCode'], 400) |
| 111 | + |
| 112 | + def test_process_row_missing_unique_id_uri(self): |
| 113 | + """ |
| 114 | + Test that process_row gives the expected output. These tests check that the row is valid and matches the expected output. |
| 115 | + """ |
| 116 | + # copy row data and remove 'UNIQUE_ID_URI' |
| 117 | + Mock_Row = deepcopy(ROW_DETAILS) |
| 118 | + Mock_Row.pop('UNIQUE_ID_URI') |
| 119 | + # call 'process_row' with required details |
| 120 | + response = process_row("EMIS", Allowed_Operations, Mock_Row) |
| 121 | + |
| 122 | + self.assertEqual(response['diagnostics']['error_message'], 'UNIQUE_ID or UNIQUE_ID_URI is missing') |
| 123 | + self.assertEqual(response['diagnostics']['statusCode'], 400) |
| 124 | + |
| 125 | +if __name__ == '__main__': |
| 126 | + unittest.main() |
| 127 | + |
0 commit comments