summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2017-03-11 10:52:43 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2017-03-14 15:40:31 -0400
commit4ece86eb41274ba059211807e23273178c3c3384 (patch)
tree46bee27a53626f467530eb4e78cc7abc4beae843 /test
parent596e322543df6ff380243c9cb0cf9997252329f6 (diff)
downloadsqlalchemy-4ece86eb41274ba059211807e23273178c3c3384.tar.gz
Emit after_rollback() event before snapshot removal
The state of the :class:`.Session` is now present when the :meth:`.SessionEvents.after_rollback` event is emitted, that is, the attribute state of objects prior to their being expired. This is now consistent with the behavior of the :meth:`.SessionEvents.after_commit` event which also emits before the attribute state of objects is expired. Change-Id: I9c572656ec5a9bfaeab817e9c95107c75aca1b51 Fixes: #3934
Diffstat (limited to 'test')
-rw-r--r--test/orm/test_events.py44
-rw-r--r--test/orm/test_transaction.py36
2 files changed, 66 insertions, 14 deletions
diff --git a/test/orm/test_events.py b/test/orm/test_events.py
index 80633c90e..f3cce7da3 100644
--- a/test/orm/test_events.py
+++ b/test/orm/test_events.py
@@ -1617,6 +1617,50 @@ class SessionEventsTest(_RemoveListeners, _fixtures.FixtureTest):
]
)
+ def test_snapshot_still_present_after_commit(self):
+ users, User = self.tables.users, self.classes.User
+
+ mapper(User, users)
+
+ sess = Session()
+
+ u1 = User(name='u1')
+
+ sess.add(u1)
+ sess.commit()
+
+ u1 = sess.query(User).first()
+
+ @event.listens_for(sess, "after_commit")
+ def assert_state(session):
+ assert 'name' in u1.__dict__
+ eq_(u1.name, 'u1')
+
+ sess.commit()
+ assert 'name' not in u1.__dict__
+
+ def test_snapshot_still_present_after_rollback(self):
+ users, User = self.tables.users, self.classes.User
+
+ mapper(User, users)
+
+ sess = Session()
+
+ u1 = User(name='u1')
+
+ sess.add(u1)
+ sess.commit()
+
+ u1 = sess.query(User).first()
+
+ @event.listens_for(sess, "after_rollback")
+ def assert_state(session):
+ assert 'name' in u1.__dict__
+ eq_(u1.name, 'u1')
+
+ sess.rollback()
+ assert 'name' not in u1.__dict__
+
class SessionLifecycleEventsTest(_RemoveListeners, _fixtures.FixtureTest):
run_inserts = None
diff --git a/test/orm/test_transaction.py b/test/orm/test_transaction.py
index c4f6a1eaf..374b6a9e5 100644
--- a/test/orm/test_transaction.py
+++ b/test/orm/test_transaction.py
@@ -434,12 +434,12 @@ class SessionTransactionTest(FixtureTest):
sess.add(User(name='u1'))
sess.flush()
sess.rollback()
- assert_raises_message(sa_exc.InvalidRequestError,
- "This Session's transaction has been "
- r"rolled back by a nested rollback\(\) "
- "call. To begin a new transaction, "
- r"issue Session.rollback\(\) first.",
- sess.begin, subtransactions=True)
+ assert_raises_message(
+ sa_exc.InvalidRequestError,
+ "This session is in 'inactive' state, due to the SQL transaction "
+ "being rolled back; no further SQL can be emitted within this "
+ "transaction.",
+ sess.begin, subtransactions=True)
sess.close()
def test_no_sql_during_commit(self):
@@ -465,6 +465,19 @@ class SessionTransactionTest(FixtureTest):
"SQL can be emitted within this transaction.",
sess.execute, "select 1")
+ def test_no_sql_during_rollback(self):
+ sess = create_session(bind=testing.db, autocommit=False)
+
+ @event.listens_for(sess, "after_rollback")
+ def go(session):
+ session.execute("select 1")
+ assert_raises_message(
+ sa_exc.InvalidRequestError,
+ "This session is in 'inactive' state, due to the SQL transaction "
+ "being rolled back; no further SQL can be emitted within this "
+ "transaction.",
+ sess.rollback)
+
def test_no_prepare_wo_twophase(self):
sess = create_session(bind=testing.db, autocommit=False)
@@ -491,9 +504,9 @@ class SessionTransactionTest(FixtureTest):
trans2.rollback()
assert_raises_message(
sa_exc.InvalidRequestError,
- r"This Session's transaction has been rolled back by a nested "
- r"rollback\(\) call. To begin a new transaction, issue "
- r"Session.rollback\(\) first.",
+ "This session is in 'inactive' state, due to the SQL transaction "
+ "being rolled back; no further SQL can be emitted within this "
+ "transaction.",
trans.commit
)
@@ -622,11 +635,6 @@ class SessionTransactionTest(FixtureTest):
orm_exc.FlushError, sess.flush
)
- assert_warnings(go,
- ["Session's state has been changed on a "
- "non-active transaction - this state "
- "will be discarded."],
- )
assert u3 not in sess
def test_preserve_flush_error(self):