From a5bb368963524732798e6e128469e2c3eaa99997 Mon Sep 17 00:00:00 2001 From: Steve Laing Date: Thu, 24 Oct 2024 16:48:00 +0100 Subject: [PATCH] Add correlation ID to pilot data --- src/functions/process_pilot_data/helper.py | 22 ++++++++++++------- .../process_pilot_data/tests/test_helper.py | 6 +++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/functions/process_pilot_data/helper.py b/src/functions/process_pilot_data/helper.py index 094d168..eb9a5af 100644 --- a/src/functions/process_pilot_data/helper.py +++ b/src/functions/process_pilot_data/helper.py @@ -3,6 +3,7 @@ import os import requests import dateutil.parser +import uuid FIELDNAMES = ("nhs_number", "date_of_birth", "appointment_date", "appointment_time", "appointment_location", "appointment_type") HEADERS = { @@ -11,7 +12,7 @@ } -def process_data(raw_data): +def process_data(raw_data) -> str: data = valid_csv_data(raw_data) if not data: logging.error("No valid data found") @@ -27,12 +28,13 @@ def process_data(raw_data): return response.text -def valid_csv_data(raw_data): +def valid_csv_data(raw_data) -> list: data = [] try: reader = csv.DictReader(raw_data, FIELDNAMES) for row in reader: if valid_row(row): + row["correlation_id"] = str(uuid.uuid4()) data.append(row) except csv.Error: logging.error("Invalid CSV data") @@ -40,24 +42,28 @@ def valid_csv_data(raw_data): return data -def valid_row(row): +def valid_row(row) -> bool: return ( valid_nhs_number(row["nhs_number"]) and - valid_date_of_birth(row["date_of_birth"]) + valid_date_or_time(row["date_of_birth"]) and + valid_date_or_time(row["appointment_date"]) and + valid_date_or_time(row["appointment_time"]) and + row["appointment_location"] and + row["appointment_type"] ) -def valid_nhs_number(nhs_number): +def valid_nhs_number(nhs_number: str) -> bool: if not nhs_number: return False return len(nhs_number) == 10 and nhs_number.isdigit() -def valid_date_of_birth(date_of_birth): - if not date_of_birth: +def valid_date_or_time(val: str) -> bool: + if not val: return False try: - dateutil.parser.parse(date_of_birth) + dateutil.parser.parse(val) return True except ValueError: return False diff --git a/src/functions/process_pilot_data/tests/test_helper.py b/src/functions/process_pilot_data/tests/test_helper.py index 8030904..48eeee7 100644 --- a/src/functions/process_pilot_data/tests/test_helper.py +++ b/src/functions/process_pilot_data/tests/test_helper.py @@ -7,6 +7,10 @@ class TestHelper: @pytest.fixture def setup(self, monkeypatch): + monkeypatch.setattr( + "uuid.uuid4", + lambda: "00000000-0000-0000-0000-000000000000", + ) monkeypatch.setenv( "NOTIFY_FUNCTION_URL", "http://example.com/api/notify/message/send", @@ -29,6 +33,7 @@ def test_process_data(self, setup): "appointment_time": "10:00", "appointment_location": "London", "appointment_type": "Breast Screening", + "correlation_id": "00000000-0000-0000-0000-000000000000", }, { "nhs_number": "1111111111", @@ -37,6 +42,7 @@ def test_process_data(self, setup): "appointment_time": "11:00", "appointment_location": "Croydon", "appointment_type": "Breast Screening", + "correlation_id": "00000000-0000-0000-0000-000000000000", }, ], }