TestSqlStatementExecution.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <unistd.h>
  7. #include <AK/ScopeGuard.h>
  8. #include <LibSQL/AST/Parser.h>
  9. #include <LibSQL/Database.h>
  10. #include <LibSQL/Row.h>
  11. #include <LibSQL/SQLResult.h>
  12. #include <LibSQL/Value.h>
  13. #include <LibTest/TestCase.h>
  14. namespace {
  15. constexpr const char* db_name = "/tmp/test.db";
  16. RefPtr<SQL::SQLResult> execute(NonnullRefPtr<SQL::Database> database, String const& sql)
  17. {
  18. auto parser = SQL::AST::Parser(SQL::AST::Lexer(sql));
  19. auto statement = parser.next_statement();
  20. EXPECT(!parser.has_errors());
  21. if (parser.has_errors()) {
  22. outln(parser.errors()[0].to_string());
  23. }
  24. SQL::AST::ExecutionContext context { database };
  25. auto result = statement->execute(context);
  26. EXPECT(result->error().code == SQL::SQLErrorCode::NoError);
  27. return result;
  28. }
  29. void create_schema(NonnullRefPtr<SQL::Database> database)
  30. {
  31. auto result = execute(database, "CREATE SCHEMA TestSchema;");
  32. EXPECT(result->inserted() == 1);
  33. }
  34. void create_table(NonnullRefPtr<SQL::Database> database)
  35. {
  36. create_schema(database);
  37. auto result = execute(database, "CREATE TABLE TestSchema.TestTable ( TextColumn text, IntColumn integer );");
  38. EXPECT(result->inserted() == 1);
  39. }
  40. TEST_CASE(create_schema)
  41. {
  42. ScopeGuard guard([]() { unlink(db_name); });
  43. auto database = SQL::Database::construct(db_name);
  44. create_schema(database);
  45. auto schema = database->get_schema("TESTSCHEMA");
  46. EXPECT(schema);
  47. }
  48. TEST_CASE(create_table)
  49. {
  50. ScopeGuard guard([]() { unlink(db_name); });
  51. auto database = SQL::Database::construct(db_name);
  52. create_table(database);
  53. auto table = database->get_table("TESTSCHEMA", "TESTTABLE");
  54. EXPECT(table);
  55. }
  56. TEST_CASE(insert_into_table)
  57. {
  58. ScopeGuard guard([]() { unlink(db_name); });
  59. auto database = SQL::Database::construct(db_name);
  60. create_table(database);
  61. auto result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test', 42 );");
  62. EXPECT(result->inserted() == 1);
  63. auto table = database->get_table("TESTSCHEMA", "TESTTABLE");
  64. int count = 0;
  65. for (auto& row : database->select_all(*table)) {
  66. EXPECT_EQ(row["TEXTCOLUMN"].to_string(), "Test");
  67. EXPECT_EQ(row["INTCOLUMN"].to_int().value(), 42);
  68. count++;
  69. }
  70. EXPECT_EQ(count, 1);
  71. }
  72. TEST_CASE(select_from_table)
  73. {
  74. ScopeGuard guard([]() { unlink(db_name); });
  75. auto database = SQL::Database::construct(db_name);
  76. create_table(database);
  77. auto result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_1', 42 ), ( 'Test_2', 43 );");
  78. EXPECT(result->inserted() == 2);
  79. result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_3', 44 ), ( 'Test_4', 45 );");
  80. EXPECT(result->inserted() == 2);
  81. result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_5', 46 );");
  82. EXPECT(result->inserted() == 1);
  83. result = execute(database, "SELECT * FROM TestSchema.TestTable;");
  84. EXPECT(result->has_results());
  85. EXPECT_EQ(result->results().size(), 5u);
  86. }
  87. }