diff options
author | Jan de Visser <jan@de-visser.net> | 2021-10-23 10:47:12 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-10-25 12:59:42 +0200 |
commit | 7496f1762018c7db2144a6474f008ee908a06333 (patch) | |
tree | e609d260227783db9d9e385ad4b512aeb20563c2 /Tests/LibSQL/TestSqlStatementExecution.cpp | |
parent | 9022cf99ffdff94f8a20cb1ed65f970449179410 (diff) | |
download | serenity-7496f1762018c7db2144a6474f008ee908a06333.zip |
LibSQL Tests: Add tests for `SELECT ... WHERE ...`
Diffstat (limited to 'Tests/LibSQL/TestSqlStatementExecution.cpp')
-rw-r--r-- | Tests/LibSQL/TestSqlStatementExecution.cpp | 67 |
1 files changed, 65 insertions, 2 deletions
diff --git a/Tests/LibSQL/TestSqlStatementExecution.cpp b/Tests/LibSQL/TestSqlStatementExecution.cpp index f01a7e2da0..e64ea6b32b 100644 --- a/Tests/LibSQL/TestSqlStatementExecution.cpp +++ b/Tests/LibSQL/TestSqlStatementExecution.cpp @@ -23,11 +23,12 @@ RefPtr<SQL::SQLResult> execute(NonnullRefPtr<SQL::Database> database, String con auto parser = SQL::AST::Parser(SQL::AST::Lexer(sql)); auto statement = parser.next_statement(); EXPECT(!parser.has_errors()); - if (parser.has_errors()) { + if (parser.has_errors()) outln("{}", parser.errors()[0].to_string()); - } SQL::AST::ExecutionContext context { database }; auto result = statement->execute(context); + if (result->error().code != SQL::SQLErrorCode::NoError) + outln("{}", result->error().to_string()); return result; } @@ -147,4 +148,66 @@ TEST_CASE(select_from_table) EXPECT_EQ(result->results().size(), 5u); } +TEST_CASE(select_with_column_names) +{ + ScopeGuard guard([]() { unlink(db_name); }); + auto database = SQL::Database::construct(db_name); + create_table(database); + auto result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_1', 42 ), ( 'Test_2', 43 );"); + EXPECT(result->error().code == SQL::SQLErrorCode::NoError); + EXPECT(result->inserted() == 2); + result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_3', 44 ), ( 'Test_4', 45 );"); + EXPECT(result->error().code == SQL::SQLErrorCode::NoError); + EXPECT(result->inserted() == 2); + result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_5', 46 );"); + EXPECT(result->error().code == SQL::SQLErrorCode::NoError); + EXPECT(result->inserted() == 1); + result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable;"); + EXPECT(result->error().code == SQL::SQLErrorCode::NoError); + EXPECT(result->has_results()); + EXPECT_EQ(result->results().size(), 5u); + EXPECT_EQ(result->results()[0].size(), 1u); +} + +TEST_CASE(select_with_nonexisting_column_name) +{ + ScopeGuard guard([]() { unlink(db_name); }); + auto database = SQL::Database::construct(db_name); + create_table(database); + auto result = execute(database, + "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES " + "( 'Test_1', 42 ), " + "( 'Test_2', 43 ), " + "( 'Test_3', 44 ), " + "( 'Test_4', 45 ), " + "( 'Test_5', 46 );"); + EXPECT(result->error().code == SQL::SQLErrorCode::NoError); + EXPECT(result->inserted() == 5); + result = execute(database, "SELECT Bogus FROM TestSchema.TestTable;"); + EXPECT(result->error().code == SQL::SQLErrorCode::ColumnDoesNotExist); +} + +TEST_CASE(select_with_where) +{ + ScopeGuard guard([]() { unlink(db_name); }); + auto database = SQL::Database::construct(db_name); + create_table(database); + auto result = execute(database, + "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES " + "( 'Test_1', 42 ), " + "( 'Test_2', 43 ), " + "( 'Test_3', 44 ), " + "( 'Test_4', 45 ), " + "( 'Test_5', 46 );"); + EXPECT(result->error().code == SQL::SQLErrorCode::NoError); + EXPECT(result->inserted() == 5); + result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable WHERE IntColumn > 44;"); + EXPECT(result->error().code == SQL::SQLErrorCode::NoError); + EXPECT(result->has_results()); + EXPECT_EQ(result->results().size(), 2u); + for (auto& row : result->results()) { + EXPECT(row[1].to_int().value() > 44); + } +} + } |