After adding collective.cookiecuttr to my package's GS dependencies i got a strange error when running unit tests:
File ".buildout/eggs/wm.sampledata-0.5.2-py2.7.egg/wm/sampledata/utils.py", line 244, in doWorkflowTransitions include_children=includeChildren File "<string>", line 3, in transitionObjectsByPaths File ".buildout/eggs/plone.protect-3.1.4-py2.7.egg/plone/protect/utils.py", line 58, in _curried return callable(*args, **kw) File "<string>", line 3, in transitionObjectsByPaths File ".buildout/eggs/AccessControl-3.0.11-py2.7-linux-x86_64.egg/AccessControl/requestmethod.py", line 70, in _curried return callable(*args, **kw) File ".buildout/eggs/Products.CMFPlone-4.3.18-py2.7.egg/Products/CMFPlone/PloneTool.py", line 1265, in transitionObjectsByPaths sp = transaction.savepoint(optimistic=True) File ".buildout/eggs/transaction-1.1.1-py2.7.egg/transaction/_manager.py", line 101, in savepoint return self.get().savepoint(optimistic) File ".buildout/eggs/transaction-1.1.1-py2.7.egg/transaction/_transaction.py", line 260, in savepoint self._saveAndRaiseCommitishError() # reraises! File ".buildout/eggs/transaction-1.1.1-py2.7.egg/transaction/_transaction.py", line 257, in savepoint savepoint = Savepoint(self, optimistic, *self._resources) File ".buildout/eggs/transaction-1.1.1-py2.7.egg/transaction/_transaction.py", line 690, in __init__ savepoint = savepoint() File ".buildout/eggs/ZODB3-3.10.7-py2.7-linux-x86_64.egg/ZODB/Connection.py", line 1125, in savepoint self._commit(None) File ".buildout/eggs/ZODB3-3.10.7-py2.7-linux-x86_64.egg/ZODB/Connection.py", line 623, in _commit self._store_objects(ObjectWriter(obj), transaction) File ".buildout/eggs/ZODB3-3.10.7-py2.7-linux-x86_64.egg/ZODB/Connection.py", line 658, in _store_objects p = writer.serialize(obj) # This calls __getstate__ of obj File ".buildout/eggs/ZODB3-3.10.7-py2.7-linux-x86_64.egg/ZODB/serialize.py", line 422, in serialize return self._dump(meta, obj.__getstate__()) File ".buildout/eggs/ZODB3-3.10.7-py2.7-linux-x86_64.egg/ZODB/serialize.py", line 431, in _dump self._p.dump(state) File ".buildout/eggs/ZODB3-3.10.7-py2.7-linux-x86_64.egg/ZODB/serialize.py", line 352, in persistent_id "database connection", self._jar, obj, InvalidObjectReference: ('Attempt to store an object from a foreign database connection', <Connection at 7f0e20836210>, <collective.z3cform.datagridfield.registry.DictRow object at 0x7f0e212d07d0>)
Searching the internet did not reveal usefu informationl. @thet hat a similar issue https://github.com/bluedynamics/bda.plone.ticketshop/issues/4 but worked around it.
This is what i found out so far:
The error happens, if two or more tests within the same layer install the package collective.cookiecuttr (or a package depending on it) in the TestCase's setUp method (by using
Proof: If we comment out the installation of the registry records (done in https://github.com/fourdigits/collective.cookiecuttr/blob/0.7.6/src/collective/cookiecuttr/profiles/default/registry.xml) the error does not happen.
I worked around this issue by moving the installation out of
TestCase.setUp into a separate PloneSandboxLayer that calls
Somehow an imported registry record of type
DictRow "survives" the
tearDown (of the Demostorage) between two TestCases of the same layer (or we get the old object when installing it again).
Fact is, it referes a stale database connection in
._jar leading to the error above when calling
#breakpoint in ZODB.serialize: (Pdb) repr(obj) '<collective.z3cform.datagridfield.registry.DictRow object at 0x7fe4b92796e0>' (Pdb) self._jar <Connection at 7fe4b8340d90> (Pdb) obj.__jar *** ConnectionStateError: Shouldn't load state for 0x19f744a58b895605 when the connection is closed
To me this smells like
collective.z3cform.datagridfield (combined with
plone.registry is leading to test isolation issues here.
If other users get a similar error this thread hopefully helps them to solve the issue more quickly now.
However, it should be possible to quickinstall a package that installs
collective.z3cform.datagridfield values to the registry in a TestCase's
setUp w/o running into test isolation problems.