-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_delta.py
More file actions
1158 lines (952 loc) · 46.3 KB
/
test_delta.py
File metadata and controls
1158 lines (952 loc) · 46.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import decimal
import json
import os
import time
import unittest
from unittest.mock import MagicMock, patch
from botocore.exceptions import ClientError
TEST_DEAD_LETTER_QUEUE_URL = "https://sqs.eu-west-2.amazonaws.com/123456789012/test-queue"
os.environ["AWS_SQS_QUEUE_URL"] = TEST_DEAD_LETTER_QUEUE_URL
os.environ["DELTA_TABLE_NAME"] = "my_delta_table"
os.environ["DELTA_TTL_DAYS"] = "14"
os.environ["SOURCE"] = "my_source"
import delta # noqa: E402 — must come after env vars are set
from delta import ( # noqa: E402
_event_to_operation,
_extract_value,
_normalize_record,
get_creation_and_expiry_times,
get_imms_id,
get_vaccine_type,
handler,
process_record,
)
from mappings import ActionFlag, EventName, Operation # noqa: E402
from utils_for_converter_tests import RecordConfig, ValuesForTests, make_mock_logger # noqa: E402
SUCCESS_RESPONSE = {"ResponseMetadata": {"HTTPStatusCode": 200}}
DUPLICATE_RESPONSE = ClientError({"Error": {"Code": "ConditionalCheckFailedException"}}, "PutItem")
EXCEPTION_RESPONSE = ClientError({"Error": {"Code": "InternalServerError"}}, "PutItem")
FAIL_RESPONSE = {"ResponseMetadata": {"HTTPStatusCode": 500}}
_DEFAULT_SEQUENCE = "49590338322303844748686548458181664417"
def _make_stream_record(
*,
event_name: str = "INSERT",
operation: str | None = Operation.CREATE,
patient_sk: str | None = None,
sequence_number: str | None = _DEFAULT_SEQUENCE,
imms: str = '{"foo": 1.23}',
imms_id: str = "test-imms-id",
supplier: str | None = None,
) -> dict:
pk_value = f"Immunization#{imms_id}"
is_remove = event_name == EventName.DELETE_PHYSICAL # "REMOVE"
if is_remove:
# REMOVE events have no NewImage — PK lives in Keys only
dynamodb_envelope: dict = {
"Keys": {
"PK": {"S": pk_value},
"PatientSK": {"S": patient_sk or "covid#test-patient-sk"},
},
}
if supplier is not None:
dynamodb_envelope["Keys"]["SupplierSystem"] = {"S": supplier}
else:
new_image: dict = {
"PK": {"S": pk_value},
"Imms": {"S": imms},
"PatientSK": {"S": patient_sk or "covid#test-patient-sk"},
}
if operation is not None:
new_image["Operation"] = {"S": operation}
if supplier is not None:
new_image["SupplierSystem"] = {"S": supplier}
dynamodb_envelope = {"NewImage": new_image}
if sequence_number is not None:
dynamodb_envelope["SequenceNumber"] = sequence_number
return {
"eventID": f"evt-{int(time.time() * 1000)}",
"eventName": event_name,
"dynamodb": dynamodb_envelope,
}
def _deep_update(base: dict, overrides: dict) -> dict:
"""Recursively merge overrides into base (used in TestNormalizeRecord)."""
for key, val in overrides.items():
if isinstance(val, dict) and isinstance(base.get(key), dict):
_deep_update(base[key], val)
else:
base[key] = val
return base
class TestExtractValue(unittest.TestCase):
"""Direct unit tests for _extract_value — catches regressions in DDB marshalling."""
def test_single_key_dict_unwraps(self):
self.assertEqual(_extract_value({"S": "hello"}), "hello")
def test_single_key_dict_numeric(self):
self.assertEqual(_extract_value({"N": "42"}), "42")
def test_multi_key_dict_returned_as_is(self):
val = {"S": "a", "N": "1"}
self.assertIs(_extract_value(val), val)
def test_string_returned_as_is(self):
self.assertEqual(_extract_value("plain"), "plain")
def test_none_returned_as_is(self):
self.assertIsNone(_extract_value(None))
def test_integer_returned_as_is(self):
self.assertEqual(_extract_value(99), 99)
def test_empty_dict_returned_as_is(self):
self.assertEqual(_extract_value({}), {})
class TestGetImmsId(unittest.TestCase):
def test_standard_pk(self):
self.assertEqual(get_imms_id("Immunization#abc-123"), "abc-123")
def test_no_hash_returns_unknown(self):
self.assertEqual(get_imms_id("badvalue"), "unknown")
def test_none_returns_unknown(self):
self.assertEqual(get_imms_id(None), "unknown")
def test_empty_string_returns_unknown(self):
self.assertEqual(get_imms_id(""), "unknown")
def test_multiple_hashes_returns_second_segment(self):
# Only the first split is used
self.assertEqual(get_imms_id("Immunization#abc#extra"), "abc")
class TestGetVaccineType(unittest.TestCase):
def test_standard_patient_sk(self):
self.assertEqual(get_vaccine_type("covid#patient-1"), "covid")
def test_whitespace_is_stripped(self):
self.assertEqual(get_vaccine_type(" flu #patient-1"), "flu")
def test_lowercase_normalisation(self):
self.assertEqual(get_vaccine_type("COVID#patient-1"), "covid")
def test_none_returns_unknown(self):
self.assertEqual(get_vaccine_type(None), "unknown")
def test_empty_string_returns_unknown(self):
self.assertEqual(get_vaccine_type(""), "unknown")
class TestEventToOperation(unittest.TestCase):
"""
_event_to_operation is error-path only
"""
def test_insert_maps_to_create(self):
self.assertEqual(_event_to_operation("INSERT"), Operation.CREATE)
def test_remove_maps_to_delete_physical(self):
# EventName.DELETE_PHYSICAL == "REMOVE"
self.assertEqual(_event_to_operation(EventName.DELETE_PHYSICAL), Operation.DELETE_PHYSICAL)
self.assertEqual(_event_to_operation("REMOVE"), Operation.DELETE_PHYSICAL)
def test_modify_maps_to_update_as_best_effort(self):
# Acceptable ambiguity on error path only
self.assertEqual(_event_to_operation("MODIFY"), Operation.UPDATE)
def test_unknown_event_falls_back_to_update(self):
self.assertEqual(_event_to_operation("UNKNOWN_EVENT"), Operation.UPDATE)
class TestNormalizeRecord(unittest.TestCase):
def _make_raw(self, **overrides) -> dict:
base = _make_stream_record(
event_name="INSERT",
operation=Operation.CREATE,
patient_sk="covid#patient-1",
sequence_number=_DEFAULT_SEQUENCE,
imms='{"foo": 1}',
imms_id="imms-abc",
)
_deep_update(base, overrides)
return base
def test_sequence_number_from_envelope(self):
norm = _normalize_record(self._make_raw())
self.assertEqual(norm.sequence_number, _DEFAULT_SEQUENCE)
def test_primary_key_from_new_image_pk(self):
norm = _normalize_record(self._make_raw())
self.assertEqual(norm.primary_key, "Immunization#imms-abc")
def test_imms_id_derived_from_primary_key(self):
norm = _normalize_record(self._make_raw())
self.assertEqual(norm.imms_id, "imms-abc")
def test_patient_sort_key_from_new_image(self):
norm = _normalize_record(self._make_raw())
self.assertEqual(norm.patient_sort_key, "covid#patient-1")
def test_patient_sort_key_absent_gives_none_and_unknown_vaccine_type(self):
record = self._make_raw()
del record["dynamodb"]["NewImage"]["PatientSK"]
norm = _normalize_record(record)
self.assertIsNone(norm.patient_sort_key)
self.assertEqual(norm.vaccine_type, "unknown")
def test_operation_from_new_image(self):
norm = _normalize_record(self._make_raw())
self.assertEqual(norm.operation, Operation.CREATE)
def test_operation_absent_is_none(self):
norm = _normalize_record(_make_stream_record(operation=None))
self.assertIsNone(norm.operation)
def test_supplier_system_from_new_image(self):
norm = _normalize_record(_make_stream_record(supplier="RAVS"))
self.assertEqual(norm.supplier_system, "RAVS")
def test_supplier_system_absent_is_none(self):
norm = _normalize_record(self._make_raw())
self.assertIsNone(norm.supplier_system)
def test_approximate_creation_datetime_used_when_present(self):
record = self._make_raw()
record["dynamodb"]["ApproximateCreationDateTime"] = 1708264245.0
norm = _normalize_record(record)
self.assertEqual(norm.creation_timestamp, 1708264245.0)
@patch("delta.time.time", return_value=9999999.0)
def test_approximate_creation_datetime_absent_falls_back_to_now(self, _):
record = self._make_raw()
record["dynamodb"].pop("ApproximateCreationDateTime", None)
self.assertEqual(_normalize_record(record).creation_timestamp, 9999999.0)
def test_resource_field_preferred_over_imms(self):
record = self._make_raw()
record["dynamodb"]["NewImage"]["Resource"] = {"S": '{"resourceType": "Immunization"}'}
self.assertEqual(_normalize_record(record).imms_raw, '{"resourceType": "Immunization"}')
def test_imms_string_used_when_resource_absent(self):
self.assertEqual(_normalize_record(self._make_raw()).imms_raw, '{"foo": 1}')
def test_both_resource_and_imms_absent_gives_none(self):
record = self._make_raw()
del record["dynamodb"]["NewImage"]["Imms"]
self.assertIsNone(_normalize_record(record).imms_raw)
def test_resource_field_from_real_dynamodb_stream_shape(self):
record = {
"eventID": "1",
"eventName": "INSERT",
"eventSource": "aws:dynamodb",
"awsRegion": "eu-west-2",
"dynamodb": {
"ApproximateCreationDateTime": 1708264245.0,
"SequenceNumber": _DEFAULT_SEQUENCE,
"Keys": {"PK": {"S": "Immunization#imms-abc"}},
"NewImage": {
"PK": {"S": "Immunization#imms-abc"},
"PatientSK": {"S": "covid#patient-1"},
"Operation": {"S": Operation.CREATE},
"SupplierSystem": {"S": "RAVS"},
"Resource": {"S": '{"resourceType":"Immunization"}'},
"Imms": {"S": '{"foo":1}'},
},
"StreamViewType": "NEW_AND_OLD_IMAGES",
},
}
self.assertEqual(_normalize_record(record).imms_raw, '{"resourceType":"Immunization"}')
def test_imms_fallback_from_real_dynamodb_stream_shape(self):
record = {
"eventID": "2",
"eventName": "INSERT",
"eventSource": "aws:dynamodb",
"awsRegion": "eu-west-2",
"dynamodb": {
"ApproximateCreationDateTime": 1708264245.0,
"SequenceNumber": _DEFAULT_SEQUENCE,
"Keys": {"PK": {"S": "Immunization#imms-abc"}},
"NewImage": {
"PK": {"S": "Immunization#imms-abc"},
"PatientSK": {"S": "covid#patient-1"},
"Operation": {"S": Operation.CREATE},
"SupplierSystem": {"S": "RAVS"},
"Imms": {"S": '{"foo":1}'},
},
"StreamViewType": "NEW_AND_OLD_IMAGES",
},
}
self.assertEqual(_normalize_record(record).imms_raw, '{"foo":1}')
class DeltaHandlerTestCase(unittest.TestCase):
# TODO refactor for dependency injection, eg process_record, send_firehose etc
def setUp(self):
self.logger_patcher = patch("delta.logger", make_mock_logger())
self.mock_logger = self.logger_patcher.start()
self.mock_delta_table = MagicMock()
self.mock_sqs_client = MagicMock()
self.get_delta_table_patcher = patch("delta.get_delta_table", return_value=self.mock_delta_table)
self.get_delta_table_patcher.start()
self.send_log_to_firehose_patcher = patch("delta.send_log_to_firehose")
self.mock_send_log_to_firehose = self.send_log_to_firehose_patcher.start()
self.sqs_client_patcher = patch("delta.get_sqs_client", return_value=self.mock_sqs_client)
self.sqs_client_patcher.start()
def tearDown(self):
patch.stopall()
def _call_process_record(self, record):
return process_record(
record,
self.mock_delta_table,
)
def test_handler_success_insert(self):
# Arrange
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
suppliers = ["RAVS", "EMIS"]
for supplier in suppliers:
imms_id = f"test-insert-imms-{supplier}-id"
event = ValuesForTests.get_event(
event_name=EventName.CREATE,
operation=Operation.CREATE,
imms_id=imms_id,
supplier=supplier,
)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.mock_delta_table.put_item.assert_called()
self.mock_send_log_to_firehose.assert_called() # check logged
put_item_call_args = self.mock_delta_table.put_item.call_args # check data written to DynamoDB
put_item_data = put_item_call_args.kwargs["Item"]
self.assertIn("Imms", put_item_data)
self.assertEqual(put_item_data["Imms"]["ACTION_FLAG"], ActionFlag.CREATE)
self.assertEqual(put_item_data["Operation"], Operation.CREATE)
self.assertEqual(put_item_data["SupplierSystem"], supplier)
self.mock_sqs_client.send_message.assert_not_called()
@patch("delta.process_record")
def test_partial_success_contract_and_dlq_routing(self, mock_process_record):
event = {"Records": [{"id": "r1"}, {"id": "r2"}, {"id": "r3"}]}
partial_msg = "Partial success: successfully synced into delta, but issues found within record"
mock_process_record.side_effect = [
(
True,
{
"record": "id1",
"operation_type": Operation.CREATE,
"statusCode": "200",
"statusDesc": "Successfully synched into delta",
},
),
(
True,
{"record": "id2", "operation_type": Operation.UPDATE, "statusCode": "207", "statusDesc": partial_msg},
),
(
False,
{"record": "id3", "operation_type": Operation.UPDATE, "statusCode": "500", "statusDesc": "Exception"},
),
]
response = handler(event, None)
self.assertTrue(response)
self.assertEqual(self.mock_send_log_to_firehose.call_count, 3)
self.assertEqual(self.mock_sqs_client.send_message.call_count, 1)
sent_payloads = [c.args[1] for c in self.mock_send_log_to_firehose.call_args_list]
self.assertTrue(any(p["operation_outcome"]["statusDesc"] == partial_msg for p in sent_payloads))
def test_handler_exception(self):
"""Ensure that sqs_client exceptions do not cause the lambda handler itself to raise an exception"""
# Arrange
self.mock_sqs_client.send_message.side_effect = Exception("SQS error")
self.mock_delta_table.put_item.return_value = FAIL_RESPONSE
event = ValuesForTests.get_event()
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.mock_logger.exception.assert_any_call("Error sending record to DLQ")
def test_handler_raises_exception_if_called_with_unexpected_event(self):
"""Tests that when the Lambda is invoked with an unexpected event format i.e. no "Records" key, then an
exception will be raised. The DDB Stream configuration will then ensure that the event is forwarded to the DLQ.
Note: this would only ever happen if we misconfigured the Lambda or tested manually with a bad event."""
# Arrange
event = {"invalid_format": True}
# Act
with self.assertRaises(KeyError):
handler(event, None)
# Assert
self.mock_sqs_client.send_message.assert_not_called()
def test_handler_processing_failure(self):
# Arrange
self.mock_delta_table.put_item.return_value = FAIL_RESPONSE
event = ValuesForTests.get_event()
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.mock_sqs_client.send_message.assert_called_with(
QueueUrl=TEST_DEAD_LETTER_QUEUE_URL, MessageBody=json.dumps(event["Records"][0])
)
def test_handler_success_update(self):
# Arrange
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
imms_id = "test-update-imms-id"
event = ValuesForTests.get_event(event_name=EventName.UPDATE, operation=Operation.UPDATE, imms_id=imms_id)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.mock_delta_table.put_item.assert_called()
self.mock_send_log_to_firehose.assert_called() # check logged
put_item_call_args = self.mock_delta_table.put_item.call_args # check data written to DynamoDB
put_item_data = put_item_call_args.kwargs["Item"]
self.assertIn("Imms", put_item_data)
self.assertEqual(put_item_data["Imms"]["ACTION_FLAG"], ActionFlag.UPDATE)
self.assertEqual(put_item_data["Operation"], Operation.UPDATE)
self.assertEqual(put_item_data["ImmsID"], imms_id)
self.mock_sqs_client.send_message.assert_not_called()
def test_handler_success_delete_physical(self):
# Arrange
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
imms_id = "test-update-imms-id"
event = ValuesForTests.get_event(
event_name=EventName.DELETE_PHYSICAL,
operation=Operation.DELETE_PHYSICAL,
imms_id=imms_id,
)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.mock_delta_table.put_item.assert_called()
self.mock_send_log_to_firehose.assert_called() # check logged
put_item_call_args = self.mock_delta_table.put_item.call_args # check data written to DynamoDB
put_item_data = put_item_call_args.kwargs["Item"]
self.assertIn("Imms", put_item_data)
self.assertEqual(put_item_data["Operation"], Operation.DELETE_PHYSICAL)
self.assertEqual(put_item_data["Imms"], "") # check imms has been blanked out
self.mock_sqs_client.send_message.assert_not_called()
def test_handler_success_delete_logical(self):
# Arrange
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
imms_id = "test-update-imms-id"
event = ValuesForTests.get_event(
event_name=EventName.UPDATE,
operation=Operation.DELETE_LOGICAL,
imms_id=imms_id,
)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.mock_delta_table.put_item.assert_called()
self.mock_send_log_to_firehose.assert_called() # check logged
put_item_call_args = self.mock_delta_table.put_item.call_args # check data written to DynamoDB
put_item_data = put_item_call_args.kwargs["Item"]
self.assertIn("Imms", put_item_data)
self.assertEqual(put_item_data["Imms"]["ACTION_FLAG"], ActionFlag.DELETE_LOGICAL)
self.assertEqual(put_item_data["Operation"], Operation.DELETE_LOGICAL)
self.assertEqual(put_item_data["ImmsID"], imms_id)
self.mock_sqs_client.send_message.assert_not_called()
@patch("delta.logger.info")
def test_dps_record_skipped(self, mock_logger_info):
event = ValuesForTests.get_event(supplier="DPSFULL")
response = handler(event, None)
self.assertTrue(response)
# Check logging and Firehose were called
mock_logger_info.assert_any_call("Record from DPS skipped")
self.mock_send_log_to_firehose.assert_called()
self.mock_sqs_client.send_message.assert_not_called()
@patch("delta.Converter")
def test_partial_success_with_errors(self, mock_converter):
expected_error_records = [
{
"code": 10,
"field": "PERSON_DOB",
"value": "196513-28",
"message": "Unexpected exception [ValueError]: Invalid isoformat string: '196513-28'",
}
]
mock_converter_instance = MagicMock()
mock_converter_instance.run_conversion.return_value = {"ABC": "DEF"}
mock_converter_instance.get_error_records.return_value = expected_error_records
mock_converter.return_value = mock_converter_instance
# Mock DynamoDB put_item success
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
event = ValuesForTests.get_event()
response = handler(event, None)
self.assertTrue(response)
# Check logging and Firehose were called
self.mock_logger.info.assert_called()
self.assertEqual(self.mock_send_log_to_firehose.call_count, 1)
self.mock_send_log_to_firehose.assert_called_once()
# Get the actual argument passed to send_log_to_firehose
args, kwargs = self.mock_send_log_to_firehose.call_args
sent_payload = args[1] # Second positional arg
operation_outcome = sent_payload["operation_outcome"]
self.assertIn(
"Partial success: successfully synced into delta, but issues found within record",
operation_outcome["statusDesc"],
)
self.assertEqual(operation_outcome["diagnostics"], expected_error_records)
self.mock_logger.warning.assert_called_once_with(
"Partial success: record synced with conversion errors",
extra={"conversion_errors": expected_error_records},
)
def test_send_message_multi_records_diverse(self):
# Arrange
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
records_config = [
RecordConfig(EventName.CREATE, Operation.CREATE, "id1", ActionFlag.CREATE),
RecordConfig(EventName.UPDATE, Operation.UPDATE, "id2", ActionFlag.UPDATE),
RecordConfig(
EventName.DELETE_LOGICAL,
Operation.DELETE_LOGICAL,
"id3",
ActionFlag.DELETE_LOGICAL,
),
RecordConfig(EventName.DELETE_PHYSICAL, Operation.DELETE_PHYSICAL, "id4"),
]
event = ValuesForTests.get_multi_record_event(records_config)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.assertEqual(self.mock_delta_table.put_item.call_count, len(records_config))
self.assertEqual(self.mock_send_log_to_firehose.call_count, len(records_config))
def test_send_message_skipped_records_diverse(self):
"""Check skipped records sent to firehose but not to DynamoDB"""
# Arrange
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
records_config = [
RecordConfig(EventName.CREATE, Operation.CREATE, "id1", ActionFlag.CREATE),
RecordConfig(EventName.UPDATE, Operation.UPDATE, "id2", ActionFlag.UPDATE),
RecordConfig(
EventName.CREATE,
Operation.CREATE,
"id-skip",
ActionFlag.CREATE,
"DPSFULL",
),
RecordConfig(EventName.DELETE_PHYSICAL, Operation.DELETE_PHYSICAL, "id4"),
]
event = ValuesForTests.get_multi_record_event(records_config)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.assertEqual(self.mock_delta_table.put_item.call_count, 3)
self.assertEqual(self.mock_send_log_to_firehose.call_count, len(records_config))
def test_send_message_multi_create(self):
# Arrange
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
records_config = [
RecordConfig(EventName.CREATE, Operation.CREATE, "create-id1", ActionFlag.CREATE),
RecordConfig(EventName.CREATE, Operation.CREATE, "create-id2", ActionFlag.CREATE),
RecordConfig(EventName.CREATE, Operation.CREATE, "create-id3", ActionFlag.CREATE),
]
event = ValuesForTests.get_multi_record_event(records_config)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.assertEqual(self.mock_delta_table.put_item.call_count, 3)
self.assertEqual(self.mock_send_log_to_firehose.call_count, 3)
def test_send_message_multi_update(self):
# Arrange
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
records_config = [
RecordConfig(EventName.UPDATE, Operation.UPDATE, "update-id1", ActionFlag.UPDATE),
RecordConfig(EventName.UPDATE, Operation.UPDATE, "update-id2", ActionFlag.UPDATE),
RecordConfig(EventName.UPDATE, Operation.UPDATE, "update-id3", ActionFlag.UPDATE),
]
event = ValuesForTests.get_multi_record_event(records_config)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.assertEqual(self.mock_delta_table.put_item.call_count, 3)
self.assertEqual(self.mock_send_log_to_firehose.call_count, 3)
def test_send_message_multi_logical_delete(self):
# Arrange
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
records_config = [
RecordConfig(
EventName.DELETE_LOGICAL,
Operation.DELETE_LOGICAL,
"delete-id1",
ActionFlag.DELETE_LOGICAL,
),
RecordConfig(
EventName.DELETE_LOGICAL,
Operation.DELETE_LOGICAL,
"delete-id2",
ActionFlag.DELETE_LOGICAL,
),
RecordConfig(
EventName.DELETE_LOGICAL,
Operation.DELETE_LOGICAL,
"delete-id3",
ActionFlag.DELETE_LOGICAL,
),
]
event = ValuesForTests.get_multi_record_event(records_config)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.assertEqual(self.mock_delta_table.put_item.call_count, 3)
self.assertEqual(self.mock_send_log_to_firehose.call_count, 3)
def test_send_message_multi_physical_delete(self):
# Arrange
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
records_config = [
RecordConfig(EventName.DELETE_PHYSICAL, Operation.DELETE_PHYSICAL, "remove-id1"),
RecordConfig(EventName.DELETE_PHYSICAL, Operation.DELETE_PHYSICAL, "remove-id2"),
RecordConfig(EventName.DELETE_PHYSICAL, Operation.DELETE_PHYSICAL, "remove-id3"),
]
event = ValuesForTests.get_multi_record_event(records_config)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.assertEqual(self.mock_delta_table.put_item.call_count, 3)
self.assertEqual(self.mock_send_log_to_firehose.call_count, 3)
def test_single_error_in_multi(self):
# Arrange
self.mock_delta_table.put_item.side_effect = [
SUCCESS_RESPONSE,
FAIL_RESPONSE,
SUCCESS_RESPONSE,
]
records_config = [
RecordConfig(EventName.CREATE, Operation.CREATE, "ok-id1", ActionFlag.CREATE),
RecordConfig(EventName.UPDATE, Operation.UPDATE, "fail-id1.2", ActionFlag.UPDATE),
RecordConfig(EventName.DELETE_PHYSICAL, Operation.DELETE_PHYSICAL, "ok-id1.3"),
]
event = ValuesForTests.get_multi_record_event(records_config)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.assertEqual(self.mock_delta_table.put_item.call_count, 3)
self.assertEqual(self.mock_send_log_to_firehose.call_count, 3)
self.assertEqual(self.mock_logger.error.call_count, 1)
self.assertEqual(self.mock_sqs_client.send_message.call_count, 1)
def test_single_exception_in_multi(self):
# Arrange
# 2nd record fails
self.mock_delta_table.put_item.side_effect = [
SUCCESS_RESPONSE,
EXCEPTION_RESPONSE,
SUCCESS_RESPONSE,
]
records_config = [
RecordConfig(EventName.CREATE, Operation.CREATE, "ok-id2.1", ActionFlag.CREATE),
RecordConfig(EventName.UPDATE, Operation.UPDATE, "exception-id2.2", ActionFlag.UPDATE),
RecordConfig(EventName.DELETE_PHYSICAL, Operation.DELETE_PHYSICAL, "ok-id2.3"),
]
event = ValuesForTests.get_multi_record_event(records_config)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.assertEqual(self.mock_sqs_client.send_message.call_count, 1)
self.assertEqual(self.mock_delta_table.put_item.call_count, len(records_config))
self.assertEqual(self.mock_send_log_to_firehose.call_count, len(records_config))
def test_single_duplicate_in_multi(self):
# Arrange
self.mock_delta_table.put_item.side_effect = [
SUCCESS_RESPONSE,
DUPLICATE_RESPONSE,
SUCCESS_RESPONSE,
]
records_config = [
RecordConfig(EventName.CREATE, Operation.CREATE, "ok-id2.1", ActionFlag.CREATE),
RecordConfig(EventName.UPDATE, Operation.UPDATE, "duplicate-id2.2", ActionFlag.UPDATE),
RecordConfig(EventName.DELETE_PHYSICAL, Operation.DELETE_PHYSICAL, "ok-id2.3"),
]
event = ValuesForTests.get_multi_record_event(records_config)
# Act
result = handler(event, None)
# Assert
self.assertTrue(result)
self.assertEqual(self.mock_delta_table.put_item.call_count, len(records_config))
self.assertEqual(self.mock_send_log_to_firehose.call_count, len(records_config))
@patch("delta.process_record")
@patch("delta.send_log_to_firehose")
def test_handler_calls_process_record_for_each_event(self, mock_send_log_to_firehose, mock_process_record):
# Arrange
event = {"Records": [{"a": "record1"}, {"a": "record2"}, {"a": "record3"}]}
# Mock process_record to always return True
mock_process_record.return_value = True, {}
mock_send_log_to_firehose.return_value = None
# Act
result = handler(event, {})
# Assert
self.assertTrue(result)
self.assertEqual(mock_process_record.call_count, len(event["Records"]))
@patch("delta.process_record")
@patch("delta.send_log_to_firehose")
def test_handler_sends_all_to_firehose(self, mock_send_log_to_firehose, mock_process_record):
# event with 3 records
event = {"Records": [{"a": "record1"}, {"a": "record2"}, {"a": "record3"}]}
return_ok = (True, {})
return_fail = (False, {})
mock_send_log_to_firehose.return_value = None
mock_process_record.side_effect = [return_ok, return_fail, return_ok]
# Act
result = handler(event, {})
# Assert
self.assertTrue(result)
self.assertEqual(mock_process_record.call_count, len(event["Records"]))
# check that all records were sent to firehose
self.assertEqual(mock_send_log_to_firehose.call_count, len(event["Records"]))
# Only send the failed record to SQS DLQ
self.assertEqual(self.mock_sqs_client.send_message.call_count, 1)
def _get_put_item_payload(self) -> dict:
"""Helper: return the Item dict from the most recent put_item call."""
return self.mock_delta_table.put_item.call_args.kwargs["Item"]
def _assert_timestamp_fields(self, item: dict) -> None:
"""
Asserts:
- DateTimeStamp is present
- SequenceNumber is present
- SequenceNumber is consistent
"""
self.assertIn("DateTimeStamp", item, "DateTimeStamp missing — breaks DPS backward compat (SearchIndex GSI)")
self.assertIn(
"SequenceNumber",
item,
"SequenceNumber missing — breaks OperationSequenceIndex GSI tie-break range key",
)
self.assertNotIn(
"DateTimeStampWithSequence",
item,
"DateTimeStampWithSequence must NOT be present — it has been retired in favour of "
"native multi-attribute key_schema in OperationSequenceIndex (provider >= 6.33.0)",
)
dt: str = item["DateTimeStamp"]
self.assertGreater(len(dt), 0, "DateTimeStamp must not be empty")
self.assertIsInstance(item["SequenceNumber"], str, "SequenceNumber must be a string")
def test_create_put_item_has_correct_timestamp_fields(self):
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
event = ValuesForTests.get_event(event_name=EventName.CREATE, operation=Operation.CREATE, imms_id="ts-create")
handler(event, None)
item = self._get_put_item_payload()
self._assert_timestamp_fields(item)
self.assertEqual(item["Operation"], Operation.CREATE)
def test_update_put_item_has_correct_timestamp_fields(self):
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
event = ValuesForTests.get_event(event_name=EventName.UPDATE, operation=Operation.UPDATE, imms_id="ts-update")
handler(event, None)
item = self._get_put_item_payload()
self._assert_timestamp_fields(item)
self.assertEqual(item["Operation"], Operation.UPDATE)
def test_delete_logical_put_item_has_correct_timestamp_fields(self):
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
event = ValuesForTests.get_event(
event_name=EventName.UPDATE, operation=Operation.DELETE_LOGICAL, imms_id="ts-del-logical"
)
handler(event, None)
item = self._get_put_item_payload()
self._assert_timestamp_fields(item)
self.assertEqual(item["Operation"], Operation.DELETE_LOGICAL)
# DELETE_LOGICAL retains the full Imms payload with ACTION_FLAG set to DELETE_LOGICAL
# Only DELETE_PHYSICAL (REMOVE stream events) blanks Imms to ""
self.assertNotEqual(item["Imms"], "", "DELETE_LOGICAL must retain Imms payload")
self.assertIsInstance(item["Imms"], dict, "DELETE_LOGICAL Imms must be a dict (flat JSON)")
self.assertEqual(
item["Imms"].get("ACTION_FLAG"),
ActionFlag.DELETE_LOGICAL,
"DELETE_LOGICAL Imms must have ACTION_FLAG set to DELETE_LOGICAL",
)
def test_delete_physical_put_item_has_correct_timestamp_fields(self):
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
event = ValuesForTests.get_event(
event_name=EventName.DELETE_PHYSICAL, operation=Operation.DELETE_PHYSICAL, imms_id="ts-del-phys"
)
handler(event, None)
item = self._get_put_item_payload()
self._assert_timestamp_fields(item)
self.assertEqual(item["Operation"], Operation.DELETE_PHYSICAL)
# DELETE_PHYSICAL (REMOVE stream event) blanks Imms to empty string
self.assertEqual(item["Imms"], "", "Physical delete must blank out Imms")
def test_missing_operation_on_modify_routes_to_dlq(self):
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
record = _make_stream_record(
event_name="MODIFY",
operation=None, # Operation deliberately absent from NewImage
patient_sk="covid#missing-op-id",
sequence_number="49590338322303844748686548458181664417",
)
event = {"Records": [record]}
result = handler(event, None)
self.assertTrue(result)
self.mock_delta_table.put_item.assert_not_called()
self.mock_sqs_client.send_message.assert_called_once()
dlq_call_kwargs = self.mock_sqs_client.send_message.call_args.kwargs
self.assertEqual(dlq_call_kwargs["QueueUrl"], TEST_DEAD_LETTER_QUEUE_URL)
class TestGetCreationAndExpiryTimesWithSequence(unittest.TestCase):
def test_get_creation_and_expiry_times(self):
"""Test that the function returns datetime_iso and expiry_timestamp (2-tuple)."""
creation_timestamp = 1708264245.0 # 2024-02-18 14:30:45 UTC
datetime_iso, expiry_timestamp = get_creation_and_expiry_times(creation_timestamp)
from datetime import UTC, datetime
expected_datetime = datetime.fromtimestamp(creation_timestamp, UTC).isoformat()
self.assertEqual(datetime_iso, expected_datetime)
expected_expiry = int(creation_timestamp) + (14 * 24 * 60 * 60)
self.assertEqual(expiry_timestamp, expected_expiry)
def test_datetime_stamp_lexicographic_ordering(self):
"""
DateTimeStamp (ISO8601) sorts correctly across different seconds.
"""
ts1 = 1708264245.0
ts2 = 1708264246.0 # one second later
dt1, _ = get_creation_and_expiry_times(ts1)
dt2, _ = get_creation_and_expiry_times(ts2)
self.assertLess(dt1, dt2, "Earlier timestamp must produce a lexicographically smaller DateTimeStamp")
def test_sequence_number_ordering_independent_of_timestamp(self):
"""
Verify that SequenceNumber strings from DynamoDB streams sort correctly
when used as the tie-break in OperationSequenceIndex.
"""
seq1 = "49590338322303844748686548458181664417"
seq2 = "49590338322303844748686548458181664418"
self.assertLess(seq1, seq2, "SequenceNumber must sort lexicographically for correct tie-break ordering")
unsorted = [seq2, seq1]
sorted_seqs = sorted(unsorted)
self.assertEqual(sorted_seqs, [seq1, seq2])
def test_expiry_is_ttl_days_from_creation(self):
"""ExpiresAt must be exactly DELTA_TTL_DAYS * 86400 seconds from creation."""
creation_timestamp = 1708264245.0
_, expiry_timestamp = get_creation_and_expiry_times(creation_timestamp)
expected_expiry = int(creation_timestamp) + (14 * 24 * 60 * 60)
self.assertEqual(expiry_timestamp, expected_expiry)
class DeltaRecordProcessorTestCase(unittest.TestCase):
def setUp(self):
self.logger_patcher = patch("delta.logger", make_mock_logger())
self.mock_logger = self.logger_patcher.start()
self.mock_delta_table = MagicMock()
self.mock_sqs_client = MagicMock()
self.get_delta_table_patcher = patch("delta.get_delta_table", return_value=self.mock_delta_table)
self.get_delta_table_patcher.start()
def tearDown(self):
patch.stopall()
def _call_process_record(self, record):
return process_record(
record,
self.mock_delta_table,
)
def test_multi_record_success(self):
self.mock_delta_table.put_item.return_value = SUCCESS_RESPONSE
records = [
_make_stream_record(
event_name="INSERT", operation=Operation.CREATE, patient_sk="covid#ok-id-1", imms='{"a":1.1}'
),
_make_stream_record(
event_name="MODIFY", operation=Operation.UPDATE, patient_sk="covid#ok-id-2", imms='{"b":2.2}'
),
_make_stream_record(event_name="REMOVE", operation=None, patient_sk="covid#ok-id-3", imms='{"c":3.3}'),
]
for idx, record in enumerate(records, start=1):
success, outcome = self._call_process_record(record)
self.assertTrue(success)
self.assertIn("record", outcome)
self.assertIn("operation_type", outcome)
self.assertEqual(self.mock_delta_table.put_item.call_count, idx)
self.mock_sqs_client.send_message.assert_not_called()
def test_multi_record_success_with_fail(self):
self.mock_delta_table.put_item.side_effect = [
SUCCESS_RESPONSE,
ClientError({"Error": {"Code": "InternalServerError"}}, "PutItem"),