-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_duo.py
285 lines (207 loc) · 9.17 KB
/
test_duo.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
# -*- coding: utf-8 -*-
"""tests -- Unit tests for duo.
Mocking AWS services is HARD. Moto is easy.
"""
from __future__ import unicode_literals
from six import with_metaclass, string_types, text_type, iteritems
try:
import unittest2 as unittest
except ImportError:
import unittest
import datetime
import boto
import moto
class DynamoDBTests(unittest.TestCase):
# Default settings for describing the table we want to work with,
# in lieu of actual values from AWS.
default_item_data = dict(
table_name = 'test_table',
hash_key_name = 'test_hash_key',
range_key_name = 'test_range_key',
hash_key_value = 'fred',
range_key_value = 'flintstone',
item_attrs = {},
key = 'foo',
secret = 'bar',
)
def setUp(self):
super(DynamoDBTests, self).setUp()
for key, value in iteritems(self.default_item_data):
setattr(self, key, value)
self.dynamo_patcher = moto.mock_dynamodb()
self.dynamo_patcher.start()
import duo
self.duo = duo
self.db = duo.DynamoDB(key=self.key, secret=self.secret)
self.schema = self.db.connection.create_schema(
hash_key_name=self.hash_key_name,
hash_key_proto_value=str,
range_key_name=self.range_key_name,
range_key_proto_value=str,
)
self.db.connection.create_table(self.table_name, self.schema, 10, 10)
def tearDown(self):
self.dynamo_patcher.stop()
class DuoTests(DynamoDBTests):
def test_getitem_on_db_should_return_table_of_given_name(self):
"""duo.DynamoDB()[name] should return a table of the given name.
"""
table = self.db[self.table_name]
self.assertIsInstance(table.table, boto.dynamodb.table.Table)
self.assertEqual(table.table_name, self.table_name)
def test_getitem_on_table_should_return_item(self):
"""duo.Table()[hash_key, range_Key] should return a duo.Item().
"""
table = self.db[self.table_name]
item = table[self.hash_key_value, self.range_key_value]
self.assertIsInstance(item, boto.dynamodb.item.Item)
self.assertIsInstance(item, self.duo.Item)
self.assertEqual(item[self.hash_key_name], self.hash_key_value)
def test_getitem_on_table_with_registered_item_subclass_should_return_subclass(self):
"""duo.Table()[hash_key, range_Key] should return the registered duo.Item subclass.
"""
class TestItemSubclass(self.duo.Item):
table_name = self.table_name
table = self.db[self.table_name]
item = table[self.hash_key_value, self.range_key_value]
self.assertIsInstance(item, TestItemSubclass)
self.assertEqual(item[self.hash_key_name], self.hash_key_value)
def test_unicode_fields_should_always_cast_to_unicode(self):
class TestItemSubclass(self.duo.Item):
table_name = self.table_name
foo = self.duo.UnicodeField()
table = self.db[self.table_name]
item = table[self.hash_key_value, self.range_key_value]
self.assertIsInstance(item, TestItemSubclass)
self.assertEqual(item[self.hash_key_name], self.hash_key_value)
item.foo = 'bar'
self.assertIsInstance(item['foo'], string_types)
self.assertEqual(item['foo'], 'bar')
self.assertEqual(item.foo, 'bar')
item.foo = 9
self.assertIsInstance(item['foo'], string_types)
self.assertEqual(item['foo'], '9')
self.assertEqual(item.foo, '9')
def test_integer_fields_should_always_cast_to_an_integer(self):
class TestItemSubclass(self.duo.Item):
table_name = self.table_name
foo = self.duo.IntField()
table = self.db[self.table_name]
item = table[self.hash_key_value, self.range_key_value]
self.assertIsInstance(item, TestItemSubclass)
self.assertEqual(item[self.hash_key_name], self.hash_key_value)
with self.assertRaises(ValueError):
item.foo = 'bar'
item.foo = 9
self.assertIsInstance(item['foo'], int)
self.assertEqual(item['foo'], 9)
def test_date_fields_should_always_cast_to_an_integer(self):
class TestItemSubclass(self.duo.Item):
table_name = self.table_name
foo = self.duo.DateField()
table = self.db[self.table_name]
item = table[self.hash_key_value, self.range_key_value]
self.assertIsInstance(item, TestItemSubclass)
self.assertEqual(item[self.hash_key_name], self.hash_key_value)
with self.assertRaises(ValueError):
item.foo = 'bar'
item.foo = today = datetime.date.today()
self.assertIsInstance(item['foo'], int)
self.assertEqual(item['foo'], today.toordinal())
self.assertEqual(item.foo, today)
def test_date_fields_should_accept_None_as_a_null_value(self):
class TestItemSubclass(self.duo.Item):
table_name = self.table_name
foo = self.duo.DateField()
table = self.db[self.table_name]
item = table[self.hash_key_value, self.range_key_value]
self.assertIsInstance(item, TestItemSubclass)
self.assertEqual(item[self.hash_key_name], self.hash_key_value)
item.foo = None
self.assertRaises(KeyError, item.__getitem__, 'foo')
self.assertEqual(item.foo, None)
def test_date_fields_should_work_with_default_of_None(self):
class TestItemSubclass(self.duo.Item):
table_name = self.table_name
foo = self.duo.DateField(default=None)
table = self.db[self.table_name]
item = table[self.hash_key_value, self.range_key_value]
self.assertIsInstance(item, TestItemSubclass)
self.assertEqual(item[self.hash_key_name], self.hash_key_value)
self.assertFalse('foo' in item)
self.assertEqual(item.foo, None)
def test_enum_classes_should_integrate_subclasses_as_enumerations(self):
class Placeholder(with_metaclass(self.duo.EnumMeta, object)): pass
class Foo(Placeholder): pass
class Bar(Placeholder): pass
class Baz(Placeholder): pass
self.assertEqual(int(Foo), 0)
self.assertEqual(text_type(Foo), 'Foo')
self.assertIs(Placeholder[0], Foo)
self.assertIs(Placeholder['Foo'], Foo)
self.assertIs(Placeholder.Foo, Foo)
self.assertEqual(int(Bar), 1)
self.assertEqual(text_type(Bar), 'Bar')
self.assertIs(Placeholder[1], Bar)
self.assertIs(Placeholder['Bar'], Bar)
self.assertIs(Placeholder.Bar, Bar)
self.assertEqual(int(Baz), 2)
self.assertEqual(text_type(Baz), 'Baz')
self.assertIs(Placeholder[2], Baz)
self.assertIs(Placeholder['Baz'], Baz)
self.assertIs(Placeholder.Baz, Baz)
def test_choice_fields_should_always_cast_to_unicode(self):
class Placeholder(with_metaclass(self.duo.EnumMeta, object)): pass
class Foo(Placeholder): pass
class Bar(Placeholder): pass
class Baz(Placeholder): pass
class TestItemSubclass(self.duo.Item):
table_name = self.table_name
place = self.duo.ChoiceField(enum_type=Placeholder)
table = self.db[self.table_name]
item = table[self.hash_key_value, self.range_key_value]
self.assertIsInstance(item, TestItemSubclass)
self.assertEqual(item[self.hash_key_name], self.hash_key_value)
with self.assertRaises(KeyError):
item.place = 'bar'
item.place = 'Bar'
self.assertIsInstance(item['place'], string_types)
self.assertEqual(item['place'], 'Bar')
self.assertIs(item.place, Bar)
def test_enum_fields_should_always_cast_to_an_int(self):
class Placeholder(with_metaclass(self.duo.EnumMeta, object)): pass
class Foo(Placeholder): pass
class Bar(Placeholder): pass
class Baz(Placeholder): pass
class TestItemSubclass(self.duo.Item):
table_name = self.table_name
place = self.duo.EnumField(enum_type=Placeholder)
table = self.db[self.table_name]
item = table[self.hash_key_value, self.range_key_value]
self.assertIsInstance(item, TestItemSubclass)
self.assertEqual(item[self.hash_key_name], self.hash_key_value)
with self.assertRaises(KeyError):
item.place = 'bar'
item.place = 'Bar'
self.assertIsInstance(item['place'], int)
self.assertEqual(item['place'], 1)
self.assertIs(item.place, Bar)
def test_enum_classes_should_compare(self):
class Placeholder(with_metaclass(self.duo.EnumMeta, object)): pass
class Foo(Placeholder): pass
class Bar(Placeholder): pass
class Baz(Placeholder): pass
self.assertEqual(Foo, 0)
self.assertEqual(Foo, 'Foo')
self.assertEqual(Foo, Foo)
self.assertLess(Foo, Bar)
self.assertEqual(Bar, 1)
self.assertEqual(Bar, 'Bar')
self.assertEqual(Bar, Bar)
self.assertGreater(Bar, Foo)
self.assertLess(Bar, Baz)
self.assertEqual(Baz, 2)
self.assertEqual(Baz, 'Baz')
self.assertEqual(Baz, Baz)
self.assertGreater(Baz, Bar)
self.assertLess(Baz, 3)