[Checkins] SVN: zope.component/tseaver-test_cleanup/ Move getNextUtilty / queryNextUtility API doctests to Sphinx / unittests.
Tres Seaver
cvs-admin at zope.org
Sun Jun 17 21:37:00 UTC 2012
Log message for revision 126904:
Move getNextUtilty / queryNextUtility API doctests to Sphinx / unittests.
Changed:
_U zope.component/tseaver-test_cleanup/
U zope.component/tseaver-test_cleanup/docs/api.rst
U zope.component/tseaver-test_cleanup/src/zope/component/tests/test___init__.py
U zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py
-=-
Modified: zope.component/tseaver-test_cleanup/docs/api.rst
===================================================================
--- zope.component/tseaver-test_cleanup/docs/api.rst 2012-06-17 21:36:53 UTC (rev 126903)
+++ zope.component/tseaver-test_cleanup/docs/api.rst 2012-06-17 21:36:57 UTC (rev 126904)
@@ -143,6 +143,9 @@
>>> queryUtility(I1) is ob
True
+Named Utilities
+###############
+
Registering a utility without a name does not mean that it is available
when looking for the utility with a name:
@@ -167,7 +170,128 @@
>>> queryUtility(I1, 'foo') is ob
True
+Delegated Utility Lookup
+########################
+It is common for a utility to delegate its answer to a utility
+providing the same interface in one of the component registry's
+bases. Let's first create a global utility:
+
+.. doctest::
+
+ >>> from zope.interface import Interface
+ >>> from zope.interface import implementer
+ >>> class IMyUtility(Interface):
+ ... pass
+
+ >>> from zope.component.tests.test_doctests \
+ ... import ConformsToIComponentLookup
+ >>> @implementer(IMyUtility)
+ ... class MyUtility(ConformsToIComponentLookup):
+ ... def __init__(self, id, sm):
+ ... self.id = id
+ ... self.sitemanager = sm
+ ... def __repr__(self):
+ ... return "%s('%s')" % (self.__class__.__name__, self.id)
+
+ >>> gutil = MyUtility('global', gsm)
+ >>> gsm.registerUtility(gutil, IMyUtility, 'myutil')
+
+Now, let's create two registries and set up the bases hierarchy:
+
+.. doctest::
+
+ >>> from zope.interface.registry import Components
+ >>> sm1 = Components('sm1', bases=(gsm, ))
+ >>> sm1_1 = Components('sm1_1', bases=(sm1, ))
+
+Now we create two utilities and insert them in our folder hierarchy:
+
+.. doctest::
+
+ >>> util1 = MyUtility('one', sm1)
+ >>> sm1.registerUtility(util1, IMyUtility, 'myutil')
+ >>> IComponentLookup(util1) is sm1
+ True
+
+ >>> util1_1 = MyUtility('one-one', sm1_1)
+ >>> sm1_1.registerUtility(util1_1, IMyUtility, 'myutil')
+ >>> IComponentLookup(util1_1) is sm1_1
+ True
+
+Now, if we ask `util1_1` for its next available utility we get the
+``one`` utility:
+
+.. doctest::
+
+ >>> from zope.component import getNextUtility
+ >>> getNextUtility(util1_1, IMyUtility, 'myutil')
+ MyUtility('one')
+
+Next we ask `util1` for its next utility and we should get the global version:
+
+.. doctest::
+
+ >>> getNextUtility(util1, IMyUtility, 'myutil')
+ MyUtility('global')
+
+However, if we ask the global utility for the next one, an error is raised
+
+.. doctest::
+
+ >>> getNextUtility(gutil, IMyUtility,
+ ... 'myutil') #doctest: +NORMALIZE_WHITESPACE
+ Traceback (most recent call last):
+ ...
+ ComponentLookupError:
+ No more utilities for <InterfaceClass zope.component.tests.test_doctests.IMyUtility>,
+ 'myutil' have been found.
+
+You can also use `queryNextUtility` and specify a default:
+
+.. doctest::
+
+ >>> from zope.component import queryNextUtility
+ >>> queryNextUtility(gutil, IMyUtility, 'myutil', 'default')
+ 'default'
+
+Let's now ensure that the function also works with multiple registries. First
+we create another base registry:
+
+.. doctest::
+
+ >>> myregistry = Components()
+
+We now set up another utility into that registry:
+
+.. doctest::
+
+ >>> custom_util = MyUtility('my_custom_util', myregistry)
+ >>> myregistry.registerUtility(custom_util, IMyUtility, 'my_custom_util')
+
+We add it as a base to the local site manager:
+
+.. doctest::
+
+ >>> sm1.__bases__ = (myregistry,) + sm1.__bases__
+
+Both the ``myregistry`` and global utilities should be available:
+
+.. doctest::
+
+ >>> queryNextUtility(sm1, IMyUtility, 'my_custom_util')
+ MyUtility('my_custom_util')
+ >>> queryNextUtility(sm1, IMyUtility, 'myutil')
+ MyUtility('global')
+
+Note, if the context cannot be converted to a site manager, the default is
+retruned:
+
+.. doctest::
+
+ >>> queryNextUtility(object(), IMyUtility, 'myutil', 'default')
+ 'default'
+
.. testcleanup::
from zope.component.testing import tearDown
Modified: zope.component/tseaver-test_cleanup/src/zope/component/tests/test___init__.py
===================================================================
--- zope.component/tseaver-test_cleanup/src/zope/component/tests/test___init__.py 2012-06-17 21:36:53 UTC (rev 126903)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test___init__.py 2012-06-17 21:36:57 UTC (rev 126904)
@@ -191,7 +191,87 @@
getGlobalSiteManager().registerUtility(obj1, IFoo)
self.assertTrue(queryUtility(IFoo, context=context) is obj2)
+ def test_getNextUtility_global(self):
+ from zope.component import getGlobalSiteManager
+ from zope.component import getNextUtility
+ from zope.component.interface import ComponentLookupError
+ gsm = getGlobalSiteManager()
+ gutil = _makeMyUtility('global', gsm)
+ gsm.registerUtility(gutil, IMyUtility, 'myutil')
+ self.assertRaises(ComponentLookupError,
+ getNextUtility, gutil, IMyUtility, 'myutil')
+ def test_queryNextUtility_global(self):
+ from zope.component import getGlobalSiteManager
+ from zope.component import queryNextUtility
+ gsm = getGlobalSiteManager()
+ gutil = _makeMyUtility('global', gsm)
+ gsm.registerUtility(gutil, IMyUtility, 'myutil')
+ self.assertEqual(queryNextUtility(gutil, IMyUtility, 'myutil'), None)
+
+ def test_getNextUtility_nested(self):
+ from zope.component import getGlobalSiteManager
+ from zope.component import getNextUtility
+ from zope.component.interfaces import IComponentLookup
+ from zope.interface.registry import Components
+ gsm = getGlobalSiteManager()
+ gutil = _makeMyUtility('global', gsm)
+ gsm.registerUtility(gutil, IMyUtility, 'myutil')
+ sm1 = Components('sm1', bases=(gsm, ))
+ sm1_1 = Components('sm1_1', bases=(sm1, ))
+ util1 = _makeMyUtility('one', sm1)
+ sm1.registerUtility(util1, IMyUtility, 'myutil')
+ self.assertTrue(IComponentLookup(util1) is sm1)
+ self.assertTrue(getNextUtility(util1, IMyUtility, 'myutil') is gutil)
+ util1_1 = _makeMyUtility('one-one', sm1_1)
+ sm1_1.registerUtility(util1_1, IMyUtility, 'myutil')
+ self.assertTrue(IComponentLookup(util1_1) is sm1_1)
+ self.assertTrue(getNextUtility(util1_1, IMyUtility, 'myutil') is util1)
+
+ def test_queryNextUtility_nested(self):
+ from zope.component import getGlobalSiteManager
+ from zope.component import queryNextUtility
+ from zope.interface.registry import Components
+ gsm = getGlobalSiteManager()
+ gutil = _makeMyUtility('global', gsm)
+ gsm.registerUtility(gutil, IMyUtility, 'myutil')
+ sm1 = Components('sm1', bases=(gsm, ))
+ sm1_1 = Components('sm1_1', bases=(sm1, ))
+ util1 = _makeMyUtility('one', sm1)
+ sm1.registerUtility(util1, IMyUtility, 'myutil')
+ util1_1 = _makeMyUtility('one-one', sm1_1)
+ sm1_1.registerUtility(util1_1, IMyUtility, 'myutil')
+ myregistry = Components()
+ custom_util = _makeMyUtility('my_custom_util', myregistry)
+ myregistry.registerUtility(custom_util, IMyUtility, 'my_custom_util')
+ sm1.__bases__ = (myregistry,) + sm1.__bases__
+ # Both the ``myregistry`` and global utilities should be available:
+ self.assertTrue(queryNextUtility(sm1, IMyUtility, 'my_custom_util')
+ is custom_util)
+ self.assertTrue(queryNextUtility(sm1, IMyUtility, 'myutil')
+ is gutil)
+
+
+IMyUtility = None
+def _makeMyUtility(name, sm):
+ global IMyUtility
+ from zope.interface import Interface
+ from zope.interface import implementer
+ from zope.component.tests.test_doctests import ConformsToIComponentLookup
+
+ if IMyUtility is None:
+ class IMyUtility(Interface):
+ pass
+
+ @implementer(IMyUtility)
+ class MyUtility(ConformsToIComponentLookup):
+ def __init__(self, id, sm):
+ self.id = id
+ self.sitemanager = sm
+
+ return MyUtility(name, sm)
+
+
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(PackageAPITests),
Modified: zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py
===================================================================
--- zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py 2012-06-17 21:36:53 UTC (rev 126903)
+++ zope.component/tseaver-test_cleanup/src/zope/component/tests/test_doctests.py 2012-06-17 21:36:57 UTC (rev 126904)
@@ -852,107 +852,6 @@
>>> tearDown()
"""
-def test_next_utilities():
- """
- It is common for a utility to delegate its answer to a utility
- providing the same interface in one of the component registry's
- bases. Let's first create a global utility::
-
- >>> from zope.component.testing import setUp, tearDown
- >>> setUp()
- >>> import zope.interface
- >>> class IMyUtility(zope.interface.Interface):
- ... pass
-
- >>> class MyUtility(ConformsToIComponentLookup):
- ... zope.interface.implements(IMyUtility)
- ... def __init__(self, id, sm):
- ... self.id = id
- ... self.sitemanager = sm
- ... def __repr__(self):
- ... return "%s('%s')" % (self.__class__.__name__, self.id)
-
- >>> from zope.component import getGlobalSiteManager
- >>> gsm = getGlobalSiteManager()
-
- >>> gutil = MyUtility('global', gsm)
- >>> gsm.registerUtility(gutil, IMyUtility, 'myutil')
-
- Now, let's create two registries and set up the bases hierarchy::
-
- >>> from zope.interface.registry import Components
- >>> sm1 = Components('sm1', bases=(gsm, ))
- >>> sm1_1 = Components('sm1_1', bases=(sm1, ))
-
- Now we create two utilities and insert them in our folder hierarchy:
-
- >>> util1 = MyUtility('one', sm1)
- >>> sm1.registerUtility(util1, IMyUtility, 'myutil')
- >>> IComponentLookup(util1) is sm1
- True
-
- >>> util1_1 = MyUtility('one-one', sm1_1)
- >>> sm1_1.registerUtility(util1_1, IMyUtility, 'myutil')
- >>> IComponentLookup(util1_1) is sm1_1
- True
-
- Now, if we ask `util1_1` for its next available utility we get the
- ``one`` utility::
-
- >>> from zope.component import getNextUtility
- >>> getNextUtility(util1_1, IMyUtility, 'myutil')
- MyUtility('one')
-
- Next we ask `util1` for its next utility and we should get the global version:
-
- >>> getNextUtility(util1, IMyUtility, 'myutil')
- MyUtility('global')
-
- However, if we ask the global utility for the next one, an error is raised
-
- >>> getNextUtility(gutil, IMyUtility,
- ... 'myutil') #doctest: +NORMALIZE_WHITESPACE
- Traceback (most recent call last):
- ...
- ComponentLookupError:
- No more utilities for <InterfaceClass zope.component.tests.test_doctests.IMyUtility>,
- 'myutil' have been found.
-
- You can also use `queryNextUtility` and specify a default:
-
- >>> from zope.component import queryNextUtility
- >>> queryNextUtility(gutil, IMyUtility, 'myutil', 'default')
- 'default'
-
- Let's now ensure that the function also works with multiple registries. First
- we create another base registry:
-
- >>> myregistry = Components()
-
- We now set up another utility into that registry:
-
- >>> custom_util = MyUtility('my_custom_util', myregistry)
- >>> myregistry.registerUtility(custom_util, IMyUtility, 'my_custom_util')
-
- We add it as a base to the local site manager:
-
- >>> sm1.__bases__ = (myregistry,) + sm1.__bases__
-
- Both the ``myregistry`` and global utilities should be available:
-
- >>> queryNextUtility(sm1, IMyUtility, 'my_custom_util')
- MyUtility('my_custom_util')
- >>> queryNextUtility(sm1, IMyUtility, 'myutil')
- MyUtility('global')
-
- Note, if the context cannot be converted to a site manager, the default is
- retruned:
-
- >>> queryNextUtility(object(), IMyUtility, 'myutil', 'default')
- 'default'
- >>> tearDown()
- """
-
def dont_leak_utility_registrations_in__subscribers():
"""
More information about the checkins
mailing list