Skip to content

Commit 21e076d

Browse files
committed
refactor connection
1 parent 933d834 commit 21e076d

File tree

9 files changed

+224
-186
lines changed

9 files changed

+224
-186
lines changed

pyproject.toml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,40 @@ target-version = "py39"
4040
src = ["ydb_dbapi", "tests"]
4141

4242
[tool.ruff.lint]
43-
select = ["F", "E"]
43+
ignore = [
44+
"D", # Allow not to have docstring for each method
45+
"ANN101", # Allow not to specify `Self` type for `self` param
46+
"ANN102", # Allow not to specify `Class` type for `cls` param
47+
"ANN401", # Allow to use Any type
48+
"FIX002", # Allow to use TODO in code
49+
"TD001", # Allow to specify FIXME (still trigger FIX001)
50+
"TD002", # Allow not to specify author of todo
51+
"TD003", # Allow not to specify ticket of todo (it doesn't work)
52+
"PERF401", # Allow not to use list comprehension each time
53+
"FBT", # Allow boolean positional arguments
54+
"TCH002", # Allow not to use `if TYPE_CHECKING` for all imports, which don't used explicitly
55+
"TCH003", # Allow not to use `if TYPE_CHECKING` for std lib imports
56+
"ISC001", # Conflicts with formatter
57+
"COM812", # Conflicts with formatter,
58+
# Ignores below could be deleted
59+
"EM101", # Allow to use string literals in exceptions
60+
"TRY003", # Allow specifying long messages outside the exception class
61+
]
62+
select = ["ALL"]
63+
64+
# Allow fix for all enabled rules (when `--fix`) is provided.
65+
fixable = ["ALL"]
66+
unfixable = ["B"]
67+
68+
# Allow unused variables when underscore-prefixed.
69+
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
70+
71+
[tool.ruff.lint.isort]
72+
force-single-line = true
73+
74+
[tool.ruff.lint.per-file-ignores]
75+
"**/test_*.py" = ["S", "SLF", "ANN201", "ARG", "PLR2004"]
76+
"__init__.py" = ["F401", "F403"]
4477

4578
[tool.pytest.ini_options]
4679
asyncio_mode = "auto"

tests/conftest.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import pytest
2-
import ydb
1+
from __future__ import annotations
32

4-
from typing import (
5-
Any,
6-
Callable,
7-
Generator,
8-
Optional,
9-
)
3+
from collections.abc import Generator
4+
from typing import Any
5+
from typing import Callable
106

7+
import pytest
8+
import ydb
119
from testcontainers.core.generic import DbContainer
1210
from testcontainers.core.generic import wait_container_is_ready
1311
from testcontainers.core.utils import setup_logger
@@ -18,7 +16,7 @@
1816
class YDBContainer(DbContainer):
1917
def __init__(
2018
self,
21-
name: Optional[str] = None,
19+
name: str | None = None,
2220
port: str = "2135",
2321
image: str = "ydbplatform/local-ydb:trunk",
2422
**kwargs: Any,
@@ -116,7 +114,7 @@ def connection_kwargs(ydb_container: YDBContainer) -> dict:
116114
}
117115

118116

119-
@pytest.fixture()
117+
@pytest.fixture
120118
async def driver(ydb_container, event_loop):
121119
driver = ydb.aio.Driver(
122120
connection_string=ydb_container.get_connection_string()
@@ -147,3 +145,11 @@ async def session_pool(driver: ydb.aio.Driver):
147145
)
148146

149147
yield session_pool
148+
149+
@pytest.fixture
150+
async def session(session_pool: ydb.aio.QuerySessionPool):
151+
session = await session_pool.acquire()
152+
153+
yield session
154+
155+
await session_pool.release(session)

tests/test_connection.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import pytest
44
import pytest_asyncio
55
import ydb
6-
76
import ydb_dbapi as dbapi
87

98

