Skip to content

Commit 0daa246

Browse files
authored
Merge pull request #103 from ydb-platform/data-convs-tests
Add tests for datatypes converters
2 parents b0ef996 + 771bf4c commit 0daa246

File tree

1 file changed

+83
-29
lines changed

1 file changed

+83
-29
lines changed

tests/aio/test_types.py

Lines changed: 83 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,103 @@
11
import pytest
22
import ydb
3-
import datetime
43

4+
from datetime import date, datetime, timedelta
5+
from decimal import Decimal
6+
from uuid import uuid4
57

6-
@pytest.mark.parametrize("enabled", [False, True])
8+
9+
@pytest.mark.parametrize(
10+
"value,ydb_type",
11+
[
12+
(True, "Bool"),
13+
(-125, "Int8"),
14+
(None, "Int8?"),
15+
(-32766, "Int16"),
16+
(-1123, "Int32"),
17+
(-2157583648, "Int64"),
18+
(255, "UInt8"),
19+
(65534, "UInt16"),
20+
(5555, "UInt32"),
21+
(2157583649, "UInt64"),
22+
(3.1415, "Double"),
23+
(".31415926535e1", "DyNumber"),
24+
(Decimal("3.1415926535"), "Decimal(28, 10)"),
25+
(b"Hello, YDB!", "String"),
26+
("Hello, 🐍!", "Utf8"),
27+
('{"foo": "bar"}', "Json"),
28+
(b'{"foo"="bar"}', "Yson"),
29+
('{"foo":"bar"}', "JsonDocument"),
30+
(uuid4(), "Uuid"),
31+
([1, 2, 3], "List<Int8>"),
32+
# ({1, 2, 3}, "Set<Int8>"), # FIXME: AttributeError: 'set' object has no attribute 'items'
33+
([b"a", b"b", b"c"], "List<String>"),
34+
({"a": 1001, "b": 1002}, "Dict<Utf8, Int32>"),
35+
(("a", 1001), "Tuple<Utf8, Int32>"),
36+
({"foo": True, "bar": None}, "Struct<foo:Bool?, bar:Int32?>"),
37+
(100, "Date"),
38+
(100, "Datetime"),
39+
(-100, "Interval"),
40+
(100, "Timestamp"),
41+
(1511789040123456, "Timestamp"),
42+
],
43+
)
744
@pytest.mark.asyncio
8-
async def test_interval(driver, database, enabled):
9-
client = ydb.TableClient(
10-
driver, ydb.TableClientSettings().with_native_interval_in_result_sets(enabled)
11-
)
12-
session = await client.session().create()
45+
async def test_types(driver, database, value, ydb_type):
46+
session = await driver.table_client.session().create()
1347
prepared = await session.prepare(
14-
"DECLARE $param as Interval;\n SELECT $param as value",
48+
f"DECLARE $param as {ydb_type}; SELECT $param as value"
1549
)
1650

17-
param = datetime.timedelta(microseconds=-100) if enabled else -100
1851
result = await session.transaction().execute(
19-
prepared, {"$param": param}, commit_tx=True
52+
prepared, {"$param": value}, commit_tx=True
2053
)
21-
assert result[0].rows[0].value == param
54+
assert result[0].rows[0].value == value
55+
56+
57+
test_td = timedelta(microseconds=-100)
58+
test_now = datetime.utcnow()
59+
test_today = date.today()
60+
test_dt_today = datetime.today()
2261

2362

24-
@pytest.mark.parametrize("enabled", [False, True])
63+
@pytest.mark.parametrize(
64+
"value,ydb_type,result_value",
65+
[
66+
# FIXME: TypeError: 'datetime.date'/'datetime.datetime' object cannot be interpreted as an integer
67+
# (test_today, 'Date', test_today),
68+
# (test_dt_today, "Datetime", test_dt_today),
69+
(365, "Date", date(1971, 1, 1)),
70+
(3600 * 24 * 365, "Datetime", datetime(1971, 1, 1, 0, 0)),
71+
(test_td, "Interval", test_td),
72+
(test_now, "Timestamp", test_now),
73+
(
74+
1511789040123456,
75+
"Timestamp",
76+
datetime.fromisoformat("2017-11-27 13:24:00.123456"),
77+
),
78+
('{"foo": "bar"}', "Json", {"foo": "bar"}),
79+
('{"foo": "bar"}', "JsonDocument", {"foo": "bar"}),
80+
],
81+
)
2582
@pytest.mark.asyncio
26-
async def test_timestamp(driver, database, enabled):
27-
client = ydb.TableClient(
28-
driver, ydb.TableClientSettings().with_native_timestamp_in_result_sets(enabled)
83+
async def test_types_native(driver, database, value, ydb_type, result_value):
84+
settings = (
85+
ydb.TableClientSettings()
86+
.with_native_date_in_result_sets(True)
87+
.with_native_datetime_in_result_sets(True)
88+
.with_native_timestamp_in_result_sets(True)
89+
.with_native_interval_in_result_sets(True)
90+
.with_native_json_in_result_sets(True)
2991
)
92+
93+
client = ydb.TableClient(driver, settings)
3094
session = await client.session().create()
31-
prepared = await session.prepare(
32-
"DECLARE $param as Timestamp;\n SELECT $param as value",
33-
)
3495

35-
param = datetime.datetime.utcnow() if enabled else 100
36-
result = await session.transaction().execute(
37-
prepared, {"$param": param}, commit_tx=True
96+
prepared = await session.prepare(
97+
f"DECLARE $param as {ydb_type}; SELECT $param as value"
3898
)
39-
assert result[0].rows[0].value == param
4099

41100
result = await session.transaction().execute(
42-
prepared, {"$param": 1511789040123456}, commit_tx=True
101+
prepared, {"$param": value}, commit_tx=True
43102
)
44-
if enabled:
45-
assert result[0].rows[0].value == datetime.datetime.fromisoformat(
46-
"2017-11-27 13:24:00.123456"
47-
)
48-
else:
49-
assert result[0].rows[0].value == 1511789040123456
103+
assert result[0].rows[0].value == result_value

0 commit comments

Comments
 (0)