Skip to content

Commit 354dc03

Browse files
committed
Query
1 parent 8fa0d6c commit 354dc03

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

lambdas/id_sync/src/ieds_db_operations.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@ def ieds_check_exist(id: str) -> bool:
2121
logger.info(f"ieds_check_exist. Get ID: {id}")
2222
search_patient_pk = f"Patient#{id}"
2323

24-
response = get_ieds_table().get_item(Key={'PatientPk': search_patient_pk})
25-
found = 'Item' in response
24+
response = get_ieds_table().query(
25+
IndexName='PatientGSI',
26+
KeyConditionExpression=Key('PatientPK').eq(search_patient_pk),
27+
Limit=1
28+
)
29+
30+
items = response.get('Items', [])
31+
found = len(items) > 0
2632
logger.info(f"ieds_check_exist. Record found: {found} for ID: {id}")
2733
return found
2834

2935

36+
BATCH_SIZE = 25
37+
38+
3039
def ieds_update_patient_id(old_id: str, new_id: str) -> dict:
3140
"""Update the patient ID in the IEDS table."""
3241
logger.info(f"ieds_update_patient_id. Update patient ID from {old_id} to {new_id}")
@@ -73,26 +82,26 @@ def ieds_update_patient_id(old_id: str, new_id: str) -> dict:
7382
})
7483

7584
logger.info("Transacting items in IEDS table...")
76-
# ✅ Fix: Initialize success tracking
85+
# success tracking
7786
all_batches_successful = True
7887
total_batches = 0
7988

80-
# Batch transact in chunks of 25
81-
for i in range(0, len(transact_items), 25):
82-
batch = transact_items[i:i+25]
89+
# Batch transact in chunks of BATCH_SIZE
90+
for i in range(0, len(transact_items), BATCH_SIZE):
91+
batch = transact_items[i:i+BATCH_SIZE]
8392
total_batches += 1
8493
logger.info(f"Transacting batch {total_batches} of size: {len(batch)}")
8594

8695
response = ieds_table.transact_write_items(TransactItems=batch)
8796
logger.info("Batch update complete. Response: %s", response)
8897

89-
# ✅ Fix: Check each batch response
98+
# Check each batch response
9099
if response['ResponseMetadata']['HTTPStatusCode'] != 200:
91100
all_batches_successful = False
92101
logger.error(
93102
f"Batch {total_batches} failed with status: {response['ResponseMetadata']['HTTPStatusCode']}")
94103

95-
# ✅ Fix: Consolidated response handling outside the loop
104+
# Consolidated response handling
96105
logger.info(
97106
f"All batches complete. Total batches: {total_batches}, All successful: {all_batches_successful}")
98107

@@ -143,7 +152,7 @@ def get_items_to_update(old_patient_pk: str) -> list:
143152
logger.info(f"Getting items to update for old patient PK: {old_patient_pk}")
144153
response = get_ieds_table().query(
145154
KeyConditionExpression=Key('PatientPK').eq(old_patient_pk),
146-
Limit=25 # Adjust limit as needed
155+
Limit=BATCH_SIZE
147156
)
148157

149158
if 'Items' not in response or not response['Items']:

lambdas/id_sync/tests/test_ieds_db_operations.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,18 @@ def test_ieds_check_exist_record_exists(self):
292292
# Arrange
293293
patient_id = "test-patient-123"
294294
mock_response = {
295-
'Item': {'PK': 'Patient#test-patient-123', 'SK': 'RECORD#1'},
295+
'Items': [{'PK': 'Patient#test-patient-123', 'SK': 'RECORD#1'}],
296+
'Count': 1
296297
}
297-
self.mock_table.get_item.return_value = mock_response
298+
self.mock_table.query.return_value = mock_response
298299

299300
# Act
300301
result = ieds_db_operations.ieds_check_exist(patient_id)
301302

302303
# Assert
303304
self.assertTrue(result)
304305

305-
self.mock_table.get_item.assert_called_once()
306+
self.mock_table.query.assert_called_once()
306307

307308
def test_ieds_check_exist_record_not_exists(self):
308309
"""Test when no record exists in IEDS table"""
@@ -320,14 +321,14 @@ def test_ieds_check_exist_record_not_exists(self):
320321
# Assert
321322
self.assertFalse(result)
322323

