Skip to content

Commit 780ac5a

Browse files
authored
VED-47-Delta-Expiry (#785)
* DELTA_TTF in terraform * mock DELTA_TTL * mock DELTA_TTL II * test for expiresAt * DELTA_TTL_DAYS
1 parent 0d367f1 commit 780ac5a

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

delta_backend/src/delta.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
failure_queue_url = os.environ["AWS_SQS_QUEUE_URL"]
1818
delta_table_name = os.environ["DELTA_TABLE_NAME"]
1919
delta_source = os.environ["SOURCE"]
20+
delta_ttl_days = os.environ["DELTA_TTL_DAYS"]
2021
region_name = "eu-west-2"
2122
logging.basicConfig()
2223
logger = logging.getLogger()
@@ -73,7 +74,7 @@ def get_imms_id(primary_key: str) -> str:
7374

7475
def get_creation_and_expiry_times(creation_timestamp: float) -> (str, int):
7576
creation_datetime = datetime.fromtimestamp(creation_timestamp, UTC)
76-
expiry_datetime = creation_datetime + timedelta(days=30)
77+
expiry_datetime = creation_datetime + timedelta(days=int(delta_ttl_days))
7778
expiry_timestamp = int(expiry_datetime.timestamp())
7879
return creation_datetime.isoformat(), expiry_timestamp
7980

delta_backend/tests/test_convert.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
2+
import os
23
import unittest
34
from copy import deepcopy
5+
from datetime import datetime
46
from unittest.mock import patch, Mock
57
from moto import mock_aws
68
from boto3 import resource as boto3_resource
@@ -11,6 +13,7 @@
1113
MOCK_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
}
1619
request_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)

delta_backend/tests/test_delta.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
## @TODO: # Note: Environment variables shared across tests, thus aligned
1414
os.environ["AWS_SQS_QUEUE_URL"] = TEST_QUEUE_URL
1515
os.environ["DELTA_TABLE_NAME"] = "my_delta_table"
16+
os.environ["DELTA_TTL_DAYS"] = "14"
1617
os.environ["SOURCE"] = "my_source"
1718

1819
from delta import send_message, handler, process_record # Import after setting environment variables

terraform/delta.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ resource "aws_lambda_function" "delta_sync_lambda" {
138138
environment {
139139
variables = {
140140
DELTA_TABLE_NAME = aws_dynamodb_table.delta-dynamodb-table.name
141+
DELTA_TTL_DAYS = 30
141142
AWS_SQS_QUEUE_URL = aws_sqs_queue.dlq.id
142143
SOURCE = "IEDS"
143144
SPLUNK_FIREHOSE_NAME = module.splunk.firehose_stream_name

0 commit comments

Comments
 (0)