[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services/ConnectionService/tests - testConnectionConfiguration.py:1.1 testConnectionService.py:1.5

Viktorija Zaksiene ryzaja@codeworks.lt
Mon, 9 Dec 2002 10:27:13 -0500


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

Modified Files:
	testConnectionService.py 
Added Files:
	testConnectionConfiguration.py 
Log Message:
ConnectionService is no longer a container of database adapters; now the
latter are added directly into a package and bound to the connection service
via the common configuration mechanism.



=== Added File Zope3/lib/python/Zope/App/OFS/Services/ConnectionService/tests/testConnectionConfiguration.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.
#
##############################################################################
"""Unit test for ConnectionConfiguration.

$Id: testConnectionConfiguration.py,v 1.1 2002/12/09 15:26:42 ryzaja Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite

from Interface import Interface

from Zope.App.OFS.Services.ConnectionService.ConnectionConfiguration \
     import ConnectionConfiguration

from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup \
     import PlacefulSetup
from Zope.App.Traversing import traverse
from Zope.App.OFS.Services.ServiceManager.ServiceManager \
     import ServiceManager
from Zope.App.OFS.Container.ZopeContainerAdapter import ZopeContainerAdapter
from Zope.App.OFS.Services.ConfigurationInterfaces \
     import Active, Unregistered, Registered
from Zope.App.RDB.IZopeDatabaseAdapter import IZopeDatabaseAdapter
from Zope.App.DependencyFramework.IDependable import IDependable
from Zope.App.RDB.IConnectionService import IConnectionService
from Zope.App.OFS.Services.ConfigurationInterfaces import IConfigurable
from Zope.App.OFS.Services.Configuration import ConfigurationRegistry
from Zope.App.OFS.Services.ServiceManager.ServiceConfiguration \
     import ServiceConfiguration
from Zope.ContextWrapper import ContextMethod
from Zope.Proxy.ContextWrapper import ContextWrapper

class DependableStub:

    __implements__ = IDependable

    def addDependent(self, location):
        pass

    def removeDependent(self, location):
        pass

    def dependents(self):
        pass


class TestDA(DependableStub):

    __implements__ = IZopeDatabaseAdapter, IDependable


class ConnectionServiceStub(DependableStub):

    __implements__ = IConnectionService, IConfigurable, IDependable

    def __init__(self):
        self.bindings = {}

    def queryConfigurationsFor(self, cfg, default=None):
        return self.queryConfigurations(cfg.connectionName)
    queryConfigurationsFor = ContextMethod(queryConfigurationsFor)

    def queryConfigurations(self, name, default=None):
        registry = self.bindings.get(name, default)
        return ContextWrapper(registry, self)
    queryConfigurations = ContextMethod(queryConfigurations)

    def createConfigurationsFor(self, cfg):
        return self.createConfigurations(cfg.connectionName)
    createConfigurationsFor = ContextMethod(createConfigurationsFor)

    def createConfigurations(self, name):
        try:
            registry = self.bindings[name]
        except KeyError:
            self.bindings[name] = registry = ConfigurationRegistry()
        return ContextWrapper(registry, self)
    createConfigurations = ContextMethod(createConfigurations)


class TestConnectionConfiguration(PlacefulSetup, TestCase):

    def setUp(self):
        PlacefulSetup.setUp(self)
        self.buildFolders()
        self.rootFolder.setServiceManager(ServiceManager())
        sm = self.rootFolder.getServiceManager()

        self.default = traverse(self.rootFolder,
                           '++etc++Services/Packages/default')
        self.default.setObject('da', TestDA())
        self.da = traverse(self.default, 'da')

        self.cm = ZopeContainerAdapter(traverse(self.default, "configure"))
        self.cm.setObject('', ConnectionConfiguration('conn_name',
                            '/++etc++Services/Packages/default/da'))
        self.config = traverse(self.default, 'configure/1')

        self.default.setObject('conn_srv', ConnectionServiceStub())
        self.service = traverse(self.default, 'conn_srv')

        self.cm.setObject('', ServiceConfiguration('SQLDatabaseConnections',
                            '/++etc++Services/Packages/default/conn_srv'))
        traverse(self.default, 'configure/2').status = Active

    def tearDown(self):
        PlacefulSetup.tearDown(self)

    def test_getComponent(self):
        self.assertEqual(self.config.getComponent(), self.da)

    def test_status(self):
        self.assertEqual(self.config.status, Unregistered)
        self.config.status = Active
        self.assertEqual(self.config.status, Active)
        cr = self.service.queryConfigurations('conn_name')
        self.assertEqual(cr.active(), self.config)

    # Unit tests for ComponentConfiguration should take care
    # of test_manage_afterAdd, test_manage_beforeDelete_and_unregistered,
    # test_disallow_delete_when_active


def test_suite():
    return makeSuite(TestConnectionConfiguration)


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


=== Zope3/lib/python/Zope/App/OFS/Services/ConnectionService/tests/testConnectionService.py 1.4 => 1.5 ===
--- Zope3/lib/python/Zope/App/OFS/Services/ConnectionService/tests/testConnectionService.py:1.4	Wed Dec  4 12:57:17 2002
+++ Zope3/lib/python/Zope/App/OFS/Services/ConnectionService/tests/testConnectionService.py	Mon Dec  9 10:26:42 2002
@@ -16,21 +16,39 @@
 """
 
 import unittest
-from Zope.App.tests.PlacelessSetup import PlacelessSetup
 from Zope.ComponentArchitecture.GlobalServiceManager import \
      serviceManager as sm
+from Zope.App.Traversing import traverse
 from Zope.App.RDB.IZopeConnection import IZopeConnection
 from Zope.App.RDB.IZopeDatabaseAdapter import IZopeDatabaseAdapter
 from Zope.App.RDB.IConnectionService import \
      IConnectionService
+from Zope.App.OFS.Container.ZopeContainerAdapter import ZopeContainerAdapter
 from Zope.App.OFS.Services.ConnectionService.ConnectionService import \
      ConnectionService
+from Zope.App.OFS.Services.ServiceManager.ServiceManager \
+     import ServiceManager
+from Zope.App.OFS.Services.ServiceManager.ServiceConfiguration \
+     import ServiceConfiguration
+from Zope.App.DependencyFramework.IDependable import IDependable
+from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup \
+     import PlacefulSetup
+from Zope.App.OFS.Annotation.IAnnotatable import IAnnotatable
+from Zope.App.OFS.Annotation.IAttributeAnnotatable import IAttributeAnnotatable
+from Zope.App.OFS.Annotation.AttributeAnnotations import AttributeAnnotations
+from Zope.App.OFS.Annotation.IAnnotations import IAnnotations
+from Zope.App.DependencyFramework.IDependable import IDependable
+from Zope.App.DependencyFramework.Dependable import Dependable
+from Zope.ComponentArchitecture.GlobalAdapterService import provideAdapter
+from Zope.App.OFS.Services.ConfigurationInterfaces \
+     import Active, Unregistered, Registered
+from Zope.App.OFS.Services.ConnectionService.ConnectionConfiguration \
+     import ConnectionConfiguration
 
 
 class DAStub:
-    """ """
 
-    __implements__ = IZopeDatabaseAdapter
+    __implements__ = IZopeDatabaseAdapter, IAttributeAnnotatable
 
     def __call__(self):
         return 'Connection'
@@ -41,15 +59,38 @@
     return list
 
 
-class TestConnectionService(unittest.TestCase, PlacelessSetup):
+class TestConnectionService(unittest.TestCase, PlacefulSetup):
 
     def setUp(self):
-        PlacelessSetup.setUp(self)
-        self.service = ConnectionService()
-        self.service.setObject('conn1', DAStub())
-        self.service.setObject('conn2', DAStub())
-        sm.defineService('SQLDatabaseConnections', IConnectionService)
-        sm.provideService('SQLDatabaseConnections', self.service)
+        PlacefulSetup.setUp(self)
+
+        provideAdapter(IAttributeAnnotatable,
+                       IAnnotations, AttributeAnnotations)
+        provideAdapter(IAnnotatable, IDependable, Dependable)
+
+        self.buildFolders()
+        self.rootFolder.setServiceManager(ServiceManager())
+        sm = self.rootFolder.getServiceManager()
+
+        self.default = traverse(self.rootFolder,
+                           '++etc++Services/Packages/default')
+        self.default.setObject('conn_srv', ConnectionService())
+        self.service = traverse(self.default, 'conn_srv')
+
+        self.cm = ZopeContainerAdapter(traverse(self.default, "configure"))
+        self.cm.setObject('', ServiceConfiguration('SQLDatabaseConnections',
+                            '/++etc++Services/Packages/default/conn_srv'))
+        traverse(self.default, 'configure/1').status = Active
+
+        self.default.setObject('da1', DAStub())
+        self.default.setObject('da2', DAStub())
+
+        self.cm.setObject('', ConnectionConfiguration('conn1',
+                            '/++etc++Services/Packages/default/da1'))
+        traverse(self.default, 'configure/2').status = Active
+        self.cm.setObject('', ConnectionConfiguration('conn2',
+                            '/++etc++Services/Packages/default/da2'))
+        traverse(self.default, 'configure/3').status = Active
 
     def testGetConnection(self):
         self.assertEqual('Connection',
@@ -68,10 +109,6 @@
         self.assertEqual(['conn1', 'conn2'],
                          sort(self.service.getAvailableConnections()))
 
-    def testIsAddable(self):
-        self.assertEqual(1, self.service.isAddable(IZopeDatabaseAdapter))
-        self.assertEqual(0, self.service.isAddable(IZopeConnection))
-        
 
 def test_suite():
     suite = unittest.TestSuite()