1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
#include <yql/essentials/parser/pg_wrapper/interface/raw_parser.h>
#include <util/stream/str.h>
#include <util/system/thread.h>
#include <library/cpp/testing/unittest/registar.h>
using namespace NYql;
class TEvents : public IPGParseEvents {
public:
void OnResult(const List* raw) override {
Result = PrintPGTree(raw);
}
void OnError(const TIssue& issue) override {
Issue = issue;
}
TMaybe<TIssue> Issue;
TMaybe<TString> Result;
};
const TStringBuf ExpectedSelect1 = "({RAWSTMT :stmt {SELECTSTMT :distinctClause <> :intoClause <> :targetList "
"({RESTARGET :name <> :indirection <> :val {A_CONST :val 1 :location 7} :location 7}) :fromClause <> :whereClause "
"<> :groupClause <> :groupDistinct false :havingClause <> :windowClause <> :valuesLists <> :sortClause <>"
" :limitOffset <> :limitCount <> :limitOption 0 :lockingClause <> :withClause <> :op 0 :all false :larg <> :rarg <>}"
" :stmt_location 0 :stmt_len 0})";
const TString Error1 = "ERROR: syntax error at or near \"SELECT1\"\n";
Y_UNIT_TEST_SUITE(ParseTests) {
Y_UNIT_TEST(TestOk) {
TEvents events;
PGParse(TString("SELECT 1"), events);
UNIT_ASSERT(events.Result);
UNIT_ASSERT(!events.Issue);
UNIT_ASSERT_NO_DIFF(*events.Result, ExpectedSelect1);
}
Y_UNIT_TEST(TestFail) {
TEvents events;
PGParse(TString(" \n SELECT1"), events);
UNIT_ASSERT(!events.Result);
UNIT_ASSERT(events.Issue);
auto msg = events.Issue->GetMessage();
UNIT_ASSERT_NO_DIFF(msg, Error1);
UNIT_ASSERT_VALUES_EQUAL(events.Issue->Position.Row, 2);
UNIT_ASSERT_VALUES_EQUAL(events.Issue->Position.Column, 3);
}
Y_UNIT_TEST(TestErrorPosUtf8) {
{
TEvents events;
PGParse(TString("/* привет */SELECT1"), events);
UNIT_ASSERT(!events.Result);
UNIT_ASSERT(events.Issue);
auto msg = events.Issue->GetMessage();
UNIT_ASSERT_VALUES_EQUAL(events.Issue->Position.Row, 1);
UNIT_ASSERT_VALUES_EQUAL(events.Issue->Position.Column, 13);
}
{
TEvents events;
PGParse(TString("/* привет */\n\nSELECT1"), events);
UNIT_ASSERT(!events.Result);
UNIT_ASSERT(events.Issue);
auto msg = events.Issue->GetMessage();
UNIT_ASSERT_NO_DIFF(msg, Error1);
UNIT_ASSERT_VALUES_EQUAL(events.Issue->Position.Row, 3);
UNIT_ASSERT_VALUES_EQUAL(events.Issue->Position.Column, 1);
}
}
}
const ui32 threadsCount = 10;
Y_UNIT_TEST_SUITE(MTParseTests) {
Y_UNIT_TEST(TestOk) {
TVector<THolder<TThread>> threads;
for (ui32 i = 0; i < threadsCount; ++i) {
threads.emplace_back(MakeHolder<TThread>([]() {
ui32 iters = 10000;
#if defined(_san_enabled_)
iters /= 100;
#endif
for (ui32 i = 0; i < iters; ++i) {
TEvents events;
PGParse(TString("SELECT 1"), events);
Y_ENSURE(events.Result);
Y_ENSURE(!events.Issue);
Y_ENSURE(*events.Result == ExpectedSelect1);
}
}));
}
for (ui32 i = 0; i < threadsCount; ++i) {
threads[i]->Start();
}
for (ui32 i = 0; i < threadsCount; ++i) {
threads[i]->Join();
}
}
Y_UNIT_TEST(TestFail) {
TVector<THolder<TThread>> threads;
for (ui32 i = 0; i < threadsCount; ++i) {
threads.emplace_back(MakeHolder<TThread>([]() {
ui32 iters = 10000;
#if defined(_san_enabled_)
iters /= 100;
#endif
for (ui32 i = 0; i < iters; ++i) {
TEvents events;
PGParse(TString(" \n SELECT1"), events);
Y_ENSURE(!events.Result);
Y_ENSURE(events.Issue);
auto msg = events.Issue->GetMessage();
Y_ENSURE(msg == Error1);
Y_ENSURE(events.Issue->Position.Row == 2);
Y_ENSURE(events.Issue->Position.Column == 3);
}
}));
}
for (ui32 i = 0; i < threadsCount; ++i) {
threads[i]->Start();
}
for (ui32 i = 0; i < threadsCount; ++i) {
threads[i]->Join();
}
}
}
|