-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathtest_dynamodbstreams.py
More file actions
443 lines (378 loc) · 16.7 KB
/
test_dynamodbstreams.py
File metadata and controls
443 lines (378 loc) · 16.7 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
import boto3
import pytest
from moto import mock_aws
class TestCore:
stream_arn = None
mocks = []
def setup_method(self):
self.mock = mock_aws()
self.mock.start()
# create a table with a stream
conn = boto3.client("dynamodb", region_name="us-east-1")
resp = conn.create_table(
TableName="test-streams",
KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}],
ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 1},
StreamSpecification={
"StreamEnabled": True,
"StreamViewType": "NEW_AND_OLD_IMAGES",
},
)
self.stream_arn = resp["TableDescription"]["LatestStreamArn"]
def teardown_method(self):
conn = boto3.client("dynamodb", region_name="us-east-1")
conn.delete_table(TableName="test-streams")
self.stream_arn = None
try:
self.mock.stop()
except RuntimeError:
pass
def test_verify_stream(self):
conn = boto3.client("dynamodb", region_name="us-east-1")
resp = conn.describe_table(TableName="test-streams")
assert "LatestStreamArn" in resp["Table"]
def test_describe_stream(self):
conn = boto3.client("dynamodbstreams", region_name="us-east-1")
resp = conn.describe_stream(StreamArn=self.stream_arn)
assert "StreamDescription" in resp
desc = resp["StreamDescription"]
assert desc["StreamArn"] == self.stream_arn
assert desc["TableName"] == "test-streams"
def test_list_streams(self):
conn = boto3.client("dynamodbstreams", region_name="us-east-1")
resp = conn.list_streams()
assert resp["Streams"][0]["StreamArn"] == self.stream_arn
resp = conn.list_streams(TableName="no-stream")
assert not resp["Streams"]
def test_get_shard_iterator(self):
conn = boto3.client("dynamodbstreams", region_name="us-east-1")
resp = conn.describe_stream(StreamArn=self.stream_arn)
shard_id = resp["StreamDescription"]["Shards"][0]["ShardId"]
resp = conn.get_shard_iterator(
StreamArn=self.stream_arn,
ShardId=shard_id,
ShardIteratorType="TRIM_HORIZON",
)
assert "ShardIterator" in resp
def test_get_shard_iterator_at_sequence_number(self):
conn = boto3.client("dynamodbstreams", region_name="us-east-1")
resp = conn.describe_stream(StreamArn=self.stream_arn)
shard_id = resp["StreamDescription"]["Shards"][0]["ShardId"]
resp = conn.get_shard_iterator(
StreamArn=self.stream_arn,
ShardId=shard_id,
ShardIteratorType="AT_SEQUENCE_NUMBER",
SequenceNumber=resp["StreamDescription"]["Shards"][0][
"SequenceNumberRange"
]["StartingSequenceNumber"],
)
assert "ShardIterator" in resp
def test_get_shard_iterator_after_sequence_number(self):
conn = boto3.client("dynamodbstreams", region_name="us-east-1")
resp = conn.describe_stream(StreamArn=self.stream_arn)
shard_id = resp["StreamDescription"]["Shards"][0]["ShardId"]
resp = conn.get_shard_iterator(
StreamArn=self.stream_arn,
ShardId=shard_id,
ShardIteratorType="AFTER_SEQUENCE_NUMBER",
SequenceNumber=resp["StreamDescription"]["Shards"][0][
"SequenceNumberRange"
]["StartingSequenceNumber"],
)
assert "ShardIterator" in resp
def test_get_records_empty(self):
conn = boto3.client("dynamodbstreams", region_name="us-east-1")
resp = conn.describe_stream(StreamArn=self.stream_arn)
shard_id = resp["StreamDescription"]["Shards"][0]["ShardId"]
resp = conn.get_shard_iterator(
StreamArn=self.stream_arn, ShardId=shard_id, ShardIteratorType="LATEST"
)
iterator_id = resp["ShardIterator"]
resp = conn.get_records(ShardIterator=iterator_id)
assert "Records" in resp
assert len(resp["Records"]) == 0
def test_get_records_seq(self):
conn = boto3.client("dynamodb", region_name="us-east-1")
conn.put_item(
TableName="test-streams",
Item={"id": {"S": "entry1"}, "first_col": {"S": "foo"}},
)
conn.put_item(
TableName="test-streams",
Item={
"id": {"S": "entry1"},
"first_col": {"S": "bar"},
"second_col": {"S": "baz"},
"a": {"L": [{"M": {"b": {"S": "bar1"}}}]},
},
)
conn.delete_item(TableName="test-streams", Key={"id": {"S": "entry1"}})
conn = boto3.client("dynamodbstreams", region_name="us-east-1")
resp = conn.describe_stream(StreamArn=self.stream_arn)
shard_id = resp["StreamDescription"]["Shards"][0]["ShardId"]
resp = conn.get_shard_iterator(
StreamArn=self.stream_arn,
ShardId=shard_id,
ShardIteratorType="TRIM_HORIZON",
)
iterator_id = resp["ShardIterator"]
resp = conn.get_records(ShardIterator=iterator_id)
assert len(resp["Records"]) == 3
assert resp["Records"][0]["eventName"] == "INSERT"
assert resp["Records"][1]["eventName"] == "MODIFY"
assert resp["Records"][2]["eventName"] == "REMOVE"
sequence_number_modify = resp["Records"][1]["dynamodb"]["SequenceNumber"]
# now try fetching from the next shard iterator, it should be
# empty
resp = conn.get_records(ShardIterator=resp["NextShardIterator"])
assert len(resp["Records"]) == 0
# check that if we get the shard iterator AT_SEQUENCE_NUMBER will get the MODIFY event
resp = conn.get_shard_iterator(
StreamArn=self.stream_arn,
ShardId=shard_id,
ShardIteratorType="AT_SEQUENCE_NUMBER",
SequenceNumber=sequence_number_modify,
)
iterator_id = resp["ShardIterator"]
resp = conn.get_records(ShardIterator=iterator_id)
assert len(resp["Records"]) == 2
assert resp["Records"][0]["eventName"] == "MODIFY"
assert resp["Records"][1]["eventName"] == "REMOVE"
# check that if we get the shard iterator AFTER_SEQUENCE_NUMBER will get the DELETE event
resp = conn.get_shard_iterator(
StreamArn=self.stream_arn,
ShardId=shard_id,
ShardIteratorType="AFTER_SEQUENCE_NUMBER",
SequenceNumber=sequence_number_modify,
)
iterator_id = resp["ShardIterator"]
resp = conn.get_records(ShardIterator=iterator_id)
assert len(resp["Records"]) == 1
assert resp["Records"][0]["eventName"] == "REMOVE"
class TestEdges:
mocks = []
def setup_method(self):
self.mock = mock_aws()
self.mock.start()
def teardown_method(self):
self.mock.stop()
def test_enable_stream_on_table(self):
conn = boto3.client("dynamodb", region_name="us-east-1")
resp = conn.create_table(
TableName="test-streams",
KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}],
ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 1},
)
assert "StreamSpecification" not in resp["TableDescription"]
resp = conn.update_table(
TableName="test-streams",
StreamSpecification={"StreamViewType": "KEYS_ONLY", "StreamEnabled": True},
)
assert "StreamSpecification" in resp["TableDescription"]
assert resp["TableDescription"]["StreamSpecification"] == {
"StreamEnabled": True,
"StreamViewType": "KEYS_ONLY",
}
assert "LatestStreamLabel" in resp["TableDescription"]
# now try to enable it again
with pytest.raises(conn.exceptions.ResourceInUseException):
resp = conn.update_table(
TableName="test-streams",
StreamSpecification={
"StreamViewType": "OLD_IMAGES",
"StreamEnabled": True,
},
)
def test_stream_with_range_key(self):
dyn = boto3.client("dynamodb", region_name="us-east-1")
resp = dyn.create_table(
TableName="test-streams",
KeySchema=[
{"AttributeName": "id", "KeyType": "HASH"},
{"AttributeName": "color", "KeyType": "RANGE"},
],
AttributeDefinitions=[
{"AttributeName": "id", "AttributeType": "S"},
{"AttributeName": "color", "AttributeType": "S"},
],
ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 1},
StreamSpecification={"StreamViewType": "NEW_IMAGES", "StreamEnabled": True},
)
stream_arn = resp["TableDescription"]["LatestStreamArn"]
streams = boto3.client("dynamodbstreams", region_name="us-east-1")
resp = streams.describe_stream(StreamArn=stream_arn)
shard_id = resp["StreamDescription"]["Shards"][0]["ShardId"]
resp = streams.get_shard_iterator(
StreamArn=stream_arn, ShardId=shard_id, ShardIteratorType="LATEST"
)
iterator_id = resp["ShardIterator"]
dyn.put_item(
TableName="test-streams", Item={"id": {"S": "row1"}, "color": {"S": "blue"}}
)
dyn.put_item(
TableName="test-streams",
Item={"id": {"S": "row2"}, "color": {"S": "green"}},
)
resp = streams.get_records(ShardIterator=iterator_id)
assert len(resp["Records"]) == 2
assert resp["Records"][0]["eventName"] == "INSERT"
assert resp["Records"][1]["eventName"] == "INSERT"
class TestStreamRecordImages:
"""Verify that stream records include the correct image keys.
AWS behavior:
- INSERT: NewImage present, OldImage absent
- MODIFY: both NewImage and OldImage present
- REMOVE: OldImage present, NewImage absent
Empty images (e.g. OldImage on INSERT) must be omitted entirely,
not included as empty dicts.
"""
@staticmethod
def _get_records(table_name, stream_arn):
"""Helper to read all records from a stream."""
streams = boto3.client("dynamodbstreams", region_name="us-east-1")
desc = streams.describe_stream(StreamArn=stream_arn)
shard_id = desc["StreamDescription"]["Shards"][0]["ShardId"]
resp = streams.get_shard_iterator(
StreamArn=stream_arn,
ShardId=shard_id,
ShardIteratorType="TRIM_HORIZON",
)
return streams.get_records(ShardIterator=resp["ShardIterator"])["Records"]
@mock_aws
def test_new_and_old_images_insert_has_no_old_image(self):
"""INSERT with NEW_AND_OLD_IMAGES should not have OldImage."""
client = boto3.client("dynamodb", region_name="us-east-1")
resp = client.create_table(
TableName="img-test",
KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
StreamSpecification={
"StreamEnabled": True,
"StreamViewType": "NEW_AND_OLD_IMAGES",
},
)
stream_arn = resp["TableDescription"]["LatestStreamArn"]
client.put_item(
TableName="img-test",
Item={"pk": {"S": "k1"}, "val": {"S": "a"}},
)
records = self._get_records("img-test", stream_arn)
assert len(records) == 1
rec = records[0]
assert rec["eventName"] == "INSERT"
assert "NewImage" in rec["dynamodb"]
assert rec["dynamodb"]["NewImage"]["val"] == {"S": "a"}
# INSERT must NOT have OldImage
assert "OldImage" not in rec["dynamodb"]
@mock_aws
def test_new_and_old_images_modify_has_both(self):
"""MODIFY with NEW_AND_OLD_IMAGES should have both images."""
client = boto3.client("dynamodb", region_name="us-east-1")
resp = client.create_table(
TableName="img-test",
KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
StreamSpecification={
"StreamEnabled": True,
"StreamViewType": "NEW_AND_OLD_IMAGES",
},
)
stream_arn = resp["TableDescription"]["LatestStreamArn"]
client.put_item(
TableName="img-test",
Item={"pk": {"S": "k1"}, "val": {"S": "original"}},
)
client.put_item(
TableName="img-test",
Item={"pk": {"S": "k1"}, "val": {"S": "updated"}},
)
records = self._get_records("img-test", stream_arn)
assert len(records) == 2
modify = records[1]
assert modify["eventName"] == "MODIFY"
assert "OldImage" in modify["dynamodb"]
assert "NewImage" in modify["dynamodb"]
assert modify["dynamodb"]["OldImage"]["val"] == {"S": "original"}
assert modify["dynamodb"]["NewImage"]["val"] == {"S": "updated"}
@mock_aws
def test_new_and_old_images_remove_has_no_new_image(self):
"""REMOVE with NEW_AND_OLD_IMAGES should not have NewImage."""
client = boto3.client("dynamodb", region_name="us-east-1")
resp = client.create_table(
TableName="img-test",
KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
StreamSpecification={
"StreamEnabled": True,
"StreamViewType": "NEW_AND_OLD_IMAGES",
},
)
stream_arn = resp["TableDescription"]["LatestStreamArn"]
client.put_item(
TableName="img-test",
Item={"pk": {"S": "k1"}, "val": {"S": "a"}},
)
client.delete_item(TableName="img-test", Key={"pk": {"S": "k1"}})
records = self._get_records("img-test", stream_arn)
remove = records[1]
assert remove["eventName"] == "REMOVE"
assert "OldImage" in remove["dynamodb"]
assert remove["dynamodb"]["OldImage"]["val"] == {"S": "a"}
# REMOVE must NOT have NewImage
assert "NewImage" not in remove["dynamodb"]
@mock_aws
def test_new_image_only_remove_has_no_new_image(self):
"""REMOVE with NEW_IMAGE should not have NewImage (nothing new)."""
client = boto3.client("dynamodb", region_name="us-east-1")
resp = client.create_table(
TableName="img-test",
KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
StreamSpecification={
"StreamEnabled": True,
"StreamViewType": "NEW_IMAGE",
},
)
stream_arn = resp["TableDescription"]["LatestStreamArn"]
client.put_item(
TableName="img-test",
Item={"pk": {"S": "k1"}, "val": {"S": "a"}},
)
client.delete_item(TableName="img-test", Key={"pk": {"S": "k1"}})
records = self._get_records("img-test", stream_arn)
remove = records[1]
assert remove["eventName"] == "REMOVE"
# NEW_IMAGE on a REMOVE: there is no new image
assert "NewImage" not in remove["dynamodb"]
assert "OldImage" not in remove["dynamodb"]
@mock_aws
def test_old_image_only_insert_has_no_old_image(self):
"""INSERT with OLD_IMAGE should not have OldImage (nothing old)."""
client = boto3.client("dynamodb", region_name="us-east-1")
resp = client.create_table(
TableName="img-test",
KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
StreamSpecification={
"StreamEnabled": True,
"StreamViewType": "OLD_IMAGE",
},
)
stream_arn = resp["TableDescription"]["LatestStreamArn"]
client.put_item(
TableName="img-test",
Item={"pk": {"S": "k1"}, "val": {"S": "a"}},
)
records = self._get_records("img-test", stream_arn)
insert = records[0]
assert insert["eventName"] == "INSERT"
# OLD_IMAGE on an INSERT: there is no old image
assert "OldImage" not in insert["dynamodb"]
assert "NewImage" not in insert["dynamodb"]