/usr/lib/python2.7/dist-packages/zope/component/tests/test_hooks.py is in python-zope.component 4.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ##############################################################################
#
# Copyright (c) 2012 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Tests for z.c.hooks
"""
import unittest
class Test_read_property(unittest.TestCase):
def _getTargetClass(self):
from zope.component.hooks import read_property
return read_property
def test_via_instance(self):
class Foo(object):
@self._getTargetClass()
def bar(self):
return 'BAR'
foo = Foo()
self.assertEqual(foo.bar, 'BAR')
foo.bar = 'BAZ'
self.assertEqual(foo.bar, 'BAZ')
del foo.bar
self.assertEqual(foo.bar, 'BAR')
def test_via_class(self):
class Foo(object):
@self._getTargetClass()
def bar(self):
return 'BAR'
bar = Foo.bar
self.assertTrue(isinstance(bar, self._getTargetClass()))
self.assertEqual(bar.func(object()), 'BAR')
class SiteInfoTests(unittest.TestCase):
def _getTargetClass(self):
from zope.component.hooks import SiteInfo
return SiteInfo
def _makeOne(self):
return self._getTargetClass()()
def test_initial(self):
from zope.component.globalregistry import getGlobalSiteManager
gsm = getGlobalSiteManager()
si = self._makeOne()
self.assertEqual(si.site, None)
self.assertTrue(si.sm is gsm)
def test_adapter_hook(self):
_hook = object()
class _Registry(object):
adapter_hook = _hook
class _SiteManager(object):
adapters = _Registry()
si = self._makeOne()
si.sm = _SiteManager()
self.assertFalse('adapter_hook' in si.__dict__)
self.assertTrue(si.adapter_hook is _hook)
self.assertTrue('adapter_hook' in si.__dict__)
del si.adapter_hook
self.assertFalse('adapter_hook' in si.__dict__)
class Test_setSite(unittest.TestCase):
def _callFUT(self, site):
from zope.component.hooks import setSite
return setSite(site)
def test_w_None(self):
from zope.component import hooks
from zope.component.globalregistry import getGlobalSiteManager
gsm = getGlobalSiteManager()
_SM2 = object()
_SITE = object()
_HOOK = object()
siteinfo = _DummySiteInfo()
siteinfo.sm = _SM2
siteinfo.site = _SITE
siteinfo.adapterhook = _HOOK
with _Monkey(hooks, siteinfo=siteinfo):
self._callFUT(None)
self.assertTrue(siteinfo.sm is gsm)
self.assertTrue(siteinfo.site is None)
self.assertFalse('adapter_hook' in siteinfo.__dict__)
def test_w_site(self):
from zope.component import hooks
_SM2 = object()
class _Site(object):
def getSiteManager(self):
return _SM2
siteinfo = _DummySiteInfo()
_site = _Site()
with _Monkey(hooks, siteinfo=siteinfo):
self._callFUT(_site)
self.assertTrue(siteinfo.sm is _SM2)
self.assertTrue(siteinfo.site is _site)
self.assertFalse('adapter_hook' in siteinfo.__dict__)
class Test_getSite(unittest.TestCase):
def _callFUT(self):
from zope.component.hooks import getSite
return getSite()
def test_w_None(self):
from zope.component import hooks
from zope.component.globalregistry import getGlobalSiteManager
gsm = getGlobalSiteManager()
siteinfo = _DummySiteInfo()
with _Monkey(hooks, siteinfo=siteinfo):
self.assertTrue(self._callFUT() is None)
def test_w_site(self):
from zope.component import hooks
from zope.component.globalregistry import getGlobalSiteManager
gsm = getGlobalSiteManager()
_SM2 = object()
_SITE = object()
siteinfo = _DummySiteInfo()
siteinfo.sm = _SM2
siteinfo.site = _SITE
with _Monkey(hooks, siteinfo=siteinfo):
self.assertTrue(self._callFUT() is _SITE)
class Test_site(unittest.TestCase):
def _callFUT(self, new_site):
from zope.component.hooks import site
return site(new_site)
def test_it(self):
from zope.component import hooks
from zope.component.globalregistry import getGlobalSiteManager
gsm = getGlobalSiteManager()
_SM2 = object()
class _Site(object):
def getSiteManager(self):
return _SM2
_site = _Site()
siteinfo = _DummySiteInfo()
self.assertTrue(siteinfo.site is None)
self.assertTrue(siteinfo.sm is _SM)
with _Monkey(hooks, siteinfo=siteinfo):
with self._callFUT(_site):
self.assertTrue(siteinfo.site is _site)
self.assertTrue(siteinfo.sm is _SM2)
self.assertTrue(siteinfo.site is None)
self.assertTrue(siteinfo.sm is gsm)
class Test_getSiteManager(unittest.TestCase):
def _callFUT(self, context=None):
from zope.component.hooks import getSiteManager
return getSiteManager(context)
def test_default(self):
from zope.component import hooks
from zope.component.globalregistry import getGlobalSiteManager
gsm = getGlobalSiteManager()
_SM2 = object()
siteinfo = _DummySiteInfo()
siteinfo.sm = _SM2
with _Monkey(hooks, siteinfo=siteinfo):
self.assertTrue(self._callFUT() is _SM2)
def test_w_explicit_context_no_IComponentLookup(self):
from zope.component import hooks
from zope.component.globalregistry import getGlobalSiteManager
gsm = getGlobalSiteManager()
_SM2 = object()
siteinfo = _DummySiteInfo()
siteinfo.sm = _SM2
with _Monkey(hooks, siteinfo=siteinfo):
self.assertTrue(self._callFUT(object()) is gsm)
def test_w_explicit_context_w_IComponentLookup(self):
from zope.interface import Interface
from zope.component import hooks
from zope.component.globalregistry import getGlobalSiteManager
from zope.component.interfaces import IComponentLookup
class _Lookup(object):
def __init__(self, context):
self.context = context
gsm = getGlobalSiteManager()
gsm.registerAdapter(_Lookup, (Interface,), IComponentLookup, '')
_SM2 = object()
siteinfo = _DummySiteInfo()
siteinfo.sm = _SM2
context = object()
with _Monkey(hooks, siteinfo=siteinfo):
sm = self._callFUT(context)
self.assertTrue(isinstance(sm, _Lookup))
self.assertTrue(sm.context is context)
class Test_adapter_hook(unittest.TestCase):
def _callFUT(self, interface, object, name='', default=None):
from zope.component.hooks import adapter_hook
return adapter_hook(interface, object, name, default)
def test_success(self):
from zope.interface import Interface
from zope.component import hooks
from zope.component.globalregistry import getGlobalSiteManager
class IFoo(Interface):
pass
gsm = getGlobalSiteManager()
_ADAPTER = object()
_DEFAULT = object()
_CONTEXT = object()
_called = []
def _adapter_hook(interface, object, name, default):
_called.append((interface, object, name, default))
return _ADAPTER
siteinfo = _DummySiteInfo()
siteinfo.adapter_hook = _adapter_hook
with _Monkey(hooks, siteinfo=siteinfo):
adapter = self._callFUT(IFoo, _CONTEXT, 'bar', _DEFAULT)
self.assertTrue(adapter is _ADAPTER)
self.assertEqual(_called, [(IFoo, _CONTEXT, 'bar', _DEFAULT)])
def test_hook_raises(self):
from zope.interface import Interface
from zope.component import hooks
from zope.component.globalregistry import getGlobalSiteManager
from zope.component.interfaces import ComponentLookupError
class IFoo(Interface):
pass
gsm = getGlobalSiteManager()
_DEFAULT = object()
_CONTEXT = object()
_called = []
def _adapter_hook(interface, object, name, default):
_called.append((interface, object, name, default))
raise ComponentLookupError('testing')
siteinfo = _DummySiteInfo()
siteinfo.adapter_hook = _adapter_hook
with _Monkey(hooks, siteinfo=siteinfo):
adapter = self._callFUT(IFoo, _CONTEXT, 'bar', _DEFAULT)
self.assertTrue(adapter is _DEFAULT)
self.assertEqual(_called, [(IFoo, _CONTEXT, 'bar', _DEFAULT)])
class Test_setHooks(unittest.TestCase):
def _callFUT(self):
from zope.component.hooks import setHooks
return setHooks()
def test_it(self):
import zope.component._api
from zope.component import hooks
class _Hook(object):
def __init__(self):
self._hooked = None
def sethook(self, value):
self._hooked = value
adapter_hook = _Hook()
getSiteManager = _Hook()
with _Monkey(zope.component._api,
adapter_hook=adapter_hook,
getSiteManager=getSiteManager):
self._callFUT()
self.assertEqual(adapter_hook._hooked, hooks.adapter_hook)
self.assertEqual(getSiteManager._hooked, hooks.getSiteManager)
class Test_resetHooks(unittest.TestCase):
def _callFUT(self):
from zope.component.hooks import resetHooks
return resetHooks()
def test_it(self):
import zope.component._api
from zope.component import hooks
class _Hook(object):
def __init__(self):
self._reset = False
def reset(self):
self._reset = True
adapter_hook = _Hook()
getSiteManager = _Hook()
with _Monkey(zope.component._api,
adapter_hook=adapter_hook,
getSiteManager=getSiteManager):
# Access the adapter_hook of the site info to be
# sure it caches
getattr(hooks.siteinfo, 'adapter_hook')
self.assertTrue('adapter_hook' in hooks.siteinfo.__dict__)
self._callFUT()
self.assertTrue(adapter_hook._reset)
self.assertTrue(getSiteManager._reset)
# adapter_hook cache also reset
self.assertFalse('adapter_hook' in hooks.siteinfo.__dict__)
_SM = object()
class _DummySiteInfo(object):
sm = _SM
site = None
class _Monkey(object):
# context-manager for replacing module names in the scope of a test.
def __init__(self, module, **kw):
self.module = module
self.to_restore = dict([(key, getattr(module, key)) for key in kw])
for key, value in kw.items():
setattr(module, key, value)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
for key, value in self.to_restore.items():
setattr(self.module, key, value)
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(Test_read_property),
unittest.makeSuite(SiteInfoTests),
unittest.makeSuite(Test_setSite),
unittest.makeSuite(Test_getSite),
unittest.makeSuite(Test_site),
unittest.makeSuite(Test_getSiteManager),
unittest.makeSuite(Test_adapter_hook),
unittest.makeSuite(Test_setHooks),
unittest.makeSuite(Test_resetHooks),
))
|