[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services/tests - TestingIConfigurable.py:1.1.2.1 test_adapter.py:1.1.2.1

Jim Fulton jim@zope.com
Tue, 10 Dec 2002 09:38:37 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Services/tests
In directory cvs.zope.org:/tmp/cvs-serv16226/tests

Added Files:
      Tag: AdapterAndView-branch
	TestingIConfigurable.py test_adapter.py 
Log Message:
Added local adapter service Python code.

=== Added File Zope3/lib/python/Zope/App/OFS/Services/tests/TestingIConfigurable.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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.
# 
##############################################################################
"""XXX short summary goes here.

XXX longer description goes here.

$Id: TestingIConfigurable.py,v 1.1.2.1 2002/12/10 14:38:36 jim Exp $
"""
from Zope.App.OFS.Services.ConfigurationInterfaces import IConfigurable
from Interface.Verify import verifyObject
from Zope.Proxy.ContextWrapper import getWrapperContainer

class TestingIConfigurable:
    """Base class for testing implementors of IConfigurable

    Subclasses must implement:

      - createTestingConfigurable()
        that returns a new configurable object with no configurations.

        This configuration object must be in the context of something
        that is not None.

      - createTestingConfiguration()
        that returns a configuration object.

    """

    def _assertInContext(self, ob, parent):
        """Assert that we have the proper context

        The container of ob must be the parent, and the parent must
        have some context.

        """
        self.assertEqual(getWrapperContainer(ob), parent)
        self.failIf(getWrapperContainer(getWrapperContainer(ob)) is None)

    def test_implements_IConfigurable(self):
        verifyObject(IConfigurable, self.createTestingConfigurable())

    def test_queryConfigurationsFor_no_config(self):
        configurable = self.createTestingConfigurable()
        configuration = self.createTestingConfiguration()
        self.failIf(configurable.queryConfigurationsFor(configuration))

        self.assertEqual(
            configurable.queryConfigurationsFor(configuration, 42),
            42)

    def test_createConfigurationsFor(self):
        configurable = self.createTestingConfigurable()
        configuration = self.createTestingConfiguration()
        registry = configurable.createConfigurationsFor(configuration)

        self.assertEqual(getWrapperContainer(registry), configurable)

        # If we call it again, we should get the same object
        self.assertEqual(configurable.createConfigurationsFor(configuration),
                         registry)

        self._assertInContext(registry, configurable)

        return registry

    def test_queryConfigurationsFor(self):
        configurable = self.createTestingConfigurable()
        configuration = self.createTestingConfiguration()

        cregistry = configurable.createConfigurationsFor(configuration)


        registry = configurable.queryConfigurationsFor(configuration)
        self.assertEqual(registry, cregistry)
        self._assertInContext(registry, configurable)

        registry = configurable.queryConfigurationsFor(configuration, 42)
        self.assertEqual(registry, cregistry)
        self._assertInContext(registry, configurable)


=== Added File Zope3/lib/python/Zope/App/OFS/Services/tests/test_adapter.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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.
#
##############################################################################
"""Test the adapter module

$Id: test_adapter.py,v 1.1.2.1 2002/12/10 14:38:36 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from TestingIConfigurable import TestingIConfigurable
from Zope.App.OFS.Services.adapter import AdapterService
from Interface import Interface
from Zope.Proxy.ContextWrapper import ContextWrapper
from Zope.ComponentArchitecture.Exceptions import ComponentLookupError
from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup \
     import PlacefulSetup
from Zope.App.OFS.Services.ServiceManager.ServiceManager import ServiceManager
from Zope.ComponentArchitecture import getServiceManager
from Zope.App.Traversing import traverse

class I1(Interface):
    pass

class I1E(I1):
    pass

class I2B(Interface):
    pass

class I2(I2B):
    pass

class I3(Interface):
    pass



class Configuration:
    forInterface = I1
    providedInterface = I2

    def getAdapter(self, object):
        return self.factory(object)

    def activated(self): pass
    def deactivated(self): pass

class C: pass
    
class Test(PlacefulSetup, TestingIConfigurable, TestCase):

    def createTestingConfigurable(self):
        return ContextWrapper(AdapterService(), C())

    def createTestingConfiguration(self):
        return Configuration()

    def test_queryAdapter_no_adapter(self):
        service = AdapterService()
        class O:
            __implements__ = I1

        o = O()
        self.assertEqual(service.queryAdapter(o, I2), None)
        self.assertEqual(service.queryAdapter(o, I2, 42), 42)

        self.assertEqual(service.queryAdapter(o, I1), o)
        self.assertEqual(service.queryAdapter(o, I1, 42), o)

    def test_getAdapter_no_adapter(self):
        service = AdapterService()
        class O:
            __implements__ = I1

        o = O()
        self.assertRaises(ComponentLookupError, service.getAdapter, O(), I2)
        self.assertEqual(service.getAdapter(o, I1), o)

    def test_queryAdapter_and_getAdapter(self):
        self.buildFolders()
        self.rootFolder.setServiceManager(ServiceManager())

        sm = traverse(self.rootFolder, '++etc++Services')

        configure = traverse(sm, 'Packages/default/configure')
        configuration = Configuration()
        configure.setObject('', configuration)
        configuration = traverse(configure, '1')

        service = ContextWrapper(AdapterService(), sm)
        class O:
            __implements__ = I1

        class A:
            def __init__(self, object):
                self.context = object

        configuration.factory = A
        
        registry = service.createConfigurationsFor(configuration)
        registry.register(configuration)
        registry.activate(configuration)

        o = O()

        for m in 'queryAdapter', 'getAdapter':
            for r in I1, I1E:
                for p in I2B, I2:
                    o = O()
                    o.__implements__ = r

                    adapter = getattr(service, m)(o, p)
                    self.assertEqual(adapter.__class__, A)
                    self.assertEqual(adapter.context, o)

            self.assertEqual(getattr(service, m)(o, I1), o)

        self.assertEqual(service.queryAdapter(o, I3), None)
        self.assertEqual(service.queryAdapter(o, I3, 42), 42)
        self.assertRaises(ComponentLookupError, service.getAdapter, O(), I3)
        


def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')