Skip to content

Commit 053db43

Browse files
committed
debug
1 parent ccd959f commit 053db43

File tree

3 files changed

+85
-71
lines changed

3 files changed

+85
-71
lines changed

backend/src/fhir_repository.py

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -392,70 +392,70 @@ def delete_immunization(
392392
response=error.response,
393393
)
394394

395-
def find_immunizations(self, patient_identifier: str, vaccine_types: list):
396-
"""it should find all of the specified patient's Immunization events for all of the specified vaccine_types"""
397-
398-
# ✅ Add debug logging
399-
import logging
400-
logger = logging.getLogger(__name__)
401-
402-
logger.info("SAW fi...1: find_immunizations called with patient_identifier: '%s', vaccine_types: %s",
403-
patient_identifier, vaccine_types)
404-
405-
# Create the patient PK and log it
406-
patient_pk = _make_patient_pk(patient_identifier)
407-
logger.info("SAW fi...2: patient_pk created: '%s'", patient_pk)
408-
409-
condition = Key("PatientPK").eq(patient_pk)
410-
is_not_deleted = Attr("DeletedAt").not_exists() | Attr("DeletedAt").eq("reinstated")
411-
412-
logger.info("SAW fi...3: executing DynamoDB query on PatientGSI index")
413-
414-
response = self.table.query(
415-
IndexName="PatientGSI",
416-
KeyConditionExpression=condition,
417-
FilterExpression=is_not_deleted,
418-
)
419-
420-
# ✅ Log the raw DynamoDB response
421-
logger.info("SAW fi...4: DynamoDB query response - Count: %s, ScannedCount: %s",
422-
response.get("Count", 0), response.get("ScannedCount", 0))
423-
424-
if "Items" in response:
425-
raw_items = response["Items"]
426-
logger.info("SAW fi...5: total items returned from DynamoDB: %d", len(raw_items))
395+
def find_immunizations(self, patient_identifier: str, vaccine_types: list):
396+
"""it should find all of the specified patient's Immunization events for all of the specified vaccine_types"""
397+
398+
# ✅ Add debug logging
399+
import logging
400+
logger = logging.getLogger(__name__)
401+
402+
logger.info("SAW fi...1: find_immunizations called with patient_identifier: '%s', vaccine_types: %s",
403+
patient_identifier, vaccine_types)
404+
405+
# Create the patient PK and log it
406+
patient_pk = _make_patient_pk(patient_identifier)
407+
logger.info("SAW fi...2: patient_pk created: '%s'", patient_pk)
408+
409+
condition = Key("PatientPK").eq(patient_pk)
410+
is_not_deleted = Attr("DeletedAt").not_exists() | Attr("DeletedAt").eq("reinstated")
411+
412+
logger.info("SAW fi...3: executing DynamoDB query on PatientGSI index")
413+
414+
response = self.table.query(
415+
IndexName="PatientGSI",
416+
KeyConditionExpression=condition,
417+
FilterExpression=is_not_deleted,
418+
)
427419

428-
# Log first few items for debugging
429-
if raw_items:
430-
logger.info("SAW fi...6: sample raw item keys: %s", list(raw_items[0].keys()))
431-
logger.info("SAW fi...7: first few PatientSK values: %s",
432-
[item.get("PatientSK", "MISSING") for item in raw_items[:3]])
420+
# ✅ Log the raw DynamoDB response
421+
logger.info("SAW fi...4: DynamoDB query response - Count: %s, ScannedCount: %s",
422+
response.get("Count", 0), response.get("ScannedCount", 0))
433423

434-
# Filter the response to contain only the requested vaccine types
435-
items = [x for x in raw_items if x["PatientSK"].split("#")[0] in vaccine_types]
424+
if "Items" in response:
425+
raw_items = response["Items"]
426+
logger.info("SAW fi...5: total items returned from DynamoDB: %d", len(raw_items))
427+
428+
# Log first few items for debugging
429+
if raw_items:
430+
logger.info("SAW fi...6: sample raw item keys: %s", list(raw_items[0].keys()))
431+
logger.info("SAW fi...7: first few PatientSK values: %s",
432+
[item.get("PatientSK", "MISSING") for item in raw_items[:3]])
433+
434+
# Filter the response to contain only the requested vaccine types
435+
items = [x for x in raw_items if x["PatientSK"].split("#")[0] in vaccine_types]
436+
437+
logger.info("SAW fi...8: after vaccine_types filtering (%s): %d items", vaccine_types, len(items))
438+
439+
if items:
440+
# Log the vaccine types found
441+
found_vaccine_types = [item["PatientSK"].split("#")[0] for item in items]
442+
logger.info("SAW fi...9: found vaccine types: %s", found_vaccine_types)
443+
else:
444+
# Debug why no items matched
445+
all_vaccine_types = [item["PatientSK"].split("#")[0] for item in raw_items]
446+
logger.warning("SAW fi...10: no items matched vaccine_types filter!")
447+
logger.warning("SAW fi...11: requested vaccine_types: %s", vaccine_types)
448+
logger.warning("SAW fi...12: available vaccine_types in data: %s", list(set(all_vaccine_types)))
436449

437-
logger.info("SAW fi...8: after vaccine_types filtering (%s): %d items", vaccine_types, len(items))
450+
# Return a list of the FHIR immunization resource JSON items
451+
final_resources = [json.loads(item["Resource"]) for item in items]
452+
logger.info("SAW fi...13: returning %d FHIR resources", len(final_resources))
438453

