Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

aroundthecode / zope.interface   python

Repository URL to install this package:

Version: 4.6.0 

/ interface / tests / test_registry.py

##############################################################################
#
# Copyright (c) 2001, 2002, 2009 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.
#
##############################################################################
"""Component Registry Tests"""
# pylint:disable=protected-access
import unittest

from zope.interface import Interface
from zope.interface.adapter import VerifyingAdapterRegistry

from zope.interface.registry import Components

class ComponentsTests(unittest.TestCase):

    def _getTargetClass(self):
        return Components

    def _makeOne(self, name='test', *args, **kw):
        return self._getTargetClass()(name, *args, **kw)

    def _wrapEvents(self):
        from zope.interface import registry
        _events = []
        def _notify(*args, **kw):
            _events.append((args, kw))
        _monkey = _Monkey(registry, notify=_notify)
        return _monkey, _events

    def test_ctor_no_bases(self):
        from zope.interface.adapter import AdapterRegistry
        comp = self._makeOne('testing')
        self.assertEqual(comp.__name__, 'testing')
        self.assertEqual(comp.__bases__, ())
        self.assertTrue(isinstance(comp.adapters, AdapterRegistry))
        self.assertTrue(isinstance(comp.utilities, AdapterRegistry))
        self.assertEqual(comp.adapters.__bases__, ())
        self.assertEqual(comp.utilities.__bases__, ())
        self.assertEqual(comp._utility_registrations, {})
        self.assertEqual(comp._adapter_registrations, {})
        self.assertEqual(comp._subscription_registrations, [])
        self.assertEqual(comp._handler_registrations, [])

    def test_ctor_w_base(self):
        base = self._makeOne('base')
        comp = self._makeOne('testing', (base,))
        self.assertEqual(comp.__name__, 'testing')
        self.assertEqual(comp.__bases__, (base,))
        self.assertEqual(comp.adapters.__bases__, (base.adapters,))
        self.assertEqual(comp.utilities.__bases__, (base.utilities,))

    def test___repr__(self):
        comp = self._makeOne('testing')
        self.assertEqual(repr(comp), '<Components testing>')

    # test _init_registries / _init_registrations via only caller, __init__.

    def test_assign_to___bases__(self):
        base1 = self._makeOne('base1')
        base2 = self._makeOne('base2')
        comp = self._makeOne()
        comp.__bases__ = (base1, base2)
        self.assertEqual(comp.__bases__, (base1, base2))
        self.assertEqual(comp.adapters.__bases__,
                         (base1.adapters, base2.adapters))
        self.assertEqual(comp.utilities.__bases__,
                         (base1.utilities, base2.utilities))

    def test_registerUtility_with_component_name(self):
        from zope.interface.declarations import named, InterfaceClass


        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')

        @named(u'foo')
        class Foo(object):
            pass
        foo = Foo()
        _info = u'info'

        comp = self._makeOne()
        comp.registerUtility(foo, ifoo, info=_info)
        self.assertEqual(
            comp._utility_registrations[ifoo, u'foo'],
            (foo, _info, None))

    def test_registerUtility_both_factory_and_component(self):
        def _factory():
            raise NotImplementedError()
        _to_reg = object()
        comp = self._makeOne()
        self.assertRaises(TypeError, comp.registerUtility,
                          component=_to_reg, factory=_factory)

    def test_registerUtility_w_component(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Registered
        from zope.interface.registry import UtilityRegistration

        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = u'info'
        _name = u'name'
        _to_reg = object()
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_to_reg, ifoo, _name, _info)
        self.assertTrue(comp.utilities._adapters[0][ifoo][_name] is _to_reg)
        self.assertEqual(comp._utility_registrations[ifoo, _name],
                         (_to_reg, _info, None))
        self.assertEqual(comp.utilities._subscribers[0][ifoo][''], (_to_reg,))
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.assertTrue(isinstance(event, Registered))
        self.assertTrue(isinstance(event.object, UtilityRegistration))
        self.assertTrue(event.object.registry is comp)
        self.assertTrue(event.object.provided is ifoo)
        self.assertTrue(event.object.name is _name)
        self.assertTrue(event.object.component is _to_reg)
        self.assertTrue(event.object.info is _info)
        self.assertTrue(event.object.factory is None)

    def test_registerUtility_w_factory(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Registered
        from zope.interface.registry import UtilityRegistration

        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = u'info'
        _name = u'name'
        _to_reg = object()
        def _factory():
            return _to_reg
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(None, ifoo, _name, _info, factory=_factory)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.assertTrue(isinstance(event, Registered))
        self.assertTrue(isinstance(event.object, UtilityRegistration))
        self.assertTrue(event.object.registry is comp)
        self.assertTrue(event.object.provided is ifoo)
        self.assertTrue(event.object.name is _name)
        self.assertTrue(event.object.component is _to_reg)
        self.assertTrue(event.object.info is _info)
        self.assertTrue(event.object.factory is _factory)

    def test_registerUtility_no_provided_available(self):
        class Foo(object):
            pass

        _info = u'info'
        _name = u'name'
        _to_reg = Foo()
        comp = self._makeOne()
        self.assertRaises(TypeError,
                          comp.registerUtility, _to_reg, None, _name, _info)

    def test_registerUtility_wo_provided(self):
        from zope.interface.declarations import directlyProvides
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Registered
        from zope.interface.registry import UtilityRegistration

        class IFoo(InterfaceClass):
            pass
        class Foo(object):
            pass
        ifoo = IFoo('IFoo')
        _info = u'info'
        _name = u'name'
        _to_reg = Foo()
        directlyProvides(_to_reg, ifoo)
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_to_reg, None, _name, _info)
        self.assertEqual(len(_events), 1)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.assertTrue(isinstance(event, Registered))
        self.assertTrue(isinstance(event.object, UtilityRegistration))
        self.assertTrue(event.object.registry is comp)
        self.assertTrue(event.object.provided is ifoo)
        self.assertTrue(event.object.name is _name)
        self.assertTrue(event.object.component is _to_reg)
        self.assertTrue(event.object.info is _info)
        self.assertTrue(event.object.factory is None)

    def test_registerUtility_duplicates_existing_reg(self):
        from zope.interface.declarations import InterfaceClass

        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = u'info'
        _name = u'name'
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo, _name, _info)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_to_reg, ifoo, _name, _info)
        self.assertEqual(len(_events), 0)

    def test_registerUtility_w_different_info(self):
        from zope.interface.declarations import InterfaceClass

        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info1 = u'info1'
        _info2 = u'info2'
        _name = u'name'
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo, _name, _info1)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_to_reg, ifoo, _name, _info2)
        self.assertEqual(len(_events), 2)  # unreg, reg
        self.assertEqual(comp._utility_registrations[(ifoo, _name)],
                         (_to_reg, _info2, None))  # replaced
        self.assertEqual(comp.utilities._subscribers[0][ifoo][u''],
                         (_to_reg,))

    def test_registerUtility_w_different_names_same_component(self):
        from zope.interface.declarations import InterfaceClass

        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = u'info'
        _name1 = u'name1'
        _name2 = u'name2'
        _other_reg = object()
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_other_reg, ifoo, _name1, _info)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_to_reg, ifoo, _name2, _info)
        self.assertEqual(len(_events), 1)  # reg
        self.assertEqual(comp._utility_registrations[(ifoo, _name1)],
                         (_other_reg, _info, None))
        self.assertEqual(comp._utility_registrations[(ifoo, _name2)],
                         (_to_reg, _info, None))
        self.assertEqual(comp.utilities._subscribers[0][ifoo][u''],
                         (_other_reg, _to_reg,))

    def test_registerUtility_replaces_existing_reg(self):
        from zope.interface.declarations import InterfaceClass
        from zope.interface.interfaces import Unregistered
        from zope.interface.interfaces import Registered
        from zope.interface.registry import UtilityRegistration

        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = u'info'
        _name = u'name'
        _before, _after = object(), object()
        comp = self._makeOne()
        comp.registerUtility(_before, ifoo, _name, _info)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_after, ifoo, _name, _info)
        self.assertEqual(len(_events), 2)
        args, kw = _events[0]
        event, = args
        self.assertEqual(kw, {})
        self.assertTrue(isinstance(event, Unregistered))
        self.assertTrue(isinstance(event.object, UtilityRegistration))
        self.assertTrue(event.object.registry is comp)
        self.assertTrue(event.object.provided is ifoo)
        self.assertTrue(event.object.name is _name)
        self.assertTrue(event.object.component is _before)
        self.assertTrue(event.object.info is _info)
        self.assertTrue(event.object.factory is None)
        args, kw = _events[1]
        event, = args
        self.assertEqual(kw, {})
        self.assertTrue(isinstance(event, Registered))
        self.assertTrue(isinstance(event.object, UtilityRegistration))
        self.assertTrue(event.object.registry is comp)
        self.assertTrue(event.object.provided is ifoo)
        self.assertTrue(event.object.name is _name)
        self.assertTrue(event.object.component is _after)
        self.assertTrue(event.object.info is _info)
        self.assertTrue(event.object.factory is None)

    def test_registerUtility_w_existing_subscr(self):
        from zope.interface.declarations import InterfaceClass

        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = u'info'
        _name1 = u'name1'
        _name2 = u'name2'
        _to_reg = object()
        comp = self._makeOne()
        comp.registerUtility(_to_reg, ifoo, _name1, _info)
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_to_reg, ifoo, _name2, _info)
        self.assertEqual(comp.utilities._subscribers[0][ifoo][''], (_to_reg,))

    def test_registerUtility_wo_event(self):
        from zope.interface.declarations import InterfaceClass

        class IFoo(InterfaceClass):
            pass
        ifoo = IFoo('IFoo')
        _info = u'info'
        _name = u'name'
        _to_reg = object()
        comp = self._makeOne()
        _monkey, _events = self._wrapEvents()
        with _monkey:
            comp.registerUtility(_to_reg, ifoo, _name, _info, False)
        self.assertEqual(len(_events), 0)

    def test_registerUtility_changes_object_identity_after(self):
Loading ...