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

Jim Fulton jim@zope.com
Thu, 19 Dec 2002 15:38:27 -0500


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

Added Files:
	TestingIConfigurable.py test_adapter.py test_field.py 
	test_view.py 
Log Message:
Merged changes made by Albertas and Jim from the AdapterAndView-branch
branch:

- Added TTW adapter service

  Todo: 

    o Named adapters

    o Getting classes in persistent modules working so we can actually
      create TTW adapters.

- Added TTW view service

  o View service

  o View configurations
 
    For configuting views from view factories

  o Page configurations 

    For configuring views based on templates (and optional classes)

  o View (sub)packages.  These get added to packages. You configure
    them with default configuration info and then add page templates
    to them. Added page temlates are automatically added as views with
    the same name.




=== Zope3/lib/python/Zope/App/OFS/Services/tests/TestingIConfigurable.py 1.1 => 1.2 ===
--- /dev/null	Thu Dec 19 15:38:27 2002
+++ Zope3/lib/python/Zope/App/OFS/Services/tests/TestingIConfigurable.py	Thu Dec 19 15:38:26 2002
@@ -0,0 +1,90 @@
+##############################################################################
+#
+# 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$
+"""
+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)


=== Zope3/lib/python/Zope/App/OFS/Services/tests/test_adapter.py 1.1 => 1.2 ===
--- /dev/null	Thu Dec 19 15:38:27 2002
+++ Zope3/lib/python/Zope/App/OFS/Services/tests/test_adapter.py	Thu Dec 19 15:38:26 2002
@@ -0,0 +1,240 @@
+##############################################################################
+#
+# 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$
+"""
+
+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.App.OFS.Services.adapter import AdapterConfiguration
+from Zope.App.OFS.Content.Folder.RootFolder import RootFolder
+from Zope.ComponentArchitecture import getServiceManager
+from Zope.App.Traversing import traverse
+from Zope.ComponentArchitecture.IServiceService import IServiceService
+from Zope.ComponentArchitecture.GlobalAdapterService import provideAdapter
+
+class I1(Interface):
+    pass
+
+class I1E(I1):
+    pass
+
+class I2B(Interface):
+    pass
+
+class I2(I2B):
+    pass
+
+class I3(Interface):
+    pass
+
+class I4(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 A:
+    def __init__(self, object):
+        self.context = object
+
+
+class TestAdapterService(PlacefulSetup, TestingIConfigurable, TestCase):
+
+    def setUp(self):
+        PlacefulSetup.setUp(self)
+        self.buildFolders()
+        self.rootFolder.setServiceManager(ServiceManager())
+        self._service = ContextWrapper(AdapterService(), self.rootFolder)
+
+
+    def test_implements_IAdapterService(self):
+        from Zope.ComponentArchitecture.IAdapterService import IAdapterService
+        from Interface.Verify import verifyObject
+
+        verifyObject(IAdapterService, self._service)
+
+    def createTestingConfigurable(self):
+        return ContextWrapper(AdapterService(), C())
+
+    def createTestingConfiguration(self):
+        return Configuration()
+
+    def test_queryAdapter_no_adapter(self):
+        service = self._service
+        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 = self._service
+        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):
+        service = self._service
+
+        sm = traverse(self.rootFolder, '++etc++Services')
+
+        configure = traverse(sm, 'Packages/default/configure')
+        configuration = Configuration()
+        configure.setObject('', configuration)
+        configuration = traverse(configure, '1')
+
+        class O:
+            __implements__ = I1
+
+        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_queryAdapter_delegation(self):
+        service = self._service
+        
+        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')
+
+        class O:
+            __implements__ = I1
+
+        configuration.factory = A
+
+        registry = service.createConfigurationsFor(configuration)
+        registry.register(configuration)
+        registry.activate(configuration)
+
+        o = O()
+
+        class A2(A): pass
+
+        provideAdapter(I1, I4, A2)
+    
+        adapter = service.queryAdapter(o, I4)
+        self.assertEqual(adapter.__class__, A2)
+        self.assertEqual(adapter.context, o)
+
+    def test_queryAdapter_delegation_w_no_adapters_locally(self):
+        service = self._service
+
+        class O:
+            __implements__ = I1
+
+        o = O()
+
+        class A2(A): pass
+
+        provideAdapter(I1, I4, A2)
+    
+        adapter = service.queryAdapter(o, I4)
+        self.assertEqual(adapter.__class__, A2)
+        self.assertEqual(adapter.context, o)
+
+    def test_getRegisteredMatching(self):
+        self.test_queryAdapter_and_getAdapter()
+        registry = self._service.queryConfigurations(I1, I2, '')
+
+        for args in ((), (I1E, ), (None, I2), (I1E, I2), ):
+            r = self._service.getRegisteredMatching(*args)
+            self.assertEqual(list(r), [(I1, I2, registry)])
+
+class PhonyServiceManager:
+
+    __implements__ = IServiceService
+
+    def resolve(self, name):
+        if name == 'Foo.Bar.A':
+            return A
+        
+class TestAdapterConfiguration(PlacefulSetup, TestCase):
+
+    def setUp(self):
+        PlacefulSetup.setUp(self)
+        rootFolder = RootFolder()
+        rootFolder.setServiceManager(PhonyServiceManager())
+        self.configuration = ContextWrapper(
+            AdapterConfiguration(I1, I2, "Foo.Bar.A"),
+            rootFolder,
+            )
+    
+    def test_getAdapter(self):
+        c = C()
+        adapter = self.configuration.getAdapter(c)
+        self.assertEqual(adapter.__class__, A)
+        self.assertEqual(adapter.context, c)
+        self.assertEqual(self.configuration.forInterface, I1)
+        self.assertEqual(self.configuration.providedInterface, I2)
+
+def test_suite():
+    return TestSuite((
+        makeSuite(TestAdapterService),
+        makeSuite(TestAdapterConfiguration),
+        ))
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')


