|
| 1 | +import pytest |
| 2 | + |
| 3 | +import ydb |
| 4 | + |
| 5 | +from datetime import date, datetime, timedelta, timezone |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def settings_on(): |
| 10 | + settings = ( |
| 11 | + ydb.QueryClientSettings() |
| 12 | + .with_native_date_in_result_sets(True) |
| 13 | + .with_native_datetime_in_result_sets(True) |
| 14 | + .with_native_timestamp_in_result_sets(True) |
| 15 | + .with_native_interval_in_result_sets(True) |
| 16 | + .with_native_json_in_result_sets(True) |
| 17 | + ) |
| 18 | + return settings |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def settings_off(): |
| 23 | + settings = ( |
| 24 | + ydb.QueryClientSettings() |
| 25 | + .with_native_date_in_result_sets(False) |
| 26 | + .with_native_datetime_in_result_sets(False) |
| 27 | + .with_native_timestamp_in_result_sets(False) |
| 28 | + .with_native_interval_in_result_sets(False) |
| 29 | + .with_native_json_in_result_sets(False) |
| 30 | + ) |
| 31 | + return settings |
| 32 | + |
| 33 | + |
| 34 | +test_td = timedelta(microseconds=-100) |
| 35 | +test_now = datetime.utcnow() |
| 36 | +test_today = test_now.date() |
| 37 | +test_dt_today = datetime.today() |
| 38 | +tz4h = timezone(timedelta(hours=4)) |
| 39 | + |
| 40 | + |
| 41 | +params = pytest.mark.parametrize( |
| 42 | + "value,ydb_type,casted_result,uncasted_type", |
| 43 | + [ |
| 44 | + (test_today, "Date", test_today, int), |
| 45 | + (365, "Date", date(1971, 1, 1), int), |
| 46 | + (3600 * 24 * 365, "Datetime", datetime(1971, 1, 1, 0, 0), int), |
| 47 | + (datetime(1970, 1, 1, 4, 0, tzinfo=tz4h), "Timestamp", datetime(1970, 1, 1, 0, 0), int), |
| 48 | + (test_td, "Interval", test_td, int), |
| 49 | + (test_now, "Timestamp", test_now, int), |
| 50 | + ( |
| 51 | + 1511789040123456, |
| 52 | + "Timestamp", |
| 53 | + datetime.fromisoformat("2017-11-27 13:24:00.123456"), |
| 54 | + int, |
| 55 | + ), |
| 56 | + ('{"foo": "bar"}', "Json", {"foo": "bar"}, str), |
| 57 | + ('{"foo": "bar"}', "JsonDocument", {"foo": "bar"}, str), |
| 58 | + ], |
| 59 | +) |
| 60 | + |
| 61 | + |
| 62 | +class TestQueryClientSettings: |
| 63 | + @params |
| 64 | + def test_driver_turn_on(self, driver_sync, settings_on, value, ydb_type, casted_result, uncasted_type): |
| 65 | + driver_sync._driver_config.query_client_settings = settings_on |
| 66 | + pool = ydb.QuerySessionPool(driver_sync) |
| 67 | + result = pool.execute_with_retries( |
| 68 | + f"DECLARE $param as {ydb_type}; SELECT $param as value", |
| 69 | + {"$param": (value, getattr(ydb.PrimitiveType, ydb_type))}, |
| 70 | + ) |
| 71 | + assert result[0].rows[0].value == casted_result |
| 72 | + pool.stop() |
| 73 | + |
| 74 | + @params |
| 75 | + def test_driver_turn_off(self, driver_sync, settings_off, value, ydb_type, casted_result, uncasted_type): |
| 76 | + driver_sync._driver_config.query_client_settings = settings_off |
| 77 | + pool = ydb.QuerySessionPool(driver_sync) |
| 78 | + result = pool.execute_with_retries( |
| 79 | + f"DECLARE $param as {ydb_type}; SELECT $param as value", |
| 80 | + {"$param": (value, getattr(ydb.PrimitiveType, ydb_type))}, |
| 81 | + ) |
| 82 | + assert type(result[0].rows[0].value) == uncasted_type |
| 83 | + pool.stop() |
| 84 | + |
| 85 | + @params |
| 86 | + def test_session_pool_turn_on(self, driver_sync, settings_on, value, ydb_type, casted_result, uncasted_type): |
| 87 | + pool = ydb.QuerySessionPool(driver_sync, query_client_settings=settings_on) |
| 88 | + result = pool.execute_with_retries( |
| 89 | + f"DECLARE $param as {ydb_type}; SELECT $param as value", |
| 90 | + {"$param": (value, getattr(ydb.PrimitiveType, ydb_type))}, |
| 91 | + ) |
| 92 | + assert result[0].rows[0].value == casted_result |
| 93 | + pool.stop() |
| 94 | + |
| 95 | + @params |
| 96 | + def test_session_pool_turn_off(self, driver_sync, settings_off, value, ydb_type, casted_result, uncasted_type): |
| 97 | + pool = ydb.QuerySessionPool(driver_sync, query_client_settings=settings_off) |
| 98 | + result = pool.execute_with_retries( |
| 99 | + f"DECLARE $param as {ydb_type}; SELECT $param as value", |
| 100 | + {"$param": (value, getattr(ydb.PrimitiveType, ydb_type))}, |
| 101 | + ) |
| 102 | + assert type(result[0].rows[0].value) == uncasted_type |
| 103 | + pool.stop() |
| 104 | + |
| 105 | + @pytest.mark.asyncio |
| 106 | + @params |
| 107 | + async def test_driver_async_turn_on(self, driver, settings_on, value, ydb_type, casted_result, uncasted_type): |
| 108 | + driver._driver_config.query_client_settings = settings_on |
| 109 | + pool = ydb.aio.QuerySessionPool(driver) |
| 110 | + result = await pool.execute_with_retries( |
| 111 | + f"DECLARE $param as {ydb_type}; SELECT $param as value", |
| 112 | + {"$param": (value, getattr(ydb.PrimitiveType, ydb_type))}, |
| 113 | + ) |
| 114 | + assert result[0].rows[0].value == casted_result |
| 115 | + await pool.stop() |
| 116 | + |
| 117 | + @pytest.mark.asyncio |
| 118 | + @params |
| 119 | + async def test_driver_async_turn_off(self, driver, settings_off, value, ydb_type, casted_result, uncasted_type): |
| 120 | + driver._driver_config.query_client_settings = settings_off |
| 121 | + pool = ydb.aio.QuerySessionPool(driver) |
| 122 | + result = await pool.execute_with_retries( |
| 123 | + f"DECLARE $param as {ydb_type}; SELECT $param as value", |
| 124 | + {"$param": (value, getattr(ydb.PrimitiveType, ydb_type))}, |
| 125 | + ) |
| 126 | + assert type(result[0].rows[0].value) == uncasted_type |
| 127 | + await pool.stop() |
| 128 | + |
| 129 | + @pytest.mark.asyncio |
| 130 | + @params |
| 131 | + async def test_session_pool_async_turn_on(self, driver, settings_on, value, ydb_type, casted_result, uncasted_type): |
| 132 | + pool = ydb.aio.QuerySessionPool(driver, query_client_settings=settings_on) |
| 133 | + result = await pool.execute_with_retries( |
| 134 | + f"DECLARE $param as {ydb_type}; SELECT $param as value", |
| 135 | + {"$param": (value, getattr(ydb.PrimitiveType, ydb_type))}, |
| 136 | + ) |
| 137 | + assert result[0].rows[0].value == casted_result |
| 138 | + await pool.stop() |
| 139 | + |
| 140 | + @pytest.mark.asyncio |
| 141 | + @params |
| 142 | + async def test_session_pool_async_turn_off( |
| 143 | + self, driver, settings_off, value, ydb_type, casted_result, uncasted_type |
| 144 | + ): |
| 145 | + pool = ydb.aio.QuerySessionPool(driver, query_client_settings=settings_off) |
| 146 | + result = await pool.execute_with_retries( |
| 147 | + f"DECLARE $param as {ydb_type}; SELECT $param as value", |
| 148 | + {"$param": (value, getattr(ydb.PrimitiveType, ydb_type))}, |
| 149 | + ) |
| 150 | + assert type(result[0].rows[0].value) == uncasted_type |
| 151 | + await pool.stop() |
0 commit comments