Skip to content

Commit a1f513a

Browse files
committed
tx context manager tests
1 parent bbd47da commit a1f513a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

tests/query/test_query_transaction.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
from ydb.query.transaction import QueryTxStateEnum
34

45
class TestQueryTransaction:
56
def test_tx_begin(self, tx):
@@ -46,3 +47,22 @@ def test_tx_execute_raises_after_rollback(self, tx):
4647
tx.rollback()
4748
with pytest.raises(RuntimeError):
4849
tx.execute("select 1;")
50+
51+
def test_context_manager_rollbacks_tx(self, tx):
52+
with tx:
53+
tx.begin()
54+
55+
assert tx._tx_state._state == QueryTxStateEnum.ROLLBACKED
56+
57+
def test_context_manager_normal_flow(self, tx):
58+
with tx:
59+
tx.begin()
60+
tx.execute("select 1;")
61+
tx.commit()
62+
63+
assert tx._tx_state._state == QueryTxStateEnum.COMMITTED
64+
65+
def test_context_manager_does_not_hide_exceptions(self, tx):
66+
with pytest.raises(RuntimeError):
67+
with tx:
68+
tx.commit()

ydb/query/transaction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def __exit__(self, *args, **kwargs):
184184
Closes a transaction context manager and rollbacks transaction if
185185
it is not rolled back explicitly
186186
"""
187+
self._ensure_prev_stream_finished()
187188
if self._tx_state.tx_id is not None:
188189
# It's strictly recommended to close transactions directly
189190
# by using commit_tx=True flag while executing statement or by

0 commit comments

Comments
 (0)