@@ -25,14 +24,13 @@ async def _test_isolation_level_read_only(
2524

2625
connection.set_isolation_level(isolation_level)
2726

28-
await connection.begin()
29-
3027
async with connection.cursor() as cursor:
3128
query = "UPSERT INTO foo(id) VALUES (1)"
3229
if read_only:
3330
with pytest.raises(dbapi.DatabaseError):
3431
await cursor.execute(query)
3532
await cursor.finish_query()
33+
3634
else:
3735
await cursor.execute(query)
3836

tests/test_cursor.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44

55
@pytest.mark.asyncio
6-
async def test_cursor_ddl(session_pool):
7-
cursor = ydb_dbapi.Cursor(session_pool=session_pool)
6+
async def test_cursor_ddl(session):
7+
cursor = ydb_dbapi.Cursor(session=session)
88

99
yql = """
1010
CREATE TABLE table (
@@ -27,8 +27,8 @@ async def test_cursor_ddl(session_pool):
2727

2828

2929
@pytest.mark.asyncio
30-
async def test_cursor_dml(session_pool):
31-
cursor = ydb_dbapi.Cursor(session_pool=session_pool)
30+
async def test_cursor_dml(session):
31+
cursor = ydb_dbapi.Cursor(session=session)
3232
yql_text = """
3333
INSERT INTO table (id, val) VALUES
3434
(1, 1),
@@ -39,7 +39,7 @@ async def test_cursor_dml(session_pool):
3939
await cursor.execute(query=yql_text)
4040
assert await cursor.fetchone() is None
4141

42-
cursor = ydb_dbapi.Cursor(session_pool=session_pool)
42+
cursor = ydb_dbapi.Cursor(session=session)
4343

4444
yql_text = """
4545
SELECT COUNT(*) FROM table as sum
@@ -53,8 +53,8 @@ async def test_cursor_dml(session_pool):
5353

5454

5555
@pytest.mark.asyncio
56-
async def test_cursor_fetch_one(session_pool):
57-
cursor = ydb_dbapi.Cursor(session_pool=session_pool)
56+
async def test_cursor_fetch_one(session):
57+
cursor = ydb_dbapi.Cursor(session=session)
5858
yql_text = """
5959
INSERT INTO table (id, val) VALUES
6060
(1, 1),
@@ -64,7 +64,7 @@ async def test_cursor_fetch_one(session_pool):
6464
await cursor.execute(query=yql_text)
6565
assert await cursor.fetchone() is None
6666

67-
cursor = ydb_dbapi.Cursor(session_pool=session_pool)
67+
cursor = ydb_dbapi.Cursor(session=session)
6868

6969
yql_text = """
7070
SELECT id, val FROM table
@@ -82,8 +82,8 @@ async def test_cursor_fetch_one(session_pool):
8282

8383

8484
@pytest.mark.asyncio
85-
async def test_cursor_fetch_many(session_pool):
86-
cursor = ydb_dbapi.Cursor(session_pool=session_pool)
85+
async def test_cursor_fetch_many(session):
86+
cursor = ydb_dbapi.Cursor(session=session)
8787
yql_text = """
8888
INSERT INTO table (id, val) VALUES
8989
(1, 1),
@@ -95,7 +95,7 @@ async def test_cursor_fetch_many(session_pool):
9595
await cursor.execute(query=yql_text)
9696
assert await cursor.fetchone() is None
9797

98-
cursor = ydb_dbapi.Cursor(session_pool=session_pool)
98+
cursor = ydb_dbapi.Cursor(session=session)
9999

100100
yql_text = """
101101
SELECT id, val FROM table
@@ -120,8 +120,8 @@ async def test_cursor_fetch_many(session_pool):
120120

121121

122122
@pytest.mark.asyncio
123-
async def test_cursor_fetch_all(session_pool):
124-
cursor = ydb_dbapi.Cursor(session_pool=session_pool)
123+
async def test_cursor_fetch_all(session):
124+
cursor = ydb_dbapi.Cursor(session=session)
125125
yql_text = """
126126
INSERT INTO table (id, val) VALUES
127127
(1, 1),
@@ -132,7 +132,7 @@ async def test_cursor_fetch_all(session_pool):
132132
await cursor.execute(query=yql_text)
133133
assert await cursor.fetchone() is None
134134

135-
cursor = ydb_dbapi.Cursor(session_pool=session_pool)
135+
cursor = ydb_dbapi.Cursor(session=session)
136136

137137
yql_text = """
138138
SELECT id, val FROM table
@@ -152,8 +152,8 @@ async def test_cursor_fetch_all(session_pool):
152152

153153

154154
@pytest.mark.asyncio
155-
async def test_cursor_next_set(session_pool):
156-
cursor = ydb_dbapi.Cursor(session_pool=session_pool)
155+
async def test_cursor_next_set(session):
156+
cursor = ydb_dbapi.Cursor(session=session)
157157
yql_text = """SELECT 1 as val; SELECT 2 as val;"""
158158

159159
await cursor.execute(query=yql_text)

ydb_dbapi/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
from .errors import * # noqa
2-
from .connection import Connection, IsolationLevel, connect # noqa
3-
from .cursors import Cursor # noqa
1+
from .connection import Connection
2+
from .connection import IsolationLevel
3+
from .connection import connect
4+
from .cursors import Cursor
5+
from .errors import *

0 commit comments

Comments
 (0)