TestSqlDatabase.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/BTree.h>
  9. #include <LibSQL/Database.h>
  10. #include <LibSQL/Heap.h>
  11. #include <LibSQL/Meta.h>
  12. #include <LibSQL/Row.h>
  13. #include <LibSQL/Value.h>
  14. #include <LibTest/TestCase.h>
  15. NonnullRefPtr<SQL::SchemaDef> setup_schema(SQL::Database&);
  16. NonnullRefPtr<SQL::SchemaDef> setup_table(SQL::Database&);
  17. void insert_into_table(SQL::Database&, int);
  18. void verify_table_contents(SQL::Database&, int);
  19. void insert_and_verify(int);
  20. NonnullRefPtr<SQL::SchemaDef> setup_schema(SQL::Database& db)
  21. {
  22. auto schema = SQL::SchemaDef::construct("TestSchema");
  23. db.add_schema(schema);
  24. return schema;
  25. }
  26. NonnullRefPtr<SQL::SchemaDef> setup_table(SQL::Database& db)
  27. {
  28. auto schema = setup_schema(db);
  29. auto table = SQL::TableDef::construct(schema, "TestTable");
  30. db.add_table(table);
  31. table->append_column("TextColumn", SQL::SQLType::Text);
  32. table->append_column("IntColumn", SQL::SQLType::Integer);
  33. EXPECT_EQ(table->num_columns(), 2u);
  34. db.add_table(table);
  35. return table;
  36. }
  37. void insert_into_table(SQL::Database& db, int count)
  38. {
  39. auto table = db.get_table("TestSchema", "TestTable");
  40. EXPECT(table);
  41. for (int ix = 0; ix < count; ix++) {
  42. SQL::Row row(*table);
  43. StringBuilder builder;
  44. builder.appendff("Test{}", ix);
  45. row["TextColumn"] = builder.build();
  46. row["IntColumn"] = ix;
  47. EXPECT(db.insert(row));
  48. }
  49. }
  50. void verify_table_contents(SQL::Database& db, int expected_count)
  51. {
  52. auto table = db.get_table("TestSchema", "TestTable");
  53. EXPECT(table);
  54. int sum = 0;
  55. int count = 0;
  56. for (auto& row : db.select_all(*table)) {
  57. StringBuilder builder;
  58. builder.appendff("Test{}", row["IntColumn"].to_int().value());
  59. EXPECT_EQ(row["TextColumn"].to_string(), builder.build());
  60. count++;
  61. sum += row["IntColumn"].to_int().value();
  62. }
  63. EXPECT_EQ(count, expected_count);
  64. EXPECT_EQ(sum, (expected_count * (expected_count - 1)) / 2);
  65. }
  66. void insert_and_verify(int count)
  67. {
  68. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  69. {
  70. auto db = SQL::Database::construct("/tmp/test.db");
  71. setup_table(db);
  72. db->commit();
  73. }
  74. {
  75. auto db = SQL::Database::construct("/tmp/test.db");
  76. insert_into_table(db, count);
  77. db->commit();
  78. }
  79. {
  80. auto db = SQL::Database::construct("/tmp/test.db");
  81. verify_table_contents(db, count);
  82. }
  83. }
  84. TEST_CASE(create_heap)
  85. {
  86. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  87. auto heap = SQL::Heap::construct("/tmp/test.db");
  88. EXPECT_EQ(heap->version(), 0x00000001u);
  89. }
  90. TEST_CASE(create_database)
  91. {
  92. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  93. auto db = SQL::Database::construct("/tmp/test.db");
  94. db->commit();
  95. }
  96. TEST_CASE(add_schema_to_database)
  97. {
  98. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  99. auto db = SQL::Database::construct("/tmp/test.db");
  100. setup_schema(db);
  101. db->commit();
  102. }
  103. TEST_CASE(get_schema_from_database)
  104. {
  105. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  106. {
  107. auto db = SQL::Database::construct("/tmp/test.db");
  108. setup_schema(db);
  109. db->commit();
  110. }
  111. {
  112. auto db = SQL::Database::construct("/tmp/test.db");
  113. auto schema = db->get_schema("TestSchema");
  114. EXPECT(schema);
  115. }
  116. }
  117. TEST_CASE(add_table_to_database)
  118. {
  119. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  120. auto db = SQL::Database::construct("/tmp/test.db");
  121. setup_table(db);
  122. db->commit();
  123. }
  124. TEST_CASE(get_table_from_database)
  125. {
  126. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  127. {
  128. auto db = SQL::Database::construct("/tmp/test.db");
  129. setup_table(db);
  130. db->commit();
  131. }
  132. {
  133. auto db = SQL::Database::construct("/tmp/test.db");
  134. auto table = db->get_table("TestSchema", "TestTable");
  135. EXPECT(table);
  136. EXPECT_EQ(table->name(), "TestTable");
  137. EXPECT_EQ(table->num_columns(), 2u);
  138. }
  139. }
  140. TEST_CASE(insert_one_into_and_select_from_table)
  141. {
  142. insert_and_verify(1);
  143. }
  144. TEST_CASE(insert_two_into_table)
  145. {
  146. insert_and_verify(2);
  147. }
  148. TEST_CASE(insert_10_into_table)
  149. {
  150. insert_and_verify(10);
  151. }
  152. TEST_CASE(insert_100_into_table)
  153. {
  154. insert_and_verify(100);
  155. }