[Zodb-checkins] CVS: Zope3/src/zodb/code/tests - test_class.py:1.1 test_module.py:1.3

Jeremy Hylton jeremy@zope.com
Mon, 30 Dec 2002 14:21:43 -0500


Update of /cvs-repository/Zope3/src/zodb/code/tests
In directory cvs.zope.org:/tmp/cvs-serv1953/tests

Modified Files:
	test_module.py 
Added Files:
	test_class.py 
Log Message:
Add test_class module for testing persistent classes.

There is some overlap with test_module, but I haven't moved all the
old class tests into test_class.  But do refactor test_module to
define a base class with setUp() and tearDown() that can be used by
test_class.


=== Added File Zope3/src/zodb/code/tests/test_class.py ===
##############################################################################
#
# 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.
#
##############################################################################
import os
import unittest

import zodb.db
from zodb.storage.mapping import DB, MappingStorage

from zodb.code.tests.test_module import TestBase
from zodb.code.module import newModule

from transaction import get_transaction


class TestClass(TestBase):

    class_with_init = """class Foo:
    def __init__(self, arg):
        self.var = arg""" "\n"

    def testClassWithInit(self):
        mgr = newModule(self.registry, "testclass", self.class_with_init)
        get_transaction().commit()
        import testclass
        x = testclass.Foo(12)
        self.assertEqual(x.var, 12)

    class_and_instance = """class Foo:
    def __init__(self, arg):
        self.var = arg

    # The class must have a getinitargs because the instance
    # will be pickled during module conversion.

    def __getinitargs__(self):
        return self.var,

x = Foo(12)""" "\n"

    def testClassAndInstance(self):
        mgr = newModule(self.registry, "testclass", self.class_and_instance)
        get_transaction().commit()
        import testclass
        self.assertEqual(testclass.x.var, 12)

    cross_module_import = "from testclass import Foo"

    def testCrossModuleImport(self):
        mgr = newModule(self.registry, "testclass", self.class_with_init)
        get_transaction().commit()
        mgr2 = newModule(self.registry, "another", self.cross_module_import)
        get_transaction().commit()

    update_in_place1 = """class Foo:
    def meth(self, arg):
        return arg * 3""" "\n"
    
    update_in_place2 = """class Foo:
    def meth(self, arg):
        return arg + 3""" "\n"

    def testUpdateInPlace(self):
        mgr = newModule(self.registry, "testclass", self.update_in_place1)
        get_transaction().commit()
        import testclass
        inst = testclass.Foo()
        self.assertEqual(inst.meth(4), 12)

        mgr.update(self.update_in_place2)
        get_transaction().commit()
        self.assertEqual(inst.meth(4), 7)
        

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


=== Zope3/src/zodb/code/tests/test_module.py 1.2 => 1.3 ===
--- Zope3/src/zodb/code/tests/test_module.py:1.2	Wed Dec 25 09:12:18 2002
+++ Zope3/src/zodb/code/tests/test_module.py	Mon Dec 30 14:21:42 2002
@@ -15,7 +15,6 @@
 import unittest
 
 from zodb.storage.mapping import DB, MappingStorage
-from zodb.utils import u64
 import zodb.db
 
 from persistence.dict import PersistentDict
@@ -83,7 +82,7 @@
             return mod
         return self._saved_import(name, globals, locals, fromlist)
 
-class TestModule(unittest.TestCase):
+class TestBase(unittest.TestCase):
 
     def setUp(self):
         self.db = DB()
@@ -99,6 +98,8 @@
     def tearDown(self):
         self.importer.uninstall()
 
+class TestModule(TestBase):
+    
     def testModule(self):
         mgr = PersistentModuleManager(self.registry)
         mgr.new("pmtest", open(self._pmtest).read())