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

Marius Gedminas mgedmin@codeworks.lt
Tue, 10 Dec 2002 14:16:05 -0500


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

Added Files:
      Tag: named-component-configuration-branch
	testConfigurations.py testNameConfigurable.py 
Log Message:
Refactoring of configuration views:
  - new interfaces INamedComponentConfiguration, INameConfigurable,
    implemented in NamedComponentConfiguration, NameConfigurable, simplify
    the case where configurations are identified by a name (service types,
    connections, caches, queries, etc)
  - common views for INamedComponentConfiguration, INameConfigurable
  - refactored ServiceManager and ConnectionService to take advantage of the
    new infrastructure
  - incidentally wrote several unit tests for configuration classes
  - removed caching from ComponentConnection.getComponent; this exposed a bug
    in LocalEventService tests



=== Added File Zope3/lib/python/Zope/App/OFS/Services/tests/testConfigurations.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 tests for configuration classes defined in
Zope.App.OFS.Services.Configuration

$Id: testConfigurations.py,v 1.1.2.1 2002/12/10 19:16:04 mgedmin Exp $
"""

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')


=== Added File Zope3/lib/python/Zope/App/OFS/Services/tests/testNameConfigurable.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.
#
##############################################################################
"""NameConfigurable tests

$Id: testNameConfigurable.py,v 1.1.2.1 2002/12/10 19:16:04 mgedmin Exp $
"""

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',))


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


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