11import json
22import unittest
3- from unittest .mock import patch
3+ from unittest .mock import call , patch
44
55from exceptions .id_sync_exception import IdSyncException
66from record_processor import process_record
@@ -12,6 +12,7 @@ class TestRecordProcessor(unittest.TestCase):
1212 "gender" : "male" ,
1313 "birthDate" : "1980-01-01" ,
1414 }
15+ MOCK_IMMUNISATION_IDENTIFIER_PK = {"IdentifierPK" : "someSystem#12345" }
1516
1617 def setUp (self ):
1718 """Set up test fixtures and mocks"""
@@ -64,7 +65,10 @@ def test_process_record_success_update_required(self):
6465 pds_id = "9000000008"
6566 nhs_number = "9000000009"
6667
67- test_sqs_record = {"body" : json .dumps ({"subject" : nhs_number })}
68+ test_sqs_record = {
69+ "body" : json .dumps ({"subject" : nhs_number , "id" : "test-mns-event-id" }),
70+ "messageId" : "test-sqs-message-id" ,
71+ }
6872
6973 # pds_get_patient_details should return details used by demographics_match
7074 self .mock_pds_get_patient_details .return_value = {
@@ -79,6 +83,7 @@ def test_process_record_success_update_required(self):
7983
8084 # Provide one IEDS item that will match demographics via demographics_match
8185 matching_item = {
86+ ** self .MOCK_IMMUNISATION_IDENTIFIER_PK ,
8287 "Resource" : {
8388 "resourceType" : "Immunization" ,
8489 "contained" : [
@@ -90,7 +95,7 @@ def test_process_record_success_update_required(self):
9095 "birthDate" : "1980-01-01" ,
9196 }
9297 ],
93- }
98+ },
9499 }
95100 self .mock_get_items_from_patient_id .return_value = [matching_item ]
96101
@@ -103,13 +108,25 @@ def test_process_record_success_update_required(self):
103108 # Assert
104109 self .assertEqual (result , success_response )
105110 self .mock_pds_get_patient_details .assert_called_once_with (nhs_number )
111+ self .mock_logger .info .assert_has_calls (
112+ [
113+ call ("Processing record with SQS messageId: %s" , "test-sqs-message-id" ),
114+ call ("Processing MNS event with id: %s" , "test-mns-event-id" ),
115+ call ("NHS Number has changed. Performing updates on relevant IEDS records" ),
116+ call ("Fetched IEDS resources. IEDS count: %d" , 1 ),
117+ call ("Update required for imms identifier: %s. Demographic data matched" , "someSystem#12345" ),
118+ ]
119+ )
106120
107121 def test_process_record_demographics_mismatch_skips_update (self ):
108122 """If no IEDS item matches demographics, the update should be skipped"""
109123 # Arrange
110124 pds_id = "pds-1"
111125 nhs_number = "nhs-1"
112- test_sqs_record = {"body" : json .dumps ({"subject" : nhs_number })}
126+ test_sqs_record = {
127+ "body" : json .dumps ({"subject" : nhs_number , "id" : "test-mns-event-id" }),
128+ "messageId" : "test-sqs-message-id" ,
129+ }
113130
114131 self .mock_pds_get_patient_details .return_value = {
115132 "name" : [{"given" : ["Alice" ], "family" : "Smith" }],
@@ -125,6 +142,7 @@ def test_process_record_demographics_mismatch_skips_update(self):
125142
126143 # IEDS items exist but do not match demographics
127144 non_matching_item = {
145+ ** self .MOCK_IMMUNISATION_IDENTIFIER_PK ,
128146 "Resource" : {
129147 "resourceType" : "Immunization" ,
130148 "contained" : [
@@ -136,7 +154,7 @@ def test_process_record_demographics_mismatch_skips_update(self):
136154 "birthDate" : "1990-01-01" ,
137155 }
138156 ],
139- }
157+ },
140158 }
141159 self .mock_get_items_from_patient_id .return_value = [non_matching_item ]
142160
@@ -146,6 +164,16 @@ def test_process_record_demographics_mismatch_skips_update(self):
146164 # Assert
147165 self .assertEqual (result ["status" ], "success" )
148166 self .assertEqual (result ["message" ], "No records matched PDS demographics; update skipped" )
167+ self .mock_logger .info .assert_has_calls (
168+ [
169+ call ("Processing record with SQS messageId: %s" , "test-sqs-message-id" ),
170+ call ("Processing MNS event with id: %s" , "test-mns-event-id" ),
171+ call ("NHS Number has changed. Performing updates on relevant IEDS records" ),
172+ call ("Fetched IEDS resources. IEDS count: %d" , 1 ),
173+ call ("No update required for imms identifier: %s. Demographic data did not match" , "someSystem#12345" ),
174+ call ("No records matched PDS demographics: %d" , 1 ),
175+ ]
176+ )
149177
150178 def test_invalid_body_parsing_returns_error (self ):
151179 """When body is a malformed string, process_record should return an error"""
0 commit comments