2014-10-09 01:30:22 +02:00
|
|
|
/*
|
|
|
|
* sql_tests.cpp
|
|
|
|
*/
|
|
|
|
|
2017-02-10 21:41:34 +01:00
|
|
|
#include "thirdparty/microtest/microtest.h"
|
2017-04-07 15:47:51 +02:00
|
|
|
|
2014-10-16 15:54:51 +02:00
|
|
|
#include "SQLParser.h"
|
2017-04-07 15:47:51 +02:00
|
|
|
#include "util/sqlhelper.h"
|
|
|
|
|
|
|
|
#include "sql_asserts.h"
|
2014-12-02 01:27:02 +01:00
|
|
|
|
2014-11-26 16:20:55 +01:00
|
|
|
using namespace hsql;
|
2014-10-09 01:30:22 +02:00
|
|
|
|
2014-12-15 16:43:32 +01:00
|
|
|
|
|
|
|
TEST(DeleteStatementTest) {
|
2017-04-07 16:26:00 +02:00
|
|
|
SQLParserResult result;
|
2017-07-21 02:47:45 +02:00
|
|
|
SQLParser::parse("DELETE FROM students WHERE grade > 2.0;", &result);
|
2017-03-07 02:01:00 +01:00
|
|
|
|
2017-04-07 16:26:00 +02:00
|
|
|
ASSERT(result.isValid());
|
|
|
|
ASSERT_EQ(result.size(), 1);
|
|
|
|
ASSERT(result.getStatement(0)->type() == kStmtDelete);
|
|
|
|
|
|
|
|
const DeleteStatement* stmt = (const DeleteStatement*) result.getStatement(0);
|
2017-03-07 02:01:00 +01:00
|
|
|
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);
|
2014-10-09 01:30:22 +02:00
|
|
|
}
|
|
|
|
|
2014-12-15 16:43:32 +01:00
|
|
|
TEST(CreateStatementTest) {
|
2017-04-07 16:26:00 +02:00
|
|
|
SQLParserResult result;
|
2018-10-19 14:18:03 +02:00
|
|
|
SQLParser::parse("CREATE TABLE students (name VARCHAR(50), student_number INT, city INTEGER NULL, grade DOUBLE NOT NULL, comment TEXT)", &result);
|
2017-04-07 16:26:00 +02:00
|
|
|
|
|
|
|
ASSERT(result.isValid());
|
|
|
|
ASSERT_EQ(result.size(), 1);
|
|
|
|
ASSERT_EQ(result.getStatement(0)->type(), kStmtCreate);
|
2017-03-07 02:01:00 +01:00
|
|
|
|
2017-04-07 16:26:00 +02:00
|
|
|
const CreateStatement* stmt = (const CreateStatement*) result.getStatement(0);
|
2017-04-21 16:15:07 +02:00
|
|
|
ASSERT_EQ(stmt->type, kCreateTable);
|
2017-03-07 02:01:00 +01:00
|
|
|
ASSERT_STREQ(stmt->tableName, "students");
|
|
|
|
ASSERT_NOTNULL(stmt->columns);
|
2018-10-19 14:18:03 +02:00
|
|
|
ASSERT_EQ(stmt->columns->size(), 5);
|
2017-03-07 02:01:00 +01:00
|
|
|
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");
|
2018-10-19 14:18:03 +02:00
|
|
|
ASSERT_STREQ(stmt->columns->at(4)->name, "comment");
|
|
|
|
ASSERT_EQ(stmt->columns->at(0)->type, (ColumnType{DataType::VARCHAR, 50}));
|
|
|
|
ASSERT_EQ(stmt->columns->at(1)->type, ColumnType{DataType::INT});
|
|
|
|
ASSERT_EQ(stmt->columns->at(2)->type, ColumnType{DataType::INT});
|
|
|
|
ASSERT_EQ(stmt->columns->at(3)->type, ColumnType{DataType::DOUBLE});
|
|
|
|
ASSERT_EQ(stmt->columns->at(4)->type, ColumnType{DataType::TEXT});
|
|
|
|
ASSERT_EQ(stmt->columns->at(0)->nullable, false);
|
|
|
|
ASSERT_EQ(stmt->columns->at(1)->nullable, false);
|
|
|
|
ASSERT_EQ(stmt->columns->at(2)->nullable, true);
|
|
|
|
ASSERT_EQ(stmt->columns->at(3)->nullable, false);
|
|
|
|
ASSERT_EQ(stmt->columns->at(3)->nullable, false);
|
2014-12-02 01:27:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-15 16:43:32 +01:00
|
|
|
TEST(UpdateStatementTest) {
|
2017-04-07 16:26:00 +02:00
|
|
|
SQLParserResult result;
|
2018-11-12 15:35:31 +01:00
|
|
|
SQLParser::parse("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max O''Mustermann';", &result);
|
2017-04-07 16:26:00 +02:00
|
|
|
|
|
|
|
ASSERT(result.isValid());
|
|
|
|
ASSERT_EQ(result.size(), 1);
|
|
|
|
ASSERT_EQ(result.getStatement(0)->type(), kStmtUpdate);
|
2017-03-07 02:01:00 +01:00
|
|
|
|
2017-04-07 16:26:00 +02:00
|
|
|
const UpdateStatement* stmt = (const UpdateStatement*) result.getStatement(0);
|
2017-03-07 02:01:00 +01:00
|
|
|
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));
|
2017-06-29 13:40:24 +02:00
|
|
|
ASSERT_EQ(stmt->where->opType, kOpEquals);
|
2017-03-07 02:01:00 +01:00
|
|
|
ASSERT_STREQ(stmt->where->expr->name, "name");
|
2018-11-12 15:35:31 +01:00
|
|
|
ASSERT_STREQ(stmt->where->expr2->name, "Max O'Mustermann");
|
2014-12-02 01:27:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-15 16:43:32 +01:00
|
|
|
TEST(InsertStatementTest) {
|
2017-03-07 02:01:00 +01:00
|
|
|
TEST_PARSE_SINGLE_SQL(
|
|
|
|
"INSERT INTO students VALUES ('Max Mustermann', 12345, 'Musterhausen', 2.0)",
|
|
|
|
kStmtInsert,
|
|
|
|
InsertStatement,
|
|
|
|
result,
|
|
|
|
stmt);
|
2014-12-02 01:27:02 +01:00
|
|
|
|
2017-03-07 02:01:00 +01:00
|
|
|
ASSERT_EQ(stmt->values->size(), 4);
|
|
|
|
// TODO
|
2014-12-03 16:53:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-15 16:43:32 +01:00
|
|
|
TEST(DropTableStatementTest) {
|
2017-03-07 02:01:00 +01:00
|
|
|
TEST_PARSE_SINGLE_SQL(
|
|
|
|
"DROP TABLE students",
|
|
|
|
kStmtDrop,
|
|
|
|
DropStatement,
|
|
|
|
result,
|
|
|
|
stmt);
|
|
|
|
|
2017-10-12 20:19:59 +02:00
|
|
|
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);
|
2017-04-21 16:15:07 +02:00
|
|
|
ASSERT_EQ(stmt->type, kDropTable);
|
2017-03-07 02:01:00 +01:00
|
|
|
ASSERT_NOTNULL(stmt->name);
|
|
|
|
ASSERT_STREQ(stmt->name, "students");
|
2014-12-15 16:43:32 +01:00
|
|
|
}
|
|
|
|
|
2017-04-08 02:37:30 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:09:02 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2017-04-21 16:15:07 +02:00
|
|
|
|
|
|
|
SQLParserResult parse_and_move(std::string query) {
|
|
|
|
hsql::SQLParserResult result;
|
2017-07-21 02:47:45 +02:00
|
|
|
hsql::SQLParser::parse(query, &result);
|
2017-04-21 16:15:07 +02:00
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:41 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-01-17 15:43:38 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-04-07 16:26:00 +02:00
|
|
|
TEST_MAIN();
|