Пример #1
0
    def test_uses_configured_site_manager(self):
        from zope.interface.registry import Components
        from zope.component import getSiteManager
        from zope.component.testfiles.components import comp, IApp

        registry = Components()
        def dummy(context=None):
            return registry
        getSiteManager.sethook(dummy)

        try:
            self._callFUT('registerUtility', comp, IApp, u'')
            self.assertTrue(registry.getUtility(IApp) is comp)
        finally:
            getSiteManager.reset()
Пример #2
0
    def test_uses_configured_site_manager(self):
        from zope.interface.registry import Components
        from zope.component import getSiteManager
        from zope.component.testfiles.components import comp, IApp
        from zope.component._compat import _BLANK

        registry = Components()

        def dummy(context=None):
            return registry

        getSiteManager.sethook(dummy)

        try:
            self._callFUT('registerUtility', comp, IApp, _BLANK)
            self.assertTrue(registry.getUtility(IApp) is comp)
        finally:
            getSiteManager.reset()
    def test_register_in_current_site(self):
        from zope.component._api import getSiteManager
        from zope.component.globalregistry import getGlobalSiteManager
        from zope.interface.registry import Components
        from zope.interface import Interface
        from zope.interface.interfaces import IInterface

        class IFoo(Interface):
            pass

        site_man = Components()
        def get(_context=None):
            return site_man
        getSiteManager.sethook(get)
        self.addCleanup(getSiteManager.reset)

        self._callFUT('foo', IFoo)

        self.assertIs(site_man.getUtility(IInterface, name='foo'),
                      IFoo)
        self.assertIsNone(
            getGlobalSiteManager().queryUtility(IInterface, name='foo')
        )
