@@ -7,18 +7,27 @@ class TestRecordProcessor(unittest.TestCase):
77
88 def setUp (self ):
99 """Set up test fixtures and mocks"""
10+ # Patch logger
1011 self .logger_patcher = patch ('record_processor.logger' )
1112 self .mock_logger = self .logger_patcher .start ()
1213
14+ # PDS helpers
1315 self .pds_get_patient_id_patcher = patch ('record_processor.pds_get_patient_id' )
1416 self .mock_pds_get_patient_id = self .pds_get_patient_id_patcher .start ()
1517
18+ self .pds_get_patient_details_patcher = patch ('record_processor.pds_get_patient_details' )
19+ self .mock_pds_get_patient_details = self .pds_get_patient_details_patcher .start ()
20+
21+ # IEDS helpers
1622 self .ieds_check_exist_patcher = patch ('record_processor.ieds_check_exist' )
1723 self .mock_ieds_check_exist = self .ieds_check_exist_patcher .start ()
1824
1925 self .ieds_update_patient_id_patcher = patch ('record_processor.ieds_update_patient_id' )
2026 self .mock_ieds_update_patient_id = self .ieds_update_patient_id_patcher .start ()
2127
28+ self .get_items_from_patient_id_patcher = patch ('record_processor.get_items_from_patient_id' )
29+ self .mock_get_items_from_patient_id = self .get_items_from_patient_id_patcher .start ()
30+
2231 def tearDown (self ):
2332 patch .stopall ()
2433
@@ -27,7 +36,6 @@ def test_process_record_success_no_update_required(self):
2736 # Arrange
2837 test_id = "54321"
2938 with patch ('record_processor.ieds_check_exist' , return_value = True ):
30-
3139 test_record = {"body" : {"subject" : test_id }}
3240 self .mock_pds_get_patient_id .return_value = test_id
3341
@@ -43,34 +51,82 @@ def test_process_record_success_no_update_required(self):
4351 self .mock_pds_get_patient_id .assert_called_once_with (test_id )
4452
4553 def test_process_record_success_update_required (self ):
46- """Test successful processing when patient ID differs"""
54+ """Test successful processing when patient ID differs and demographics match """
4755 # Arrange
4856 pds_id = "9000000008"
4957 nhs_number = "9000000009"
5058
5159 test_sqs_record = {"body" : {"subject" : nhs_number }}
5260 self .mock_pds_get_patient_id .return_value = pds_id
61+
62+ # pds_get_patient_details should return details used by demographics_match
63+ self .mock_pds_get_patient_details .return_value = {
64+ "name" : [{"given" : ["John" ], "family" : "Doe" }],
65+ "gender" : "male" ,
66+ "birthDate" : "1980-01-01" ,
67+ }
68+
69+ # Provide one IEDS item that will match demographics via demographics_match
70+ matching_item = {
71+ "Resource" : {
72+ "resourceType" : "Patient" ,
73+ "name" : [{"given" : ["John" ], "family" : "Doe" }],
74+ "gender" : "male" ,
75+ "birthDate" : "1980-01-01" ,
76+ }
77+ }
78+ self .mock_get_items_from_patient_id .return_value = [matching_item ]
79+
5380 success_response = {"status" : "success" }
5481 self .mock_ieds_update_patient_id .return_value = success_response
82+
5583 # Act
5684 result = process_record (test_sqs_record )
57- self .maxDiff = None
58- # Assert
59- expected_result = success_response
60- self .assertEqual (result , expected_result )
6185
62- # Verify calls
86+ # Assert
87+ self .assertEqual (result , success_response )
6388 self .mock_pds_get_patient_id .assert_called_once_with (nhs_number )
6489
90+ def test_process_record_demographics_mismatch_skips_update (self ):
91+ """If no IEDS item matches demographics, the update should be skipped"""
92+ # Arrange
93+ pds_id = "pds-1"
94+ nhs_number = "nhs-1"
95+ test_sqs_record = {"body" : {"subject" : nhs_number }}
96+
97+ self .mock_pds_get_patient_id .return_value = pds_id
98+ self .mock_pds_get_patient_details .return_value = {
99+ "name" : [{"given" : ["Alice" ], "family" : "Smith" }],
100+ "gender" : "female" ,
101+ "birthDate" : "1995-05-05" ,
102+ }
103+
104+ # IEDS items exist but do not match demographics
105+ non_matching_item = {
106+ "Resource" : {
107+ "resourceType" : "Patient" ,
108+ "name" : [{"given" : ["Bob" ], "family" : "Jones" }],
109+ "gender" : "male" ,
110+ "birthDate" : "1990-01-01" ,
111+ }
112+ }
113+ self .mock_get_items_from_patient_id .return_value = [non_matching_item ]
114+
115+ # Act
116+ result = process_record (test_sqs_record )
117+
118+ # Assert: update skipped
119+ self .assertEqual (result ["status" ], "success" )
120+ self .assertIn ("update skipped" , result ["message" ])
121+
65122 def test_process_record_no_records_exist (self ):
66123 """Test when no records exist for the patient ID"""
67-
68124 # Arrange
69125 test_id = "12345"
70126 with patch ('record_processor.ieds_check_exist' , return_value = False ):
127+ test_record = {"body" : {"subject" : test_id }}
71128
72129 # Act
73- test_record = {"body" : {"subject" : test_id }}
74130 result = process_record (test_record )
75131
76132 # Assert
@@ -86,6 +142,7 @@ def test_process_record_pds_returns_none_id(self):
86142 test_id = "12345a"
87143 self .mock_pds_get_patient_id .return_value = None
88144 test_record = {"body" : {"subject" : test_id }}
145+
89146 # Act & Assert
90147 result = process_record (test_record )
91148 self .assertEqual (result ["status" ], "success" )
@@ -112,6 +169,18 @@ def test_body_is_string(self):
112169 new_test_id = "nhs-number-2"
113170
114171 self .mock_pds_get_patient_id .return_value = new_test_id
172+ # Mock demographics so update proceeds
173+ self .mock_pds_get_patient_details .return_value = {
174+ "name" : [{"given" : ["A" ], "family" : "B" }],
175+ "gender" : "female" , "birthDate" : "1990-01-01"
176+ }
177+ self .mock_get_items_from_patient_id .return_value = [{
178+ "Resource" : {
179+ "resourceType" : "Patient" ,
180+ "name" : [{"given" : ["A" ], "family" : "B" }],
181+ "gender" : "female" , "birthDate" : "1990-01-01"
182+ }
183+ }]
115184 self .mock_ieds_update_patient_id .return_value = {"status" : "success" }
116185 # Act
117186 result = process_record (test_record )
0 commit comments