[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services/tests - testConfigurations.py:1.2 testNameConfigurable.py:1.2 testConfigurationStatusProperty.py:1.3

Marius Gedminas mgedmin@codeworks.lt
Thu, 12 Dec 2002 06:33:05 -0500


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

Modified Files:
	testConfigurationStatusProperty.py 
Added Files:
	testConfigurations.py testNameConfigurable.py 
Log Message:
Merge named-component-configuration-branch



=== Zope3/lib/python/Zope/App/OFS/Services/tests/testConfigurations.py 1.1 => 1.2 ===
--- /dev/null	Thu Dec 12 06:33:04 2002
+++ Zope3/lib/python/Zope/App/OFS/Services/tests/testConfigurations.py	Thu Dec 12 06:32:34 2002
@@ -0,0 +1,149 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Unit tests for configuration classes defined in
+Zope.App.OFS.Services.Configuration
+
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+
+from Interface import Interface
+from Zope.App.OFS.Services.ConfigurationInterfaces \
+     import Active, Registered, Unregistered
+from Zope.App.DependencyFramework.Exceptions import DependencyError
+from Zope.App.OFS.Services.Configuration import SimpleConfiguration
+from Zope.App.OFS.Services.Configuration import ComponentConfiguration
+from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup import PlacefulSetup
+from Zope.App.OFS.Services.tests.TestingServiceManager import TestingServiceManager
+from Zope.Proxy.ContextWrapper import ContextWrapper
+from Zope.App.DependencyFramework.IDependable import IDependable
+from Zope.App.Traversing import traverse
+from Zope.Security.Proxy import Proxy
+
+
+class ITestComponent(Interface):
+    pass
+
+class ComponentStub:
+
+    __implements__ = IDependable
+
+    _dependents = ()
+
+    def addDependent(self, location):
+        self._dependents = tuple(
+            [d for d in self._dependents if d != location]
+            +
+            [location]
+            )
+
+    def removeDependent(self, location):
+        self._dependents = tuple(
+            [d for d in self._dependents if d != location]
+            )
+
+    def dependents(self):
+        return self._dependents
+
+
+
+class TestSimpleConfiguration(TestCase):
+
+    def test_manage_beforeDelete(self):
+        container = object()
+        cfg = SimpleConfiguration()
+
+        # cannot delete an active configuration
+        cfg.status = Active
+        self.assertRaises(DependencyError, cfg.manage_beforeDelete, cfg,
+                          container)
+
+        # deletion of a registered configuration causes it to become unregistered
+        cfg.status = Registered
+        cfg.manage_beforeDelete(cfg, container)
+        self.assertEquals(cfg.status, Unregistered)
+
+
+class TestComponentConfiguration(TestSimpleConfiguration, PlacefulSetup):
+
+    def setUp(self):
+        PlacefulSetup.setUp(self)
+        self.buildFolders()
+        self.__sm = TestingServiceManager()
+        self.rootFolder.setServiceManager(self.__sm)
+
+    def test_getComponent(self):
+        # set up a component
+        path, component = 'foo', object()
+        self.rootFolder.setObject(path, component)
+        # set up a configuration
+        cfg = ComponentConfiguration(path)
+        cfg = ContextWrapper(cfg, self.rootFolder)
+        # check that getComponent finds the configuration
+        self.assertEquals(cfg.getComponent(), component)
+
+    def test_getComponent_permission(self):
+        # set up a component
+        path, component = 'foo', object()
+        self.rootFolder.setObject(path, component)
+        # set up a configuration
+        cfg = ComponentConfiguration(path, 'Zope.TopSecret')
+        cfg.getInterface = lambda: ITestComponent
+        cfg = ContextWrapper(cfg, self.rootFolder)
+        # check that getComponent finds the configuration
+        result = cfg.getComponent()
+        self.assertEquals(result, component)
+        self.failUnless(type(result) is Proxy)
+
+    def test_manage_afterAdd(self):
+        # set up a component
+        path, component = 'foo', ComponentStub()
+        self.rootFolder.setObject(path, component)
+        # set up a configuration
+        cfg = ComponentConfiguration(path)
+        self.rootFolder.setObject('cfg', cfg)
+        cfg = traverse(self.rootFolder, 'cfg')
+        # simulate IAddNotifiable
+        cfg.manage_afterAdd(cfg, self.rootFolder)
+        # check that the dependency tracking works
+        self.assertEquals(component.dependents(), ('/cfg',))
+
+    def test_manage_beforeDelete_dependents(self):
+        # set up a component
+        path, component = 'foo', ComponentStub()
+        self.rootFolder.setObject(path, component)
+        component.addDependent('/cfg')
+        # set up a configuration
+        cfg = ComponentConfiguration(path)
+        cfg.status = Unregistered
+        self.rootFolder.setObject('cfg', cfg)
+        cfg = traverse(self.rootFolder, 'cfg')
+        # simulate IDeleteNotifiable
+        cfg.manage_beforeDelete(cfg, self.rootFolder)
+        # check that the dependency tracking works
+        self.assertEquals(component.dependents(), ())
+
+
+# NamedComponentConfiguration is too simple to need testing at the moment
+
+
+def test_suite():
+    return TestSuite((
+        makeSuite(TestSimpleConfiguration),
+        makeSuite(TestComponentConfiguration),
+        ))
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')


=== Zope3/lib/python/Zope/App/OFS/Services/tests/testNameConfigurable.py 1.1 => 1.2 ===
--- /dev/null	Thu Dec 12 06:33:04 2002
+++ Zope3/lib/python/Zope/App/OFS/Services/tests/testNameConfigurable.py	Thu Dec 12 06:32:34 2002
@@ -0,0 +1,116 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""NameConfigurable tests
+
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+
+from Zope.App.OFS.Services.Configuration import NameConfigurable
+from Zope.Proxy.ContextWrapper import ContextWrapper
+from Zope.Proxy.ContextWrapper import getWrapperContainer
+from Zope.Proxy.ContextWrapper import getWrapperContext
+
+
+class ConfigurationStub:
+
+    def __init__(self, **kw):
+        self.__dict__.update(kw)
+
+class RegistryStub:
+
+    pass
+
+
+class TestNameConfigurable(TestCase):
+
+    def setUp(self):
+        self.container = object()
+        self.subject = ContextWrapper(NameConfigurable(), self.container)
+
+    def test_queryConfigurationsFor(self):
+        subject = self.subject
+        cfg = ConfigurationStub(name="Foo")
+        self.assertEquals(subject.queryConfigurationsFor(cfg), None)
+        self.assertEquals(subject.queryConfigurationsFor(cfg, 42), 42)
+
+        registry = RegistryStub()
+        subject._bindings["Foo"] = registry
+        result = subject.queryConfigurationsFor(cfg)
+        self.assertEquals(result, registry)
+        self.assertEquals(getWrapperContainer(result), subject)
+        self.assertEquals(getWrapperContainer(getWrapperContainer(result)),
+                          self.container)
+
+    def test_queryConfigurations(self):
+        subject = self.subject
+        self.assertEquals(subject.queryConfigurations("Foo"), None)
+        self.assertEquals(subject.queryConfigurations("Foo", 42), 42)
+
+        registry = RegistryStub()
+        subject._bindings["Foo"] = registry
+        result = subject.queryConfigurations("Foo")
+        self.assertEquals(result, registry)
+        self.assertEquals(getWrapperContainer(result), subject)
+        self.assertEquals(getWrapperContainer(getWrapperContainer(result)),
+                          self.container)
+
+    def test_createConfigurationsFor(self):
+        subject = self.subject
+        cfg1 = ConfigurationStub(name='Foo')
+        cfg2 = ConfigurationStub(name='Bar')
+        r1 = subject.createConfigurationsFor(cfg1)
+        r2 = subject.createConfigurationsFor(cfg2)
+        r3 = subject.createConfigurationsFor(cfg1)
+        self.assertEquals(r1, r3)
+        self.assertNotEquals(r1, r2)
+        self.assertNotEquals(r2, r3)
+        self.assertEquals(r3, subject._bindings['Foo'])
+        self.assertEquals(getWrapperContainer(r3), subject)
+        self.assertEquals(getWrapperContainer(getWrapperContainer(r3)),
+                          self.container)
+        self.failUnless(subject._p_changed)
+
+    def test_createConfigurations(self):
+        subject = self.subject
+        r1 = subject.createConfigurations('Foo')
+        r2 = subject.createConfigurations('Bar')
+        r3 = subject.createConfigurations('Foo')
+        self.assertEquals(r1, r3)
+        self.assertNotEquals(r1, r2)
+        self.assertNotEquals(r2, r3)
+        self.assertEquals(r3, subject._bindings['Foo'])
+        self.assertEquals(getWrapperContainer(r3), subject)
+        self.assertEquals(getWrapperContainer(getWrapperContainer(r3)),
+                          self.container)
+        self.failUnless(subject._p_changed)
+
+    def test_listConfigurationNames(self):
+        subject = self.subject
+        self.assertEquals(tuple(subject.listConfigurationNames()), ())
+        subject._bindings['Foo'] = 1
+        self.assertEquals(tuple(subject.listConfigurationNames()), ('Foo',))
+        subject._bindings['Bar'] = 0   # false values should be filtered out
+        self.assertEquals(tuple(subject.listConfigurationNames()), ('Foo',))
+
+
+def test_suite():
+    return TestSuite((
+        makeSuite(TestNameConfigurable),
+        ))
+
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')


=== Zope3/lib/python/Zope/App/OFS/Services/tests/testConfigurationStatusProperty.py 1.2 => 1.3 ===
--- Zope3/lib/python/Zope/App/OFS/Services/tests/testConfigurationStatusProperty.py:1.2	Sat Nov 30 13:35:55 2002
+++ Zope3/lib/python/Zope/App/OFS/Services/tests/testConfigurationStatusProperty.py	Thu Dec 12 06:32:34 2002
@@ -29,11 +29,16 @@
 from Zope.App.OFS.Services.ConfigurationInterfaces \
      import Active, Unregistered, Registered
 from Zope.Proxy.ContextWrapper import ContextWrapper
+from Zope.ComponentArchitecture.Exceptions import ComponentLookupError
+
 
 class TestingConfiguration(TestingConfiguration):
     status = ConfigurationStatusProperty("Services")
     service_type = "Test"
 
+class PassiveConfiguration(TestingConfiguration):
+    status = ConfigurationStatusProperty("NoSuchService")
+
 class TestingConfigurationRegistry(TestingConfigurationRegistry):
     class_ = TestingConfiguration
 
@@ -46,7 +51,13 @@
     def getService(self, name):
         if name == "Services":
             return self
-        raise ValueError("Wrong service name", name)
+        raise ComponentLookupError("Wrong service name", name)
+
+    def queryService(self, name, default=None):
+        if name == "Services":
+            return self
+        else:
+            return default
 
     def queryConfigurationsFor(self, configuration, default=None):
         if configuration.service_type != "Test":
@@ -68,7 +79,7 @@
         self.__sm = TestingServiceManager()
         self.rootFolder.setServiceManager(self.__sm)
 
-    def test(self):
+    def test_property(self):
 
         configa = ContextWrapper(TestingConfiguration('a'), self.rootFolder)
         self.assertEqual(configa.status, Unregistered)
@@ -106,6 +117,32 @@
         self.assertEqual(configc.status, Unregistered)
         self.assertEqual(configb.status, Registered)
         self.assertEqual(configa.status, Registered)
+
+    def test_passive(self):
+        # scenario:
+        #   1. create and configure an SQLConnectionService
+        #   2. create and configure a database adapter&connection
+        #   3. disable SQLConnectionService
+        # now the ConnectionConfiguration.status cannot access the
+        # SQLConnectionService
+
+        configa = ContextWrapper(PassiveConfiguration('a'), self.rootFolder)
+        self.assertEqual(configa.status, Unregistered)
+
+        try:
+            configa.status = Registered
+        except ComponentLookupError:
+            self.assertEqual(configa.status, Unregistered)
+        else:
+            self.fail("should complain about missing service")
+
+        try:
+            configa.status = Active
+        except ComponentLookupError:
+            self.assertEqual(configa.status, Unregistered)
+        else:
+            self.fail("should complain about missing service")
+
 
 def test_suite():
     return TestSuite((