diff --git a/backend/tests/utils/generic_utils.py b/backend/tests/utils/generic_utils.py index 51f380c77..d39d48176 100644 --- a/backend/tests/utils/generic_utils.py +++ b/backend/tests/utils/generic_utils.py @@ -6,6 +6,8 @@ from decimal import Decimal from typing import Literal, Any from jsonpath_ng.ext import parse +from datetime import datetime, date +from typing import Union, List def load_json_data(filename: str): @@ -91,3 +93,21 @@ def update_contained_resource_field( {field_to_update: update_value} ) return json_data + +def format_date_types(dates: List[Union[date, datetime]], mode: str = "auto") -> List[str]: + """ + Accepts a list of date or datetime objects and returns them as strings: + - datetime → ISO 8601 string with timezone if present + - date → 'YYYY-MM-DD' + """ + formatted = [] + + for future_date in dates: + if mode == "datetime": + formatted.append(future_date.isoformat()) # full datetime with timezone + elif mode == "date": + formatted.append(future_date.strftime('%Y-%m-%d')) # just date + else: + raise TypeError(f"Unsupported type {type(future_date)}; expected date or datetime.") + + return formatted diff --git a/backend/tests/utils/test_generic_utils.py b/backend/tests/utils/test_generic_utils.py index 16abcf427..f7014fc79 100644 --- a/backend/tests/utils/test_generic_utils.py +++ b/backend/tests/utils/test_generic_utils.py @@ -2,7 +2,10 @@ import unittest from src.models.utils.generic_utils import form_json -from tests.utils.generic_utils import load_json_data +from tests.utils.generic_utils import load_json_data, format_date_types + +import unittest +from datetime import datetime, date class TestFormJson(unittest.TestCase): @@ -70,3 +73,25 @@ def test_elements_whitespace_and_case_are_handled(self): ) self.assertEqual(res["id"], self.response["id"]) self.assertEqual(res["meta"]["versionId"], self.response["version"]) + + +class TestFormatFutureDates(unittest.TestCase): + def test_date_mode_formats_dates_and_datetimes(self): + inputs = [date(2100, 1, 2), datetime(2100, 1, 3, 12, 0, 0)] + expected = ["2100-01-02", "2100-01-03"] + self.assertEqual(format_date_types(inputs, mode="date"), expected) + + def test_datetime_mode_formats_dates_and_datetimes(self): + inputs = [date(2100, 1, 2), datetime(2100, 1, 3, 12, 0, 0)] + expected = ["2100-01-02", "2100-01-03T12:00:00"] + self.assertEqual(format_date_types(inputs, mode="datetime"), expected) + + def test_default_auto_mode_is_currently_unsupported(self): + # Current implementation raises TypeError when mode is not 'date' or 'datetime' + inputs = [date(2100, 1, 2)] + with self.assertRaises(TypeError): + format_date_types(inputs) # default mode is 'auto' + + +if __name__ == "__main__": + unittest.main() diff --git a/backend/tests/utils/values_for_tests.py b/backend/tests/utils/values_for_tests.py index 56d8b77ae..0403aa334 100644 --- a/backend/tests/utils/values_for_tests.py +++ b/backend/tests/utils/values_for_tests.py @@ -2,6 +2,9 @@ from dataclasses import dataclass from decimal import Decimal +from .generic_utils import format_date_types +from datetime import datetime, timedelta + # Lists of data types for 'invalid data type' testing integers = [-1, 0, 1] @@ -291,16 +294,20 @@ class InvalidValues: "2000-02-30", # Invalid combination of month and day ] - for_future_dates = [ - "2100-01-01", # Year in future - "2050-12-31", # Year in future - "2029-06-15", # Year in future + now = datetime.now() + sample_inputs = [ + now + timedelta(days=1), + now + timedelta(days=365), + now + timedelta(days=730) ] + for_future_dates = format_date_types(sample_inputs, mode = "date") + # Strings which are not in acceptable date time format for_date_time_string_formats_for_relaxed_timezone = [ "", # Empty string "invalid", # Invalid format + *format_date_types(sample_inputs, mode = "datetime"), "20000101", # Date digits only (i.e. without hypens) "20000101000000", # Date and time digits only "200001010000000000", # Date, time and timezone digits only @@ -392,4 +399,3 @@ class InvalidValues: ] invalid_dose_quantity = {"value": 2, "unit": "ml", "code": "258773002"} - \ No newline at end of file diff --git a/lambdas/id_sync/src/pds_details.py b/lambdas/id_sync/src/pds_details.py index d0d5f4074..e5be640c3 100644 --- a/lambdas/id_sync/src/pds_details.py +++ b/lambdas/id_sync/src/pds_details.py @@ -27,7 +27,6 @@ def pds_get_patient_details(nhs_number: str) -> dict: ) pds_service = PdsService(authenticator, pds_env) patient = pds_service.get_patient_details(nhs_number) - logger.info("get patient details. response: %s", patient) return patient except Exception as e: msg = f"Error getting PDS patient details for {nhs_number}" diff --git a/lambdas/shared/src/common/pds_service.py b/lambdas/shared/src/common/pds_service.py index c334bb963..4eedfae44 100644 --- a/lambdas/shared/src/common/pds_service.py +++ b/lambdas/shared/src/common/pds_service.py @@ -27,7 +27,6 @@ def get_patient_details(self, patient_id) -> dict | None: response = requests.get(f"{self.base_url}/{patient_id}", headers=request_headers, timeout=5) if response.status_code == 200: - logger.info(f"PDS. Response: {response.json()}") return response.json() elif response.status_code == 404: logger.info(f"PDS. Patient not found for ID: {patient_id}")