Skip to content

Commit 7dbbb6e

Browse files
committed
query session pool tests
1 parent 8bb8a6f commit 7dbbb6e

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

tests/query/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from ydb.query.session import QuerySessionSync
3+
from ydb.query.pool import QuerySessionPool
34

45

56
@pytest.fixture
@@ -25,3 +26,9 @@ def tx(session):
2526
transaction.rollback()
2627
except BaseException:
2728
pass
29+
30+
31+
@pytest.fixture
32+
def pool(driver_sync):
33+
pool = QuerySessionPool(driver_sync)
34+
yield pool
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pytest
2+
3+
from ydb.query.pool import QuerySessionPool
4+
from ydb.query.session import QuerySessionSync, QuerySessionStateEnum
5+
6+
class TestQuerySessionPool:
7+
def test_checkout_provides_created_session(self, pool: QuerySessionPool):
8+
with pool.checkout() as session:
9+
assert session._state._state == QuerySessionStateEnum.CREATED
10+
11+
assert session._state._state == QuerySessionStateEnum.CLOSED
12+
13+
def test_oneshot_query_normal(self, pool: QuerySessionPool):
14+
res = pool.execute_with_retries("select 1;")
15+
assert len(res) == 1
16+
17+
def test_oneshot_ddl_query(self, pool: QuerySessionPool):
18+
pool.execute_with_retries("create table Queen(key UInt64, PRIMARY KEY (key));")
19+
pool.execute_with_retries("drop table Queen;")
20+
21+
def test_oneshot_query_raises(self, pool: QuerySessionPool):
22+
with pytest.raises(Exception):
23+
pool.execute_with_retries("Is this the real life? Is this just fantasy?")
24+
25+
def test_retry_op_uses_created_session(self, pool: QuerySessionPool):
26+
def callee(session: QuerySessionSync):
27+
assert session._state._state == QuerySessionStateEnum.CREATED
28+
pool.retry_operation_sync(callee)
29+
30+
def test_retry_op_normal(self, pool: QuerySessionPool):
31+
def callee(session: QuerySessionSync):
32+
with session.transaction() as tx:
33+
iterator = tx.execute("select 1;", commit_tx=True)
34+
return [result_set for result_set in iterator]
35+
36+
res = pool.retry_operation_sync(callee)
37+
assert len(res) == 1
38+
39+
def test_retry_op_raises(self, pool: QuerySessionPool):
40+
def callee(session: QuerySessionSync):
41+
res = session.execute("Caught in a landslide, no escape from reality")
42+
for _ in res:
43+
pass
44+
45+
with pytest.raises(Exception):
46+
pool.retry_operation_sync(callee)

ydb/query/transaction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ def _move_to_commited(self):
316316
if self._tx_state._already_in(QueryTxStateEnum.COMMITTED):
317317
return
318318
self._tx_state._change_state(QueryTxStateEnum.COMMITTED)
319+
self._tx_state.tx_id = None
319320

320321
def execute(
321322
self,

0 commit comments

Comments
 (0)