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