Skip to content

Commit c98d958

Browse files
committed
refactor cursor test
1 parent d33aa10 commit c98d958

File tree

4 files changed

+217
-259
lines changed

4 files changed

+217
-259
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ force-single-line = true
7676

7777
[tool.ruff.lint.per-file-ignores]
7878
"**/test_*.py" = ["S", "SLF", "ANN201", "ARG", "PLR2004", "PT012"]
79-
"conftest.py" = ["ARG001"]
79+
"conftest.py" = ["S", "ARG001"]
8080
"__init__.py" = ["F401", "F403"]
8181

8282
[tool.pytest.ini_options]

tests/conftest.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,45 @@ def session_pool_sync(
185185
)
186186

187187
yield session_pool
188+
189+
190+
@pytest.fixture
191+
async def session(
192+
session_pool: ydb.aio.QuerySessionPool,
193+
) -> AsyncGenerator[ydb.aio.QuerySession]:
194+
for name in ["table", "table1", "table2"]:
195+
await session_pool.execute_with_retries(
196+
f"""
197+
DELETE FROM {name};
198+
INSERT INTO {name} (id, val) VALUES
199+
(0, 0),
200+
(1, 1),
201+
(2, 2),
202+
(3, 3)
203+
"""
204+
)
205+
206+
session = await session_pool.acquire()
207+
yield session
208+
await session_pool.release(session)
209+
210+
211+
@pytest.fixture
212+
def session_sync(
213+
session_pool_sync: ydb.QuerySessionPool,
214+
) -> Generator[ydb.QuerySession]:
215+
for name in ["table", "table1", "table2"]:
216+
session_pool_sync.execute_with_retries(
217+
f"""
218+
DELETE FROM {name};
219+
INSERT INTO {name} (id, val) VALUES
220+
(0, 0),
221+
(1, 1),
222+
(2, 2),
223+
(3, 3)
224+
"""
225+
)
226+
227+
session = session_pool_sync.acquire()
228+
yield session
229+
session_pool_sync.release(session)

tests/test_connection.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
from collections.abc import AsyncGenerator
44
from collections.abc import Generator
55
from contextlib import suppress
6+
from inspect import iscoroutine
67

78
import pytest
89
import pytest_asyncio
910
import ydb
1011
import ydb_dbapi as dbapi
11-
from sqlalchemy.util import await_only, greenlet_spawn
12-
from inspect import iscoroutine
12+
from sqlalchemy.util import await_only
13+
from sqlalchemy.util import greenlet_spawn
1314

1415

15-
def maybe_await(obj):
16+
def maybe_await(obj: callable) -> any:
1617
if not iscoroutine(obj):
1718
return obj
1819
return await_only(obj)
@@ -66,7 +67,9 @@ def _test_connection(self, connection: dbapi.Connection) -> None:
6667
with pytest.raises(dbapi.ProgrammingError):
6768
maybe_await(connection.describe("/local/foo"))
6869

69-
maybe_await(cur.execute("CREATE TABLE foo(id Int64 NOT NULL, PRIMARY KEY (id))"))
70+
maybe_await(cur.execute(
71+
"CREATE TABLE foo(id Int64 NOT NULL, PRIMARY KEY (id))"
72+
))
7073

7174
assert maybe_await(connection.check_exists("/local/foo"))
7275

@@ -113,7 +116,11 @@ def _test_cursor_raw_query(self, connection: dbapi.Connection) -> None:
113116

114117
maybe_await(cur.close())
115118

116-
def _test_errors(self, connection: dbapi.Connection, connect_method=dbapi.connect) -> None:
119+
def _test_errors(
120+
self,
121+
connection: dbapi.Connection,
122+
connect_method: callable = dbapi.connect
123+
) -> None:
117124
with pytest.raises(dbapi.InterfaceError):
118125
maybe_await(connect_method(
119126
"localhost:2136", # type: ignore
@@ -137,7 +144,9 @@ def _test_errors(self, connection: dbapi.Connection, connect_method=dbapi.connec
137144
with pytest.raises(dbapi.ProgrammingError):
138145
maybe_await(cur.execute("SELECT * FROM test"))
139146

140-
maybe_await(cur.execute("CREATE TABLE test(id Int64, PRIMARY KEY (id))"))
147+
maybe_await(cur.execute(
148+
"CREATE TABLE test(id Int64, PRIMARY KEY (id))"
149+
))
141150

142151
maybe_await(cur.execute("INSERT INTO test(id) VALUES(1)"))
143152

@@ -195,7 +204,7 @@ class TestAsyncConnection(BaseDBApiTestSuit):
195204
async def connection(
196205
self, connection_kwargs: dict
197206
) -> AsyncGenerator[dbapi.AsyncConnection]:
198-
def connect():
207+
def connect() -> dbapi.AsyncConnection:
199208
return maybe_await(dbapi.async_connect(**connection_kwargs))
200209

201210
conn = await greenlet_spawn(connect)

0 commit comments

Comments
 (0)