(sorry, meant to send this to the list)
On 05/06/2019 15:52, Mike Bayer wrote:
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?
I can run your script if you remove the "diary" thing from it
Hmm, I wonder what's different?
I'm on Python 3.7.1, SQLAlchemy 1.3.4, Postgres 11.3, here's a totally
self contained script:
import os
from sqlalchemyimport Column, Integer, Text
from sqlalchemyimport create_engine
from sqlalchemy.ext.declarativeimport declarative_base
from sqlalchemy.ormimport sessionmaker
Base = declarative_base()
Session = sessionmaker()
class Event(Base):
__tablename__ ='entry' id = Column(Integer(),primary_key=True)
text = Column(Text)
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(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()
Which gives me:
$ python sessions_are_weird.py
Traceback (most recent call last):
File "sessions_are_weird.py", line 40, in <module>
assert session.query(Event).count() == 0
AssertionError
Whereas after the rollback, I'd expect that count to be zero...
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/d8b26fa3-aca9-7391-f1b8-c7900368b266%40withers.org.
For more options, visit https://groups.google.com/d/optout.