439-
if items:
440-
# Log the vaccine types found
441-
found_vaccine_types = [item["PatientSK"].split("#")[0] for item in items]
442-
logger.info("SAW fi...9: found vaccine types: %s", found_vaccine_types)
454+
return final_resources
443455
else:
444-
# Debug why no items matched
445-
all_vaccine_types = [item["PatientSK"].split("#")[0] for item in raw_items]
446-
logger.warning("SAW fi...10: no items matched vaccine_types filter!")
447-
logger.warning("SAW fi...11: requested vaccine_types: %s", vaccine_types)
448-
logger.warning("SAW fi...12: available vaccine_types in data: %s", list(set(all_vaccine_types)))
449-
450-
# Return a list of the FHIR immunization resource JSON items
451-
final_resources = [json.loads(item["Resource"]) for item in items]
452-
logger.info("SAW fi...13: returning %d FHIR resources", len(final_resources))
453-
454-
return final_resources
455-
else:
456-
logger.error("SAW fi...14: No 'Items' key in DynamoDB response!")
457-
logger.error("SAW fi...15: Response keys: %s", list(response.keys()))
458-
raise UnhandledResponseError(message=f"Unhandled error. Query failed", response=response)
456+
logger.error("SAW fi...14: No 'Items' key in DynamoDB response!")
457+
logger.error("SAW fi...15: Response keys: %s", list(response.keys()))
458+
raise UnhandledResponseError(message=f"Unhandled error. Query failed", response=response)
459459

460460
@staticmethod
461461
def _handle_dynamo_response(response):

backend/tests/test_fhir_batch_repository.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ def setUp(self):
3131
self.table.update_item = MagicMock(return_value = {"ResponseMetadata": {"HTTPStatusCode": 200}})
3232
self.redis_patcher = patch("models.utils.validation_utils.redis_client")
3333
self.mock_redis_client = self.redis_patcher.start()
34+
self.logger_info_patcher = patch("logging.Logger.info")
35+
self.mock_logger_info = self.logger_info_patcher.start()
3436

3537
def tearDown(self):
36-
self.redis_patcher.stop()
37-
return super().tearDown()
38+
patch.stopall()
3839

3940
class TestCreateImmunization(TestImmunizationBatchRepository):
4041

backend/tests/test_fhir_service.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,18 @@ def setUp(self):
3939
self.mock_logger_info = self.logger_info_patcher.start()
4040

4141
def tearDown(self):
42-
self.redis_patcher.stop()
43-
self.logger_info_patcher.stop()
4442
super().tearDown()
43+
patch.stopall()
4544

4645
class TestServiceUrl(unittest.TestCase):
46+
47+
def setUp(self):
48+
self.logger_info_patcher = patch("logging.Logger.info")
49+
self.mock_logger_info = self.logger_info_patcher.start()
50+
51+
def tearDown(self):
52+
patch.stopall()
53+
4754
def test_get_service_url(self):
4855
"""it should create service url"""
4956
env = "int"
@@ -74,6 +81,11 @@ def setUp(self):
7481
self.imms_repo = create_autospec(ImmunizationRepository)
7582
self.validator = create_autospec(ImmunizationValidator)
7683
self.fhir_service = FhirService(self.imms_repo, self.validator)
84+
self.logger_info_patcher = patch("logging.Logger.info")
85+
self.mock_logger_info = self.logger_info_patcher.start()
86+
87+
def tearDown(self):
88+
patch.stopall()
7789

7890
def test_get_immunization_by_id_by_all(self):
7991
"""it should find an Immunization by id"""
@@ -179,10 +191,6 @@ def setUp(self):
179191
self.imms_repo = create_autospec(ImmunizationRepository)
180192
self.validator = create_autospec(ImmunizationValidator)
181193
self.fhir_service = FhirService(self.imms_repo, self.validator)
182-
self.logger_info_patcher = patch("logging.Logger.info")
183-
self.mock_logger_info = self.logger_info_patcher.start()
184-
def tearDown(self):
185-
patch.stopall()
186194

187195
def test_get_immunization_by_id(self):
188196
"""it should find an Immunization by id"""
@@ -630,9 +638,9 @@ class TestSearchImmunizations(unittest.TestCase):
630638
"""Tests for FhirService.search_immunizations"""
631639

632640
def setUp(self):
633-
self.imms_repo = create_autospec(ImmunizationRepository)
641+
self.imms_repo = MagicMock()
634642
self.validator = create_autospec(ImmunizationValidator)
635-
self.fhir_service = FhirService(self.imms_repo, self.validator)
643+
self.fhir_service = FhirService(self.imms_repo, self.validator)
636644
self.nhs_search_param = "patient.identifier"
637645
self.vaccine_type_search_param = "-immunization.target"
638646
self.sample_patient_resource = load_json_data("bundle_patient_resource.json")
@@ -643,12 +651,17 @@ def test_vaccine_type_search(self):
643651
vaccine_type = "COVID19"
644652
params = f"{self.nhs_search_param}={nhs_number}&{self.vaccine_type_search_param}={vaccine_type}"
645653

654+
self.imms_repo.find_immunizations.return_value = []
655+
646656
# When
647-
_ = self.fhir_service.search_immunizations(nhs_number, [vaccine_type], params)
657+
result = self.fhir_service.search_immunizations(nhs_number, [vaccine_type], params)
648658

649659
# Then
650660
self.imms_repo.find_immunizations.assert_called_once_with(nhs_number, [vaccine_type])
651-
661+
662+
self.assertEqual(result.type, "searchset")
663+
self.assertEqual(len([e for e in result.entry if e.resource.resource_type == "Immunization"]), 0)
664+
652665
def test_make_fhir_bundle_from_search_result(self):
653666
"""It should return a FHIR Bundle resource"""
654667
imms_ids = ["imms-1", "imms-2"]

0 commit comments

Comments
 (0)