|
| 1 | +import datetime |
| 2 | +import json |
1 | 3 | import unittest |
| 4 | +import uuid |
| 5 | +from copy import deepcopy |
| 6 | +from dataclasses import asdict |
| 7 | +from unittest.mock import patch |
2 | 8 |
|
| 9 | +import boto3 |
| 10 | +from moto import mock_aws |
3 | 11 |
|
| 12 | +from common.data_quality.completeness import MissingFields |
| 13 | +from common.data_quality.reporter import DataQualityReport, DataQualityReporter |
| 14 | +from test_common.data_quality.sample_values import VALID_BATCH_IMMUNISATION, VALID_FHIR_IMMUNISATION |
| 15 | + |
| 16 | + |
| 17 | +@mock_aws |
4 | 18 | class TestDataQualityReporter(unittest.TestCase): |
5 | | - def test_something(self): |
6 | | - self.assertEqual(True, True) # add assertion here |
| 19 | + def setUp(self): |
| 20 | + # Fix date.today() for all validation tests |
| 21 | + date_today_patcher = patch("common.data_quality.models.immunization_batch_row_model.datetime", wraps=datetime) |
| 22 | + self.mock_date_today = date_today_patcher.start() |
| 23 | + self.mock_date_today.date.today.return_value = datetime.date(2024, 5, 20) |
| 24 | + |
| 25 | + # Fix datetime.now |
| 26 | + self.mock_fixed_datetime = datetime.datetime(2024, 5, 20, 14, 12, 30, 123, tzinfo=datetime.timezone.utc) |
| 27 | + datetime_now_patcher = patch("common.data_quality.checker.datetime", wraps=datetime.datetime) |
| 28 | + self.mock_datetime_now = datetime_now_patcher.start() |
| 29 | + self.mock_datetime_now.now.return_value = self.mock_fixed_datetime |
| 30 | + |
| 31 | + # Fix generated UUID |
| 32 | + self.example_uuid = uuid.UUID("fa711f35-c08b-48c8-b498-3b151e686ddf") |
| 33 | + uuid_patcher = patch("uuid.uuid4", return_value=self.example_uuid) |
| 34 | + self.mock_uuid = uuid_patcher.start() |
| 35 | + |
| 36 | + # Set up mock S3 bucket |
| 37 | + self.bucket = "test_bucket" |
| 38 | + self.s3_client = boto3.client("s3") |
| 39 | + self.s3_client.create_bucket(Bucket=self.bucket) |
| 40 | + |
| 41 | + # Instantiate reporters |
| 42 | + self.batch_dq_reporter = DataQualityReporter(is_batch_csv=True, bucket=self.bucket) |
| 43 | + self.fhir_json_dq_reporter = DataQualityReporter(is_batch_csv=False, bucket=self.bucket) |
| 44 | + |
| 45 | + # Expected reports |
| 46 | + self.expected_dq_report_no_issues = DataQualityReport( |
| 47 | + data_quality_report_id=str(self.example_uuid), |
| 48 | + validationDate="2024-05-20T14:12:30.000Z", |
| 49 | + completeness=MissingFields(required_fields=[], mandatory_fields=[], optional_fields=[]), |
| 50 | + validity=[], |
| 51 | + timeliness_recorded_days=4, |
| 52 | + timeliness_ingested_seconds=785550, |
| 53 | + ) |
| 54 | + self.expected_dq_report_with_issues = DataQualityReport( |
| 55 | + data_quality_report_id=str(self.example_uuid), |
| 56 | + validationDate="2024-05-20T14:12:30.000Z", |
| 57 | + completeness=MissingFields( |
| 58 | + required_fields=["NHS_NUMBER", "INDICATION_CODE"], |
| 59 | + mandatory_fields=["PERSON_FORENAME", "PERSON_SURNAME"], |
| 60 | + optional_fields=["PERFORMING_PROFESSIONAL_FORENAME", "PERFORMING_PROFESSIONAL_SURNAME"], |
| 61 | + ), |
| 62 | + validity=["NHS_NUMBER", "DOSE_AMOUNT", "INDICATION_CODE"], |
| 63 | + timeliness_recorded_days=4, |
| 64 | + timeliness_ingested_seconds=785550, |
| 65 | + ) |
| 66 | + |
| 67 | + def generate_and_send_report_test_logic( |
| 68 | + self, expected_dq_report: DataQualityReport, immunisation: dict, is_batch_csv: bool |
| 69 | + ): |
| 70 | + # run generate report |
| 71 | + if is_batch_csv: |
| 72 | + self.batch_dq_reporter.generate_and_send_report(immunisation) |
| 73 | + else: |
| 74 | + self.fhir_json_dq_reporter.generate_and_send_report(immunisation) |
| 75 | + |
| 76 | + expected_json = json.dumps(asdict(expected_dq_report)) |
| 77 | + |
| 78 | + actual_json_object = self.s3_client.get_object(Bucket=self.bucket, Key=f"{str(self.example_uuid)}.json") |
| 79 | + actual_json = actual_json_object.get("Body").read().decode("utf-8") |
| 80 | + |
| 81 | + self.assertEqual(expected_json, actual_json) |
| 82 | + |
| 83 | + def test_generate_and_send_report_no_issues_batch(self): |
| 84 | + self.generate_and_send_report_test_logic( |
| 85 | + expected_dq_report=self.expected_dq_report_no_issues, |
| 86 | + immunisation=VALID_BATCH_IMMUNISATION, |
| 87 | + is_batch_csv=True, |
| 88 | + ) |
| 89 | + |
| 90 | + def test_generate_and_send_report_no_issues_api(self): |
| 91 | + self.generate_and_send_report_test_logic( |
| 92 | + expected_dq_report=self.expected_dq_report_no_issues, |
| 93 | + immunisation=VALID_FHIR_IMMUNISATION, |
| 94 | + is_batch_csv=False, |
| 95 | + ) |
| 96 | + |
| 97 | + def test_generate_and_send_report_with_issues_batch(self): |
| 98 | + batch_immunisation_with_issues = deepcopy(VALID_BATCH_IMMUNISATION) |
| 99 | + |
| 100 | + # Missing fields |
| 101 | + batch_immunisation_with_issues.pop("NHS_NUMBER") # required |
| 102 | + batch_immunisation_with_issues.pop("INDICATION_CODE") # required |
| 103 | + batch_immunisation_with_issues.pop("PERSON_FORENAME") # mandatory |
| 104 | + batch_immunisation_with_issues.pop("PERSON_SURNAME") # mandatory |
| 105 | + batch_immunisation_with_issues.pop("PERFORMING_PROFESSIONAL_FORENAME") # optional |
| 106 | + batch_immunisation_with_issues.pop("PERFORMING_PROFESSIONAL_SURNAME") # optional |
| 107 | + |
| 108 | + # Invalid fields |
| 109 | + batch_immunisation_with_issues["DOSE_AMOUNT"] = "6.789" |
| 110 | + |
| 111 | + self.generate_and_send_report_test_logic( |
| 112 | + expected_dq_report=self.expected_dq_report_with_issues, |
| 113 | + immunisation=batch_immunisation_with_issues, |
| 114 | + is_batch_csv=True, |
| 115 | + ) |
| 116 | + |
| 117 | + def test_generate_and_send_report_with_issues_api(self): |
| 118 | + fhir_immunisation_with_issues = deepcopy(VALID_FHIR_IMMUNISATION) |
| 119 | + |
| 120 | + # Missing fields |
| 121 | + fhir_immunisation_with_issues["contained"][1]["identifier"][0]["value"] = "" # required |
| 122 | + fhir_immunisation_with_issues["reasonCode"][0]["coding"][0]["code"] = "" # required |
| 123 | + fhir_immunisation_with_issues["contained"][1]["name"][0]["given"][0] = "" # mandatory |
| 124 | + fhir_immunisation_with_issues["contained"][1]["name"][0]["family"] = "" # mandatory |
| 125 | + fhir_immunisation_with_issues["contained"][0]["name"][0]["given"][0] = "" # optional |
| 126 | + fhir_immunisation_with_issues["contained"][0]["name"][0]["family"] = "" # optional |
| 127 | + |
| 128 | + # Invalid fields |
| 129 | + fhir_immunisation_with_issues["doseQuantity"]["value"] = "6.789" |
| 130 | + |
| 131 | + self.generate_and_send_report_test_logic( |
| 132 | + expected_dq_report=self.expected_dq_report_with_issues, |
| 133 | + immunisation=fhir_immunisation_with_issues, |
| 134 | + is_batch_csv=False, |
| 135 | + ) |
0 commit comments