update tests to use the new Parser interface

This commit is contained in:
Pedro Flemming 2017-04-07 16:26:00 +02:00
parent e16925e7a5
commit 6b22e22162
4 changed files with 41 additions and 72 deletions

View File

@ -16,31 +16,26 @@ int main(int argc, char *argv[]) {
std::string query = argv[1]; std::string query = argv[1];
// parse a given query // parse a given query
hsql::SQLParserResult* result = hsql::SQLParser::parseSQLString(query); hsql::SQLParserResult result;
hsql::SQLParser::parseSQLString(query, &result);
// check whether the parsing was successful // check whether the parsing was successful
if (!result) {
return -1;
}
if (result->isValid()) { if (result.isValid()) {
printf("Parsed successfully!\n"); printf("Parsed successfully!\n");
printf("Number of statements: %lu\n", result->size()); printf("Number of statements: %lu\n", result.size());
for (uint i = 0; i < result->size(); ++i) { for (uint i = 0; i < result.size(); ++i) {
// Print a statement summary. // Print a statement summary.
hsql::printStatementInfo(result->getStatement(i)); hsql::printStatementInfo(result.getStatement(i));
} }
delete result;
return 0; return 0;
} else { } else {
fprintf(stderr, "Given string is not a valid SQL query.\n"); fprintf(stderr, "Given string is not a valid SQL query.\n");
fprintf(stderr, "%s (L%d:%d)\n", fprintf(stderr, "%s (L%d:%d)\n",
result->errorMsg(), result.errorMsg(),
result->errorLine(), result.errorLine(),
result->errorColumn()); result.errorColumn());
delete result;
return -1; return -1;
} }
} }

View File

