Skip to content

Commit 957afa9

Browse files
committed
Add time window edge case testing
1 parent d9a702b commit 957afa9

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

backend/compact-connect/lambdas/python/common/tests/function/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ def build_resources(self):
3737
self.create_compact_configuration_table()
3838
self.create_provider_table()
3939
self.create_users_table()
40+
self.create_transaction_history_table()
4041

4142
# Adding a waiter allows for testing against an actual AWS account, if needed
4243
waiter = self._compact_configuration_table.meta.client.get_waiter('table_exists')
4344
waiter.wait(TableName=self._compact_configuration_table.name)
4445
waiter.wait(TableName=self._provider_table.name)
4546
waiter.wait(TableName=self._users_table.name)
47+
waiter.wait(TableName=self._transaction_history_table.name)
4648

4749
# Create a new Cognito user pool
4850
cognito_client = boto3.client('cognito-idp')
@@ -122,15 +124,29 @@ def create_provider_table(self):
122124
],
123125
)
124126

127+
def create_transaction_history_table(self):
128+
self._transaction_history_table = boto3.resource('dynamodb').create_table(
129+
KeySchema=[{'AttributeName': 'pk', 'KeyType': 'HASH'}, {'AttributeName': 'sk', 'KeyType': 'RANGE'}],
130+
AttributeDefinitions=[
131+
{'AttributeName': 'pk', 'AttributeType': 'S'},
132+
{'AttributeName': 'sk', 'AttributeType': 'S'},
133+
],
134+
TableName=os.environ['TRANSACTION_HISTORY_TABLE_NAME'],
135+
BillingMode='PAY_PER_REQUEST',
136+
)
137+
125138
def delete_resources(self):
126139
self._compact_configuration_table.delete()
127140
self._provider_table.delete()
128141
self._users_table.delete()
142+
self._transaction_history_table.delete()
129143

130144
waiter = self._users_table.meta.client.get_waiter('table_not_exists')
131145
waiter.wait(TableName=self._compact_configuration_table.name)
132146
waiter.wait(TableName=self._provider_table.name)
133147
waiter.wait(TableName=self._users_table.name)
148+
waiter = self._transaction_history_table.meta.client.get_waiter('table_not_exists')
149+
waiter.wait(TableName=self._transaction_history_table.name)
134150

135151
# Delete the Cognito user pool
136152
cognito_client = boto3.client('cognito-idp')
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from datetime import datetime
2+
3+
from moto import mock_aws
4+
5+
from .. import TstFunction
6+
7+
8+
@mock_aws
9+
class TestClient(TstFunction):
10+
def test_transaction_history_edge_times(self):
11+
"""
12+
Test for internal consistency in how transactions are stored and reported, when times land
13+
right on the edge of the reporting window.
14+
"""
15+
from cc_common.data_model.transaction_client import TransactionClient
16+
17+
client = TransactionClient(self.config)
18+
19+
# Create some records on the edge of the window we want to query
20+
start_time_string = '2024-01-01T00:00:00Z'
21+
end_time_string = '2024-01-08T00:00:00Z'
22+
start_time = datetime.fromisoformat(start_time_string)
23+
end_time = datetime.fromisoformat(end_time_string)
24+
25+
client.store_transactions(
26+
compact='aslp',
27+
transactions=[
28+
# One at the beginning of the window
29+
{
30+
'transactionId': '123',
31+
'transactionProcessor': 'authorize.net',
32+
'batch': {'batchId': 'abc', 'settlementTimeUTC': start_time_string},
33+
},
34+
# One at the end of the window
35+
{
36+
'transactionId': '456',
37+
'transactionProcessor': 'authorize.net',
38+
'batch': {'batchId': 'def', 'settlementTimeUTC': end_time_string},
39+
},
40+
],
41+
)
42+
43+
# Query the transactions in the window
44+
transactions = client.get_transactions_in_range(
45+
compact='aslp',
46+
start_epoch=int(start_time.timestamp()),
47+
end_epoch=int(end_time.timestamp()),
48+
)
49+
50+
# We expect to get the first back but not the second. Any more or less will result in
51+
# under or over reporting of transactions across reports.
52+
self.assertEqual(
53+
{
54+
'pk': 'COMPACT#aslp#TRANSACTIONS#MONTH#2024-01',
55+
'sk': 'COMPACT#aslp#TIME#1704067200#BATCH#abc#TX#123',
56+
'transactionId': '123',
57+
'transactionProcessor': 'authorize.net',
58+
'batch': {'batchId': 'abc', 'settlementTimeUTC': start_time_string},
59+
},
60+
transactions[0],
61+
)

0 commit comments

Comments
 (0)