forked from seequent/properties
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_default.py
346 lines (258 loc) · 9.07 KB
/
test_default.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import random
import unittest
import uuid
import warnings
import properties
class TestDefault(unittest.TestCase):
def test_random_default(self):
class HasColor(properties.HasProperties):
col = properties.Color('a color', default='random')
hc = HasColor()
assert hc._props['col'].default == 'random'
assert hc.col != 'random'
# 1 in 1.27e130 chance this will pass if hc.col is changing every time
for _ in range(0, 100):
assert hc.col == hc.col
def test_default_order(self):
class HasIntA(properties.HasProperties):
a = properties.Integer('int a')
hi = HasIntA()
assert hi.a is None
hi.a = 5
del(hi.a)
assert hi.a is None
with self.assertRaises(ValueError):
hi.validate()
class HasIntB(properties.HasProperties):
b = properties.Integer('int b', required=False)
hi = HasIntB()
assert hi.b is None
hi.b = 5
del(hi.b)
assert hi.b is None
assert hi.validate()
class HasIntC(properties.HasProperties):
c = properties.Integer('int c', default=5)
hi = HasIntC()
assert hi.c == 5
hi.c = 10
del(hi.c)
assert hi.c is None
class HasIntClassDef(HasIntC):
_defaults = {'c': 100}
hi = HasIntClassDef()
assert hi.c == 100
hi.c = 10
del(hi.c)
assert hi.c is None
with self.assertRaises(AttributeError):
class HasIntCError(HasIntC):
_defaults = {'z': 100}
HasIntCError()
class HasIntD(properties.HasProperties):
d = properties.Integer('int d', default=5, required=False)
hi = HasIntD()
assert hi.d == 5
hi.d = 10
del(hi.d)
assert hi.d is None
class NewDefInt(properties.Integer):
_class_default = 1000
class HasIntE(properties.HasProperties):
e = NewDefInt('int e')
hi = HasIntE()
assert hi.e == 1000
hi.e = 10
del(hi.e)
assert hi.e is None
class HasIntF(properties.HasProperties):
f = NewDefInt('int e', default=5)
hi = HasIntF()
assert hi.f == 5
hi.f = 10
del(hi.f)
assert hi.f is None
class HasIntFandG(HasIntF):
_defaults = {'f': 20, 'g': 25}
g = properties.Integer('int g', default=10)
hi = HasIntFandG()
assert hi.f == 20
assert hi.g == 25
hi = HasIntFandG(g=12)
assert hi.f == 20
assert hi.g == 12
class HasIntFGH(HasIntFandG):
h = NewDefInt('int h')
_defaults = dict(f=30)
hi = HasIntFGH()
assert hi.f == 30
assert hi.g == 25
assert hi.h == 1000
with self.assertRaises(AttributeError):
class BadDefault(HasIntFGH):
_defaults = dict(f='hi')
class HasIntFGHDefs(HasIntFGH):
_defaults = dict(h=-10)
hi = HasIntFGHDefs()
assert hi.f == 30
assert hi.g == 25
assert hi.h == -10
def test_union_default(self):
class HasUnionA(properties.HasProperties):
a = properties.Union('union', (properties.Integer(''),
properties.String('')))
hu = HasUnionA()
assert hu.a is None
hu.a = 5
hu.a = 'hi'
del(hu.a)
assert hu.a is None
class HasUnionB(properties.HasProperties):
b = properties.Union('union', (properties.Integer('', default=5),
properties.String('')))
hu = HasUnionB()
assert hu.b == 5
hu.b = 'hi'
del(hu.b)
assert hu.b is None
class HasUnionC(properties.HasProperties):
c = properties.Union('union', (
properties.Integer(''),
properties.String('', default='hi'),
properties.Integer(''))
)
hu = HasUnionC()
assert hu.c == 'hi'
hu.c = 5
del(hu.c)
assert hu.c is None
class HasUnionD(properties.HasProperties):
d = properties.Union('union', (
properties.Integer(''),
properties.String(''),
properties.Integer('')
), default=100)
hu = HasUnionD()
assert hu.d == 100
hu.d = 5
del(hu.d)
assert hu.d is None
with self.assertRaises(TypeError):
properties.Union(
'union',
(properties.Integer(''), properties.Bool('')),
default=0.5
)
with warnings.catch_warnings(record=True) as w:
properties.Union('union', (properties.Integer('', default=5),
properties.Bool('', default=True)))
assert len(w) == 1
assert issubclass(w[0].category, RuntimeWarning)
with warnings.catch_warnings(record=True) as w:
properties.Union('union', (properties.Integer('', default=5),
properties.Bool('', default=True)),
default=False)
assert len(w) > 0
assert issubclass(w[0].category, RuntimeWarning)
def twelve():
return 12
HasUnionD._props['d'].default = twelve
hu = HasUnionD()
assert hu.d == 12
HasUnionD._props['d'].default = properties.undefined
hu = HasUnionD()
assert hu.d is None
def test_instance_default(self):
class HasInt(properties.HasProperties):
a = properties.Integer('int a')
class HasInstance(properties.HasProperties):
inst = properties.Instance('has int instance', HasInt,
auto_create=True)
hi0 = HasInstance()
hi1 = HasInstance()
assert isinstance(hi0.inst, HasInt)
assert isinstance(hi1.inst, HasInt)
assert hi0.inst is not hi1.inst
hi0.inst.a = 5
assert hi1.inst.a is None
del hi0.inst
assert hi0.inst is None
class HasIntSubclass(HasInt):
pass
class HasInstanceSubclass(HasInstance):
_defaults = {'inst': HasIntSubclass}
hi2 = HasInstanceSubclass()
assert isinstance(hi2.inst, HasIntSubclass)
class HasList(properties.HasProperties):
z = properties.List('z list', HasInstance)
hl0 = HasList()
hl1 = HasList()
assert isinstance(hl0.z, list)
assert isinstance(hl1.z, list)
assert hl0.z is not hl1.z
def test_list_default(self):
class HasIntList(properties.HasProperties):
intlist = properties.List('list of ints', properties.Integer(''))
hil = HasIntList()
assert isinstance(hil.intlist, list)
assert len(hil.intlist) == 0
assert hil.intlist is not HasIntList().intlist
with warnings.catch_warnings(record=True) as w:
properties.List('list', properties.Integer('', default=5))
assert len(w) == 1
assert issubclass(w[0].category, RuntimeWarning)
def test_reset(self):
class HasInts(properties.HasProperties):
_defaults = {'b': 10}
a = properties.Integer('int a', default=1)
b = properties.Integer('int b')
@properties.observer('a')
def _set_b_to_five(self, change):
self.b = 5
hi = HasInts()
assert hi.a == 1
assert hi.b == 10
del hi.a
assert hi.a is None
assert hi.b == 5
hi._reset('b')
assert hi.b == 10
with properties.listeners_disabled():
hi._reset('a')
assert hi.a == 1
assert hi.b == 10
with self.assertRaises(AttributeError):
hi._reset('c')
class HasUid(properties.HasProperties):
uid = properties.Uuid('uid')
hu = HasUid()
with self.assertRaises(AttributeError):
hu._reset('uid')
def test_callable(self):
class HasUid(properties.HasProperties):
uid = properties.Uuid('uid')
class HasUidZero(HasUid):
_defaults = {'uid': lambda: uuid.UUID(int=0)}
huz = HasUidZero()
assert (properties.Uuid.to_json(huz.uid) ==
'00000000-0000-0000-0000-000000000000')
NUMBER = 1
def generate_int():
return NUMBER
class HasInt(properties.HasProperties):
a = properties.Integer('an int', default=generate_int)
hi = HasInt()
assert hi.a == 1
NUMBER = 2
hi._reset('a')
assert hi.a == 2
class HasNewInt(HasInt):
_defaults = {'a': lambda: generate_int()+1}
hi = HasNewInt()
assert hi.a == 3
if __name__ == '__main__':
unittest.main()