pytest 固定装置 sqlalchemy 事务回滚不起作用

发布于 01-14 08:40 字数 2124 浏览 5 评论 0原文

我正在使用 pytest 编写功能测试。我的目的是在每次测试后回滚事务,以便在每次测试中使用相同的数据。这是我的代码:

@pytest.fixture(scope='function', autouse=True)
def session(db, request):
   connection = db.engine.connect()
   transaction = connection.begin()

   options = dict(bind=connection, binds={}, autoflush=False, autocommit=True)

   db.session = db.create_scoped_session(options=options)

   def teardown():
       transaction.rollback()
       connection.close()
       db.session.rollback()
       db.session.remove()
   request.addfinalizer(teardown)

   yield db.session

但它不起作用。有些测试失败是因为之前的某些内容被更改(并且没有回滚)。一些测试因以下错误而失败:

FAILED ... - sqlalchemy.exc.IntegrityError: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely)
FAILED ... - sqlalchemy.exc.InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (r...
FAILED ... - sqlalchemy.exc.InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (raised...
FAILED ... - sqlalchemy.exc.InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (raised as a ...
FAILED ... - AssertionError: assert [{'id': 1, 'l...'Test'}, ...}] == [{'id': 1, 'l...'Test'}, ...}]
FAILED ... - AssertionError: assert 'updated-diagram' == 'test_1'
ERROR ... - sqlalchemy.exc.InvalidRequestError: No transaction is begun.

我的评论:

  • ...考虑使用 session.no_autoflush 块... - 会话选项中有 autoflush=False
  • ...开始新的与此会话的事务,首先发出 Session.rollback()... - 我在拆卸中也有这个
  • AssertionError - 两者都是由于不正确的回滚
  • sqlalchemy.exc .InvalidRequestError:没有交易开始了。 - 我不知道这里发生了什么以及为什么发生

I'm writing functional tests with pytest. My intention is to roll back transactions after each test in order to work with the same data in every test. Here's my code:

@pytest.fixture(scope='function', autouse=True)
def session(db, request):
   connection = db.engine.connect()
   transaction = connection.begin()

   options = dict(bind=connection, binds={}, autoflush=False, autocommit=True)

   db.session = db.create_scoped_session(options=options)

   def teardown():
       transaction.rollback()
       connection.close()
       db.session.rollback()
       db.session.remove()
   request.addfinalizer(teardown)

   yield db.session

But it doesn't work. Some tests fail because something before them was changed (and wasn't rolled back). And some tests fail with these errors:

FAILED ... - sqlalchemy.exc.IntegrityError: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely)
FAILED ... - sqlalchemy.exc.InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (r...
FAILED ... - sqlalchemy.exc.InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (raised...
FAILED ... - sqlalchemy.exc.InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (raised as a ...
FAILED ... - AssertionError: assert [{'id': 1, 'l...'Test'}, ...}] == [{'id': 1, 'l...'Test'}, ...}]
FAILED ... - AssertionError: assert 'updated-diagram' == 'test_1'
ERROR ... - sqlalchemy.exc.InvalidRequestError: No transaction is begun.

My comments:

  • ...consider using a session.no_autoflush block... - there is autoflush=False in session options
  • ...To begin a new transaction with this Session, first issue Session.rollback()... - I also have that in teardown
  • AssertionError - both of them are because of improper roll back
  • sqlalchemy.exc.InvalidRequestError: No transaction is begun. - I have no idea what and why happened here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文