=== Zope3/lib/python/Zope/App/OFS/Services/tests/test_field.py 1.1 => 1.2 ===
--- /dev/null	Thu Dec 19 15:38:27 2002
+++ Zope3/lib/python/Zope/App/OFS/Services/tests/test_field.py	Thu Dec 19 15:38:26 2002
@@ -0,0 +1,58 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Tests for ComponentLocation field.
+
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup \
+     import PlacefulSetup
+from Zope.App.Traversing import traverse
+from Zope.Schema.Exceptions import ValidationError
+from Interface import Interface
+from Zope.App.OFS.Services.field import ComponentLocation
+
+class I1(Interface):  pass
+
+class C:
+    __implements__ = I1
+
+class D:
+    pass
+
+class Test(PlacefulSetup, TestCase):
+
+    def test__validate(self):
+        self.buildFolders()
+        self.folder1.setObject('c', C())
+        self.folder1.setObject('d', D())
+
+        folder2 = traverse(self.rootFolder, 'folder2')
+
+        field = ComponentLocation(type=I1)
+        field = field.bind(folder2)
+
+        field.validate(u'/folder1/c')
+
+        self.assertRaises(ValidationError, field.validate, u'/folder1/d')
+        self.assertRaises(ValidationError, field.validate, u'/folder1/e')
+
+def test_suite():
+    return TestSuite((
+        makeSuite(Test),
+        ))
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')


