On 04/06/2019 23:21, Mike Bayer wrote:
On Tue, Jun 4, 2019, at 4:33 PM, Chris Withers wrote:
So, how do I roll back the further subtransaction created by the web
framework instantiating Session from a sessionmaker bound to the
connection in which begin_nested() has been called, which under non-test
running would actually be a top level transaction assuming I understand
the pattern correctly, in such as way that if the code-under-test has
committed on is session, the session being used to check expectations in
the unit test will see the results, but if it that commit has been
forgotten, it will not?
I'm not following all your code but if there are two sessions in play
I'd probably try to avoid that, there should be only one Session you
care about. the test fixtures should be external to everything and
make sure there's just the one session. if there are two in play,
I'm not sure how they both get bound to your test transaction.
Even this doesn't appear to be enough, here's the simplest reproducer
script I can get to:
import os
from diary.modelimport Session, Base, Event, Types
from sqlalchemyimport create_engine
engine = create_engine(os.environ['TEST_DB_URL'])
Session.configure(bind=engine)
conn = engine.connect()
transaction = conn.begin()
try:
Base.metadata.create_all(bind=conn,checkfirst=False)
Session.configure(bind=conn)
sub_transaction = conn.begin_nested()
try:
session = Session()
# code under test: event =
Event(date='2019-06-02',type=Types.done,text='some stuff got done')
session.add(event)
session.flush()
session.close()
# test: session.rollback()
assert session.query(Event).count() ==0 finally:
sub_transaction.rollback()
finally:
transaction.rollback()
That session.close() appears to be the problem. It's a normal and
required part of the code under test, but it throws away the
SessionTransaction without rolling it back, so by the time the test does
session.rollback(), it's doing it on a new SessionTransaction and so has
no effect and the assertion fails because there event created is still
around...
Is this a bug or am I just confusing myself and everyone else?
Chris
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/55721019-2de3-12b4-3796-90ca34b5ea4c%40withers.org.
For more options, visit https://groups.google.com/d/optout.