|
1 | 1 | import pytest |
2 | 2 | import ydb |
3 | | -import datetime |
4 | 3 |
|
| 4 | +from datetime import date, datetime, timedelta |
| 5 | +from decimal import Decimal |
| 6 | +from uuid import uuid4 |
5 | 7 |
|
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 | +) |
7 | 44 | @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() |
13 | 47 | 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" |
15 | 49 | ) |
16 | 50 |
|
17 | | - param = datetime.timedelta(microseconds=-100) if enabled else -100 |
18 | 51 | result = await session.transaction().execute( |
19 | | - prepared, {"$param": param}, commit_tx=True |
| 52 | + prepared, {"$param": value}, commit_tx=True |
20 | 53 | ) |
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() |
22 | 61 |
|
23 | 62 |
|
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 | +) |
25 | 82 | @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) |
29 | 91 | ) |
| 92 | + |
| 93 | + client = ydb.TableClient(driver, settings) |
30 | 94 | session = await client.session().create() |
31 | | - prepared = await session.prepare( |
32 | | - "DECLARE $param as Timestamp;\n SELECT $param as value", |
33 | | - ) |
34 | 95 |
|
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" |
38 | 98 | ) |
39 | | - assert result[0].rows[0].value == param |
40 | 99 |
|
41 | 100 | result = await session.transaction().execute( |
42 | | - prepared, {"$param": 1511789040123456}, commit_tx=True |
| 101 | + prepared, {"$param": value}, commit_tx=True |
43 | 102 | ) |
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