[Zodb-checkins] CVS: Zope3/lib/python/Persistence/tests - testPersistence.py:1.1.2.6
Jeremy Hylton
jeremy@zope.com
Tue, 4 Jun 2002 14:13:35 -0400
Update of /cvs-repository/Zope3/lib/python/Persistence/tests
In directory cvs.zope.org:/tmp/cvs-serv15650/tests
Modified Files:
Tag: Zope-3x-branch
testPersistence.py
Log Message:
Test Persistent and BasePersistent types.
=== Zope3/lib/python/Persistence/tests/testPersistence.py 1.1.2.5 => 1.1.2.6 ===
class Test(unittest.TestCase):
+ klass = None # override in subclass
+
def testUnsaved(self):
- p=P()
+ p = self.klass()
self.assertEqual(p.x, 0)
self.assertEqual(p._p_changed, 0)
@@ -32,21 +34,20 @@
p._p_deactivate()
self.assertEqual(p._p_changed, 0)
- p._p_changes=1
+ p._p_changes = 1
self.assertEqual(p._p_changed, 0)
- p._p_changed=None
+ p._p_changed = None
self.assertEqual(p._p_changed, 0)
del p._p_changed
self.assertEqual(p._p_changed, 0)
self.failUnless(p.__dict__)
self.assertEqual(p.x, 2)
-
def testSaved(self):
- p=P()
- p._p_oid='\0\0\0\0\0\0hi'
- dm=DM()
- p._p_jar=dm
+ p = self.klass()
+ p._p_oid = '\0\0\0\0\0\0hi'
+ dm = DM()
+ p._p_jar = dm
self.assertEqual(p._p_changed, 0)
self.assertEqual(dm.called, 0)
p.inc()
@@ -55,7 +56,7 @@
p.inc()
self.assertEqual(p._p_changed, 1)
self.assertEqual(dm.called, 1)
- p._p_changed=None
+ p._p_changed = None
self.assertEqual(p._p_changed, 1)
self.assertEqual(dm.called, 1)
p._p_deactivate()
@@ -70,7 +71,7 @@
self.assertEqual(p.x, 43)
self.assertEqual(p._p_changed, 1)
self.assertEqual(dm.called, 2)
- p._p_changed=0
+ p._p_changed = 0
self.assertEqual(p._p_changed, 0)
self.assertEqual(dm.called, 2)
self.assertEqual(p.x, 43)
@@ -79,23 +80,23 @@
self.assertEqual(dm.called, 3)
def testState(self):
- p=P()
+ p = self.klass()
self.assertEqual(p.__getstate__(), {'x': 0})
self.assertEqual(p._p_changed, 0)
p.__setstate__({'x':5})
self.assertEqual(p._p_changed, 0)
- p._v_foo=2
+ p._v_foo = 2
self.assertEqual(p.__getstate__(), {'x': 5})
self.assertEqual(p._p_changed, 0)
def testDirectChanged(self):
- p=P()
- p._p_oid=1
- dm=DM()
- p._p_jar=dm
+ p = self.klass()
+ p._p_oid = 1
+ dm = DM()
+ p._p_jar = dm
self.assertEqual(p._p_changed, 0)
self.assertEqual(dm.called, 0)
- p._p_changed=1
+ p._p_changed = 1
self.assertEqual(dm.called, 1)
def testInterface(self):
@@ -107,12 +108,11 @@
self.assert_(IPersistent.isImplementedByInstancesOf(P),
"%s does not implement IPersistent" % P)
- p = P()
+ p = self.klass()
self.assert_(IPersistent.isImplementedBy(p),
"%s does not implement IPersistent" % p)
-
-from Persistence import Persistent
+from Persistence import Persistent, BasePersistent
class P(Persistent):
def __init__(self):
@@ -120,6 +120,22 @@
def inc(self):
self.x += 1
+class B(BasePersistent):
+
+ __slots__ = ["x"]
+
+ def __init__(self):
+ self.x = 0
+
+ def inc(self):
+ self.x += 1
+
+ def __getstate__(self):
+ return {'x': self.x}
+
+ def __setstate__(self, state):
+ self.x = state['x']
+
class DM:
def __init__(self):
self.called = 0
@@ -128,11 +144,19 @@
def setstate(self, ob):
ob.__setstate__({'x': 42})
-##############################################################################
+class PersistentTest(Test):
+ klass = P
+
+class BasePersistentTest(Test):
+ klass = B
+
+# XXX should test BasePersistent, too
def test_suite():
- loader = unittest.TestLoader()
- return loader.loadTestsFromTestCase(Test)
+ s = unittest.TestSuite()
+ for klass in PersistentTest, BasePersistentTest:
+ s.addTest(unittest.makeSuite(klass))
+ return s
if __name__ == '__main__':
unittest.TextTestRunner().run(test_suite())