323-
self.mock_table.get_item.assert_called_once()
324+
self.mock_table.query.assert_called_once()
324325

325326
def test_ieds_check_exist_empty_id(self):
326327
"""Test with empty patient ID"""
327328
# Arrange
328329
patient_id = ""
329330
mock_response = {'some_key': 'some_value'}
330-
self.mock_table.get_item.return_value = mock_response
331+
self.mock_table.query.return_value = mock_response
331332

332333
# Act
333334
result = ieds_db_operations.ieds_check_exist(patient_id)
@@ -336,7 +337,7 @@ def test_ieds_check_exist_empty_id(self):
336337
self.assertFalse(result)
337338

338339
# Verify query with empty ID
339-
self.mock_table.get_item.assert_called_once()
340+
self.mock_table.query.assert_called_once()
340341

341342
def test_ieds_check_exist_none_id(self):
342343
"""Test with None patient ID"""
@@ -352,13 +353,13 @@ def test_ieds_check_exist_none_id(self):
352353
self.assertFalse(result)
353354

354355
# Verify query with None ID
355-
self.mock_table.get_item.assert_called_once()
356+
self.mock_table.query.assert_called_once()
356357

357358
def test_ieds_check_exist_query_exception(self):
358359
"""Test exception handling during query"""
359360
# Arrange
360361
patient_id = "test-patient-error"
361-
self.mock_table.get_item.side_effect = Exception("DynamoDB query failed")
362+
self.mock_table.query.side_effect = Exception("DynamoDB query failed")
362363

363364
# Act & Assert
364365
with self.assertRaises(Exception) as context:
@@ -367,7 +368,7 @@ def test_ieds_check_exist_query_exception(self):
367368
self.assertEqual(str(context.exception), "DynamoDB query failed")
368369

369370
# Verify query was attempted
370-
self.mock_table.get_item.assert_called_once()
371+
self.mock_table.query.assert_called_once()
371372

372373
def test_ieds_check_exist_missing_count_field(self):
373374
"""Test when response doesn't have Count field"""
@@ -387,13 +388,15 @@ def test_ieds_check_exist_count_greater_than_one(self):
387388
# Arrange
388389
patient_id = "test-patient-multiple"
389390
mock_response = {
390-
'Item': {
391-
'PK': 'Patient#test-patient-multiple',
392-
'SK': 'RECORD#1'
393-
},
391+
'Items': [
392+
{
393+
'PK': 'Patient#test-patient-multiple',
394+
'SK': 'RECORD#1'
395+
}
396+
],
394397
'Count': 2 # Even though Limit=1, Count could theoretically be higher
395398
}
396-
self.mock_table.get_item.return_value = mock_response
399+
self.mock_table.query.return_value = mock_response
397400

398401
# Act
399402
result = ieds_db_operations.ieds_check_exist(patient_id)
@@ -444,10 +447,10 @@ def test_ieds_update_patient_id_success(self):
444447
# Act
445448
result = ieds_db_operations.ieds_update_patient_id(old_id, new_id)
446449

447-
# Assert
450+
# Assert - ✅ Fix: Update expected message to match actual implementation
448451
expected_result = {
449452
"status": "success",
450-
"message": f"Updated IEDS, patient ID: {old_id} to {new_id}. {len(mock_items)} items updated in 1 batches."
453+
"message": f"IEDS update, patient ID: {old_id}=>{new_id}. {len(mock_items)} updated 1."
451454
}
452455
self.assertEqual(result, expected_result)
453456

@@ -617,11 +620,8 @@ def test_ieds_update_patient_id_special_characters(self):
617620
result = ieds_db_operations.ieds_update_patient_id(old_id, new_id)
618621

619622
# Assert
620-
expected_result = {
621-
"status": "success",
622-
"message": f"Updated IEDS, patient ID: {old_id} to {new_id}. {len(mock_items)} items updated in 1 batches."
623-
}
624-
self.assertEqual(result, expected_result)
623+
self.assertEqual(result["status"], "success")
624+
self.assertEqual(result["message"], f"IEDS update, patient ID: {old_id}=>{new_id}. {len(mock_items)} updated 1.")
625625

626626
# Verify transact_write_items was called with special characters
627627
self.mock_table.transact_write_items.assert_called_once()

0 commit comments

Comments
 (0)