Skip to content

Commit bd94a92

Browse files
Test datetime2 format (#18556)
1 parent ebe630e commit bd94a92

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
import pytest
3+
from ydb.tests.library.compatibility.fixtures import MixedClusterFixture
4+
from ydb.tests.oss.ydb_sdk_import import ydb
5+
6+
from datetime import datetime, timedelta
7+
8+
9+
class TestDatetime2Format(MixedClusterFixture):
10+
@pytest.fixture(autouse=True, scope="function")
11+
def setup(self):
12+
yield from self.setup_cluster()
13+
14+
def test_simple(self):
15+
16+
with ydb.QuerySessionPool(self.driver) as session_pool:
17+
rows = 100
18+
table_name = 'dates_table'
19+
20+
# ---------------- CREATE TABLE ------------------
21+
query = f"""
22+
CREATE TABLE {table_name} (
23+
id Uint32,
24+
event_date DateTime,
25+
PRIMARY KEY (id)
26+
) WITH (
27+
PARTITION_AT_KEYS = ({", ".join(str(i) for i in range(1, rows))})
28+
);
29+
"""
30+
session_pool.execute_with_retries(query)
31+
32+
# ---------------- INSERT ------------------
33+
start_date = datetime(2023, 1, 1)
34+
values = []
35+
36+
for i in range(1, rows):
37+
date = (start_date + timedelta(days=i - 1)).strftime("%Y-%m-%dT%H:%M:%SZ")
38+
values.append(f"({i}, CAST(\"{date}\" AS DateTime))")
39+
40+
query = f"""
41+
UPSERT INTO {table_name} (id, event_date) VALUES {",\n ".join(values)};
42+
"""
43+
44+
session_pool.execute_with_retries(query)
45+
46+
# ---------------- SELECT ------------------
47+
query = f"""
48+
SELECT
49+
DateTime::Format('%Y-%m-%d')(event_date) as date
50+
FROM {table_name} order by date;
51+
"""
52+
result_sets = session_pool.execute_with_retries(query)
53+
assert result_sets[0].rows[0]['date'] == b'2023-01-01'

ydb/tests/compatibility/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ TEST_SRCS(
1515
test_stress.py
1616
test_statistics.py
1717
test_rolling.py
18+
test_datetime2.py
1819
)
1920

2021
SIZE(LARGE)

0 commit comments

Comments
 (0)