Пример #4
0
class TestUtility(unittest.TestCase):

    def setUp(self):
        self.components = Components('comps')

    def test_register_utility(self):
        test_object = U1(1)
        self.components.registerUtility(test_object)
        self.assertEqual(self.components.getUtility(I1), test_object)

    def test_register_utility_with_factory(self):
        test_object = U1(1)
        def factory():
           return test_object
        self.components.registerUtility(factory=factory)
        self.assertEqual(self.components.getUtility(I1), test_object)
        self.assertTrue(self.components.unregisterUtility(factory=factory))

    def test_register_utility_with_component_and_factory(self):
        def factory():
            return U1(1)
        self.assertRaises(
            TypeError,
            self.components.registerUtility, U1(1), factory=factory)

    def test_unregister_utility_with_and_without_component_and_factory(self):
        def factory():
            return U1(1)
        self.assertRaises(
            TypeError,
            self.components.unregisterUtility, U1(1), factory=factory)
        self.assertRaises(TypeError, self.components.unregisterUtility)

    def test_register_utility_with_no_interfaces(self):
        self.assertRaises(TypeError, self.components.registerUtility, A)

    def test_register_utility_with_two_interfaces(self):
        self.assertRaises(TypeError, self.components.registerUtility, U12(1))

    def test_register_utility_with_arguments(self):
        test_object1 = U12(1)
        test_object2 = U12(2)
        self.components.registerUtility(test_object1, I2)
        self.components.registerUtility(test_object2, I2, 'name')
        self.assertEqual(self.components.getUtility(I2), test_object1)
        self.assertEqual(self.components.getUtility(I2, 'name'), test_object2)

    def test_get_none_existing_utility(self):
        from zope.interface.interfaces import ComponentLookupError
        self.assertRaises(ComponentLookupError, self.components.getUtility, I3)

    def test_query_none_existing_utility(self):
        self.assertTrue(self.components.queryUtility(I3) is None)
        self.assertEqual(self.components.queryUtility(I3, default=42), 42)

    def test_registered_utilities_and_sorting(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object3, I2, u'name')
        self.components.registerUtility(test_object2, I2)

        sorted_utilities = sorted(self.components.registeredUtilities())
        sorted_utilities_name = map(lambda x: getattr(x, 'name'),
                                    sorted_utilities)
        sorted_utilities_component = map(lambda x: getattr(x, 'component'),
                                         sorted_utilities)
        sorted_utilities_provided = map(lambda x: getattr(x, 'provided'),
                                        sorted_utilities)

        self.assertEqual(len(sorted_utilities), 3)
        self.assertEqual(sorted_utilities_name, [u'', u'', u'name'])
        self.assertEqual(
            sorted_utilities_component,
            [test_object1, test_object2, test_object3])
        self.assertEqual(sorted_utilities_provided, [I1, I2, I2])

    def test_duplicate_utility(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        test_object4 = U1(4)
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object2, I2)
        self.components.registerUtility(test_object3, I2, u'name')
        self.assertEqual(self.components.getUtility(I1), test_object1)

        self.components.registerUtility(test_object4, info=u'use 4 now')
        self.assertEqual(self.components.getUtility(I1), test_object4)

    def test_unregister_utility(self):
        test_object = U1(1)
        self.components.registerUtility(test_object)
        self.assertEqual(self.components.getUtility(I1), test_object)
        self.assertTrue(self.components.unregisterUtility(provided=I1))
        self.assertFalse(self.components.unregisterUtility(provided=I1))

    def test_unregister_utility_extended(self):
        test_object = U1(1)
        self.components.registerUtility(test_object)
        self.assertFalse(self.components.unregisterUtility(U1(1)))
        self.assertEqual(self.components.queryUtility(I1), test_object)
        self.assertTrue(self.components.unregisterUtility(test_object))
        self.assertTrue(self.components.queryUtility(I1) is None)

    def test_get_utilities_for(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object2, I2)
        self.components.registerUtility(test_object3, I2, u'name')

        sorted_utilities = sorted(self.components.getUtilitiesFor(I2))
        self.assertEqual(len(sorted_utilities), 2)
        self.assertEqual(sorted_utilities[0], (u'', test_object2))
        self.assertEqual(sorted_utilities[1], (u'name', test_object3))

    def test_get_all_utilities_registered_for(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        test_object4 = U('ext')
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object2, I2)
        self.components.registerUtility(test_object3, I2, u'name')
        self.components.registerUtility(test_object4, I2e)

        sorted_utilities = sorted(self.components.getUtilitiesFor(I2))
        self.assertEqual(len(sorted_utilities), 2)
        self.assertEqual(sorted_utilities[0], (u'', test_object2))
        self.assertEqual(sorted_utilities[1], (u'name', test_object3))

        all_utilities = self.components.getAllUtilitiesRegisteredFor(I2)
        self.assertEqual(len(all_utilities), 3)
        self.assertTrue(test_object2 in all_utilities)
        self.assertTrue(test_object3 in all_utilities)
        self.assertTrue(test_object4 in all_utilities)

        self.assertTrue(self.components.unregisterUtility(test_object4, I2e))
        self.assertEqual(self.components.getAllUtilitiesRegisteredFor(I2e), [])

    def test_utility_events(self):
        from zope.event import subscribers
        old_subscribers = subscribers[:]
        subscribers[:] = []

        test_object = U1(1)
        def log_event(event):
            self.assertEqual(event.object.component, test_object)
        subscribers.append(log_event)
        self.components.registerUtility(test_object)

        subscribers[:] = old_subscribers

    def test_dont_leak_utility_registrations_in__subscribers(self):
        """
        We've observed utilities getting left in _subscribers when they
        get unregistered.

        """
        class C:
            def __init__(self, name):
                self.name = name
            def __repr__(self):
                return "C(%s)" % self.name

        c1 = C(1)
        c2 = C(2)

        self.components.registerUtility(c1, I1)
        self.components.registerUtility(c1, I1)
        utilities = list(self.components.getAllUtilitiesRegisteredFor(I1))
        self.assertEqual(len(utilities), 1)
        self.assertEqual(utilities[0], c1)

        self.assertTrue(self.components.unregisterUtility(provided=I1))
        utilities = list(self.components.getAllUtilitiesRegisteredFor(I1))
        self.assertEqual(len(utilities), 0)

        self.components.registerUtility(c1, I1)
        self.components.registerUtility(c2, I1)

        utilities = list(self.components.getAllUtilitiesRegisteredFor(I1))
        self.assertEqual(len(utilities), 1)
        self.assertEqual(utilities[0], c2)
