Repository navigation
Expand file tree
/
Copy pathsql_tests.cpp
More file actions
227 lines (180 loc) · 5.81 KB
/
Copy pathsql_tests.cpp
File metadata and controls
227 lines (180 loc) · 5.81 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* sql_tests.cpp
*/
#include "thirdparty/microtest/microtest.h"
#include "SQLParser.h"
#include "util/sqlhelper.h"
#include "sql_asserts.h"
using namespace hsql;
TEST(DeleteStatementTest) {
SQLParserResult result;
SQLParser::parse("DELETE FROM students WHERE grade > 2.0;", &result);
ASSERT(result.isValid());
ASSERT_EQ(result.size(), 1);
ASSERT(result.getStatement(0)->type() == kStmtDelete);
const DeleteStatement* stmt = (const DeleteStatement*) result.getStatement(0);
ASSERT_STREQ(stmt->tableName, "students");
ASSERT_NOTNULL(stmt->expr);
ASSERT(stmt->expr->isType(kExprOperator));
ASSERT_STREQ(stmt->expr->expr->name, "grade");
ASSERT_EQ(stmt->expr->expr2->fval, 2.0);
}
TEST(CreateStatementTest) {
SQLParserResult result;
SQLParser::parse("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)", &result);
ASSERT(result.isValid());
ASSERT_EQ(result.size(), 1);
ASSERT_EQ(result.getStatement(0)->type(), kStmtCreate);
const CreateStatement* stmt = (const CreateStatement*) result.getStatement(0);
ASSERT_EQ(stmt->type, kCreateTable);
ASSERT_STREQ(stmt->tableName, "students");
ASSERT_NOTNULL(stmt->columns);
ASSERT_EQ(stmt->columns->size(), 4);
ASSERT_STREQ(stmt->columns->at(0)->name, "name");
ASSERT_STREQ(stmt->columns->at(1)->name, "student_number");
ASSERT_STREQ(stmt->columns->at(2)->name, "city");
ASSERT_STREQ(stmt->columns->at(3)->name, "grade");
ASSERT_EQ(stmt->columns->at(0)->type, ColumnDefinition::TEXT);
ASSERT_EQ(stmt->columns->at(1)->type, ColumnDefinition::INT);
ASSERT_EQ(stmt->columns->at(2)->type, ColumnDefinition::INT);
ASSERT_EQ(stmt->columns->at(3)->type, ColumnDefinition::DOUBLE);
}
TEST(UpdateStatementTest) {
SQLParserResult result;
SQLParser::parse("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';", &result);
ASSERT(result.isValid());
ASSERT_EQ(result.size(), 1);
ASSERT_EQ(result.getStatement(0)->type(), kStmtUpdate);
const UpdateStatement* stmt = (const UpdateStatement*) result.getStatement(0);
ASSERT_NOTNULL(stmt->table);
ASSERT_STREQ(stmt->table->name, "students");
ASSERT_NOTNULL(stmt->updates);
ASSERT_EQ(stmt->updates->size(), 2);
ASSERT_STREQ(stmt->updates->at(0)->column, "grade");
ASSERT_STREQ(stmt->updates->at(1)->column, "name");
ASSERT(stmt->updates->at(0)->value->isType(kExprLiteralFloat));
ASSERT(stmt->updates->at(1)->value->isType(kExprLiteralString));
ASSERT_EQ(stmt->updates->at(0)->value->fval, 5.0);
ASSERT_STREQ(stmt->updates->at(1)->value->name, "test");
ASSERT_NOTNULL(stmt->where);
ASSERT(stmt->where->isType(kExprOperator));
ASSERT_EQ(stmt->where->opType, kOpEquals);
ASSERT_STREQ(stmt->where->expr->name, "name");
ASSERT_STREQ(stmt->where->expr2->name, "Max Mustermann");
}
TEST(InsertStatementTest) {
TEST_PARSE_SINGLE_SQL(
"INSERT INTO students VALUES ('Max Mustermann', 12345, 'Musterhausen', 2.0)",
kStmtInsert,
InsertStatement,
result,
stmt);
ASSERT_EQ(stmt->values->size(), 4);
// TODO
}
TEST(DropTableStatementTest) {
TEST_PARSE_SINGLE_SQL(
"DROP TABLE students",
kStmtDrop,
DropStatement,
result,
stmt);
ASSERT_FALSE(stmt->ifExists);
ASSERT_EQ(stmt->type, kDropTable);
ASSERT_NOTNULL(stmt->name);
ASSERT_STREQ(stmt->name, "students");
}
TEST(DropTableIfExistsStatementTest) {
TEST_PARSE_SINGLE_SQL(
"DROP TABLE IF EXISTS students",
kStmtDrop,
DropStatement,
result,
stmt);
ASSERT_TRUE(stmt->ifExists);
ASSERT_EQ(stmt->type, kDropTable);
ASSERT_NOTNULL(stmt->name);
ASSERT_STREQ(stmt->name, "students");
}
TEST(ReleaseStatementTest) {
TEST_PARSE_SINGLE_SQL(
"SELECT * FROM students;",
kStmtSelect,
SelectStatement,
result,
stmt);
ASSERT_EQ(1, result.size());
ASSERT_NULL(stmt->whereClause);
std::vector<SQLStatement*> statements = result.releaseStatements();
ASSERT_EQ(0, result.size());
for (SQLStatement* stmt : statements) {
delete stmt;
}
}
TEST(ShowTableStatementTest) {
TEST_PARSE_SINGLE_SQL(
"SHOW TABLES;",
kStmtShow,
ShowStatement,
result,
stmt);
ASSERT_EQ(stmt->type, kShowTables);
ASSERT_NULL(stmt->name);
}
TEST(ShowColumnsStatementTest) {
TEST_PARSE_SINGLE_SQL(
"SHOW COLUMNS students;",
kStmtShow,
ShowStatement,
result,
stmt);
ASSERT_EQ(stmt->type, kShowColumns);
ASSERT_NOTNULL(stmt->name);
ASSERT_STREQ(stmt->name, "students");
}
SQLParserResult parse_and_move(std::string query) {
hsql::SQLParserResult result;
hsql::SQLParser::parse(query, &result);
// Moves on return.
return result;
}
SQLParserResult move_in_and_back(SQLParserResult res) {
// Moves on return.
return res;
}
TEST(MoveSQLResultTest) {
SQLParserResult res = parse_and_move("SELECT * FROM test;");
ASSERT(res.isValid());
ASSERT_EQ(1, res.size());
// Moved around.
SQLParserResult new_res = move_in_and_back(std::move(res));
// Original object should be invalid.
ASSERT_FALSE(res.isValid());
ASSERT_EQ(0, res.size());
ASSERT(new_res.isValid());
ASSERT_EQ(1, new_res.size());
}
TEST(HintTest) {
TEST_PARSE_SINGLE_SQL(
"SELECT * FROM students WITH HINT(NO_CACHE, SAMPLE_RATE(10));",
kStmtSelect,
SelectStatement,
result,
stmt);
ASSERT_NOTNULL(stmt->hints);
ASSERT_EQ(2, stmt->hints->size());
ASSERT_STREQ("NO_CACHE", stmt->hints->at(0)->name);
ASSERT_STREQ("SAMPLE_RATE", stmt->hints->at(1)->name);
ASSERT_EQ(1, stmt->hints->at(1)->exprList->size());
ASSERT_EQ(10, stmt->hints->at(1)->exprList->at(0)->ival);
}
TEST(StringLengthTest) {
TEST_PARSE_SQL_QUERY(
"SELECT * FROM bar; INSERT INTO foo VALUES (4);\t\n SELECT * FROM foo;",
result,
3);
ASSERT_EQ(result.getStatement(0)->stringLength, 18);
ASSERT_EQ(result.getStatement(1)->stringLength, 28);
ASSERT_EQ(result.getStatement(2)->stringLength, 21);
}
TEST_MAIN();