forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_dynamo.py
319 lines (250 loc) · 9.9 KB
/
test_dynamo.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
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
from website import dynamo
import unittest
from unittest import mock
import os
import contextlib
class Helpers:
def __init__(self):
# Necessary to make pylint happy
self.table = None
def insert(self, *rows):
for row in rows:
self.table.create(row)
def insert_sample_data(self):
self.insert(
dict(id='key', sort=1, x=1, y=1, m=9),
dict(id='key', sort=2, x=1, y=3, m=9),
dict(id='key', sort=3, x=1, y=2, m=8))
def get_pages(self, key, **kwargs):
ret = []
p = self.table.get_many(key, **kwargs)
while True:
if p.records:
ret.append(p.records)
if not p.next_page_token: break
p = self.table.get_many(key, **kwargs, pagination_token=p.next_page_token)
return ret
class TestDynamoAbstraction(unittest.TestCase):
def setUp(self):
self.table = dynamo.Table(dynamo.MemoryStorage(), 'table', 'id')
def test_set_manipulation(self):
"""Test that adding to a set and removing from a set works."""
self.table.create(dict(
id='key',
values=set(['a', 'b', 'c']),
))
self.table.update(dict(id='key'), dict(
values=dynamo.DynamoAddToStringSet('x', 'y'),
))
final = self.table.get(dict(id='key'))
self.assertEqual(final['values'], set(['a', 'b', 'c', 'x', 'y']))
self.table.update(dict(id='key'), dict(
values=dynamo.DynamoRemoveFromStringSet('b', 'c'),
))
final = self.table.get(dict(id='key'))
self.assertEqual(final['values'], set(['a', 'x', 'y']))
def test_cannot_set_manipulate_lists(self):
"""Test that if a value originally got created as a 'list', we cannot
use set manipulation on it.
This also doesn't work in Real DynamoDB, our in-memory API shouldn't make
this appear to work.
"""
self.table.create(dict(
id='key',
values=['a', 'b', 'c'], # List instead of set
))
with self.assertRaises(TypeError):
self.table.update(dict(id='key'), dict(
values=dynamo.DynamoAddToStringSet('x', 'y'),
))
def test_sets_are_serialized_properly(self):
"""Test that adding to a set and removing from a set works."""
with with_clean_file('test.json'):
table = dynamo.Table(dynamo.MemoryStorage('test.json'), 'table', 'id')
table.create(dict(
id='key',
values=set(['a', 'b', 'c']),
))
table = dynamo.Table(dynamo.MemoryStorage('test.json'), 'table', 'id')
final = table.get(dict(id='key'))
self.assertEqual(final['values'], set(['a', 'b', 'c']))
class TestSortKeysInMemory(unittest.TestCase):
"""Test that the operations work on an in-memory table with a sort key."""
def setUp(self):
self.table = dynamo.Table(dynamo.MemoryStorage(), 'table', partition_key='id', sort_key='sort')
def test_put_and_get(self):
self.table.create(dict(
id='key',
sort='sort',
value='V',
))
ret = self.table.get(dict(id='key', sort='sort'))
self.assertEqual(ret['value'], 'V')
def test_put_and_get_many(self):
# Insert in reverse order
self.table.create(dict(id='key', sort='b', value='B'))
self.table.create(dict(id='key', sort='a', value='A'))
# Get them back in the right order
ret = list(self.table.get_many(dict(id='key')))
self.assertEqual(ret, [
dict(id='key', sort='a', value='A'),
dict(id='key', sort='b', value='B'),
])
def test_updates(self):
# Two updates to a record with a sort key
self.table.update(dict(id='key', sort='s'), dict(x='x'))
self.table.update(dict(id='key', sort='s'), dict(y='y'))
ret = self.table.get(dict(id='key', sort='s'))
self.assertEqual(ret, dict(id='key', sort='s', x='x', y='y'))
class TestQueryInMemory(unittest.TestCase, Helpers):
"""Test that the query work on an in-memory table."""
def setUp(self):
self.table = dynamo.Table(dynamo.MemoryStorage(), 'table', partition_key='id', sort_key='sort',
indexed_fields=[dynamo.IndexKey('x', 'y'), dynamo.IndexKey('m')])
def test_query(self):
self.table.create({'id': 'key', 'sort': 1, 'm': 'val'})
self.table.create({'id': 'key', 'sort': 2, 'm': 'another'})
ret = list(self.table.get_many({'id': 'key'}))
self.assertEqual(ret, [
{'id': 'key', 'sort': 1, 'm': 'val'},
{'id': 'key', 'sort': 2, 'm': 'another'}
])
def test_between_query(self):
self.table.create({'id': 'key', 'sort': 1, 'x': 'x'})
self.table.create({'id': 'key', 'sort': 2, 'x': 'y'})
self.table.create({'id': 'key', 'sort': 3, 'x': 'z'})
ret = list(self.table.get_many({
'id': 'key',
'sort': dynamo.Between(2, 5),
}))
self.assertEqual(ret, [
{'id': 'key', 'sort': 2, 'x': 'y'},
{'id': 'key', 'sort': 3, 'x': 'z'},
])
def test_query_index(self):
self.table.create({'id': 'key', 'sort': 1, 'm': 'val'})
self.table.create({'id': 'key', 'sort': 2, 'm': 'another'})
ret = list(self.table.get_many({'m': 'val'}))
self.assertEqual(ret, [
{'id': 'key', 'sort': 1, 'm': 'val'}
])
def test_query_index_with_partition_key(self):
self.table.create({'id': 'key', 'sort': 1, 'x': 'val', 'y': 0})
self.table.create({'id': 'key', 'sort': 2, 'x': 'val', 'y': 1})
self.table.create({'id': 'key', 'sort': 3, 'x': 'val', 'y': 1})
self.table.create({'id': 'key', 'sort': 4, 'x': 'another_val', 'y': 2})
ret = list(self.table.get_many({'x': 'val'}))
self.assertEqual(ret, [
{'id': 'key', 'sort': 1, 'x': 'val', 'y': 0},
{'id': 'key', 'sort': 2, 'x': 'val', 'y': 1},
{'id': 'key', 'sort': 3, 'x': 'val', 'y': 1}
])
def test_query_index_with_partition_sort_key(self):
self.table.create({'id': 'key', 'sort': 1, 'x': 'val', 'y': 0})
self.table.create({'id': 'key', 'sort': 2, 'x': 'val', 'y': 1})
self.table.create({'id': 'key', 'sort': 3, 'x': 'val', 'y': 1})
self.table.create({'id': 'key', 'sort': 4, 'x': 'another_val', 'y': 2})
ret = list(self.table.get_many({'x': 'val', 'y': 1}))
self.assertEqual(ret, [
{'id': 'key', 'sort': 2, 'x': 'val', 'y': 1},
{'id': 'key', 'sort': 3, 'x': 'val', 'y': 1}
])
def test_query_index_sort_key_between(self):
self.table.create({'id': 'key', 'sort': 1, 'x': 'val', 'y': 1})
self.table.create({'id': 'key', 'sort': 2, 'x': 'val', 'y': 3})
self.table.create({'id': 'key', 'sort': 3, 'x': 'val', 'y': 6})
ret = list(self.table.get_many({
'x': 'val',
'y': dynamo.Between(2, 5),
}))
self.assertEqual(ret, [
{'id': 'key', 'sort': 2, 'x': 'val', 'y': 3}
])
def test_paginated_query(self):
self.insert_sample_data()
pages = self.get_pages({ 'id': 'key' }, limit=1)
self.assertEqual(pages, [
[dict(id='key', sort=1, x=1, y=1, m=9)],
[dict(id='key', sort=2, x=1, y=3, m=9)],
[dict(id='key', sort=3, x=1, y=2, m=8)],
])
def test_paginated_query_reverse(self):
self.insert_sample_data()
pages = self.get_pages({ 'id': 'key' }, limit=1, reverse=True)
self.assertEqual(pages, [
[dict(id='key', sort=3, x=1, y=2, m=8)],
[dict(id='key', sort=2, x=1, y=3, m=9)],
[dict(id='key', sort=1, x=1, y=1, m=9)],
])
def test_paginated_query_on_sortkey_index(self):
self.insert_sample_data()
pages = self.get_pages({ 'x': 1 }, limit=1)
self.assertEqual(pages, [
[dict(id='key', sort=1, x=1, y=1, m=9)],
[dict(id='key', sort=3, x=1, y=2, m=8)],
[dict(id='key', sort=2, x=1, y=3, m=9)],
])
def test_paginated_query_on_sortkey_index_reverse(self):
self.insert_sample_data()
pages = self.get_pages({ 'x': 1 }, limit=1, reverse=True)
self.assertEqual(pages, [
[dict(id='key', sort=2, x=1, y=3, m=9)],
[dict(id='key', sort=3, x=1, y=2, m=8)],
[dict(id='key', sort=1, x=1, y=1, m=9)],
])
def test_paginated_query_on_partitionkey_index(self):
self.insert_sample_data()
pages = self.get_pages({ 'm': 9 }, limit=1)
self.assertEqual(pages, [
[dict(id='key', sort=1, x=1, y=1, m=9)],
[dict(id='key', sort=2, x=1, y=3, m=9)],
])
def test_paginated_query_on_partitionkey_index_reverse(self):
self.insert_sample_data()
pages = self.get_pages({ 'm': 9 }, limit=1, reverse=True)
self.assertEqual(pages, [
[dict(id='key', sort=2, x=1, y=3, m=9)],
[dict(id='key', sort=1, x=1, y=1, m=9)],
])
def test_paginated_scan(self):
self.insert(
dict(id='key', sort=1, y=1),
dict(id='key', sort=2, y=3),
dict(id='key', sort=3, y=6))
ret = self.table.scan(limit=1)
self.assertEqual(ret[0], {'id': 'key', 'sort': 1, 'y': 1})
ret = self.table.scan(limit=1, pagination_token=ret.next_page_token)
self.assertEqual(ret[0], {'id': 'key', 'sort': 2, 'y': 3})
ret = self.table.scan(limit=1, pagination_token=ret.next_page_token)
self.assertEqual(ret[0], {'id': 'key', 'sort': 3, 'y': 6})
self.assertIsNone(ret.next_page_token)
class TestSortKeysAgainstAws(unittest.TestCase):
"""Test that the operations send out appropriate Dynamo requests."""
def setUp(self):
self.db = mock.Mock()
self.table = dynamo.Table(dynamo.AwsDynamoStorage(self.db, ''), 'table', partition_key='id', sort_key='sort')
self.db.query.return_value = { 'Items': [] }
def test_between_query(self):
self.table.get_many({
'id': 'key',
'sort': dynamo.Between(2, 5),
})
self.db.query.assert_called_with(
KeyConditionExpression='#id = :id AND #sort BETWEEN :sort_min AND :sort_max',
ExpressionAttributeValues={':id': {'S': 'key'}, ':sort_min': {'N': '2'}, ':sort_max': {'N': '5'}},
ExpressionAttributeNames={'#id': 'id', '#sort': 'sort'},
TableName=mock.ANY,
ScanIndexForward=mock.ANY)
def try_to_delete(filename):
if os.path.exists(filename):
os.unlink(filename)
@contextlib.contextmanager
def with_clean_file(filename):
"""Remove file before starting, and after running.
Intended for tempfiles used in tests.
"""
try_to_delete(filename)
try:
yield
finally:
try_to_delete(filename)