[Zope-Checkins] CVS: Zope3/lib/python/Persistence/tests - testPersistence.py:1.1.2.1

Jim Fulton jim@zope.com
Thu, 22 Nov 2001 16:01:34 -0500


Update of /cvs-repository/Zope3/lib/python/Persistence/tests
In directory cvs.zope.org:/tmp/cvs-serv1759/tests

Added Files:
      Tag: Zope-3x-branch
	testPersistence.py 
Log Message:
Initial Python-only Persistent implementation for Python 2.2
without ExtensionClass. Tests too.

Renamed the modules that define Persistent and PersistentMapping
to be private. The classes are exported at the package level.



=== Added File Zope3/lib/python/Persistence/tests/testPersistence.py ===
##############################################################################
# Copyright (c) 2001 Zope Corporation and Contributors.  All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (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 unittest, sys
        

class Test(unittest.TestCase):

    def testUnsaved(self):
        p=P()
        
        self.assertEqual(p.x, 0)
        self.assertEqual(p._p_changed, 0)
        self.assertEqual(p._p_jar, None)
        self.assertEqual(p._p_oid, None)
        self.assertEqual(p._p_serial, None)
        p.inc()
        p.inc()
        self.assertEqual(p.x, 2)
        self.assertEqual(p._p_changed, 0)

        p._p_deactivate()
        self.assertEqual(p._p_changed, 0)
        p._p_changes=1
        self.assertEqual(p._p_changed, 0)
        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
        self.assertEqual(p._p_changed, 0)
        self.assertEqual(dm.called, 0)
        p.inc()
        self.assertEqual(p._p_changed, 1)
        self.assertEqual(dm.called, 1)
        p.inc()
        self.assertEqual(p._p_changed, 1)
        self.assertEqual(dm.called, 1)
        p._p_changed=None
        self.assertEqual(p._p_changed, 1)
        self.assertEqual(dm.called, 1)
        p._p_deactivate()
        self.assertEqual(p._p_changed, 1)
        self.assertEqual(dm.called, 1)
        del p._p_changed
        self.assertEqual(p._p_changed, None)
        self.assertEqual(dm.called, 1)
        p.inc()
        self.assertEqual(p.x, 43)
        self.assertEqual(p._p_changed, 1)
        self.assertEqual(dm.called, 2)
        p._p_changed=0
        self.assertEqual(p._p_changed, 0)
        self.assertEqual(dm.called, 2)
        self.assertEqual(p.x, 43)
        p.inc()
        self.assertEqual(p._p_changed, 1)
        self.assertEqual(dm.called, 3)
        

    def testState(self):
        p=P()
        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
        self.assertEqual(p.__getstate__(), {'x': 5})
        self.assertEqual(p._p_changed, 0)
        

from Persistence import Persistent

class P(Persistent):
    def __init__(self): self.x=0
    def inc(self): self.x += 1


class DM:
    def __init__(self): self.called=0
    def register(self, ob): self.called += 1
    def setstate(self, ob):
        ob.__setstate__({'x': 42})

##############################################################################

def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())