[Zodb-checkins] CVS: ZODB3/Persistence/tests - testPersistent.py:1.1.2.2

Jeremy Hylton jeremy at zope.com
Thu Jul 3 15:16:12 EDT 2003


Update of /cvs-repository/ZODB3/Persistence/tests
In directory cvs.zope.org:/tmp/cvs-serv26871/Persistence/tests

Modified Files:
      Tag: zodb33-devel-branch
	testPersistent.py 
Log Message:
Add tests for _p_changed and _p_serial.


=== ZODB3/Persistence/tests/testPersistent.py 1.1.2.1 => 1.1.2.2 ===
--- ZODB3/Persistence/tests/testPersistent.py:1.1.2.1	Thu Jul  3 13:25:14 2003
+++ ZODB3/Persistence/tests/testPersistent.py	Thu Jul  3 14:16:06 2003
@@ -23,6 +23,7 @@
     def __init__(self):
         self.cache = PickleCache(self)
         self.oid = 1
+        self.registered = {}
 
     def add(self, obj):
         obj._p_oid = p64(self.oid)
@@ -36,8 +37,13 @@
     # the following methods must be implemented to be a jar
 
     def setklassstate(self):
+        # I don't know what this method does, but the pickle cache
+        # constructor calls it.
         pass
 
+    def register(self, obj):
+        self.registered[obj] = 1
+
 class P(Persistent):
     pass
 
@@ -72,7 +78,60 @@
         def setoid():
             obj._p_jar = 12
         self.assertRaises(ValueError, setoid)
+
+    def testChanged(self):
+        obj = P()
+        self.jar.add(obj)
+
+        # The value returned for _p_changed can be one of:
+        # 0 -- it is not changed
+        # 1 -- it is changed
+        # None -- it is a ghost
+
+        obj.x = 1
+        self.assertEqual(obj._p_changed, 1)
+        self.assert_(obj in self.jar.registered)
+
+        obj._p_changed = 0
+        self.assertEqual(obj._p_changed, 0)
+        self.jar.registered.clear()
+
+        obj._p_changed = 1
+        self.assertEqual(obj._p_changed, 1)
+        self.assert_(obj in self.jar.registered)
+
+        # setting obj._p_changed to None ghostifies if the
+        # object is in the up-to-date state, but not otherwise.
+        obj._p_changed = None
+        self.assertEqual(obj._p_changed, 1)
+        obj._p_changed = 0
+        # Now it's a ghost.
+        obj._p_changed = None
+        self.assertEqual(obj._p_changed, None)
+
+        obj = P()
+        self.jar.add(obj)
+        obj._p_changed = 1
+        # You can transition directly from modified to ghost if
+        # you delete the _p_changed attribute.
+        del obj._p_changed
+        self.assertEqual(obj._p_changed, None)
+
+    def testSerial(self):
+        noserial = "\000" * 8
+        obj = P()
+        self.assertEqual(obj._p_serial, noserial)
         
+        def set(val):
+            obj._p_serial = val
+        self.assertRaises(ValueError, set, 1)
+        self.assertRaises(ValueError, set, "0123")
+        self.assertRaises(ValueError, set, "012345678")
+        self.assertRaises(ValueError, set, u"01234567")
+
+        obj._p_serial = "01234567"
+        del obj._p_serial
+        self.assertEqual(obj._p_serial, noserial)
         
 
 def test_suite():




More information about the Zodb-checkins mailing list