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
|
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"
|