=== Zope3/lib/python/Zope/App/OFS/Services/tests/test_view.py 1.1 => 1.2 ===
--- /dev/null	Thu Dec 19 15:38:27 2002
+++ Zope3/lib/python/Zope/App/OFS/Services/tests/test_view.py	Thu Dec 19 15:38:26 2002
@@ -0,0 +1,260 @@
+##############################################################################
+#
+# 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 view module
+
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from TestingIConfigurable import TestingIConfigurable
+from Zope.App.OFS.Services.view import ViewService
+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.App.OFS.Services.view import ViewConfiguration
+from Zope.App.OFS.Content.Folder.RootFolder import RootFolder
+from Zope.ComponentArchitecture import getServiceManager
+from Zope.App.Traversing import traverse
+from Zope.ComponentArchitecture.IServiceService import IServiceService
+from Zope.ComponentArchitecture.GlobalViewService import provideView
+from Zope.Publisher.Browser.BrowserRequest import TestRequest
+from Zope.Publisher.Browser.IBrowserPresentation import IBrowserPresentation
+from Zope.App.OFS.Services.interfaces import IZPTTemplate
+from Zope.App.OFS.Services.view import PageConfiguration, BoundTemplate
+from Interface.Verify import verifyObject
+from Zope.ComponentArchitecture.IViewService import IViewService
+
+class I1(Interface):
+    pass
+
+class I1E(I1):
+    pass
+
+I2 = IBrowserPresentation
+
+class I3(Interface):
+    pass
+
+class I4(Interface):
+    pass
+
+
+class Configuration:
+    forInterface = I1
+    presentationType = I2
+    viewName = 'test'
+    layer = 'default'
+
+    def getView(self, object, request):
+        return self.factory(object, request)
+
+    def activated(self): pass
+    def deactivated(self): pass
+
+class C: pass
+
+class A:
+    def __init__(self, object, request):
+        self.context = object
+        self.request = request
+
+
+class TestViewService(PlacefulSetup, TestingIConfigurable, TestCase):
+
+    def setUp(self):
+        PlacefulSetup.setUp(self)
+        self.buildFolders()
+        self.rootFolder.setServiceManager(ServiceManager())
+        self._service = ContextWrapper(ViewService(), self.rootFolder)
+
+    def test_implements_IViewService(self):
+        from Zope.ComponentArchitecture.IViewService import IViewService
+        from Interface.Verify import verifyObject
+
+        verifyObject(IViewService, self._service)
+
+
+    def createTestingConfigurable(self):
+        return ContextWrapper(ViewService(), C())
+
+    def createTestingConfiguration(self):
+        return Configuration()
+
+    def test_implements_IViewService(self):
+        verifyObject(IViewService, ViewService())
+
+    def test_queryView_no_view(self):
+        service = self._service
+        class O:
+            __implements__ = I1
+
+        o = O()
+        request = TestRequest()
+        self.assertEqual(service.queryView(o, 'test', request), None)
+        self.assertEqual(service.queryView(o, 'test', request, 42), 42)
+
+    def test_getView_no_view(self):
+        service = self._service
+        class O:
+            __implements__ = I1
+
+        o = O()
+        request = TestRequest()
+        self.assertRaises(ComponentLookupError,
+                          service.getView, O(), 'test', request)
+
+    def test_queryView_and_getView(self):
+        service = self._service
+
+        sm = traverse(self.rootFolder, '++etc++Services')
+
+        configure = traverse(sm, 'Packages/default/configure')
+        configuration = Configuration()
+        configure.setObject('', configuration)
+        configuration = traverse(configure, '1')
+
+        class O:
+            __implements__ = I1
+
+        configuration.factory = A
+        
+        registry = service.createConfigurationsFor(configuration)
+        registry.register(configuration)
+        registry.activate(configuration)
+
+        o = O()
+        request = TestRequest()
+
+        for m in 'queryView', 'getView':
+            for r in I1, I1E:
+                o = O()
+                o.__implements__ = r
+
+                view = getattr(service, m)(o, 'test', request)
+                self.assertEqual(view.__class__, A)
+                self.assertEqual(view.context, o)
+                self.assertEqual(view.request, request)
+
+    def test_queryView_delegation(self):
+        service = self._service
+        
+        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')
+
+        class O:
+            __implements__ = I1
+
+        o = O()
+        request = TestRequest()
+
+        class A2(A): pass
+
+        provideView(I1, 'test', IBrowserPresentation, A2)
+    
+        view = service.queryView(o, 'test', request)
+        self.assertEqual(view.__class__, A2)
+        self.assertEqual(view.context, o)
+        self.assertEqual(view.request, request)
+
+    def test_getRegisteredMatching(self):
+        self.test_queryView_and_getView()
+        registry = self._service.queryConfigurationsFor(Configuration())
+
+        for args in ((), (I1E, ), (None, I2), (I1E, I2), ):
+            r = self._service.getRegisteredMatching(*args)
+            self.assertEqual(list(r), [(I1, I2, registry, 'default', 'test')])
+
+class PhonyServiceManager(ServiceManager):
+
+    __implements__ = IServiceService
+
+    def resolve(self, name):
+        if name == 'Foo.Bar.A':
+            return A
+        
+class TestViewConfiguration(PlacefulSetup, TestCase):
+
+    def setUp(self):
+        PlacefulSetup.setUp(self)
+        rootFolder = RootFolder()
+        rootFolder.setServiceManager(PhonyServiceManager())
+        self.configuration = ContextWrapper(
+            ViewConfiguration(I1, 'test', IBrowserPresentation, "Foo.Bar.A"),
+            rootFolder,
+            )
+    
+    def test_getView(self):
+        c = C()
+        request = TestRequest()
+        view = self.configuration.getView(c, request)
+        self.assertEqual(view.__class__, A)
+        self.assertEqual(view.context, c)
+        self.assertEqual(view.request, request)
+        self.assertEqual(self.configuration.forInterface, I1)
+        self.assertEqual(self.configuration.presentationType, I2)
+
+class PhonyTemplate:
+
+    __implements__ = IZPTTemplate
+        
+class TestPageConfiguration(PlacefulSetup, TestCase):
+
+    def setUp(self):
+        PlacefulSetup.setUp(self)
+        rootFolder = RootFolder()
+        rootFolder.setServiceManager(PhonyServiceManager())
+        default = traverse(rootFolder, '++etc++Services/Packages/default')
+        self.__template = PhonyTemplate()
+        default.setObject('t', self.__template)
+        self.__configuration = ContextWrapper(
+            PageConfiguration(I1, 'test', IBrowserPresentation,
+                              "Foo.Bar.A",
+                              '/++etc++Services/Packages/default/t',
+                              ),
+            rootFolder,
+            )
+    
+    def test_getView(self):
+        c = C()
+        request = TestRequest()
+        view = self.__configuration.getView(c, request)
+        self.assertEqual(view.__class__, BoundTemplate)
+        self.assertEqual(view.template, self.__template)
+
+        view = view.view
+        self.assertEqual(view.__class__, A)
+        self.assertEqual(view.context, c)
+        self.assertEqual(view.request, request)
+        self.assertEqual(self.__configuration.forInterface, I1)
+        self.assertEqual(self.__configuration.presentationType, I2)
+
+def test_suite():
+    return TestSuite((
+        makeSuite(TestViewService),
+        makeSuite(TestViewConfiguration),
+        makeSuite(TestPageConfiguration),
+        ))
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')