-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_datastructures.py
529 lines (382 loc) · 16.4 KB
/
test_datastructures.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# stdlib imports
import sys
import unittest
from zope import interface
from zope.testing.cleanup import CleanUp
from nti.externalization.tests import ExternalizationLayerTest
from nti.testing.matchers import is_false
from nti.testing.matchers import is_true
from hamcrest import assert_that
from hamcrest import has_property
from hamcrest import is_
from hamcrest import is_not as does_not
from hamcrest import none
# disable: accessing protected members, too many methods
# pylint: disable=W0212,R0904
# pylint: disable=inherit-non-class,attribute-defined-outside-init,abstract-method
# pylint:disable=no-value-for-parameter,too-many-function-args
class CommonTestMixins(object):
def _makeOne(self, context=None):
raise NotImplementedError()
def test_ext_getattr_default(self):
io = self._makeOne()
assert_that(io._ext_getattr(self, 'no_such_attribute', None),
is_(none()))
def test_ext_getattr_no_default(self):
io = self._makeOne()
get = io._ext_getattr
with self.assertRaises(AttributeError):
get(self, 'no_such_attribute')
def test_ext_replacement_getattr_default(self):
io = self._makeOne()
assert_that(io._ext_replacement_getattr('no_such_attribute', None),
is_(none()))
class TestAbstractDynamicObjectIO(CommonTestMixins,
ExternalizationLayerTest):
def _makeOne(self, context=None):
from nti.externalization.datastructures import AbstractDynamicObjectIO
class IO(AbstractDynamicObjectIO):
_ext_setattr = staticmethod(setattr)
_ext_getattr = staticmethod(getattr)
def _ext_all_possible_keys(self):
return frozenset(self.__dict__)
return IO()
def test_ext_dict_key_already_exists(self):
inst = self._makeOne()
inst.Creator = inst.creator = "creator"
result = inst.toExternalDictionary()
assert_that(result,
is_({u'Class': 'IO', u'Creator': u'creator'}))
inst._excluded_out_ivars_ = frozenset()
result = inst.toExternalDictionary()
assert_that(result,
is_({u'Class': 'IO', u'Creator': u'creator', 'creator': 'creator'}))
def test_ext_dict_primitive_keys_bypass_toExternalObject(self):
# XXX: We used to monkey-patch datastructures.toExternalObject to
# make it explode if called, but we can't do that anymore, so this
# may not be testing what we think it is.
inst = self._makeOne()
inst._ext_primitive_out_ivars_ = frozenset({'ivar',})
inst.ivar = 42
result = inst.toExternalDictionary()
assert_that(result,
is_({'Class': 'IO', 'ivar': 42}))
def test_ext_dict_sets_parent_on_replacement_value(self):
class InnerDict(object):
def toExternalObject(self, **kw):
return {'key': 42}
class InnerList(object):
def toExternalObject(self, **kw):
return [76]
inst = self._makeOne()
inst.ivar = [InnerDict(), InnerList()]
inst.dict = InnerDict()
inst.list = InnerList()
result = inst.toExternalDictionary()
assert_that(result,
is_({
u'Class': 'IO',
'ivar': [{'key': 42}, [76]],
'dict': {'key': 42},
'list': [76],
}))
assert_that(result['ivar'], has_property('__parent__', inst))
assert_that(result['ivar'][0], does_not(has_property('__parent__', inst)))
assert_that(result['ivar'][1], does_not(has_property('__parent__', inst)))
assert_that(result['dict'], does_not(has_property('__parent__', inst)))
assert_that(result['list'], does_not(has_property('__parent__', inst)))
def test_ext_dict_prefer_oid(self):
merge_from = {'OID': 'oid', 'ID': 'id'}
inst = self._makeOne()
result = inst.toExternalDictionary(mergeFrom=merge_from)
assert_that(result,
is_({u'Class': 'IO', 'OID': 'oid', 'ID': 'id'}))
inst._prefer_oid_ = True
result = inst.toExternalDictionary(mergeFrom=merge_from)
assert_that(result,
is_({u'Class': 'IO', 'OID': 'oid', 'ID': 'oid'}))
def test_ext_accept_external_id_false(self):
inst = self._makeOne()
assert_that(inst._ext_accept_external_id(self, self), is_false())
def test_update_takes_external_fields(self):
parsed = {'ContainerId': 'container',
'Creator': 'creator',
'ID': 'id'}
inst = self._makeOne()
inst.creator = None
inst.id = None
inst.containerId = None
inst.updateFromExternalObject(parsed)
assert_that(inst, has_property('creator', 'creator'))
assert_that(inst, has_property('containerId', 'container'))
# We default to ignoring external id
assert_that(inst, has_property('id', none()))
inst._ext_accept_external_id = lambda *args: True
inst.updateFromExternalObject(parsed)
assert_that(inst, has_property('id', 'id'))
class TestInterfaceObjectIO(CleanUp,
CommonTestMixins,
unittest.TestCase):
def _getTargetClass(self):
from nti.externalization.datastructures import InterfaceObjectIO
return InterfaceObjectIO
def _makeOne(self, context=None, iface_upper_bound=interface.Interface):
return self._getTargetClass()(context, iface_upper_bound=iface_upper_bound)
def test_find_primitive_keys_dne(self):
ext_self = self
inst = self._makeOne(ext_self, iface_upper_bound=interface.Interface)
result = inst._ext_find_primitive_keys()
assert_that(inst, has_property('schema', interface.Interface))
assert_that(result, is_(set()))
def test_find_primitive_keys_plain_attribute(self):
class I(interface.Interface):
ivar = interface.Attribute("An attribute")
@interface.implementer(I)
class O(object):
pass
ext_self = O()
inst = self._makeOne(ext_self, iface_upper_bound=I)
possible_keys = inst._ext_all_possible_keys()
assert_that(possible_keys, is_({'ivar'}))
result = inst._ext_find_primitive_keys()
assert_that(result, is_(set()))
def test_find_primitive_keys_float(self):
from zope.schema import Float
class I(interface.Interface):
ivar = Float() # a plain _type is allowed
@interface.implementer(I)
class O(object):
pass
ext_self = O()
inst = self._makeOne(ext_self, iface_upper_bound=I)
possible_keys = inst._ext_all_possible_keys()
assert_that(possible_keys, is_({'ivar'}))
result = inst._ext_find_primitive_keys()
assert_that(result, is_({'ivar'}))
def test_find_primitive_keys_number(self):
from zope.schema import Float
class Number(Float):
_type = (int, float) # a tuple is allowed
class I(interface.Interface):
ivar = Number()
@interface.implementer(I)
class O(object):
pass
ext_self = O()
inst = self._makeOne(ext_self, iface_upper_bound=I)
possible_keys = inst._ext_all_possible_keys()
assert_that(possible_keys, is_({'ivar'}))
result = inst._ext_find_primitive_keys()
assert_that(result, is_({'ivar'}))
def test_ext_getattr(self):
ext_self = self
inst = self._makeOne(ext_self, iface_upper_bound=interface.Interface)
assert_that(self._makeOne, is_(inst._ext_getattr(ext_self, '_makeOne')))
def test_ext_accept_external_id_no_field(self):
# If there is no id field, the value is false
class I(interface.Interface):
pass
@interface.implementer(I)
class O(object):
pass
ext_self = O()
inst = self._makeOne(ext_self, iface_upper_bound=I)
assert_that(inst._ext_accept_external_id(ext_self, None), is_false())
def test_ext_accept_external_id_no_tagged_value(self):
# If there is no id field, the value is false
class I(interface.Interface):
id = interface.Attribute("An id")
@interface.implementer(I)
class O(object):
pass
ext_self = O()
inst = self._makeOne(ext_self, iface_upper_bound=I)
assert_that(inst._ext_accept_external_id(ext_self, None), is_false())
def test_ext_accept_external_id_tagged_value_true(self):
# If there is no id field, the value is false
class I(interface.Interface):
id = interface.Attribute("An id")
id.setTaggedValue('__external_accept_id__', True)
@interface.implementer(I)
class O(object):
pass
ext_self = O()
inst = self._makeOne(ext_self, iface_upper_bound=I)
assert_that(inst._ext_accept_external_id(ext_self, None), is_true())
def test_toExternalObject_trivial(self):
ext_self = self
inst = self._makeOne(ext_self, iface_upper_bound=interface.Interface)
result = inst.toExternalObject()
assert_that(result,
is_({'Class': self.__class__.__name__}))
def test_toExternalObject_external_class_name_iro(self):
class IBruce(interface.Interface):
interface.taggedValue('__external_class_name__', 'Batman')
class IDick(IBruce):
pass
@interface.implementer(IBruce)
class Bruce(object):
pass
@interface.implementer(IDick)
class Dick(object):
pass
bruce = Bruce()
dick = Dick()
bruce_inst = self._makeOne(bruce, iface_upper_bound=interface.Interface)
result = bruce_inst.toExternalObject()
assert_that(result,
is_({'Class': 'Batman'}))
# there's no tagged value, so we get the same thing
dick_inst = self._makeOne(dick, iface_upper_bound=interface.Interface)
result = dick_inst.toExternalObject()
assert_that(result,
is_({'Class': 'Batman'}))
# The actual interface gets passed if it is a callable
def callable_ext_class_name(iface, ext_self):
if iface is IDick:
return 'Boy Wonder'
return 'Dark Knight'
IBruce.setTaggedValue('__external_class_name__', callable_ext_class_name)
result = bruce_inst.toExternalObject()
assert_that(result,
is_({'Class': 'Dark Knight'}))
result = dick_inst.toExternalObject()
assert_that(result,
is_({'Class': 'Boy Wonder'}))
# And we can override in IDick
IDick.setTaggedValue('__external_class_name__', 'Robin')
result = dick_inst.toExternalObject()
assert_that(result,
is_({'Class': 'Robin'}))
def test_updateFromExternalObject_trivial(self):
class O(object):
pass
ext_self = O()
inst = self._makeOne(ext_self, iface_upper_bound=interface.Interface)
# No values, nothing to do.
inst.updateFromExternalObject({})
def test_updateFromExternalObject_float(self):
from zope.schema import Float
from zope.schema.interfaces import RequiredMissing
from zope.schema.interfaces import WrongType
class I(interface.Interface):
ivar = Float(required=True) # a plain _type is allowed
@interface.implementer(I)
class O(object):
ivar = None
ext_self = O()
inst = self._makeOne(ext_self, iface_upper_bound=I)
# A literal is fine
inst.updateFromExternalObject({'ivar': 1.0})
assert_that(ext_self, has_property('ivar', 1.0))
# A text string is fine
inst.updateFromExternalObject({'ivar': u'2.0'})
assert_that(ext_self, has_property('ivar', 2.0))
# A byte string is NOT fine
with self.assertRaises(WrongType):
inst.updateFromExternalObject({'ivar': b'3.0'})
assert_that(ext_self, has_property('ivar', 2.0))
# Now a validation error after set
ext_self = O()
inst = self._makeOne(ext_self, iface_upper_bound=I)
with self.assertRaises(RequiredMissing):
inst.updateFromExternalObject({})
def test_updateFromExternalObject_Object(self):
from zope.schema import Object
from zope.schema.interfaces import RequiredMissing
from zope.schema.interfaces import SchemaNotProvided
class IRequired(interface.Interface):
pass
class I(interface.Interface):
ivar = Object(IRequired)
@interface.implementer(I)
class O(object):
ivar = None
ext_self = O()
inst = self._makeOne(ext_self, iface_upper_bound=I)
with self.assertRaises(RequiredMissing):
inst.updateFromExternalObject({})
with self.assertRaises(SchemaNotProvided):
inst.updateFromExternalObject({'ivar': self})
@interface.implementer(IRequired)
class Required(object):
pass
inst.updateFromExternalObject({'ivar': Required()})
def test_find_factory_for_named_value(self):
from zope import component
from zope.schema import Int
class I(interface.Interface):
field = Int()
@interface.implementer(I)
class O(object):
pass
inst = self._makeOne(O(), iface_upper_bound=I)
# Key not in schema: not an error (XXX should it be?)
assert_that(inst.find_factory_for_named_value('missing', {}, component),
is_(none()))
# object that's not-string: not an error (XXX: should it be? we don't
# test for 'callable' for performance)
I['field'].setTaggedValue('__external_factory__', self)
assert_that(inst.find_factory_for_named_value('field', {}, component),
is_(self))
# string object is looked up as a utility
I['field'].setTaggedValue('__external_factory__', 'some factory')
with self.assertRaises(component.ComponentLookupError):
inst.find_factory_for_named_value('field', {}, component)
class TestModuleScopedInterfaceObjectIO(TestInterfaceObjectIO):
def _getTargetClass(self):
from nti.externalization.datastructures import ModuleScopedInterfaceObjectIO
class IO(ModuleScopedInterfaceObjectIO):
_ext_search_module = sys.modules[__name__]
return IO
def test_finding_linear_interface(self):
class IRoot(interface.Interface):
pass
class IChild(IRoot):
pass
class IGrandChild(IChild):
pass
class ISister(IRoot):
pass
@interface.implementer(IGrandChild, ISister)
class Inconsistent(object):
pass
mod = sys.modules[__name__]
class IO(self._getTargetClass()):
_ext_search_module = mod
with self.assertRaises(TypeError):
IO(Inconsistent())
@interface.implementer(ISister)
class Sister(object):
pass
@interface.implementer(IGrandChild)
class InconsistentGrandChild(Sister):
pass
with self.assertRaises(TypeError):
IO(InconsistentGrandChild())
@interface.implementer(IGrandChild)
class Consistent(object):
pass
io = IO(Consistent())
assert_that(io, has_property('_iface', IGrandChild))
class TestExternalizableInstanceDict(CommonTestMixins,
unittest.TestCase):
def _makeOne(self, context=None):
from nti.externalization.datastructures import ExternalizableInstanceDict
class FUT(ExternalizableInstanceDict):
def _ext_replacement(self):
return context
return FUT()
def test_can_also_subclass_persistent(self):
from nti.externalization.datastructures import ExternalizableInstanceDict
from persistent import Persistent
class Base(ExternalizableInstanceDict):
pass
class P(Base, Persistent):
pass
self.assertIsNotNone(P)