@ -16,8 +16,6 @@ TEST(SelectTest) {
ASSERT_NULL(stmt->whereClause); ASSERT_NULL(stmt->whereClause);
ASSERT_NULL(stmt->groupBy); ASSERT_NULL(stmt->groupBy);
delete result;
} }
TEST(SelectExprTest) { TEST(SelectExprTest) {
@ -55,8 +53,6 @@ TEST(SelectExprTest) {
ASSERT_EQ(stmt->selectList->at(2)->exprList->at(1)->exprList->size(), 1); ASSERT_EQ(stmt->selectList->at(2)->exprList->at(1)->exprList->size(), 1);
ASSERT(stmt->selectList->at(2)->exprList->at(1)->exprList->at(0)->isType(kExprColumnRef)); ASSERT(stmt->selectList->at(2)->exprList->at(1)->exprList->at(0)->isType(kExprColumnRef));
ASSERT_STREQ(stmt->selectList->at(2)->exprList->at(1)->exprList->at(0)->getName(), "un"); ASSERT_STREQ(stmt->selectList->at(2)->exprList->at(1)->exprList->at(0)->getName(), "un");
delete result;
} }
@ -76,8 +72,6 @@ TEST(SelectHavingTest) {
ASSERT(group->having->isSimpleOp('<')); ASSERT(group->having->isSimpleOp('<'));
ASSERT(group->having->expr->isType(kExprFunctionRef)); ASSERT(group->having->expr->isType(kExprFunctionRef));
ASSERT(group->having->expr2->isType(kExprLiteralFloat)); ASSERT(group->having->expr2->isType(kExprLiteralFloat));
delete result;
} }
@ -91,8 +85,6 @@ TEST(SelectDistinctTest) {
ASSERT(stmt->selectDistinct); ASSERT(stmt->selectDistinct);
ASSERT_NULL(stmt->whereClause); ASSERT_NULL(stmt->whereClause);
delete result;
} }
TEST(SelectGroupDistinctTest) { TEST(SelectGroupDistinctTest) {
@ -107,8 +99,6 @@ TEST(SelectGroupDistinctTest) {
ASSERT_EQ(stmt->selectList->size(), 3); ASSERT_EQ(stmt->selectList->size(), 3);
ASSERT(!stmt->selectList->at(1)->distinct); ASSERT(!stmt->selectList->at(1)->distinct);
ASSERT(stmt->selectList->at(2)->distinct); ASSERT(stmt->selectList->at(2)->distinct);
delete result;
} }
TEST(OrderByTest) { TEST(OrderByTest) {
@ -128,8 +118,6 @@ TEST(OrderByTest) {
ASSERT_EQ(stmt->order->at(1)->type, kOrderDesc); ASSERT_EQ(stmt->order->at(1)->type, kOrderDesc);
ASSERT_STREQ(stmt->order->at(1)->expr->name, "city"); ASSERT_STREQ(stmt->order->at(1)->expr->name, "city");
delete result;
} }
TEST(SelectBetweenTest) { TEST(SelectBetweenTest) {
@ -154,8 +142,6 @@ TEST(SelectBetweenTest) {
ASSERT_EQ(where->exprList->at(0)->ival, 1); ASSERT_EQ(where->exprList->at(0)->ival, 1);
ASSERT(where->exprList->at(1)->isType(kExprColumnRef)); ASSERT(where->exprList->at(1)->isType(kExprColumnRef));
ASSERT_STREQ(where->exprList->at(1)->getName(), "c"); ASSERT_STREQ(where->exprList->at(1)->getName(), "c");
delete result;
} }
TEST(SelectConditionalSelectTest) { TEST(SelectConditionalSelectTest) {
@ -194,8 +180,6 @@ TEST(SelectConditionalSelectTest) {
SelectStatement* ex_select = cond2->select; SelectStatement* ex_select = cond2->select;
ASSERT_STREQ(ex_select->fromTable->getName(), "test"); ASSERT_STREQ(ex_select->fromTable->getName(), "test");
delete result;
} }
TEST(SelectCaseWhen) { TEST(SelectCaseWhen) {
@ -220,6 +204,4 @@ TEST(SelectCaseWhen) {
ASSERT(caseExpr->expr->isType(kExprOperator)); ASSERT(caseExpr->expr->isType(kExprOperator));
ASSERT(caseExpr->expr->isSimpleOp('=')); ASSERT(caseExpr->expr->isSimpleOp('='));
ASSERT_EQ(caseExpr->exprList->size(), 2); ASSERT_EQ(caseExpr->exprList->size(), 2);
delete result;
} }

View File

@ -3,20 +3,21 @@
#define TEST_PARSE_SQL_QUERY(query, result, numStatements) \ #define TEST_PARSE_SQL_QUERY(query, result, numStatements) \
const SQLParserResult* result = SQLParser::parseSQLString(query); \ SQLParserResult result; \
ASSERT(result->isValid()); \ SQLParser::parseSQLString(query, &result); \
ASSERT_EQ(result->size(), numStatements); ASSERT(result.isValid()); \
ASSERT_EQ(result.size(), numStatements);
#define TEST_PARSE_SINGLE_SQL(query, stmtType, stmtClass, result, outputVar) \ #define TEST_PARSE_SINGLE_SQL(query, stmtType, stmtClass, result, outputVar) \
TEST_PARSE_SQL_QUERY(query, result, 1); \ TEST_PARSE_SQL_QUERY(query, result, 1); \
ASSERT_EQ(result->getStatement(0)->type(), stmtType); \ ASSERT_EQ(result.getStatement(0)->type(), stmtType); \
const stmtClass* outputVar = (const stmtClass*) result->getStatement(0); const stmtClass* outputVar = (const stmtClass*) result.getStatement(0);
#define TEST_CAST_STMT(result, stmt_index, stmtType, stmtClass, outputVar) \ #define TEST_CAST_STMT(result, stmt_index, stmtType, stmtClass, outputVar) \
ASSERT_EQ(result->getStatement(stmt_index)->type(), stmtType); \ ASSERT_EQ(result.getStatement(stmt_index)->type(), stmtType); \
const stmtClass* outputVar = (const stmtClass*) result->getStatement(stmt_index); const stmtClass* outputVar = (const stmtClass*) result.getStatement(stmt_index);
#endif #endif

View File

@ -13,28 +13,30 @@ using namespace hsql;
TEST(DeleteStatementTest) { TEST(DeleteStatementTest) {
const SQLParserResult* result = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;"); SQLParserResult result;
ASSERT(result->isValid()); SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;", &result);
ASSERT_EQ(result->size(), 1);
ASSERT(result->getStatement(0)->type() == kStmtDelete);
const DeleteStatement* stmt = (const DeleteStatement*) result->getStatement(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_STREQ(stmt->tableName, "students");
ASSERT_NOTNULL(stmt->expr); ASSERT_NOTNULL(stmt->expr);
ASSERT(stmt->expr->isType(kExprOperator)); ASSERT(stmt->expr->isType(kExprOperator));
ASSERT_STREQ(stmt->expr->expr->name, "grade"); ASSERT_STREQ(stmt->expr->expr->name, "grade");
ASSERT_EQ(stmt->expr->expr2->fval, 2.0); ASSERT_EQ(stmt->expr->expr2->fval, 2.0);
delete result;
} }
TEST(CreateStatementTest) { TEST(CreateStatementTest) {
const SQLParserResult* result = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)"); SQLParserResult result;
ASSERT(result->isValid()); SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)", &result);
ASSERT_EQ(result->size(), 1);
ASSERT_EQ(result->getStatement(0)->type(), kStmtCreate);
const CreateStatement* stmt = (const CreateStatement*) result->getStatement(0); 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_EQ(stmt->type, CreateStatement::kTable);
ASSERT_STREQ(stmt->tableName, "students"); ASSERT_STREQ(stmt->tableName, "students");
ASSERT_NOTNULL(stmt->columns); ASSERT_NOTNULL(stmt->columns);
@ -47,18 +49,18 @@ TEST(CreateStatementTest) {
ASSERT_EQ(stmt->columns->at(1)->type, ColumnDefinition::INT); ASSERT_EQ(stmt->columns->at(1)->type, ColumnDefinition::INT);
ASSERT_EQ(stmt->columns->at(2)->type, ColumnDefinition::INT); ASSERT_EQ(stmt->columns->at(2)->type, ColumnDefinition::INT);
ASSERT_EQ(stmt->columns->at(3)->type, ColumnDefinition::DOUBLE); ASSERT_EQ(stmt->columns->at(3)->type, ColumnDefinition::DOUBLE);
delete result;
} }
TEST(UpdateStatementTest) { TEST(UpdateStatementTest) {
const SQLParserResult* result = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';"); SQLParserResult result;
ASSERT(result->isValid()); SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';", &result);
ASSERT_EQ(result->size(), 1);
ASSERT_EQ(result->getStatement(0)->type(), kStmtUpdate);
const UpdateStatement* stmt = (const UpdateStatement*) result->getStatement(0); 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_NOTNULL(stmt->table);
ASSERT_STREQ(stmt->table->name, "students"); ASSERT_STREQ(stmt->table->name, "students");
@ -76,9 +78,6 @@ TEST(UpdateStatementTest) {
ASSERT(stmt->where->isSimpleOp('=')); ASSERT(stmt->where->isSimpleOp('='));
ASSERT_STREQ(stmt->where->expr->name, "name"); ASSERT_STREQ(stmt->where->expr->name, "name");
ASSERT_STREQ(stmt->where->expr2->name, "Max Mustermann"); ASSERT_STREQ(stmt->where->expr2->name, "Max Mustermann");
\
delete result;
} }
@ -92,8 +91,6 @@ TEST(InsertStatementTest) {
ASSERT_EQ(stmt->values->size(), 4); ASSERT_EQ(stmt->values->size(), 4);
// TODO // TODO
delete result;
} }
@ -108,8 +105,6 @@ TEST(DropTableStatementTest) {
ASSERT_EQ(stmt->type, DropStatement::kTable); ASSERT_EQ(stmt->type, DropStatement::kTable);
ASSERT_NOTNULL(stmt->name); ASSERT_NOTNULL(stmt->name);
ASSERT_STREQ(stmt->name, "students"); ASSERT_STREQ(stmt->name, "students");
delete result;
} }
@ -132,8 +127,8 @@ TEST(PrepareStatementTest) {
ASSERT_EQ(prep1->placeholders.size(), 3); ASSERT_EQ(prep1->placeholders.size(), 3);
ASSERT_EQ(prep1->query->size(), 2); ASSERT_EQ(prep1->query->size(), 2);
TEST_CAST_STMT(prep1->query, 0, kStmtInsert, InsertStatement, insert); TEST_CAST_STMT((*prep1->query), 0, kStmtInsert, InsertStatement, insert);
TEST_CAST_STMT(prep1->query, 1, kStmtSelect, SelectStatement, select); TEST_CAST_STMT((*prep1->query), 1, kStmtSelect, SelectStatement, select);
ASSERT(insert->values->at(0)->isType(kExprPlaceholder)); ASSERT(insert->values->at(0)->isType(kExprPlaceholder));
ASSERT(select->selectList->at(0)->isType(kExprPlaceholder)); ASSERT(select->selectList->at(0)->isType(kExprPlaceholder));
@ -156,8 +151,6 @@ TEST(PrepareStatementTest) {
// Deallocate Statement // Deallocate Statement
ASSERT_EQ(drop->type, DropStatement::kPreparedStatement); ASSERT_EQ(drop->type, DropStatement::kPreparedStatement);
ASSERT_STREQ(drop->name, "stmt"); ASSERT_STREQ(drop->name, "stmt");
delete result;
} }
@ -166,8 +159,6 @@ TEST(ExecuteStatementTest) {
ASSERT_STREQ(stmt->name, "test"); ASSERT_STREQ(stmt->name, "test");
ASSERT_EQ(stmt->parameters->size(), 2); ASSERT_EQ(stmt->parameters->size(), 2);
delete result;
} }
TEST_MAIN(); TEST_MAIN();