[Zope3-checkins] CVS: Zope3/src/zope/app/module/tests - __init__.py:1.1 test_module.py:1.1 test_modulelookup.py:1.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Mar 10 12:00:56 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/module/tests
In directory cvs.zope.org:/tmp/cvs-serv6102/src/zope/app/module/tests

Added Files:
	__init__.py test_module.py test_modulelookup.py 
Log Message:


Moved persistent module (manager) code to zope.app.module. I did not provide
any module aliases, since noone uses this feature yet based on a recent ML
poll. If someone really needs the aliases, please let me know and I will add
them.


=== Added File Zope3/src/zope/app/module/tests/__init__.py ===
# Import this.


=== Added File Zope3/src/zope/app/module/tests/test_module.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.
#
##############################################################################
"""Peristent Module tests

$Id: test_module.py,v 1.1 2004/03/10 17:00:55 srichter Exp $
"""
import unittest

from zope.interface import Interface, implements
from zope.app.services.tests.placefulsetup import PlacefulSetup
from zope.app.traversing import traverse
from zope.app.module import Manager
from ZODB.tests.util import DB
from transaction import get_transaction


class ITestService(Interface):
    pass

class TestService:

    implements(ITestService)


NAME = 'zope.app.services.tests.sample1'

called = 0

SOURCE = """\
class C:
    def __init__(self, v):
        self.ini = v

x = 1
from zope.app.module.tests import test_module

test_module.called += 1
"""

class LocalModuleTests(PlacefulSetup, unittest.TestCase):

    def setUp(self):
        PlacefulSetup.setUp(self, site=True)
        self.sm = traverse(self.rootFolder, "++etc++site")
        default = traverse(self.sm, "default")
        old_called = called
        default[NAME] = Manager(NAME, SOURCE)
        self.manager = traverse(default, NAME)
        self.assertEqual(called, old_called)
        self.manager.execute()
        self.assertEqual(called, old_called + 1)

    def test_module_persistence(self):
        db = DB()
        conn = db.open()
        root = conn.root()
        root['Application'] = self.rootFolder
        get_transaction().commit()
        # The findModule() here is only for the
        # RegistrationManagerContainer, not the SiteManager.
        default = traverse(self.rootFolder, "++etc++site/default")
        m = default.findModule(NAME)

        c = m.C(42)
        self.assertEqual(c.ini, 42)
        self.assertEqual(m.x, 1)

        # This tests that the module can be seen from a different
        # connection; an earlier version had a bug that requires this
        # regression check.
        conn2 = db.open()
        rootFolder2 = conn2.root()['Application']
        default = traverse(rootFolder2, "++etc++site/default")
        m = default.findModule(NAME)

        c = m.C(42)
        self.assertEqual(c.ini, 42)
        self.assertEqual(m.x, 1)

    def test_recompile(self):
        old_called = called
        self.manager.source += "\n"
        self.assertEqual(called, old_called)
        m = self.manager.getModule()
        self.assertEqual(called, old_called+1)
        m = self.manager.getModule()
        self.assertEqual(called, old_called+1)
        


def test_suite():
    return unittest.makeSuite(LocalModuleTests)

if __name__=='__main__':
    unittest.main(defaultTest="test_suite")


=== Added File Zope3/src/zope/app/module/tests/test_modulelookup.py ===
##############################################################################
#
# Copyright (c) 2003 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.
#
##############################################################################
"""Local module lookup tests

Note that when we get around to implementing module services, those
tests will go here too.

$Id: test_modulelookup.py,v 1.1 2004/03/10 17:00:55 srichter Exp $
"""
from zope.testing.doctestunit import DocTestSuite

from zope.app.services.registration import RegistrationManagerContainer
from zope.app.module.interfaces import IModuleManager
from zope.interface import implements
from zope.app.container.contained import Contained, setitem
from zope.app.tests.placelesssetup import setUp, tearDown

class MyModuleManager(object):
    implements(IModuleManager)
    
    def __init__(self, module):
        self.module = module

    def getModule(self):
        return self.module

class MyFolder(RegistrationManagerContainer, dict, Contained):
    def __setitem__(self, name, object):
        setitem(self, super(MyFolder, self).__setitem__, name, object)


def test_findMoule():
    """
    Tests for RegistrationManagerContainer.findModule().

    >>> folder = MyFolder()
    >>> folder['m1.py'] = MyModuleManager(1)
    >>> folder['m1'] = MyModuleManager(0)
    >>> folder['m2'] = MyModuleManager(2)
    >>> next = MyFolder()
    >>> next['m3'] = MyModuleManager(3)
    >>> next['z.y.m4'] = MyModuleManager(4)
    >>> folder.__parent__ = next

    >>> folder.findModule('m1')
    1
    >>> folder.findModule('m2')
    2
    >>> folder.findModule('m3')
    3
    >>> folder.findModule('z.y.m4')
    4
    >>> folder.findModule('m5')
    Traceback (most recent call last):
    ...
    ImportError: m5

    >>> import zope.app.module.tests.test_modulelookup
    >>> m = folder.findModule('zope.app.module.tests.test_modulelookup')
    >>> int(m is zope.app.module.tests.test_modulelookup)
    1
    
    """

def test_resolve():
    """
    >>> folder = MyFolder()
    >>> import zope.app.module.tests.test_modulelookup
    >>> f = folder.resolve(
    ...    'zope.app.module.tests.test_modulelookup.test_resolve')
    >>> int(f is zope.app.module.tests.test_modulelookup.test_resolve)
    1
    """

def test_suite():
    return DocTestSuite(setUp=setUp, tearDown=tearDown)

if __name__ == '__main__': unittest.main()




More information about the Zope3-Checkins mailing list