[ZODB-Dev] Why does this useage of __setstate__ fail?
Mika, David P (GE, Research)
mika at crd.ge.com
Wed Jan 16 21:43:21 EST 2008
> Can someone explain why the test below (test_persistence) is failing?
> I am adding an attribute after object creation with __setstate__, but
> I can't get the new attribute to persist.
>
> ------------------------------------------------
> import transaction
> import unittest
> from persistent import Persistent
> from ZODB import FileStorage, DB
>
> TMP_DB = 'zodb-test-filestorage.fs'
>
> class User(Persistent):
> def __init__(self):
> pass
> def __setstate__(self, state):
> Persistent.__setstate__(self, state)
> if not hasattr(self, 'color'):
> self.color = 'blue'
> def getColor(self):
> return self.color
> def setColor(self, color):
> self.color = color
> transaction.commit()
>
> class ZODB_TestCase(unittest.TestCase):
> def setUp(self):
> storage = FileStorage.FileStorage(TMP_DB)
> self.db = DB(storage)
> conn = self.db.open()
> dbroot = conn.root()
> # Ensure that a 'userdb' key is present
> # in the root
> if not dbroot.has_key('userdb'):
> from BTrees.OOBTree import OOBTree
> dbroot['userdb'] = OOBTree()
> self.userdb = dbroot['userdb']
>
> self.id = 'amk'
>
> def tearDown(self):
> self.db.close()
>
> def test_AddUser(self):
> newuser = User()
> newuser.first_name = 'Andrew'; newuser.last_name = 'Kuchling'
> # Add object to the BTree, keyed on the ID
> self.userdb[self.id] = newuser
> # Commit the change
> transaction.commit()
>
> # setstate is not called after the constructor
> assert not hasattr(newuser, 'color')
>
> def test_setstate(self):
> newuser = self.userdb[self.id]
> # setstate is called subsequently
> assert hasattr(newuser, 'color')
> assert newuser.color == 'blue'
> newuser.setColor('red')
> assert newuser.color == 'red'
>
> def test_persistence(self):
> newuser = self.userdb[self.id]
> assert newuser.color == 'red' # this fails!
>
> if __name__ == '__main__':
> import os
> if os.path.exists(TMP_DB):
> os.unlink(TMP_DB)
> unittest.main()
>
> ----------------------------------------------------------------------
> -----------
> David Mika, PhD
> Material Systems Technologies
> k1-mb 235
> GE Research Center
> Niskayuna, NY 12309
> 518 387 4223 (phone)
> mika at crd.ge.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/zodb-dev/attachments/20080116/3f1377a5/attachment.htm
More information about the ZODB-Dev
mailing list