/usr/lib/python2.7/dist-packages/zope/component/tests/test_globalregistry.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 | ##############################################################################
#
# 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._api
"""
import unittest
class Test_getGlobalSiteManager(unittest.TestCase):
def _callFUT(self):
from zope.component.globalregistry import getGlobalSiteManager
return getGlobalSiteManager()
def test_gsm_is_IComponentLookup(self):
from zope.component.globalregistry import base
from zope.component.interfaces import IComponentLookup
gsm = self._callFUT()
self.assertTrue(gsm is base)
self.assertTrue(IComponentLookup.providedBy(gsm))
def test_gsm_is_singleton(self):
gsm = self._callFUT()
self.assertTrue(self._callFUT() is gsm)
def test_gsm_pickling(self):
from zope.component._compat import _pickle
gsm = self._callFUT()
dumped = _pickle.dumps(gsm)
loaded = _pickle.loads(dumped)
self.assertTrue(loaded is gsm)
dumped_utilities = _pickle.dumps(gsm.utilities)
loaded_utilities = _pickle.loads(dumped_utilities)
self.assertTrue(loaded_utilities is gsm.utilities)
dumped_adapters = _pickle.dumps(gsm.adapters)
loaded_adapters = _pickle.loads(dumped_adapters)
self.assertTrue(loaded_adapters is gsm.adapters)
class Test_provideUtility(unittest.TestCase):
from zope.component.testing import setUp, tearDown
def _callFUT(self, *args, **kw):
from zope.component.globalregistry import provideUtility
return provideUtility(*args, **kw)
def test_anonymous_no_provides(self):
from zope.interface import Interface
from zope.interface import implementer
from zope.component.globalregistry import getGlobalSiteManager
class IFoo(Interface):
pass
@implementer(IFoo)
class Foo(object):
pass
foo = Foo()
self._callFUT(foo)
gsm = getGlobalSiteManager()
self.assertTrue(gsm.getUtility(IFoo, '') is foo)
def test_named_w_provides(self):
from zope.interface import Interface
from zope.component.globalregistry import getGlobalSiteManager
class IFoo(Interface):
pass
class Foo(object):
pass
foo = Foo()
self._callFUT(foo, IFoo, 'named')
gsm = getGlobalSiteManager()
self.assertTrue(gsm.getUtility(IFoo, 'named') is foo)
class Test_provideAdapter(unittest.TestCase):
from zope.component.testing import setUp, tearDown
def _callFUT(self, *args, **kw):
from zope.component.globalregistry import provideAdapter
return provideAdapter(*args, **kw)
def test_anonymous_no_provides_no_adapts(self):
from zope.interface import Interface
from zope.interface import implementer
from zope.component.globalregistry import getGlobalSiteManager
from zope.component._api import adapter
class IFoo(Interface):
pass
class IBar(Interface):
pass
@implementer(IFoo)
class Foo(object):
pass
@adapter(IFoo)
@implementer(IBar)
class Bar(object):
def __init__(self, context):
self.context = context
self._callFUT(Bar)
gsm = getGlobalSiteManager()
foo = Foo()
adapted = gsm.getAdapter(foo, IBar)
self.assertTrue(isinstance(adapted, Bar))
self.assertTrue(adapted.context is foo)
def test_named_w_provides_w_adapts(self):
from zope.interface import Interface
from zope.interface import implementer
from zope.component.globalregistry import getGlobalSiteManager
class IFoo(Interface):
pass
class IBar(Interface):
pass
@implementer(IFoo)
class Foo(object):
pass
class Bar(object):
def __init__(self, context):
self.context = context
self._callFUT(Bar, (IFoo,), IBar, 'test')
gsm = getGlobalSiteManager()
foo = Foo()
adapted = gsm.getAdapter(foo, IBar, name='test')
self.assertTrue(isinstance(adapted, Bar))
self.assertTrue(adapted.context is foo)
class Test_provideSubscriptionAdapter(unittest.TestCase):
from zope.component.testing import setUp, tearDown
def _callFUT(self, *args, **kw):
from zope.component.globalregistry import provideSubscriptionAdapter
return provideSubscriptionAdapter(*args, **kw)
def test_no_provides_no_adapts(self):
from zope.interface import Interface
from zope.interface import implementer
from zope.component.globalregistry import getGlobalSiteManager
from zope.component._api import adapter
class IFoo(Interface):
pass
class IBar(Interface):
pass
@implementer(IFoo)
class Foo(object):
pass
@adapter(IFoo)
@implementer(IBar)
class Bar(object):
def __init__(self, context):
self.context = context
self._callFUT(Bar)
gsm = getGlobalSiteManager()
foo = Foo()
adapted = gsm.subscribers((foo,), IBar)
self.assertEqual(len(adapted), 1)
self.assertTrue(isinstance(adapted[0], Bar))
self.assertTrue(adapted[0].context is foo)
def test_w_provides_w_adapts(self):
from zope.interface import Interface
from zope.interface import implementer
from zope.component.globalregistry import getGlobalSiteManager
class IFoo(Interface):
pass
class IBar(Interface):
pass
@implementer(IFoo)
class Foo(object):
pass
class Bar(object):
def __init__(self, context):
self.context = context
self._callFUT(Bar, (IFoo,), IBar)
gsm = getGlobalSiteManager()
foo = Foo()
adapted = gsm.subscribers((foo,), IBar)
self.assertEqual(len(adapted), 1)
self.assertTrue(isinstance(adapted[0], Bar))
self.assertTrue(adapted[0].context is foo)
class Test_provideHandler(unittest.TestCase):
from zope.component.testing import setUp, tearDown
def _callFUT(self, *args, **kw):
from zope.component.globalregistry import provideHandler
return provideHandler(*args, **kw)
def test_no_adapts(self):
from zope.interface import Interface
from zope.interface import implementer
from zope.interface import providedBy
from zope.component.globalregistry import getGlobalSiteManager
from zope.component._api import adapter
class IFoo(Interface):
pass
@implementer(IFoo)
class Foo(object):
pass
@adapter(IFoo)
def _handler(context):
assert 0, "DON'T GO HERE"
self._callFUT(_handler)
gsm = getGlobalSiteManager()
regs = list(gsm.registeredHandlers())
self.assertEqual(len(regs), 1)
hr = regs[0]
self.assertEqual(list(hr.required), list(providedBy(Foo())))
self.assertEqual(hr.name, '')
self.assertTrue(hr.factory is _handler)
def test_w_adapts(self):
from zope.interface import Interface
from zope.component.globalregistry import getGlobalSiteManager
class IFoo(Interface):
pass
def _handler(context):
assert 0, "DON'T GO HERE"
self._callFUT(_handler, (IFoo,))
gsm = getGlobalSiteManager()
regs = list(gsm.registeredHandlers())
self.assertEqual(len(regs), 1)
hr = regs[0]
self.assertEqual(list(hr.required), [IFoo])
self.assertEqual(hr.name, '')
self.assertTrue(hr.factory is _handler)
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(Test_getGlobalSiteManager),
unittest.makeSuite(Test_provideUtility),
unittest.makeSuite(Test_provideAdapter),
unittest.makeSuite(Test_provideSubscriptionAdapter),
unittest.makeSuite(Test_provideHandler),
))
|