Kaynağa Gözat

LibSQL: Test `SQL::Heap` separately

Move the long storage test from TestSqlStatementExecution into a new
test unit called TestSqlHeap. Split it up into a flushed and non-flushed
variant so we test the write-ahead log as well.
Jelle Raaijmakers 2 yıl önce
ebeveyn
işleme
2d2911e1a3

+ 1 - 0
Tests/LibSQL/CMakeLists.txt

@@ -3,6 +3,7 @@ set(TEST_SOURCES
     TestSqlDatabase.cpp
     TestSqlExpressionParser.cpp
     TestSqlHashIndex.cpp
+    TestSqlHeap.cpp
     TestSqlStatementExecution.cpp
     TestSqlStatementParser.cpp
     TestSqlValueAndTuple.cpp

+ 55 - 0
Tests/LibSQL/TestSqlHeap.cpp

@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/ScopeGuard.h>
+#include <AK/StringBuilder.h>
+#include <LibCore/System.h>
+#include <LibSQL/Heap.h>
+#include <LibTest/TestCase.h>
+
+static constexpr auto db_path = "/tmp/test.db"sv;
+
+static NonnullRefPtr<SQL::Heap> create_heap()
+{
+    auto heap = MUST(SQL::Heap::try_create(db_path));
+    MUST(heap->open());
+    return heap;
+}
+
+TEST_CASE(heap_write_large_storage_without_flush)
+{
+    ScopeGuard guard([]() { MUST(Core::System::unlink(db_path)); });
+    auto heap = create_heap();
+    auto storage_block_id = heap->request_new_block_index();
+
+    // Write large storage spanning multiple blocks
+    StringBuilder builder;
+    MUST(builder.try_append_repeated('x', SQL::Block::DATA_SIZE * 4));
+    auto long_string = builder.string_view();
+    TRY_OR_FAIL(heap->write_storage(storage_block_id, long_string.bytes()));
+
+    // Read back
+    auto stored_long_string = TRY_OR_FAIL(heap->read_storage(storage_block_id));
+    EXPECT_EQ(long_string.bytes(), stored_long_string.bytes());
+}
+
+TEST_CASE(heap_write_large_storage_with_flush)
+{
+    ScopeGuard guard([]() { MUST(Core::System::unlink(db_path)); });
+    auto heap = create_heap();
+    auto storage_block_id = heap->request_new_block_index();
+
+    // Write large storage spanning multiple blocks
+    StringBuilder builder;
+    MUST(builder.try_append_repeated('x', SQL::Block::DATA_SIZE * 4));
+    auto long_string = builder.string_view();
+    TRY_OR_FAIL(heap->write_storage(storage_block_id, long_string.bytes()));
+    MUST(heap->flush());
+
+    // Read back
+    auto stored_long_string = TRY_OR_FAIL(heap->read_storage(storage_block_id));
+    EXPECT_EQ(long_string.bytes(), stored_long_string.bytes());
+}

+ 0 - 18
Tests/LibSQL/TestSqlStatementExecution.cpp

@@ -230,24 +230,6 @@ TEST_CASE(insert_with_placeholders)
     }
 }
 
-TEST_CASE(insert_and_retrieve_long_text_value)
-{
-    ScopeGuard guard([]() { unlink(db_name); });
-    auto database = SQL::Database::construct(db_name);
-    MUST(database->open());
-    create_table(database);
-
-    StringBuilder sb;
-    MUST(sb.try_append_repeated('x', 8192));
-    auto long_string = sb.string_view();
-    auto result = execute(database, DeprecatedString::formatted("INSERT INTO TestSchema.TestTable VALUES ('{}', 0);", long_string));
-    EXPECT(result.size() == 1);
-
-    result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable;");
-    EXPECT_EQ(result.size(), 1u);
-    EXPECT_EQ(result[0].row[0], long_string);
-}
-
 TEST_CASE(select_from_empty_table)
 {
     ScopeGuard guard([]() { unlink(db_name); });