Skip to content

Commit f18424f

Browse files
author
Vladislav Gogov
authored
Scenario test for issue: #11186 (#12138)
1 parent a417a28 commit f18424f

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

ydb/tests/olap/scenario/helpers/scenario_tests_helper.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,26 @@ def execute_scan_query(
395395
allure.attach(json.dumps(rows), 'result', allure.attachment_type.JSON)
396396
return ret
397397

398+
@allure.step('Execute query')
399+
def execute_query(
400+
self, yql: str, expected_status: ydb.StatusCode | Set[ydb.StatusCode] = ydb.StatusCode.SUCCESS
401+
):
402+
"""Run a query on the tested database.
403+
404+
Args:
405+
yql: Query text.
406+
expected_status: Expected status or set of database response statuses. If the response status is not in the expected set, an exception is thrown.
407+
408+
Example:
409+
tablename = 'testTable'
410+
sth = ScenarioTestHelper(ctx)
411+
sth.execute_query(f'INSERT INTO `{sth.get_full_path("tablename") }` (key, c) values(1, 100)')
412+
"""
413+
414+
allure.attach(yql, 'request', allure.attachment_type.TEXT)
415+
with ydb.QuerySessionPool(YdbCluster.get_ydb_driver()) as pool:
416+
self._run_with_expected_status(lambda: pool.execute_with_retries(yql), expected_status)
417+
398418
def drop_if_exist(self, names: List[str], operation) -> None:
399419
"""Erase entities in the tested database, if it exists.
400420
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from conftest import BaseTestSet
2+
from ydb.tests.olap.scenario.helpers import (
3+
ScenarioTestHelper,
4+
TestContext,
5+
CreateTable,
6+
)
7+
8+
from ydb import PrimitiveType
9+
from typing import List, Dict, Any
10+
from ydb.tests.olap.lib.utils import get_external_param
11+
import threading
12+
13+
14+
class TestInsert(BaseTestSet):
15+
schema_cnt = (
16+
ScenarioTestHelper.Schema()
17+
.with_column(name="key", type=PrimitiveType.Int32, not_null=True)
18+
.with_column(name="c", type=PrimitiveType.Int64)
19+
.with_key_columns("key")
20+
)
21+
22+
schema_log = (
23+
ScenarioTestHelper.Schema()
24+
.with_column(name="key", type=PrimitiveType.Int32, not_null=True)
25+
.with_key_columns("key")
26+
)
27+
28+
def _loop_upsert(self, ctx: TestContext, data: list):
29+
sth = ScenarioTestHelper(ctx)
30+
for batch in data:
31+
sth.bulk_upsert_data("log", self.schema_log, batch)
32+
33+
def _loop_insert(self, ctx: TestContext, rows_count: int):
34+
sth = ScenarioTestHelper(ctx)
35+
log: str = sth.get_full_path("log")
36+
cnt: str = sth.get_full_path("cnt")
37+
for i in range(rows_count):
38+
sth.execute_query(
39+
f'$cnt = SELECT CAST(COUNT(*) AS INT64) from `{log}`; INSERT INTO `{cnt}` (key, c) values({i}, $cnt)'
40+
)
41+
42+
def scenario_read_data_during_bulk_upsert(self, ctx: TestContext):
43+
sth = ScenarioTestHelper(ctx)
44+
cnt_table_name: str = "cnt"
45+
log_table_name: str = "log"
46+
batches_count = int(get_external_param("batches_count", "10"))
47+
rows_count = int(get_external_param("rows_count", "1000"))
48+
inserts_count = int(get_external_param("inserts_count", "200"))
49+
sth.execute_scheme_query(
50+
CreateTable(cnt_table_name).with_schema(self.schema_cnt)
51+
)
52+
sth.execute_scheme_query(
53+
CreateTable(log_table_name).with_schema(self.schema_log)
54+
)
55+
data: List = []
56+
for i in range(batches_count):
57+
batch: List[Dict[str, Any]] = []
58+
for j in range(rows_count):
59+
batch.append({"key": j + rows_count * i})
60+
data.append(batch)
61+
62+
thread1 = threading.Thread(target=self._loop_upsert, args=[ctx, data])
63+
thread2 = threading.Thread(target=self._loop_insert, args=[ctx, inserts_count])
64+
65+
thread1.start()
66+
thread2.start()
67+
68+
thread2.join()
69+
thread1.join()
70+
71+
rows: int = sth.get_table_rows_count(cnt_table_name)
72+
assert rows == inserts_count
73+
scan_result = sth.execute_scan_query(
74+
f"SELECT key, c FROM `{sth.get_full_path(cnt_table_name)}` ORDER BY key"
75+
)
76+
for i in range(rows):
77+
if scan_result.result_set.rows[i]["key"] != i:
78+
assert False, f"{i} ?= {scan_result.result_set.rows[i]['key']}"
79+
80+
rows: int = sth.get_table_rows_count(log_table_name)
81+
assert rows == rows_count * batches_count
82+
scan_result = sth.execute_scan_query(
83+
f"SELECT key FROM `{sth.get_full_path(log_table_name)}` ORDER BY key"
84+
)
85+
for i in range(rows):
86+
if scan_result.result_set.rows[i]["key"] != i:
87+
assert False, f"{i} ?= {scan_result.result_set.rows[i]['key']}"

ydb/tests/olap/scenario/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ PY3TEST()
1010
test_simple.py
1111
test_scheme_load.py
1212
test_alter_tiering.py
13+
test_insert.py
1314
)
1415

1516
PEERDIR(

0 commit comments

Comments
 (0)