Skip to content

Commit 5313f79

Browse files
authored
Merge pull request #505 from ydb-platform/new_tableclient_methods
Hide session management for table client methods
2 parents a1a49da + 7e51e81 commit 5313f79

File tree

6 files changed

+635
-0
lines changed

6 files changed

+635
-0
lines changed

tests/aio/test_table_client.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import pytest
2+
import ydb
3+
4+
5+
class TestTableClient:
6+
@pytest.mark.asyncio
7+
async def test_create_table(self, driver: ydb.aio.Driver):
8+
client = driver.table_client
9+
table_name = "/local/testtableclient"
10+
try:
11+
await client.drop_table(table_name)
12+
except ydb.SchemeError:
13+
pass
14+
15+
with pytest.raises(ydb.SchemeError):
16+
await client.describe_table(table_name)
17+
18+
description = (
19+
ydb.TableDescription()
20+
.with_primary_keys("key1", "key2")
21+
.with_columns(
22+
ydb.Column("key1", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
23+
ydb.Column("key2", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
24+
ydb.Column("value", ydb.OptionalType(ydb.PrimitiveType.Utf8)),
25+
)
26+
)
27+
28+
await client.create_table(table_name, description)
29+
30+
actual_description = await client.describe_table(table_name)
31+
32+
assert actual_description.columns == description.columns
33+
34+
@pytest.mark.asyncio
35+
async def test_alter_table(self, driver: ydb.aio.Driver):
36+
client = driver.table_client
37+
38+
table_name = "/local/testtableclient"
39+
try:
40+
await client.drop_table(table_name)
41+
except ydb.SchemeError:
42+
pass
43+
44+
description = (
45+
ydb.TableDescription()
46+
.with_primary_keys("key1", "key2")
47+
.with_columns(
48+
ydb.Column("key1", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
49+
ydb.Column("key2", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
50+
ydb.Column("value", ydb.OptionalType(ydb.PrimitiveType.Utf8)),
51+
)
52+
)
53+
54+
await client.create_table(table_name, description)
55+
56+
await client.alter_table(
57+
table_name,
58+
add_columns=[
59+
ydb.Column("value2", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
60+
],
61+
)
62+
63+
description = await client.describe_table(table_name)
64+
assert len(description.columns) == 4
65+
66+
@pytest.mark.asyncio
67+
async def test_copy_table(self, driver: ydb.aio.Driver):
68+
client = driver.table_client
69+
table_name = "/local/testtableclient"
70+
try:
71+
await client.drop_table(table_name)
72+
except ydb.SchemeError:
73+
pass
74+
75+
description = (
76+
ydb.TableDescription()
77+
.with_primary_keys("key1", "key2")
78+
.with_columns(
79+
ydb.Column("key1", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
80+
ydb.Column("key2", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
81+
ydb.Column("value", ydb.OptionalType(ydb.PrimitiveType.Utf8)),
82+
)
83+
)
84+
85+
await client.create_table(table_name, description)
86+
87+
await client.copy_table(table_name, table_name + "_copy")
88+
89+
copied_description = await client.describe_table(table_name + "_copy")
90+
91+
assert description.columns == copied_description.columns

tests/table/test_table_client.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import pytest
2+
import ydb
3+
4+
5+
class TestTableClient:
6+
def test_create_table(self, driver_sync: ydb.Driver):
7+
client = driver_sync.table_client
8+
table_name = "/local/testtableclient"
9+
try:
10+
client.drop_table(table_name)
11+
except ydb.SchemeError:
12+
pass
13+
14+
with pytest.raises(ydb.SchemeError):
15+
client.describe_table(table_name)
16+
17+
description = (
18+
ydb.TableDescription()
19+
.with_primary_keys("key1", "key2")
20+
.with_columns(
21+
ydb.Column("key1", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
22+
ydb.Column("key2", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
23+
ydb.Column("value", ydb.OptionalType(ydb.PrimitiveType.Utf8)),
24+
)
25+
)
26+
27+
client.create_table(table_name, description)
28+
29+
actual_description = client.describe_table(table_name)
30+
31+
assert actual_description.columns == description.columns
32+
33+
def test_alter_table(self, driver_sync: ydb.Driver):
34+
client = driver_sync.table_client
35+
36+
table_name = "/local/testtableclient"
37+
try:
38+
client.drop_table(table_name)
39+
except ydb.SchemeError:
40+
pass
41+
42+
description = (
43+
ydb.TableDescription()
44+
.with_primary_keys("key1", "key2")
45+
.with_columns(
46+
ydb.Column("key1", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
47+
ydb.Column("key2", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
48+
ydb.Column("value", ydb.OptionalType(ydb.PrimitiveType.Utf8)),
49+
)
50+
)
51+
52+
client.create_table(table_name, description)
53+
54+
client.alter_table(
55+
table_name,
56+
add_columns=[
57+
ydb.Column("value2", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
58+
],
59+
)
60+
61+
description = client.describe_table(table_name)
62+
assert len(description.columns) == 4
63+
64+
def test_copy_table(self, driver_sync: ydb.Driver):
65+
client = driver_sync.table_client
66+
table_name = "/local/testtableclient"
67+
try:
68+
client.drop_table(table_name)
69+
except ydb.SchemeError:
70+
pass
71+
72+
description = (
73+
ydb.TableDescription()
74+
.with_primary_keys("key1", "key2")
75+
.with_columns(
76+
ydb.Column("key1", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
77+
ydb.Column("key2", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
78+
ydb.Column("value", ydb.OptionalType(ydb.PrimitiveType.Utf8)),
79+
)
80+
)
81+
82+
client.create_table(table_name, description)
83+
84+
client.copy_table(table_name, table_name + "_copy")
85+
86+
copied_description = client.describe_table(table_name + "_copy")
87+
88+
assert description.columns == copied_description.columns

ydb/aio/driver.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ def __init__(
5959
self.scheme_client = scheme.SchemeClient(self)
6060
self.table_client = table.TableClient(self, config.table_client_settings)
6161
self.topic_client = topic.TopicClientAsyncIO(self, config.topic_client_settings)
62+
63+
async def stop(self, timeout=10):
64+
await self.table_client._stop_pool_if_needed(timeout=timeout)
65+
await super().stop(timeout=timeout)

0 commit comments

Comments
 (0)