TestSqlDatabase.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. * Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <unistd.h>
  8. #include <AK/ScopeGuard.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibCore/System.h>
  11. #include <LibSQL/BTree.h>
  12. #include <LibSQL/Database.h>
  13. #include <LibSQL/Heap.h>
  14. #include <LibSQL/Meta.h>
  15. #include <LibSQL/Row.h>
  16. #include <LibSQL/Value.h>
  17. #include <LibTest/TestCase.h>
  18. static NonnullRefPtr<SQL::SchemaDef> setup_schema(SQL::Database& db)
  19. {
  20. auto schema = SQL::SchemaDef::construct("TestSchema");
  21. MUST(db.add_schema(schema));
  22. return schema;
  23. }
  24. // FIXME: using the return value for SQL::TableDef to insert a row results in a segfault
  25. static NonnullRefPtr<SQL::TableDef> setup_table(SQL::Database& db)
  26. {
  27. auto schema = setup_schema(db);
  28. auto table = SQL::TableDef::construct(schema, "TestTable");
  29. table->append_column("TextColumn", SQL::SQLType::Text);
  30. table->append_column("IntColumn", SQL::SQLType::Integer);
  31. EXPECT_EQ(table->num_columns(), 2u);
  32. MUST(db.add_table(table));
  33. return table;
  34. }
  35. static void insert_into_table(SQL::Database& db, int count)
  36. {
  37. auto table = MUST(db.get_table("TestSchema", "TestTable"));
  38. for (int ix = 0; ix < count; ix++) {
  39. SQL::Row row(*table);
  40. StringBuilder builder;
  41. builder.appendff("Test{}", ix);
  42. row["TextColumn"] = builder.to_deprecated_string();
  43. row["IntColumn"] = ix;
  44. TRY_OR_FAIL(db.insert(row));
  45. }
  46. }
  47. static void verify_table_contents(SQL::Database& db, int expected_count)
  48. {
  49. auto table = MUST(db.get_table("TestSchema", "TestTable"));
  50. int sum = 0;
  51. int count = 0;
  52. auto rows = TRY_OR_FAIL(db.select_all(*table));
  53. for (auto& row : rows) {
  54. StringBuilder builder;
  55. builder.appendff("Test{}", row["IntColumn"].to_int<i32>().value());
  56. EXPECT_EQ(row["TextColumn"].to_deprecated_string(), builder.to_deprecated_string());
  57. count++;
  58. sum += row["IntColumn"].to_int<i32>().value();
  59. }
  60. EXPECT_EQ(count, expected_count);
  61. EXPECT_EQ(sum, (expected_count * (expected_count - 1)) / 2);
  62. }
  63. static void commit(SQL::Database& db)
  64. {
  65. TRY_OR_FAIL(db.commit());
  66. }
  67. static void insert_and_verify(int count)
  68. {
  69. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  70. {
  71. auto db = SQL::Database::construct("/tmp/test.db");
  72. MUST(db->open());
  73. (void)setup_table(db);
  74. commit(db);
  75. }
  76. {
  77. auto db = SQL::Database::construct("/tmp/test.db");
  78. MUST(db->open());
  79. insert_into_table(db, count);
  80. commit(db);
  81. }
  82. {
  83. auto db = SQL::Database::construct("/tmp/test.db");
  84. MUST(db->open());
  85. verify_table_contents(db, count);
  86. }
  87. }
  88. TEST_CASE(create_heap)
  89. {
  90. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  91. auto heap = MUST(SQL::Heap::create("/tmp/test.db"));
  92. TRY_OR_FAIL(heap->open());
  93. EXPECT_EQ(heap->version(), SQL::Heap::VERSION);
  94. }
  95. TEST_CASE(create_from_dev_random)
  96. {
  97. auto heap = MUST(SQL::Heap::create("/dev/random"));
  98. auto should_be_error = heap->open();
  99. EXPECT(should_be_error.is_error());
  100. }
  101. TEST_CASE(create_from_unreadable_file)
  102. {
  103. auto heap = MUST(SQL::Heap::create("/etc/shadow"));
  104. auto should_be_error = heap->open();
  105. EXPECT(should_be_error.is_error());
  106. }
  107. TEST_CASE(create_in_non_existing_dir)
  108. {
  109. auto heap = MUST(SQL::Heap::create("/tmp/bogus/test.db"));
  110. auto should_be_error = heap->open();
  111. EXPECT(should_be_error.is_error());
  112. }
  113. TEST_CASE(create_database)
  114. {
  115. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  116. auto db = SQL::Database::construct("/tmp/test.db");
  117. MUST(db->open());
  118. commit(db);
  119. }
  120. TEST_CASE(add_schema_to_database)
  121. {
  122. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  123. auto db = SQL::Database::construct("/tmp/test.db");
  124. MUST(db->open());
  125. (void)setup_schema(db);
  126. commit(db);
  127. }
  128. TEST_CASE(get_schema_from_database)
  129. {
  130. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  131. {
  132. auto db = SQL::Database::construct("/tmp/test.db");
  133. MUST(db->open());
  134. (void)setup_schema(db);
  135. commit(db);
  136. }
  137. {
  138. auto db = SQL::Database::construct("/tmp/test.db");
  139. MUST(db->open());
  140. auto schema = MUST(db->get_schema("TestSchema"));
  141. }
  142. }
  143. TEST_CASE(add_table_to_database)
  144. {
  145. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  146. auto db = SQL::Database::construct("/tmp/test.db");
  147. MUST(db->open());
  148. (void)setup_table(db);
  149. commit(db);
  150. }
  151. TEST_CASE(get_table_from_database)
  152. {
  153. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  154. {
  155. auto db = SQL::Database::construct("/tmp/test.db");
  156. MUST(db->open());
  157. (void)setup_table(db);
  158. commit(db);
  159. }
  160. {
  161. auto db = SQL::Database::construct("/tmp/test.db");
  162. MUST(db->open());
  163. auto table = MUST(db->get_table("TestSchema", "TestTable"));
  164. EXPECT_EQ(table->name(), "TestTable");
  165. EXPECT_EQ(table->num_columns(), 2u);
  166. }
  167. }
  168. TEST_CASE(insert_one_into_and_select_from_table)
  169. {
  170. insert_and_verify(1);
  171. }
  172. TEST_CASE(insert_two_into_table)
  173. {
  174. insert_and_verify(2);
  175. }
  176. TEST_CASE(insert_10_into_table)
  177. {
  178. insert_and_verify(10);
  179. }
  180. TEST_CASE(insert_100_into_table)
  181. {
  182. insert_and_verify(100);
  183. }
  184. TEST_CASE(reuse_row_storage)
  185. {
  186. ScopeGuard guard([]() { unlink("/tmp/test.db"); });
  187. auto db = SQL::Database::construct("/tmp/test.db");
  188. MUST(db->open());
  189. (void)setup_table(db);
  190. auto table = MUST(db->get_table("TestSchema", "TestTable"));
  191. // Insert row
  192. SQL::Row row(*table);
  193. row["TextColumn"] = "text value";
  194. row["IntColumn"] = 12345;
  195. TRY_OR_FAIL(db->insert(row));
  196. TRY_OR_FAIL(db->commit());
  197. auto original_size_in_bytes = MUST(db->file_size_in_bytes());
  198. // Remove row
  199. TRY_OR_FAIL(db->remove(row));
  200. TRY_OR_FAIL(db->commit());
  201. auto size_in_bytes_after_removal = MUST(db->file_size_in_bytes());
  202. EXPECT(size_in_bytes_after_removal <= original_size_in_bytes);
  203. // Insert same row again
  204. TRY_OR_FAIL(db->insert(row));
  205. TRY_OR_FAIL(db->commit());
  206. auto size_in_bytes_after_reinsertion = MUST(db->file_size_in_bytes());
  207. EXPECT(size_in_bytes_after_reinsertion <= original_size_in_bytes);
  208. }