This repository was archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathtest_table_update.py
More file actions
executable file
·198 lines (170 loc) · 5.38 KB
/
Copy pathtest_table_update.py
File metadata and controls
executable file
·198 lines (170 loc) · 5.38 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
import copy
import datetime
import decimal
import json
import uuid
import pytest
from boto3.dynamodb.types import TypeSerializer
from botocore import stub
from fixtures import context, lambda_module # pylint: disable=import-error
from helpers import compare_event # pylint: disable=import-error,no-name-in-module
lambda_module = pytest.fixture(scope="module", params=[{
"function_dir": "table_update",
"module_name": "main",
"environ": {
"ENVIRONMENT": "test",
"EVENT_BUS_NAME": "EVENT_BUS_NAME",
"POWERTOOLS_TRACE_DISABLED": "true"
}
}])(lambda_module)
context = pytest.fixture(context)
@pytest.fixture
def order():
now = datetime.datetime.now()
return {
"orderId": str(uuid.uuid4()),
"userId": str(uuid.uuid4()),
"createdDate": now.isoformat(),
"modifiedDate": now.isoformat(),
"status": "NEW",
"products": [{
"productId": str(uuid.uuid4()),
"name": "Test Product",
"package": {
"width": 1000,
"length": 900,
"height": 800,
"weight": 700
},
"price": 300,
"quantity": 4
}],
"address": {
"name": "John Doe",
"companyName": "Company Inc.",
"streetAddress": "123 Street St",
"postCode": "12345",
"city": "Town",
"state": "State",
"country": "SE",
"phoneNumber": "+123456789"
},
"deliveryPrice": 200,
"total": 1400
}
@pytest.fixture
def insert_data(order):
record = {
"awsRegion": "us-east-1",
"dynamodb": {
"Keys": {
"orderId": {"S": order["orderId"]}
},
"NewImage": {k: TypeSerializer().serialize(v) for k, v in order.items()},
"SequenceNumber": "1234567890123456789012345",
"SizeBytes": 123,
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"eventID": str(uuid.uuid4()),
"eventName": "INSERT",
"eventSource": "aws:dynamodb",
"eventVersion": "1.0"
}
event = {
"Source": "ecommerce.orders",
"Resources": [order["orderId"]],
"DetailType": "OrderCreated",
"Detail": json.dumps(order),
"EventBusName": "EVENT_BUS_NAME"
}
return {"record": record, "event": event}
@pytest.fixture
def remove_data(order):
record = {
"awsRegion": "us-east-1",
"dynamodb": {
"Keys": {
"orderId": {"S": order["orderId"]}
},
"OldImage": {k: TypeSerializer().serialize(v) for k, v in order.items()},
"SequenceNumber": "1234567890123456789012345",
"SizeBytes": 123,
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"eventID": str(uuid.uuid4()),
"eventName": "REMOVE",
"eventSource": "aws:dynamodb",
"eventVersion": "1.0"
}
event = {
"Source": "ecommerce.orders",
"Resources": [order["orderId"]],
"DetailType": "OrderDeleted",
"Detail": json.dumps(order),
"EventBusName": "EVENT_BUS_NAME"
}
return {"record": record, "event": event}
@pytest.fixture
def modify_data(order):
new_order = copy.deepcopy(order)
new_order["status"] = "COMPLETED"
record = {
"awsRegion": "us-east-1",
"dynamodb": {
"Keys": {
"orderId": {"S": order["orderId"]}
},
"OldImage": {k: TypeSerializer().serialize(v) for k, v in order.items()},
"NewImage": {k: TypeSerializer().serialize(v) for k, v in new_order.items()},
"SequenceNumber": "1234567890123456789012345",
"SizeBytes": 123,
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"eventID": str(uuid.uuid4()),
"eventName": "REMOVE",
"eventSource": "aws:dynamodb",
"eventVersion": "1.0"
}
event = {
"Source": "ecommerce.orders",
"Resources": [order["orderId"]],
"DetailType": "OrderDeleted",
"Detail": json.dumps({
"old": order,
"new": new_order,
"changed": ["status"]
}),
"EventBusName": "EVENT_BUS_NAME"
}
return {"record": record, "event": event}
def test_send_events(lambda_module, insert_data):
"""
Test send_events()
"""
eventbridge = stub.Stubber(lambda_module.eventbridge)
events = [insert_data["event"]]
response = {}
expected_params = {"Entries": events}
eventbridge.add_response("put_events", response, expected_params)
eventbridge.activate()
lambda_module.send_events(events)
eventbridge.assert_no_pending_responses()
eventbridge.deactivate()
def test_handler(lambda_module, context, insert_data):
"""
Test the Lambda function handler
"""
# Prepare Lambda event and context
event = {"Records": [insert_data["record"]]}
# Stubbing boto3
eventbridge = stub.Stubber(lambda_module.eventbridge)
# Ignore time
insert_data["event"]["Time"] = stub.ANY
expected_params = {"Entries": [insert_data["event"]]}
eventbridge.add_response("put_events", {}, expected_params)
eventbridge.activate()
# Send request
lambda_module.handler(event, context)
# Check that events were sent
eventbridge.assert_no_pending_responses()
eventbridge.deactivate()