-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_thingy.py
407 lines (262 loc) · 8.62 KB
/
test_thingy.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
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
from collections import OrderedDict
from pytest import fixture, raises
from thingy import DatabaseThingy, NamesMixin, Thingy, View, classproperty, registry
def test_classproperty():
class Test:
_foo = "bar"
@classproperty
def foo(cls):
return cls._foo
assert Test.foo == "bar"
def test_classproperty_vs_attribute_conflicts():
class Foo(Thingy):
_foo = "bar"
@classproperty
def foo(cls):
return cls._foo
assert Foo.foo == "bar"
foo = Foo()
assert foo.foo is None
foo._silent = False
with raises(AttributeError):
foo.foo
foo = Foo(foo="baz")
assert foo._foo == "bar"
assert foo.foo == "baz"
def test_property_vs_attribute_conflicts():
class Foo(Thingy):
_foo = "bar"
@property
def foo(self):
return self._foo
foo = Foo()
assert foo.foo == "bar"
with raises(AttributeError):
foo = Foo(foo="baz")
foo = Foo({"foo": "baz"})
assert foo._foo == "bar"
assert foo.foo == "bar"
assert foo.__dict__["foo"] == "baz"
def test_view_return_type():
view = View()
assert view(None) == {}
view = View(ordered=True)
assert view(None) == OrderedDict()
@fixture
def TestThingy():
class TestThingy(Thingy):
@property
def foobaz(self):
return self.foo + self.baz
return TestThingy
@fixture
def TestDatabaseThingy():
class TestDatabaseThingy(DatabaseThingy):
pass
return TestDatabaseThingy
def test_init_with_dict():
thingy = Thingy({"foo": "bar", "baz": "qux"})
assert thingy.foo == "bar"
assert thingy.baz == "qux"
def test_init_with_args():
thingy = Thingy(foo="bar", baz="qux")
assert thingy.foo == "bar"
assert thingy.baz == "qux"
def test_init_mixed():
thingy = Thingy({"foo": "bar"}, baz="qux")
assert thingy.foo == "bar"
assert thingy.baz == "qux"
def test_eq():
thingy = Thingy(foo="bar")
other_thingy = Thingy(foo="bar")
assert thingy == other_thingy
assert thingy != {"foo": "bar"}
def test_repr():
thingy = eval(repr(Thingy(bar="baz")))
assert isinstance(thingy, Thingy)
assert thingy.bar == "baz"
def test_update_with_dict():
thingy = Thingy(foo="bar", baz="qux")
thingy.update({"foo": "BAR"})
assert thingy.foo == "BAR"
assert thingy.baz == "qux"
def test_update_with_args():
thingy = Thingy({"foo": "bar", "baz": "qux"})
thingy.update(foo="BAR", baz="QUX")
assert thingy.foo == "BAR"
assert thingy.baz == "QUX"
def test_default_view():
thingy = Thingy(foo="bar", baz="qux")
assert thingy.view() == {"foo": "bar", "baz": "qux"}
def test_missing_view():
thingy = Thingy()
with raises(KeyError):
thingy.view("nop")
def test_property_exceptions():
class ScreamingThingy(Thingy):
@property
def foo(self):
raise AttributeError("Foo!")
thingy = ScreamingThingy()
with raises(AttributeError) as excinfo:
thingy.foo
assert str(excinfo.value) == "Foo!"
class ScreamingThingyChild(ScreamingThingy):
pass
thingy = ScreamingThingyChild()
with raises(AttributeError) as excinfo:
thingy.foo
assert str(excinfo.value) == "Foo!"
def test_silence():
thingy = Thingy()
assert thingy.foo is None
class ScreamingThingy(Thingy):
_silent = False
thingy = ScreamingThingy()
with raises(AttributeError):
thingy.foo
def test_add_view(TestThingy):
TestThingy.add_view("test")
thingy = TestThingy(foo="bar", baz="qux")
assert thingy.view() == {"foo": "bar", "baz": "qux"}
assert thingy.view("test") == {}
def test_add_view_override(TestThingy):
TestThingy.add_view("defaults", defaults=False)
thingy = TestThingy(foo="bar", baz="qux")
assert not thingy.view()
def test_add_view_defaults(TestThingy):
TestThingy.add_view("test", defaults=True)
thingy = TestThingy(foo="bar", baz="qux")
assert thingy.view("test") == {"foo": "bar", "baz": "qux"}
def test_add_view_include(TestThingy):
TestThingy.add_view("test", include="foo")
thingy = TestThingy(foo="bar", baz="qux")
assert thingy.view("test") == {"foo": "bar"}
def test_add_view_include_list(TestThingy):
TestThingy.add_view("test", include=["foo", "baz"])
thingy = TestThingy(foo="bar", baz="qux")
assert thingy.view("test") == {"foo": "bar", "baz": "qux"}
def test_add_view_exclude(TestThingy):
TestThingy.add_view("test", defaults=True, exclude="foo")
thingy = TestThingy(foo="bar", baz="qux")
assert thingy.view("test") == {"baz": "qux"}
def test_add_view_exclude_list(TestThingy):
TestThingy.add_view("test", defaults=True, exclude=["foo", "baz"])
thingy = TestThingy(foo="bar", baz="qux")
assert thingy.view("test") == {}
def test_tuple_aliases(TestThingy):
TestThingy.add_view("test", include=[("foo", "FOO")])
thingy = TestThingy(foo="bar", baz="qux")
assert thingy.view("test") == {"FOO": "bar"}
def test_names():
class FooBar(NamesMixin):
pass
assert FooBar.names == ["foo", "bar"]
class FooBar(NamesMixin, Thingy):
pass
assert FooBar.names == ["foo", "bar"]
def test_names_with_abbreviation():
class FOOBarQux(NamesMixin):
pass
assert FOOBarQux.names == ["foo", "bar", "qux"]
class BarFOOQux(NamesMixin):
pass
assert BarFOOQux.names == ["bar", "foo", "qux"]
class BarQuxFOO(NamesMixin):
pass
assert BarQuxFOO.names == ["bar", "qux", "foo"]
def test_database_names():
assert DatabaseThingy.names == ["database", "thingy"]
def test_database_name_from_class():
class DatabaseTable(DatabaseThingy):
pass
assert DatabaseTable.database_name == "database"
def test_database_name_from_database():
class DatabaseTable(DatabaseThingy):
_database = True
assert DatabaseTable.database_name is None
def test_database_name_from_attribute():
class DatabaseTable(DatabaseThingy):
_database_name = "foo"
assert DatabaseTable.database_name == "foo"
def test_database_name_priority():
class DatabaseTable(DatabaseThingy):
_database = True
_database_name = "foo"
assert DatabaseTable.database_name is None
def test_table_name_from_class():
class DatabaseTable(DatabaseThingy):
pass
assert DatabaseTable.table_name == "table"
def test_table_name_from_class_with_database():
class DatabaseTable(DatabaseThingy):
_database = True
assert DatabaseTable.table_name == "database_table"
def test_table_name_from_class_with_database_name():
class DatabaseTable(DatabaseThingy):
_database_name = "foo"
assert DatabaseTable.table_name == "database_table"
def test_table_name_from_table():
class DatabaseTable(DatabaseThingy):
_table = True
assert DatabaseTable.table_name is None
def test_table_name_from_attribute():
class DatabaseTable(DatabaseThingy):
_table_name = "foo"
assert DatabaseTable.table_name == "foo"
def test_table_name_priority():
class DatabaseTable(DatabaseThingy):
_table = True
_table_name = "foo"
assert DatabaseTable.table_name is None
def test_undefined_database():
class Table(DatabaseThingy):
pass
assert Table.database_name is None
with raises(AttributeError):
Table.database
class Table(DatabaseThingy):
_table = True
with raises(AttributeError):
Table.database
class DatabaseTable(DatabaseThingy):
pass
with raises(AttributeError):
DatabaseTable.database
def test_undefined_table():
class Table(DatabaseThingy):
pass
with raises(AttributeError):
Table.table
class Table(DatabaseThingy):
_database = True
with raises(AttributeError):
Table.table
def test_database():
class DatabaseTable(DatabaseThingy):
_database = True
assert DatabaseTable.database is True
class DatabaseTable(DatabaseThingy):
_table = True
@classmethod
def _get_database(cls, table, name):
return table
assert DatabaseTable.database is True
def test_table():
class DatabaseTable(DatabaseThingy):
_table = True
assert DatabaseTable.table is True
class DatabaseTable(DatabaseThingy):
_database = True
@classmethod
def _get_table(cls, database, name):
return database
assert DatabaseTable.table is True
def test_registry():
del registry[:]
class Foo(Thingy):
pass
assert registry == [Foo]
class Bar(Thingy):
pass
assert registry == [Foo, Bar]