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-03-07 02:01:00 +01:00
|
|
|
const SQLParserResult* result = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;");
|
|
|
|
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);
|
|
|
|
|
|
|
|
delete result;
|
2014-10-09 01:30:22 +02:00
|
|
|
}
|
|
|
|
|
2014-12-15 16:43:32 +01:00
|
|
|
TEST(CreateStatementTest) {
|
2017-03-07 02:01:00 +01:00
|
|
|
const SQLParserResult* result = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)");
|
|
|
|
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, CreateStatement::kTable);
|
|
|
|
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);
|
|
|
|
|
|
|
|
delete result;
|
2014-12-02 01:27:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-15 16:43:32 +01:00
|
|
|
TEST(UpdateStatementTest) {
|
2017-03-07 02:01:00 +01:00
|
|
|
const SQLParserResult* result = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';");
|
|
|
|
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(stmt->where->isSimpleOp('='));
|
|
|
|
ASSERT_STREQ(stmt->where->expr->name, "name");
|
|
|
|
ASSERT_STREQ(stmt->where->expr2->name, "Max Mustermann");
|
|
|
|
\
|
|
|
|
|
|
|
|
delete result;
|
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
|
2017-02-08 04:10:26 +01:00
|
|
|
|
2017-03-07 02:01:00 +01:00
|
|
|
delete result;
|
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);
|
|
|
|
|
|
|
|
ASSERT_EQ(stmt->type, DropStatement::kTable);
|
|
|
|
ASSERT_NOTNULL(stmt->name);
|
|
|
|
ASSERT_STREQ(stmt->name, "students");
|
|
|
|
|
|
|
|
delete result;
|
2014-12-15 16:43:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(PrepareStatementTest) {
|
2017-03-07 02:01:00 +01:00
|
|
|
std::string query = "PREPARE test {"
|
|
|
|
"INSERT INTO test VALUES(?);"
|
|
|
|
"SELECT ?, test FROM test WHERE c1 = ?;"
|
|
|
|
"};"
|
|
|
|
"PREPARE stmt: SELECT * FROM data WHERE c1 = ?;"
|
|
|
|
"DEALLOCATE PREPARE stmt;";
|
|
|
|
|
|
|
|
TEST_PARSE_SQL_QUERY(query, result, 3);
|
|
|
|
|
|
|
|
TEST_CAST_STMT(result, 0, kStmtPrepare, PrepareStatement, prep1);
|
|
|
|
TEST_CAST_STMT(result, 1, kStmtPrepare, PrepareStatement, prep2);
|
|
|
|
TEST_CAST_STMT(result, 2, kStmtDrop, DropStatement, drop);
|
|
|
|
|
|
|
|
// Prepare Statement #1
|
|
|
|
ASSERT_STREQ(prep1->name, "test");
|
|
|
|
ASSERT_EQ(prep1->placeholders.size(), 3);
|
|
|
|
ASSERT_EQ(prep1->query->size(), 2);
|
|
|
|
|
|
|
|
TEST_CAST_STMT(prep1->query, 0, kStmtInsert, InsertStatement, insert);
|
|
|
|
TEST_CAST_STMT(prep1->query, 1, kStmtSelect, SelectStatement, select);
|
|
|
|
|
|
|
|
ASSERT(insert->values->at(0)->isType(kExprPlaceholder));
|
|
|
|
ASSERT(select->selectList->at(0)->isType(kExprPlaceholder));
|
|
|
|
ASSERT(select->whereClause->expr2->isType(kExprPlaceholder));
|
|
|
|
|
|
|
|
// Check IDs of placeholders
|
|
|
|
ASSERT_EQ(insert->values->at(0)->ival, 0);
|
|
|
|
ASSERT_EQ(insert->values->at(0), prep1->placeholders[0]);
|
|
|
|
|
|
|
|
ASSERT_EQ(select->selectList->at(0)->ival, 1);
|
|
|
|
ASSERT_EQ(select->selectList->at(0), prep1->placeholders[1]);
|
|
|
|
|
|
|
|
ASSERT_EQ(select->whereClause->expr2->ival, 2);
|
|
|
|
ASSERT_EQ(select->whereClause->expr2, prep1->placeholders[2]);
|
|
|
|
|
|
|
|
// Prepare Statement #2
|
|
|
|
ASSERT_STREQ(prep2->name, "stmt");
|
|
|
|
ASSERT_EQ(prep2->placeholders.size(), 1);
|
|
|
|
|
|
|
|
// Deallocate Statement
|
|
|
|
ASSERT_EQ(drop->type, DropStatement::kPreparedStatement);
|
|
|
|
ASSERT_STREQ(drop->name, "stmt");
|
|
|
|
|
|
|
|
delete result;
|
2014-12-15 16:43:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ExecuteStatementTest) {
|
2017-03-07 02:01:00 +01:00
|
|
|
TEST_PARSE_SINGLE_SQL("EXECUTE test(1, 2);", kStmtExecute, ExecuteStatement, result, stmt);
|
2014-12-15 16:43:32 +01:00
|
|
|
|
2017-03-07 02:01:00 +01:00
|
|
|
ASSERT_STREQ(stmt->name, "test");
|
|
|
|
ASSERT_EQ(stmt->parameters->size(), 2);
|
2017-02-08 04:10:26 +01:00
|
|
|
|
2017-03-07 02:01:00 +01:00
|
|
|
delete result;
|
2017-02-08 02:59:07 +01:00
|
|
|
}
|
2017-02-10 21:41:34 +01:00
|
|
|
|
|
|
|
TEST_MAIN();
|