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];
// 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
if (!result) {
return -1;
}
if (result->isValid()) {
if (result.isValid()) {
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.
hsql::printStatementInfo(result->getStatement(i));
hsql::printStatementInfo(result.getStatement(i));
}
delete result;
return 0;
} else {
fprintf(stderr, "Given string is not a valid SQL query.\n");
fprintf(stderr, "%s (L%d:%d)\n",
result->errorMsg(),
result->errorLine(),
result->errorColumn());
delete result;
result.errorMsg(),
result.errorLine(),
result.errorColumn());
return -1;
}
}

View File

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

View File

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

View File

@ -13,28 +13,30 @@ using namespace hsql;
TEST(DeleteStatementTest) {
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);
SQLParserResult result;
SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;", &result);
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_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;
}
TEST(CreateStatementTest) {
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);
SQLParserResult result;
SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)", &result);
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_STREQ(stmt->tableName, "students");
ASSERT_NOTNULL(stmt->columns);
@ -47,18 +49,18 @@ TEST(CreateStatementTest) {
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;
}
TEST(UpdateStatementTest) {
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);
SQLParserResult result;
SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';", &result);
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_STREQ(stmt->table->name, "students");
@ -76,9 +78,6 @@ TEST(UpdateStatementTest) {
ASSERT(stmt->where->isSimpleOp('='));
ASSERT_STREQ(stmt->where->expr->name, "name");
ASSERT_STREQ(stmt->where->expr2->name, "Max Mustermann");
\
delete result;
}
@ -92,8 +91,6 @@ TEST(InsertStatementTest) {
ASSERT_EQ(stmt->values->size(), 4);
// TODO
delete result;
}
@ -108,8 +105,6 @@ TEST(DropTableStatementTest) {
ASSERT_EQ(stmt->type, DropStatement::kTable);
ASSERT_NOTNULL(stmt->name);
ASSERT_STREQ(stmt->name, "students");
delete result;
}
@ -132,8 +127,8 @@ TEST(PrepareStatementTest) {
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);
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));
@ -156,8 +151,6 @@ TEST(PrepareStatementTest) {
// Deallocate Statement
ASSERT_EQ(drop->type, DropStatement::kPreparedStatement);
ASSERT_STREQ(drop->name, "stmt");
delete result;
}
@ -166,8 +159,6 @@ TEST(ExecuteStatementTest) {
ASSERT_STREQ(stmt->name, "test");
ASSERT_EQ(stmt->parameters->size(), 2);
delete result;
}
TEST_MAIN();
TEST_MAIN();