[ZODB-Dev] Why does this useage of __setstate__ fail?

David Binger dbinger at mems-exchange.org
Thu Jan 17 04:48:54 EST 2008


Hi David,

It looks like you might be assuming that the tests run in the order
you wrote them.  The test_persistence() function actually gets called
before the test_setstate(), so there is no chance for the color to be  
'red'.

I don't think the failure of this particular test has anything to do  
with
your __setstate__() method.  When I write __setstate__() methods,
though,  I modify the state dictionary and then call  
Persistent.__setstate__.
This avoids triggering the persistent attribute machinery at all.

David Binger

On Jan 16, 2008, at 9:43 PM, Mika, David P (GE, Research) wrote:

> 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
>
>
> _______________________________________________
> For more information about ZODB, see the ZODB Wiki:
> http://www.zope.org/Wikis/ZODB/
>
> ZODB-Dev mailing list  -  ZODB-Dev at zope.org
> http://mail.zope.org/mailman/listinfo/zodb-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/zodb-dev/attachments/20080117/75882a84/attachment-0001.htm


More information about the ZODB-Dev mailing list