Пример #5
0
class TestUtility(unittest.TestCase):
    def setUp(self):
        self.components = Components('comps')

    def test_register_utility(self):
        test_object = U1(1)
        self.components.registerUtility(test_object)
        self.assertEqual(self.components.getUtility(I1), test_object)

    def test_register_utility_with_factory(self):
        test_object = U1(1)

        def factory():
            return test_object

        self.components.registerUtility(factory=factory)
        self.assertEqual(self.components.getUtility(I1), test_object)
        self.assertTrue(self.components.unregisterUtility(factory=factory))

    def test_register_utility_with_component_and_factory(self):
        def factory():
            return U1(1)

        self.assertRaises(TypeError,
                          self.components.registerUtility,
                          U1(1),
                          factory=factory)

    def test_unregister_utility_with_and_without_component_and_factory(self):
        def factory():
            return U1(1)

        self.assertRaises(TypeError,
                          self.components.unregisterUtility,
                          U1(1),
                          factory=factory)
        self.assertRaises(TypeError, self.components.unregisterUtility)

    def test_register_utility_with_no_interfaces(self):
        self.assertRaises(TypeError, self.components.registerUtility, A)

    def test_register_utility_with_two_interfaces(self):
        self.assertRaises(TypeError, self.components.registerUtility, U12(1))

    def test_register_utility_with_arguments(self):
        test_object1 = U12(1)
        test_object2 = U12(2)
        self.components.registerUtility(test_object1, I2)
        self.components.registerUtility(test_object2, I2, 'name')
        self.assertEqual(self.components.getUtility(I2), test_object1)
        self.assertEqual(self.components.getUtility(I2, 'name'), test_object2)

    def test_get_none_existing_utility(self):
        from zope.interface.interfaces import ComponentLookupError
        self.assertRaises(ComponentLookupError, self.components.getUtility, I3)

    def test_query_none_existing_utility(self):
        self.assertTrue(self.components.queryUtility(I3) is None)
        self.assertEqual(self.components.queryUtility(I3, default=42), 42)

    def test_registered_utilities_and_sorting(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object3, I2, u'name')
        self.components.registerUtility(test_object2, I2)

        sorted_utilities = sorted(self.components.registeredUtilities())
        sorted_utilities_name = map(lambda x: getattr(x, 'name'),
                                    sorted_utilities)
        sorted_utilities_component = map(lambda x: getattr(x, 'component'),
                                         sorted_utilities)
        sorted_utilities_provided = map(lambda x: getattr(x, 'provided'),
                                        sorted_utilities)

        self.assertEqual(len(sorted_utilities), 3)
        self.assertEqual(sorted_utilities_name, [u'', u'', u'name'])
        self.assertEqual(sorted_utilities_component,
                         [test_object1, test_object2, test_object3])
        self.assertEqual(sorted_utilities_provided, [I1, I2, I2])

    def test_duplicate_utility(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        test_object4 = U1(4)
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object2, I2)
        self.components.registerUtility(test_object3, I2, u'name')
        self.assertEqual(self.components.getUtility(I1), test_object1)

        self.components.registerUtility(test_object4, info=u'use 4 now')
        self.assertEqual(self.components.getUtility(I1), test_object4)

    def test_unregister_utility(self):
        test_object = U1(1)
        self.components.registerUtility(test_object)
        self.assertEqual(self.components.getUtility(I1), test_object)
        self.assertTrue(self.components.unregisterUtility(provided=I1))
        self.assertFalse(self.components.unregisterUtility(provided=I1))

    def test_unregister_utility_extended(self):
        test_object = U1(1)
        self.components.registerUtility(test_object)
        self.assertFalse(self.components.unregisterUtility(U1(1)))
        self.assertEqual(self.components.queryUtility(I1), test_object)
        self.assertTrue(self.components.unregisterUtility(test_object))
        self.assertTrue(self.components.queryUtility(I1) is None)

    def test_get_utilities_for(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object2, I2)
        self.components.registerUtility(test_object3, I2, u'name')

        sorted_utilities = sorted(self.components.getUtilitiesFor(I2))
        self.assertEqual(len(sorted_utilities), 2)
        self.assertEqual(sorted_utilities[0], (u'', test_object2))
        self.assertEqual(sorted_utilities[1], (u'name', test_object3))

    def test_get_all_utilities_registered_for(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        test_object4 = U('ext')
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object2, I2)
        self.components.registerUtility(test_object3, I2, u'name')
        self.components.registerUtility(test_object4, I2e)

        sorted_utilities = sorted(self.components.getUtilitiesFor(I2))
        self.assertEqual(len(sorted_utilities), 2)
        self.assertEqual(sorted_utilities[0], (u'', test_object2))
        self.assertEqual(sorted_utilities[1], (u'name', test_object3))

        all_utilities = self.components.getAllUtilitiesRegisteredFor(I2)
        self.assertEqual(len(all_utilities), 3)
        self.assertTrue(test_object2 in all_utilities)
        self.assertTrue(test_object3 in all_utilities)
        self.assertTrue(test_object4 in all_utilities)

        self.assertTrue(self.components.unregisterUtility(test_object4, I2e))
        self.assertEqual(self.components.getAllUtilitiesRegisteredFor(I2e), [])

    def test_utility_events(self):
        from zope.event import subscribers
        old_subscribers = subscribers[:]
        subscribers[:] = []

        test_object = U1(1)

        def log_event(event):
            self.assertEqual(event.object.component, test_object)

        subscribers.append(log_event)
        self.components.registerUtility(test_object)

        subscribers[:] = old_subscribers

    def test_dont_leak_utility_registrations_in__subscribers(self):
        """
        We've observed utilities getting left in _subscribers when they
        get unregistered.

        """
        class C:
            def __init__(self, name):
                self.name = name

            def __repr__(self):
                return "C(%s)" % self.name

        c1 = C(1)
        c2 = C(2)

        self.components.registerUtility(c1, I1)
        self.components.registerUtility(c1, I1)
        utilities = list(self.components.getAllUtilitiesRegisteredFor(I1))
        self.assertEqual(len(utilities), 1)
        self.assertEqual(utilities[0], c1)

        self.assertTrue(self.components.unregisterUtility(provided=I1))
        utilities = list(self.components.getAllUtilitiesRegisteredFor(I1))
        self.assertEqual(len(utilities), 0)

        self.components.registerUtility(c1, I1)
        self.components.registerUtility(c2, I1)

        utilities = list(self.components.getAllUtilitiesRegisteredFor(I1))
        self.assertEqual(len(utilities), 1)
        self.assertEqual(utilities[0], c2)