-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtest_dynamodb.py
283 lines (235 loc) · 10.3 KB
/
test_dynamodb.py
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
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# https://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from decimal import Decimal
import boto3.session
from boto3.compat import collections_abc
from boto3.dynamodb.conditions import Attr, Key
from boto3.dynamodb.types import Binary
from tests import unique_id, unittest
class BaseDynamoDBTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.session = boto3.session.Session(region_name='us-west-2')
cls.dynamodb = cls.session.resource('dynamodb')
cls.table_name = unique_id('boto3db')
cls.item_data = {
'MyHashKey': 'mykey',
'MyNull': None,
'MyBool': True,
'MyString': 'mystring',
'MyNumber': Decimal('1.25'),
'MyBinary': Binary(b'\x01'),
'MyStringSet': {'foo'},
'MyNumberSet': {Decimal('1.25')},
'MyBinarySet': {Binary(b'\x01')},
'MyList': ['foo'],
'MyMap': {'foo': 'bar'},
}
cls.table = cls.dynamodb.create_table(
TableName=cls.table_name,
ProvisionedThroughput={
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5,
},
KeySchema=[{"AttributeName": "MyHashKey", "KeyType": "HASH"}],
AttributeDefinitions=[
{"AttributeName": "MyHashKey", "AttributeType": "S"}
],
)
waiter = cls.dynamodb.meta.client.get_waiter('table_exists')
waiter.wait(TableName=cls.table_name)
@classmethod
def tearDownClass(cls):
cls.table.delete()
class TestDynamoDBTypes(BaseDynamoDBTest):
def test_put_get_item(self):
self.table.put_item(Item=self.item_data)
self.addCleanup(self.table.delete_item, Key={'MyHashKey': 'mykey'})
response = self.table.get_item(
Key={'MyHashKey': 'mykey'}, ConsistentRead=True
)
self.assertEqual(response['Item'], self.item_data)
class TestDynamoDBConditions(BaseDynamoDBTest):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.table.put_item(Item=cls.item_data)
@classmethod
def tearDownClass(cls):
cls.table.delete_item(Key={'MyHashKey': 'mykey'})
super().tearDownClass()
def scan(self, filter_expression):
return self.table.scan(
FilterExpression=filter_expression, ConsistentRead=True
)
def query(self, key_condition_expression, filter_expression=None):
kwargs = {
'KeyConditionExpression': key_condition_expression,
'ConsistentRead': True,
}
if filter_expression is not None:
kwargs['FilterExpression'] = filter_expression
return self.table.query(**kwargs)
def test_filter_expression(self):
r = self.scan(filter_expression=Attr('MyHashKey').eq('mykey'))
self.assertEqual(r['Items'][0]['MyHashKey'], 'mykey')
def test_key_condition_expression(self):
r = self.query(key_condition_expression=Key('MyHashKey').eq('mykey'))
self.assertEqual(r['Items'][0]['MyHashKey'], 'mykey')
def test_key_condition_with_filter_condition_expression(self):
r = self.query(
key_condition_expression=Key('MyHashKey').eq('mykey'),
filter_expression=Attr('MyString').eq('mystring'),
)
self.assertEqual(r['Items'][0]['MyString'], 'mystring')
def test_condition_less_than(self):
r = self.scan(filter_expression=Attr('MyNumber').lt(Decimal('1.26')))
self.assertTrue(r['Items'][0]['MyNumber'] < Decimal('1.26'))
def test_condition_less_than_equal(self):
r = self.scan(filter_expression=Attr('MyNumber').lte(Decimal('1.26')))
self.assertTrue(r['Items'][0]['MyNumber'] <= Decimal('1.26'))
def test_condition_greater_than(self):
r = self.scan(filter_expression=Attr('MyNumber').gt(Decimal('1.24')))
self.assertTrue(r['Items'][0]['MyNumber'] > Decimal('1.24'))
def test_condition_greater_than_equal(self):
r = self.scan(filter_expression=Attr('MyNumber').gte(Decimal('1.24')))
self.assertTrue(r['Items'][0]['MyNumber'] >= Decimal('1.24'))
def test_condition_begins_with(self):
r = self.scan(filter_expression=Attr('MyString').begins_with('my'))
self.assertTrue(r['Items'][0]['MyString'].startswith('my'))
def test_condition_between(self):
r = self.scan(
filter_expression=Attr('MyNumber').between(
Decimal('1.24'), Decimal('1.26')
)
)
self.assertTrue(r['Items'][0]['MyNumber'] > Decimal('1.24'))
self.assertTrue(r['Items'][0]['MyNumber'] < Decimal('1.26'))
def test_condition_not_equal(self):
r = self.scan(filter_expression=Attr('MyHashKey').ne('notmykey'))
self.assertNotEqual(r['Items'][0]['MyHashKey'], 'notmykey')
def test_condition_in(self):
r = self.scan(
filter_expression=Attr('MyHashKey').is_in(['notmykey', 'mykey'])
)
self.assertIn(r['Items'][0]['MyHashKey'], ['notmykey', 'mykey'])
def test_condition_exists(self):
r = self.scan(filter_expression=Attr('MyString').exists())
self.assertIn('MyString', r['Items'][0])
def test_condition_not_exists(self):
r = self.scan(filter_expression=Attr('MyFakeKey').not_exists())
self.assertNotIn('MyFakeKey', r['Items'][0])
def test_condition_contains(self):
r = self.scan(filter_expression=Attr('MyString').contains('my'))
self.assertIn('my', r['Items'][0]['MyString'])
def test_condition_size(self):
r = self.scan(
filter_expression=Attr('MyString').size().eq(len('mystring'))
)
self.assertEqual(len(r['Items'][0]['MyString']), len('mystring'))
def test_condition_attribute_type(self):
r = self.scan(filter_expression=Attr('MyMap').attribute_type('M'))
self.assertIsInstance(r['Items'][0]['MyMap'], collections_abc.Mapping)
def test_condition_and(self):
r = self.scan(
filter_expression=(
Attr('MyHashKey').eq('mykey') & Attr('MyString').eq('mystring')
)
)
item = r['Items'][0]
self.assertTrue(
item['MyHashKey'] == 'mykey' and item['MyString'] == 'mystring'
)
def test_condition_or(self):
r = self.scan(
filter_expression=(
Attr('MyHashKey').eq('mykey2')
| Attr('MyString').eq('mystring')
)
)
item = r['Items'][0]
self.assertTrue(
item['MyHashKey'] == 'mykey2' or item['MyString'] == 'mystring'
)
def test_condition_not(self):
r = self.scan(filter_expression=(~Attr('MyHashKey').eq('mykey2')))
item = r['Items'][0]
self.assertTrue(item['MyHashKey'] != 'mykey2')
def test_condition_in_map(self):
r = self.scan(filter_expression=Attr('MyMap.foo').eq('bar'))
self.assertEqual(r['Items'][0]['MyMap']['foo'], 'bar')
def test_condition_in_list(self):
r = self.scan(filter_expression=Attr('MyList[0]').eq('foo'))
self.assertEqual(r['Items'][0]['MyList'][0], 'foo')
class TestDynamodbBatchWrite(BaseDynamoDBTest):
def test_batch_write_items(self):
num_elements = 1000
items = []
for i in range(num_elements):
items.append({'MyHashKey': f'foo{i}', 'OtherKey': f'bar{i}'})
with self.table.batch_writer() as batch:
for item in items:
batch.put_item(Item=item)
# Verify all the items were added to dynamodb.
for obj in self.table.scan(ConsistentRead=True)['Items']:
self.assertIn(obj, items)
# Verify consumed capacity is None
self.assertIs(batch.consumed_capacity, None)
def test_batch_write_item_agg_capacity_none(self):
num_elements = 100
items = []
for i in range(num_elements):
items.append({'MyHashKey': f'foo{i}', 'OtherKey': f'bar{i}'})
with self.table.batch_writer(return_consumed_capacity='NONE') as batch:
for item in items:
batch.put_item(Item=item)
# Verify all the items were added to dynamodb.
for obj in self.table.scan(ConsistentRead=True)['Items']:
self.assertIn(obj, items)
# Verify consumed capacity
self.assertIs(batch.consumed_capacity, None)
def test_batch_write_item_agg_capacity_total(self):
num_elements = 100
items = []
for i in range(num_elements):
items.append({'MyHashKey': f'foo{i}', 'OtherKey': f'bar{i}'})
with self.table.batch_writer(
return_consumed_capacity='TOTAL'
) as batch:
for item in items:
batch.put_item(Item=item)
# Verify all the items were added to dynamodb.
for obj in self.table.scan(ConsistentRead=True)['Items']:
self.assertIn(obj, items)
# Verify consumed capacity
total_cu = batch.consumed_capacity[0]['CapacityUnits']
self.assertEqual(total_cu, num_elements)
self.assertNotIn('Table', batch.consumed_capacity[0])
def test_batch_write_item_agg_capacity_indexes(self):
num_elements = 100
items = []
for i in range(num_elements):
items.append({'MyHashKey': f'foo{i}', 'OtherKey': f'bar{i}'})
with self.table.batch_writer(
return_consumed_capacity='INDEXES'
) as batch:
for item in items:
batch.put_item(Item=item)
# Verify all the items were added to dynamodb.
for obj in self.table.scan(ConsistentRead=True)['Items']:
self.assertIn(obj, items)
# Verify consumed capacity
total_cu = batch.consumed_capacity[0]['CapacityUnits']
table_cu = batch.consumed_capacity[0]['Table']['CapacityUnits']
self.assertEqual(total_cu, num_elements)
self.assertEqual(table_cu, num_elements)