11import json
2+ import os
23import unittest
34from copy import deepcopy
5+ from datetime import datetime
46from unittest .mock import patch , Mock
57from moto import mock_aws
68from boto3 import resource as boto3_resource
1113MOCK_ENV_VARS = {
1214 "AWS_SQS_QUEUE_URL" : "https://sqs.eu-west-2.amazonaws.com/123456789012/test-queue" ,
1315 "DELTA_TABLE_NAME" : "immunisation-batch-internal-dev-audit-test-table" ,
16+ "DELTA_TTL_DAYS" : "14" ,
1417 "SOURCE" : "test-source" ,
1518}
1619request_json_data = ValuesForTests .json_data
@@ -82,18 +85,23 @@ def get_event(event_name=EventName.CREATE, operation="operation", supplier="EMIS
8285 def assert_dynamodb_record (self , operation_flag , action_flag , items , expected_values , expected_imms , response ):
8386 """
8487 Asserts that a record with the expected structure exists in DynamoDB.
85- Ignores dynamically generated fields like PK, DateTimeStamp, and ExpiresAt .
88+ Ignores the dynamically generated field PK .
8689 Ensures that the 'Imms' field matches exactly.
90+ Ensures that the ExpiresAt field has been calculated correctly.
8791 """
8892 self .assertTrue (response )
8993
90- filtered_items = [
91- {k : v for k , v in item .items () if k not in [ "PK" , "DateTimeStamp" , "ExpiresAt" ] }
94+ unfiltered_items = [
95+ {k : v for k , v in item .items ()}
9296 for item in items
9397 if item .get ("Operation" ) == operation_flag
9498 and item .get ("Imms" , {}).get ("ACTION_FLAG" ) == action_flag
9599 ]
96100
101+ filtered_items = [
102+ {k : v for k , v in item .items () if k not in ["PK" , "DateTimeStamp" , "ExpiresAt" ]}
103+ for item in unfiltered_items
104+ ]
97105 self .assertGreater (len (filtered_items ), 0 , f"No matching item found for { operation_flag } " )
98106
99107 imms_data = filtered_items [0 ]["Imms" ]
@@ -107,6 +115,12 @@ def assert_dynamodb_record(self, operation_flag, action_flag, items, expected_va
107115 self .assertIn (key , filtered_items [0 ], f"{ key } is missing" )
108116 self .assertEqual (filtered_items [0 ][key ], expected_value , f"{ key } mismatch" )
109117
118+ # Check that the value of ExpiresAt is DELTA_TTL_DAYS after DateTimeStamp
119+ expected_seconds = int (os .environ ["DELTA_TTL_DAYS" ]) * 24 * 60 * 60
120+ date_time = int (datetime .fromisoformat (unfiltered_items [0 ]["DateTimeStamp" ]).timestamp ())
121+ expires_at = unfiltered_items [0 ]["ExpiresAt" ]
122+ self .assertEqual (expires_at - date_time , expected_seconds )
123+
110124 def test_fhir_converter_json_direct_data (self ):
111125 """it should convert fhir json data to flat json"""
112126 json_data = json .dumps (ValuesForTests .json_data )
0 commit comments