HyriseSQLParser/src/sql_tests.cpp

113 lines
3.2 KiB
C++
Raw Normal View History

2014-10-09 01:30:22 +02:00
/*
* sql_tests.cpp
*/
#include "SQLParser.h"
2014-10-09 01:30:22 +02:00
#include <stdio.h>
#include <string>
#include <cassert>
#define STREQUALS(str1, str2) std::string(str1).compare(std::string(str2)) == 0
#define ASSERT(cond) if (!(cond)) { fprintf(stderr, "failed! Assertion (" #cond ")\n"); return; }
#define ASSERT_STR(STR1, STR2) ASSERT(STREQUALS(STR1, STR2));
void SelectTest1() {
printf("Test: SelectTest1... ");
fflush(stdout);
const char* sql = "SELECT age, name, address from table WHERE age > 12.5;";
2014-10-09 04:46:25 +02:00
Statement* sqlStatement = SQLParser::parseSQL(sql);
ASSERT(sqlStatement != NULL);
ASSERT(sqlStatement->_type == eSelect);
2014-10-09 01:30:22 +02:00
2014-10-09 04:46:25 +02:00
SelectStatement* stmt = (SelectStatement*) sqlStatement;
2014-10-09 01:30:22 +02:00
2014-10-09 04:46:25 +02:00
ASSERT(stmt->_select_list->size() == 3);
ASSERT_STR(stmt->_select_list->at(0)->name, "age");
ASSERT_STR(stmt->_select_list->at(1)->name, "name");
ASSERT_STR(stmt->_select_list->at(2)->name, "address");
2014-10-09 01:30:22 +02:00
2014-10-09 04:46:25 +02:00
ASSERT(stmt->_from_table != NULL);
ASSERT(stmt->_from_table->_type == eTableName);
ASSERT_STR(stmt->_from_table->_table_names->at(0), "table");
// WHERE
ASSERT(stmt->_where_clause != NULL);
ASSERT(stmt->_where_clause->expr->type == eExprColumnRef);
ASSERT_STR(stmt->_where_clause->expr->name, "age");
ASSERT_STR(stmt->_where_clause->name, ">");
ASSERT(stmt->_where_clause->expr2->type == eExprLiteralFloat);
ASSERT(stmt->_where_clause->expr2->float_literal == 12.5);
2014-10-09 01:30:22 +02:00
printf("passed!\n");
}
void SelectTest2() {
printf("Test: SelectTest2... ");
fflush(stdout);
const char* sql = "SELECT age, name, address FROM (SELECT age FROM table, table2);";
Statement* stmt = SQLParser::parseSQL(sql);
ASSERT(stmt != NULL);
ASSERT(stmt->_type == eSelect);
SelectStatement* select = (SelectStatement*) stmt;
ASSERT(select->_select_list->size() == 3);
ASSERT_STR(select->_select_list->at(0)->name, "age");
ASSERT_STR(select->_select_list->at(1)->name, "name");
ASSERT_STR(select->_select_list->at(2)->name, "address");
ASSERT(select->_from_table != NULL);
ASSERT(select->_from_table->_type == eTableSelect);
ASSERT(select->_from_table->_stmt != NULL);
ASSERT(select->_from_table->_stmt->_select_list->size() == 1);
ASSERT_STR(select->_from_table->_stmt->_from_table->_table_names->at(0), "table");
ASSERT_STR(select->_from_table->_stmt->_from_table->_table_names->at(1), "table2");
printf("passed!\n");
}
void SelectTest3() {
printf("Test: SelectTest3... ");
fflush(stdout);
const char* sql = "SELECT name, AVG(age) FROM table GROUP BY name";
Statement* stmt = SQLParser::parseSQL(sql);
ASSERT(stmt != NULL);
ASSERT(stmt->_type == eSelect);
SelectStatement* select = (SelectStatement*) stmt;
ASSERT(select->_select_list->size() == 2);
ASSERT(select->_select_list->at(0)->type == eExprColumnRef);
ASSERT(select->_select_list->at(1)->type == eExprFunctionRef);
2014-10-09 01:30:22 +02:00
ASSERT_STR("name", select->_select_list->at(0)->name);
ASSERT(select->_group_by != NULL);
ASSERT(select->_group_by->size() == 1);
ASSERT_STR("name", select->_group_by->at(0)->name);
printf("passed!\n");
}
int main(int argc, char *argv[]) {
printf("\n######################################\n");
printf("## Running all tests...\n\n");
SelectTest1();
SelectTest2();
SelectTest3();
printf("\n## Finished running all tests...\n");
printf("######################################\n");
return 0;
}