[TIP] fixture InvalidRequestError

Matthew Hoopes matthew.hoopes at gmail.com
Thu Sep 2 04:55:25 PDT 2010


Hi guys,

I've been going out of my mind the last couple days trying to track down
what I'm doing wrong when trying to do fixture tests. I'm building a rest
application that is to be used as a billing service. When I got started with
fixture (thanks to Ryan McKillen on this mailing list), things were going
great. However, I'm starting to get these InvalidRequestErrors, and I've
kinda hit a brick wall.

The error looks like:

Traceback (most recent call last):
  File "/var/www/Billing/billing/tests/functional/test_payments.py", line
16, in setUp
    self.data.setup()
  File
"/usr/local/lib/python2.6/dist-packages/fixture-1.4-py2.6.egg/fixture/base.py",
line 71, in setup
    self.loader.load(self.data)
  File
"/usr/local/lib/python2.6/dist-packages/fixture-1.4-py2.6.egg/fixture/loadable/loadable.py",
line 193, in load
    self.wrap_in_transaction(loader, unloading=False)
  File
"/usr/local/lib/python2.6/dist-packages/fixture-1.4-py2.6.egg/fixture/loadable/loadable.py",
line 315, in wrap_in_transaction
    self.commit()
  File
"/usr/local/lib/python2.6/dist-packages/fixture-1.4-py2.6.egg/fixture/loadable/sqlalchemy_loadable.py",
line 149, in commit
    self.session.flush()
  File
"/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py",
line 1354, in flush
    self._flush(objects)
  File
"/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py",
line 1438, in _flush
    transaction.rollback()
  File
"/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py",
line 404, in rollback
    transaction._rollback_impl()
  File
"/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py",
line 418, in _rollback_impl
    self._restore_snapshot()
  File
"/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py",
line 294, in _restore_snapshot
    self.session._update_impl(s)
  File
"/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py",
line 1257, in _update_impl
    self._attach(state)
  File
"/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/orm/session.py",
line 1284, in _attach
    (mapperutil.state_str(state), state.key)
InvalidRequestError: Can't attach instance <Product at 0x922478c>; another
instance with key (<class 'billing.model.product.Product'>, (2,)) is already
present in this session.

I've seen
http://farmdev.com/projects/fixture/using-fixture-with-pylons.html#why-do-i-keep-getting-invalidrequesterror
,
but I don't think i'm calling setup() twice before teardown(), unless I'm
missing something glaringly obvious. In each test controller, i have this
(or something very like this):

def setUp(self):
         super(TestPaymentsController, self).setUp() # in TestController, in
tests/__init__.py

         self.data = dbfixture.data(AccountData, PaymentData)


         self.data.setup()

def tearDown(self):
         # must truncate table so anything we create gets deleted - fixtures
are auto-deleted

         meta.Session.execute("truncate table payment cascade")


         meta.Session.commit()
         self.data.teardown()
         super(TestPaymentsController, self).tearDown()


         meta.Session.remove()

My tests/__init__.py is exactly the same as in the farmdev howto.

I've been throwing the meta.Session.remove() just about anywhere I can, to
no avail. I'm sorry to bother you with this, but google doesn't seem to turn
up much on this error. If anyone would be kind enough to help, I'd be very
obliged. If you need any more information, please don't hesitate to ask.

Thanks very much,
Matt Hoopes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20100902/89d8d4ae/attachment.html>


More information about the testing-in-python mailing list