[Zodb-checkins] CVS: ZODB4/Persistence/tests - testModule.py:1.16

Jeremy Hylton jeremy@zope.com
Thu, 19 Sep 2002 14:26:10 -0400


Update of /cvs-repository/ZODB4/Persistence/tests
In directory cvs.zope.org:/tmp/cvs-serv23085/Persistence/tests

Modified Files:
	testModule.py 
Log Message:
Add tests of reload() that cause objects to get re-created.


=== ZODB4/Persistence/tests/testModule.py 1.15 => 1.16 ===
--- ZODB4/Persistence/tests/testModule.py:1.15	Thu Jul 18 18:41:57 2002
+++ ZODB4/Persistence/tests/testModule.py	Thu Sep 19 14:26:10 2002
@@ -1,9 +1,11 @@
 import os
 import unittest
 
-from ZODB.MappingStorage import DB
+from ZODB.MappingStorage import DB, MappingStorage
 from ZODB.utils import U64
+import ZODB.DB
 
+from Persistence.PersistentDict import PersistentDict
 from Persistence.Module import PersistentModuleImporter
 from Persistence import tests
 from Transaction import get_transaction
@@ -189,6 +191,72 @@
         j = o.m()
         self.assertEqual(i + 1, j)
 
+class TestModuleReload(unittest.TestCase):
+
+    def setUp(self):
+        self.storage = MappingStorage()
+        self.open()
+        _dir, _file = os.path.split(tests.__file__)
+        self._pmtest = os.path.join(_dir, "_pmtest.py")
+
+    def open(self):
+        # open a new db and importer from the storage
+        self.db = ZODB.DB.DB(self.storage)
+        self.root = self.db.open().root()
+        self.importer = PersistentModuleImporter(self.root, verbose=1)
+        self.importer.install()
+
+    def close(self):
+        self.importer.uninstall()
+        self.db.close()
+
+    def testModuleReload(self):
+        self.importer.module_from_file("pmtest", self._pmtest)
+        get_transaction().commit()
+        import pmtest
+        pmtest._p_deactivate()
+        self.assertEqual(pmtest.a, 1)
+        pmtest.f(4)
+        self.close()
+        pmtest._p_deactivate()
+        self.open()
+        reload(pmtest)
+
+    def testClassReload(self):
+        self.importer.module_from_source("foo", src)
+        get_transaction().commit()
+        import foo
+        obj = foo.Foo()
+        obj.m()
+        self.root["d"] = d = PersistentDict()
+        d["m"] = obj
+        get_transaction().commit()
+        self.close()
+        foo._p_deactivate()
+        self.open()
+        reload(foo)
+
+##    def testToplevelClassReload(self):
+##        # XXX This test fails because you can't load the root if there
+##        # is a persistent module in it.
+##        self.importer.module_from_source("foo", src)
+##        get_transaction().commit()
+##        import foo
+##        obj = foo.Foo()
+##        obj.m()
+##        self.root["m"] = obj
+##        get_transaction().commit()
+##        self.close()
+##        foo._p_deactivate()
+##        self.open()
+##        reload(foo)
+
+def test_suite():
+    s = unittest.TestSuite()
+    for klass in TestModule, TestModuleReload:
+        s.addTest(unittest.makeSuite(klass))
+    return s
+
 src = """\
 from Persistence.Class import PersistentBaseClass
 
@@ -199,6 +267,3 @@
         self.x += 1
         return self.x
 """
-
-def test_suite():
-    return unittest.makeSuite(TestModule)