2014-10-09 01:30:22 +02:00
|
|
|
/*
|
|
|
|
* sql_tests.cpp
|
|
|
|
*/
|
|
|
|
|
2014-12-03 16:32:56 +01:00
|
|
|
#include "tests/test.h"
|
2014-12-18 12:11:26 +01:00
|
|
|
#include "tests/helper.h"
|
2014-10-16 15:54:51 +02:00
|
|
|
#include "SQLParser.h"
|
2014-12-02 01:27:02 +01:00
|
|
|
#include "sqlhelper.h"
|
|
|
|
|
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) {
|
2014-12-03 17:43:02 +01:00
|
|
|
SQLStatementList* stmt_list = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;");
|
2014-11-26 16:20:55 +01:00
|
|
|
ASSERT(stmt_list->isValid);
|
2014-11-26 18:20:10 +01:00
|
|
|
ASSERT_EQ(stmt_list->size(), 1);
|
2014-12-03 17:43:02 +01:00
|
|
|
ASSERT(stmt_list->at(0)->type() == kStmtDelete);
|
2014-10-09 01:30:22 +02:00
|
|
|
|
2014-11-26 16:20:55 +01:00
|
|
|
DeleteStatement* stmt = (DeleteStatement*) stmt_list->at(0);
|
|
|
|
ASSERT_STREQ(stmt->table_name, "students");
|
2014-11-26 18:20:10 +01:00
|
|
|
ASSERT_NOTNULL(stmt->expr);
|
2014-11-26 16:20:55 +01:00
|
|
|
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) {
|
2014-12-03 17:43:02 +01:00
|
|
|
SQLStatementList* stmt_list = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)");
|
2014-11-26 18:20:10 +01:00
|
|
|
ASSERT(stmt_list->isValid);
|
|
|
|
ASSERT_EQ(stmt_list->size(), 1);
|
2014-12-03 17:43:02 +01:00
|
|
|
ASSERT_EQ(stmt_list->at(0)->type(), kStmtCreate);
|
2014-11-26 18:20:10 +01:00
|
|
|
|
|
|
|
CreateStatement* stmt = (CreateStatement*) stmt_list->at(0);
|
2014-12-03 17:43:02 +01:00
|
|
|
ASSERT_EQ(stmt->type, CreateStatement::kTable);
|
2014-11-26 18:20:10 +01:00
|
|
|
ASSERT_STREQ(stmt->table_name, "students");
|
|
|
|
ASSERT_NOTNULL(stmt->columns);
|
|
|
|
ASSERT_EQ(stmt->columns->size(), 4);
|
|
|
|
ASSERT_STREQ(stmt->columns->at(0)->name, "name");
|
2014-12-03 16:32:56 +01:00
|
|
|
ASSERT_STREQ(stmt->columns->at(1)->name, "student_number");
|
|
|
|
ASSERT_STREQ(stmt->columns->at(2)->name, "city");
|
2014-11-26 18:20:10 +01:00
|
|
|
ASSERT_STREQ(stmt->columns->at(3)->name, "grade");
|
2014-12-03 16:32:56 +01:00
|
|
|
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);
|
2014-11-26 18:20:10 +01:00
|
|
|
ASSERT_EQ(stmt->columns->at(3)->type, ColumnDefinition::DOUBLE);
|
2014-12-02 01:27:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-15 16:43:32 +01:00
|
|
|
TEST(UpdateStatementTest) {
|
2014-12-03 17:43:02 +01:00
|
|
|
SQLStatementList* stmt_list = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';");
|
2014-12-02 01:27:02 +01:00
|
|
|
ASSERT(stmt_list->isValid);
|
|
|
|
ASSERT_EQ(stmt_list->size(), 1);
|
2014-12-03 17:43:02 +01:00
|
|
|
ASSERT_EQ(stmt_list->at(0)->type(), kStmtUpdate);
|
2014-12-02 01:27:02 +01:00
|
|
|
|
|
|
|
UpdateStatement* stmt = (UpdateStatement*) stmt_list->at(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");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-15 16:43:32 +01:00
|
|
|
TEST(InsertStatementTest) {
|
2014-12-03 17:43:02 +01:00
|
|
|
SQLStatementList* stmt_list = SQLParser::parseSQLString("INSERT INTO students VALUES ('Max Mustermann', 12345, 'Musterhausen', 2.0)");
|
2014-12-02 01:27:02 +01:00
|
|
|
ASSERT(stmt_list->isValid);
|
|
|
|
ASSERT_EQ(stmt_list->size(), 1);
|
2014-12-03 17:43:02 +01:00
|
|
|
ASSERT_EQ(stmt_list->at(0)->type(), kStmtInsert);
|
2014-12-02 01:27:02 +01:00
|
|
|
|
|
|
|
// TODO
|
2014-12-03 16:53:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-15 16:43:32 +01:00
|
|
|
TEST(DropTableStatementTest) {
|
2014-12-03 17:43:02 +01:00
|
|
|
SQLStatementList* stmt_list = SQLParser::parseSQLString("DROP TABLE students");
|
2014-12-03 16:53:49 +01:00
|
|
|
ASSERT(stmt_list->isValid);
|
|
|
|
ASSERT_EQ(stmt_list->size(), 1);
|
2014-12-03 17:43:02 +01:00
|
|
|
ASSERT_EQ(stmt_list->at(0)->type(), kStmtDrop);
|
2014-12-03 16:53:49 +01:00
|
|
|
|
|
|
|
DropStatement* stmt = (DropStatement*) stmt_list->at(0);
|
2014-12-03 17:43:02 +01:00
|
|
|
ASSERT_EQ(stmt->type, DropStatement::kTable);
|
2014-12-03 16:53:49 +01:00
|
|
|
ASSERT_NOTNULL(stmt->name);
|
|
|
|
ASSERT_STREQ(stmt->name, "students");
|
2014-12-15 16:43:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(PrepareStatementTest) {
|
2015-01-06 15:29:18 +01:00
|
|
|
TEST_PARSE_SINGLE_SQL("PREPARE test:"
|
|
|
|
"INSERT INTO test VALUES(?);"
|
|
|
|
"SELECT ?, test FROM test WHERE c1 = ?;"
|
|
|
|
"", kStmtPrepare, PrepareStatement, prep_stmt);
|
2014-12-15 16:43:32 +01:00
|
|
|
|
|
|
|
ASSERT_STREQ(prep_stmt->name, "test");
|
2015-01-06 15:29:18 +01:00
|
|
|
ASSERT_EQ(prep_stmt->placeholders.size(), 3);
|
2014-12-15 16:43:32 +01:00
|
|
|
|
2015-01-06 15:29:18 +01:00
|
|
|
|
|
|
|
ASSERT_EQ(prep_stmt->query->size(), 2);
|
|
|
|
ASSERT_EQ(prep_stmt->query->at(0)->type(), kStmtInsert);
|
|
|
|
ASSERT_EQ(prep_stmt->query->at(1)->type(), kStmtSelect);
|
|
|
|
|
|
|
|
|
|
|
|
InsertStatement* insert = (InsertStatement*) prep_stmt->query->at(0);
|
|
|
|
SelectStatement* select = (SelectStatement*) prep_stmt->query->at(1);
|
|
|
|
|
|
|
|
ASSERT(insert->values->at(0)->isType(kExprPlaceholder));
|
|
|
|
ASSERT(select->select_list->at(0)->isType(kExprPlaceholder));
|
|
|
|
ASSERT(select->where_clause->expr2->isType(kExprPlaceholder));
|
|
|
|
|
|
|
|
// Check IDs of placeholders
|
|
|
|
ASSERT_EQ(insert->values->at(0)->ival, 0);
|
|
|
|
ASSERT_EQ(insert->values->at(0), prep_stmt->placeholders[0]);
|
|
|
|
|
|
|
|
ASSERT_EQ(select->select_list->at(0)->ival, 1);
|
|
|
|
ASSERT_EQ(select->select_list->at(0), prep_stmt->placeholders[1]);
|
|
|
|
|
|
|
|
ASSERT_EQ(select->where_clause->expr2->ival, 2);
|
|
|
|
ASSERT_EQ(select->where_clause->expr2, prep_stmt->placeholders[2]);
|
2014-12-15 16:43:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ExecuteStatementTest) {
|
2014-12-18 12:11:26 +01:00
|
|
|
TEST_PARSE_SINGLE_SQL("EXECUTE test(1, 2);", kStmtExecute, ExecuteStatement, stmt);
|
2014-12-15 16:43:32 +01:00
|
|
|
|
|
|
|
ASSERT_STREQ(stmt->name, "test");
|
|
|
|
ASSERT_EQ(stmt->parameters->size(), 2);
|
2014-11-26 18:20:10 +01:00
|
|
|
}
|