-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_site.py
624 lines (466 loc) · 21.1 KB
/
test_site.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals, absolute_import, division
__docformat__ = "restructuredtext en"
# disable: accessing protected members, too many methods
# pylint: disable=W0212,R0904
from hamcrest import is_
from hamcrest import is_not
from hamcrest import raises
from hamcrest import calling
from hamcrest import contains
from hamcrest import assert_that
from hamcrest import has_property
from hamcrest import same_instance
from hamcrest import none
from hamcrest import not_none
from hamcrest import has_length
does_not = is_not
import unittest
from zope import interface
from zope.interface import ro
from zope.interface import implementedBy
from zope.interface import Interface
from zope.interface.interfaces import ComponentLookupError
from zope.component.hooks import getSite
from zope.component.hooks import setSite
from zope.component.hooks import site as currentSite
from zope.component.interfaces import IComponents
from zope.location.interfaces import LocationError
from zope.component import globalSiteManager as BASE
from z3c.baseregistry.baseregistry import BaseComponents
from nti.site.folder import HostSitesFolder
from nti.site.interfaces import IHostPolicyFolder
from nti.site.subscribers import threadSiteSubscriber
from nti.site.transient import HostSiteManager as HSM
from nti.site.transient import TrivialSite
from nti.site.site import get_site_for_site_names
from nti.site.site import find_site_components
from nti.site.site import get_component_hierarchy
from nti.site.site import get_component_hierarchy_names
from nti.site.tests import SharedConfiguringTestLayer
from nti.testing.matchers import validly_provides
from nti.testing.matchers import is_false
from nti.testing.base import AbstractTestBase
from persistent import Persistent
import fudge
class IMock(Interface):
pass
@interface.implementer(IMock)
class MockSite(object):
__name__ = None
__parent__ = None
def __init__(self, site_man=None):
self.site_man = site_man
def getSiteManager(self):
return self.site_man
class IFoo(Interface):
pass
@interface.implementer(IFoo)
class RootFoo(object):
pass
class TestSiteSubscriber(unittest.TestCase):
layer = SharedConfiguringTestLayer
def testProxyHostComps(self):
pers_comps = BaseComponents(BASE, 'persistent', (BASE,))
host_comps = BaseComponents(BASE, 'example.com', (BASE,))
host_sm = HSM('example.com', 'siteman', host_comps, pers_comps)
host_site = MockSite(host_sm)
host_site.__name__ = host_sm.__name__
setSite(host_site)
new_comps = BaseComponents(BASE, 'sub_site', (pers_comps,))
new_site = MockSite(new_comps)
new_site.__name__ = new_comps.__name__
interface.alsoProvides(new_site, IFoo)
threadSiteSubscriber(new_site, None)
cur_site = getSite()
# It should implement the static and dynamic
# ifaces
assert_that(cur_site, validly_provides(IFoo))
assert_that(cur_site, validly_provides(IMock))
# It should have the marker property
assert_that(cur_site.getSiteManager(),
has_property('host_components',
host_comps))
assert_that(ro.ro(cur_site.getSiteManager()),
contains(
# The first entry is synthesized
has_property('__name__', new_comps.__name__),
pers_comps,
# The host comps appear after all the bases
# in the ro of the new site
host_comps,
BASE))
def testTraverseFailsIntoSiblingSiteExceptHostPolicyFolders(self):
new_comps = BaseComponents(BASE, 'sub_site', ())
new_site = MockSite(new_comps)
new_site.__name__ = new_comps.__name__
with currentSite(None):
threadSiteSubscriber(new_site, None)
# If we walk into a site...
# ...and then try to walk into a sibling site with no apparent relationship...
new_comps2 = BaseComponents(BASE, 'sub_site', (new_comps,))
new_site2 = MockSite(new_comps2)
new_site2.__name__ = new_comps2.__name__
# ... we fail...
assert_that(calling(threadSiteSubscriber).with_args(new_site2, None),
raises(LocationError))
# ...unless they are both HostPolicyFolders...
interface.alsoProvides(new_site, IHostPolicyFolder)
interface.alsoProvides(new_site2, IHostPolicyFolder)
threadSiteSubscriber(new_site2, None)
# ... which does not change the site
assert_that(getSite(), is_(same_instance(new_site)))
def test_components_unregister_on_site_removal(self):
site_folder = HostSitesFolder()
new_site = MockSite()
interface.alsoProvides(new_site, IHostPolicyFolder)
key = 'new_site_name'
site_folder[key] = new_site
new_site_components = BaseComponents(BASE, 'new_site_name', (BASE,))
current_site_manager = getSite().getSiteManager()
current_site_manager.registerUtility(new_site_components,
name=key,
provided=IComponents)
assert_that(current_site_manager.queryUtility(IComponents, name=key),
not_none())
del site_folder[key]
assert_that(current_site_manager.queryUtility(IComponents, name=key),
none())
# Safe without registered components
site_folder[key] = new_site
del site_folder[key]
class TestGetSiteForSiteNames(AbstractTestBase):
@fudge.patch('nti.site.site.find_site_components')
def test_no_persistent_site(self, fake_find):
pers_comps = BaseComponents(BASE, 'persistent', (BASE,))
host_comps = BaseComponents(BASE, 'example.com', (BASE,))
host_sm = HSM('example.com', 'siteman', host_comps, pers_comps)
class PersistentTrivialSite(Persistent, TrivialSite):
pass
trivial_site = PersistentTrivialSite(host_sm)
fake_find.is_callable().returns(pers_comps)
x = get_site_for_site_names(('',), trivial_site)
assert_that(x, is_not(Persistent))
assert_that(x, is_(TrivialSite))
assert_that(x.__name__, is_(pers_comps.__name__))
def test_find_comps_empty(self):
assert_that(find_site_components(('',)),
is_(none()))
class TestGetComponentHierarchy(AbstractTestBase):
@fudge.patch('nti.site.site.find_site_components')
def test_get_component_hierarchy(self, fake_find):
# This is a lousy test, just testing what we already see done, not
# correct behaviour
pers_comps = BaseComponents(BASE, 'persistent', (BASE,))
class MockSite(object):
__parent__ = None
__name__ = None
def __init__(self):
self.__parent__ = {}
site_comps_1 = BaseComponents(pers_comps, '1', (pers_comps,))
site_comps_2 = BaseComponents(site_comps_1, '2', (site_comps_1,))
site = MockSite()
site.__parent__[site_comps_1.__name__] = site_comps_1
site.__parent__[site_comps_2.__name__] = site_comps_2
fake_find.is_callable().returns(site_comps_2)
x = list(get_component_hierarchy(site))
assert_that(x, is_([site_comps_2, site_comps_1]))
x = list(get_component_hierarchy_names(site))
assert_that(x, is_(['2', '1']))
x = list(get_component_hierarchy_names(site, reverse=True))
assert_that(x, is_(['1', '2']))
from nti.site.site import BTreeLocalSiteManager as BLSM
from nti.site.site import _LocalAdapterRegistry
from nti.site.site import BTreeLocalAdapterRegistry
from ZODB import DB
from ZODB.DemoStorage import DemoStorage
import transaction
import pickle
try:
import zodbpickle.fastpickle as zpickle
except ImportError:
import zodbpickle.pickle as zpickle # pypy
import BTrees.OOBTree
class TestBTreeSiteMan(AbstractTestBase):
def test_pickle_setstate_swap_class(self):
base_comps = BLSM(None)
# replace with "broken"
base_comps.adapters = _LocalAdapterRegistry()
base_comps.utilities = _LocalAdapterRegistry()
sub_comps = BLSM(None)
sub_comps.__bases__ = (base_comps,)
assert_that(sub_comps.adapters.__bases__, is_((base_comps.adapters,)))
assert_that(sub_comps.utilities.__bases__, is_((base_comps.utilities,)))
for p in pickle, zpickle:
new_base, new_sub = p.loads(p.dumps([base_comps, sub_comps]))
# Still in place
assert_that(new_sub.adapters.__bases__, is_((new_base.adapters,)))
assert_that(new_sub.utilities.__bases__, is_((new_base.utilities,)))
# And they changed type
assert_that(new_sub.adapters.__bases__[0], is_(BTreeLocalAdapterRegistry))
assert_that(new_sub.utilities.__bases__[0], is_(BTreeLocalAdapterRegistry))
def _store_base_subs_in_zodb(self, storage):
db = DB(storage)
conn = db.open()
base_comps = BLSM(None)
base_comps.btree_threshold = 0
base_comps.__name__ = u'base'
# replace with "broken"
base_comps.adapters = _LocalAdapterRegistry()
base_comps.utilities = _LocalAdapterRegistry()
sub_comps = BLSM(None)
sub_comps.__name__ = u'sub'
sub_comps.__bases__ = (base_comps,)
assert_that(sub_comps.adapters.__bases__, is_((base_comps.adapters,)))
assert_that(sub_comps.utilities.__bases__, is_((base_comps.utilities,)))
assert_that(sub_comps.utilities.__bases__[0], is_(_LocalAdapterRegistry))
conn.root()['base'] = base_comps
conn.root()['sub'] = sub_comps
transaction.commit()
conn.close()
db.close()
def test_pickle_setstate_swap_class_zodb(self):
storage = DemoStorage()
self._store_base_subs_in_zodb(storage)
db = DB(storage)
conn = db.open()
new_base = conn.root()['base']
new_base._p_activate()
new_sub = conn.root()['sub']
# Still in place
assert_that(new_sub.adapters.__bases__, is_((new_base.adapters,)))
assert_that(new_sub.utilities.__bases__, is_((new_base.utilities,)))
# And they changed type
assert_that(new_sub.adapters.__bases__[0], is_(BTreeLocalAdapterRegistry))
assert_that(new_sub.utilities.__bases__[0], is_(BTreeLocalAdapterRegistry))
# But p_changed wasn't set.
assert_that(new_sub.adapters.__bases__[0],
has_property('_p_changed', is_false()))
transaction.commit()
conn.close()
db.close()
db = DB(storage)
conn = db.open()
new_sub = conn.root()['sub']
# Now, we didn't rewrite the reference so the type stayed the same
assert_that(type(new_sub.adapters.__bases__[0]), _LocalAdapterRegistry)
assert_that(new_sub.adapters.__bases__[0],
has_property('_p_changed', is_false()))
# Loading the base changes the types
new_base = conn.root()['base']
assert_that(new_base.adapters, is_(BTreeLocalAdapterRegistry))
assert_that(new_base.adapters,
same_instance(new_sub.adapters.__bases__[0]))
assert_that(new_sub.adapters.__bases__[0], is_(BTreeLocalAdapterRegistry))
assert_that(new_sub.adapters.__bases__[0],
has_property('_p_changed', is_false()))
def test_pickle_zodb_lookup_adapter(self):
# Now, we can register a couple adapters in the base, save everything,
# and look it up in the sub (when the classes don't match)
storage = DemoStorage()
self._store_base_subs_in_zodb(storage)
db = DB(storage)
conn = db.open()
new_base = conn.root()['base']
new_base._p_activate()
new_sub = conn.root()['sub']
new_base.adapters.btree_provided_threshold = 0
new_base.adapters.btree_map_threshold = 1
# Note: this used-to cause btree-ing the map to fail. The
# implementedBy callable previously had default comparison and can't be
# stored in a btree. As of zope.interface 4.3.0, this is fixed.
new_base.registerAdapter(_foo_factory,
required=(object,),
provided=IFoo)
new_base.registerAdapter(_foo_factory2,
required=(IFoo,),
provided=IMock)
assert_that(new_base._adapter_registrations, is_(BTrees.OOBTree.OOBTree))
assert_that(new_base._adapter_registrations.keys(),
contains(
((IFoo,), IMock, u''),
((implementedBy(object),), IFoo, u'' ),
))
assert_that(new_base.adapters._provided, is_(BTrees.family64.OI.BTree))
assert_that(new_base.adapters._adapters[0], is_({}))
assert_that(new_base.adapters._adapters[1][IFoo], is_(dict))
new_base.registerAdapter(_foo_factory2,
required=(IFoo,),
provided=IFoo)
assert_that(new_base.adapters._adapters[1][IFoo], is_(BTrees.family64.OO.BTree))
transaction.commit()
conn.close()
db.close()
db = DB(storage)
conn = db.open()
new_sub = conn.root()['sub']
x = new_sub.queryAdapter(RootFoo(), IMock)
assert_that(x, is_(2))
def test_register_implemented_by_lookup_utility(self):
storage = DemoStorage()
self._store_base_subs_in_zodb(storage)
db = DB(storage)
conn = db.open()
new_base = conn.root()['base']
new_base._p_activate()
new_sub = conn.root()['sub']
new_base.utilities.btree_provided_threshold = 0
new_base.utilities.btree_map_threshold = 0
new_base.registerUtility(MockSite(),
provided=IFoo)
provided1 = new_base.adapters._provided
# In the past, we couldn't register by implemented, but now we can.
new_base.registerUtility(MockSite(),
provided=implementedBy(MockSite),
name=u'foo')
provided2 = new_base.adapters._provided
# Make sure that it only converted once
assert_that(provided1, is_(same_instance(provided2)))
assert_that(new_base._utility_registrations, is_(BTrees.OOBTree.OOBTree))
assert_that(new_base._utility_registrations.keys(),
contains(
(IFoo, u''),
((implementedBy(MockSite), u'foo')),
))
assert_that(new_base.utilities._provided, is_(BTrees.family64.OI.BTree))
assert_that(new_base.utilities._adapters[0], is_(BTrees.family64.OO.BTree))
assert_that(new_base.utilities._adapters[0][IFoo], is_(BTrees.family64.OO.BTree))
transaction.commit()
conn.close()
db.close()
db = DB(storage)
conn = db.open()
new_sub = conn.root()['sub']
x = new_sub.queryUtility(IFoo)
assert_that(x, is_(MockSite))
# But it can't actually be looked up, regardless of whether we
# convert to btrees or not
x = new_sub.queryUtility(MockSite, u'foo')
assert_that(x, is_(none()))
def test_pickle_zodb_lookup_utility(self):
# Now, we can register a couple utilities in the base, save everything,
# and look it up in the sub (when the classes don't match)
storage = DemoStorage()
self._store_base_subs_in_zodb(storage)
db = DB(storage)
conn = db.open()
new_base = conn.root()['base']
new_base._p_activate()
new_sub = conn.root()['sub']
new_base.utilities.btree_provided_threshold = 0
new_base.utilities.btree_map_threshold = 0
new_base.registerUtility(MockSite(),
provided=IFoo)
provided1 = new_base.adapters._provided
# Previously this would fail. Now it works.
new_base.registerUtility(MockSite(),
provided=implementedBy(object),
name=u'foo')
new_base.registerUtility(MockSite(),
provided=IMock,
name=u'foo')
provided2 = new_base.adapters._provided
# Make sure that it only converted once
assert_that(provided1, is_(same_instance(provided2)))
assert_that(new_base._utility_registrations, is_(BTrees.OOBTree.OOBTree))
assert_that(new_base._utility_registrations.keys(),
contains(
(IFoo, u''),
(IMock, u'foo'),
(implementedBy(object), u'foo'),
))
assert_that(new_base.utilities._provided, is_(BTrees.family64.OI.BTree))
assert_that(new_base.utilities._adapters[0], is_(BTrees.family64.OO.BTree))
assert_that(new_base.utilities._adapters[0][IFoo], is_(BTrees.family64.OO.BTree))
transaction.commit()
conn.close()
db.close()
db = DB(storage)
conn = db.open()
new_sub = conn.root()['sub']
x = new_sub.queryUtility(IFoo)
assert_that(x, is_(MockSite))
x = new_sub.queryUtility(IMock, u'foo')
assert_that(x, is_(MockSite))
def test_convert_with_utility_registered_on_class(self):
comps = BLSM(None)
comps.utilities.btree_provided_threshold = 0
comps.utilities.btree_map_threshold = 0
comps.registerUtility(component=MockSite(),
provided=implementedBy(object))
regs = list(comps.registeredUtilities())
assert_that(regs, has_length(1))
# But note that we can't actually look this up, regardless of whether
# we use BTrees or not
assert_that(regs[0], has_property('provided', implementedBy(object)))
assert_that(calling(comps.getUtility).with_args(implementedBy(object)),
raises(ComponentLookupError))
def test_convert_with_utility_no_provided(self):
comps = BLSM(None)
comps.utilities.btree_provided_threshold = 0
comps.utilities.btree_map_threshold = 0
class AUtility(object):
# Doesn't implement any interfaces
pass
# You can't easily register them this way anyway
assert_that(calling(comps.registerUtility).with_args(AUtility()),
raises(TypeError, "The utility doesn't provide a single interface"))
def test_convert_with_utility_dynamic_provided(self):
comps = BLSM(None)
comps.btree_threshold = 0
comps.utilities.btree_provided_threshold = 0
comps.utilities.btree_map_threshold = 0
class AUtility(object):
# Doesn't implement any interfaces
pass
autility = AUtility()
interface.alsoProvides(autility, IFoo)
comps.registerUtility(autility)
assert_that(comps._utility_registrations, is_(BTrees.OOBTree.OOBTree))
assert_that(comps._utility_registrations.keys(),
contains(
((IFoo, u'')),
))
assert_that(comps.utilities._provided, is_(BTrees.family64.OI.BTree))
assert_that(comps.utilities._adapters[0], is_(BTrees.family64.OO.BTree))
assert_that(comps.utilities._adapters[0][IFoo], is_(BTrees.family64.OO.BTree))
def test_convert_with_adapter_registered_on_class(self):
comps = BLSM(None)
comps.btree_threshold = 0
comps.adapters.btree_provided_threshold = 0
comps.utilities.btree_map_threshold = 0
comps.registerAdapter(
_foo_factory,
required=(object, type('str')),
provided=IFoo)
x = comps.getMultiAdapter((object(), 'str'), IFoo)
assert_that(x, is_(1))
def test_convert_mark_self_changed(self):
comps = BLSM(None)
comps.btree_threshold = 0
comps.adapters.btree_map_threshold = 1
comps.adapters.btree_provided_threshold = 0
comps.utilities.btree_map_threshold = 0
comps.registerAdapter(
_foo_factory,
required=(object, type('str')),
provided=IFoo)
comps.registerAdapter(
_foo_factory2,
required=(object, type(0)),
provided=IFoo)
x = comps.getMultiAdapter((object(), 'str'), IFoo)
assert_that(x, is_(1))
def _foo_factory(*args):
return 1
def _foo_factory2(*args):
return 2
class TestPermissiveOOBTree(unittest.TestCase):
def test_default(self):
# This is now legacy after BTrees 4.3.2, kept around for
# ensuring it still works.
from nti.site.site import _PermissiveOOBTree
tree = _PermissiveOOBTree()
key = object() # default comparison
assert_that(tree.get(key), is_(none()))