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

Jeremy Hylton jeremy@zope.com
Tue, 21 Jan 2003 17:37:32 -0500


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

Modified Files:
      Tag: new-pickle-branch
	test_class.py 
Log Message:
Add _load_name() helper to test classes using a separate connection.


=== Zope3/src/zodb/code/tests/test_class.py 1.1 => 1.1.4.1 ===
--- Zope3/src/zodb/code/tests/test_class.py:1.1	Mon Dec 30 14:21:42 2002
+++ Zope3/src/zodb/code/tests/test_class.py	Tue Jan 21 17:37:23 2003
@@ -22,20 +22,48 @@
 
 from transaction import get_transaction
 
-
 class TestClass(TestBase):
 
+    # TODO
+    # test classes with getstate and setstate
+    # make sure class invalidation works correctly
+
     class_with_init = """class Foo:
     def __init__(self, arg):
         self.var = arg""" "\n"
 
+    def _load_path(self, path):
+        # Load an object from a new connection given a database path.
+        root = self.db.open().root()
+        obj = root
+        for part in path.split("."):
+            try:
+                obj = obj[part]
+            except TypeError:
+                obj = getattr(obj, part)
+        return obj
+
+    def _load_name(self, name):
+        # Load a class from a new connection given a dotted name
+        i = name.rfind(".")
+        module = name[:i]
+        klass = name[i+1:]
+        # The following depends entirely on the internals of the
+        # implementation.
+        return self._load_path("registry._mgrs.%s._module.%s"
+                               % (module, klass))
+
     def testClassWithInit(self):
-        mgr = newModule(self.registry, "testclass", self.class_with_init)
+        self.registry.newModule("testclass", self.class_with_init)
         get_transaction().commit()
         import testclass
         x = testclass.Foo(12)
         self.assertEqual(x.var, 12)
 
+        Foo2 = self._load_name("testclass.Foo")
+        y = Foo2(12)
+        self.assertEqual(y.var, 12)
+
     class_and_instance = """class Foo:
     def __init__(self, arg):
         self.var = arg
@@ -49,17 +77,22 @@
 x = Foo(12)""" "\n"
 
     def testClassAndInstance(self):
-        mgr = newModule(self.registry, "testclass", self.class_and_instance)
+        self.registry.newModule("testclass", self.class_and_instance)
         get_transaction().commit()
         import testclass
         self.assertEqual(testclass.x.var, 12)
 
+        Foo2 = self._load_name("testclass.Foo")
+        self.assertEqual(Foo2(12).var, 12)
+        x = self._load_name("testclass.x")
+        self.assertEqual(x.var, 12)
+
     cross_module_import = "from testclass import Foo"
 
     def testCrossModuleImport(self):
-        mgr = newModule(self.registry, "testclass", self.class_with_init)
+        self.registry.newModule("testclass", self.class_with_init)
         get_transaction().commit()
-        mgr2 = newModule(self.registry, "another", self.cross_module_import)
+        self.registry.newModule("another", self.cross_module_import)
         get_transaction().commit()
 
     update_in_place1 = """class Foo:
@@ -71,13 +104,13 @@
         return arg + 3""" "\n"
 
     def testUpdateInPlace(self):
-        mgr = newModule(self.registry, "testclass", self.update_in_place1)
+        self.registry.newModule("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)
+        self.registry.updateModule("testclass", self.update_in_place2)
         get_transaction().commit()
         self.assertEqual(inst.meth(4), 7)