diff --git a/hyperpage.cpp b/hyperpage.cpp index af43c8f..19a8d38 100644 --- a/hyperpage.cpp +++ b/hyperpage.cpp @@ -147,7 +147,7 @@ void hyperpage::writer::store(const hyperpage::page &page) { sqlite3 *db = get_handle(_handle); const std::string query = - "INSERT INTO hyperpage (path, mime_type, content) VALUES (?, ?, ?);" + "INSERT INTO hyperpage (path, mime_type, content) VALUES (?, ?, ?) " "ON CONFLICT(path) DO UPDATE SET mime_type=excluded.mime_type, content=excluded.content;"; sqlite3_stmt *stmt = nullptr; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6da6b86..d6f1b38 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -52,4 +52,6 @@ endif() maxtest_add_test(unit store_load $) maxtest_add_test(unit open_database $) -maxtest_add_test(unit mime_type $) \ No newline at end of file +maxtest_add_test(unit mime_type $) +maxtest_add_test(unit overwrite_test $) +maxtest_add_test(unit archive_size_no_growth_test $) \ No newline at end of file diff --git a/tests/unit.cpp b/tests/unit.cpp index 1765979..13bd0c9 100644 --- a/tests/unit.cpp +++ b/tests/unit.cpp @@ -146,4 +146,80 @@ MAXTEST_MAIN auto file_type = hyperpage::mime_type(json_path); MAXTEST_ASSERT(file_type == json_mime_type); }; + + MAXTEST_TEST_CASE(overwrite_test) + { + std::filesystem::path db_path = std::filesystem::path(args[0]) / "hyperpage_overwrite_test.db"; + + // Remove any existing database file to start fresh + if (std::filesystem::exists(db_path)) { + std::filesystem::remove(db_path); + } + + hyperpage::writer writer(db_path.string()); + + // Create a page and store it + test_page original_page("/index.html", "text/html", "Original Content"); + writer.store(original_page); + + // Now overwrite with updated content using the same writer + test_page updated_page("/index.html", "text/html", "Updated Content"); + writer.store(updated_page); + + // Verify updated content is actually stored + hyperpage::reader reader(db_path.string()); + auto loaded_page = reader.load("/index.html"); + MAXTEST_ASSERT(loaded_page != nullptr); + MAXTEST_ASSERT(loaded_page->get_path() == "/index.html"); + MAXTEST_ASSERT(loaded_page->get_mime_type() == "text/html"); + + // Check updated content - this is where the issue should manifest + std::string updated_content = "Updated Content"; + MAXTEST_ASSERT(loaded_page->get_length() == updated_content.size()); + MAXTEST_ASSERT(match_buffers(loaded_page->get_content(), loaded_page->get_length(), + reinterpret_cast(updated_content.data()), updated_content.size())); + }; + + MAXTEST_TEST_CASE(archive_size_no_growth_test) + { + std::filesystem::path db_path = std::filesystem::path(args[0]) / "hyperpage_size_test.db"; + + // Remove any existing database file to start fresh + if (std::filesystem::exists(db_path)) { + std::filesystem::remove(db_path); + } + + // Create initial content + test_page test_page_content("/test.html", "text/html", "Test Content"); + + // Store initial content and record database size + { + hyperpage::writer writer(db_path.string()); + writer.store(test_page_content); + } + + size_t initial_size = std::filesystem::file_size(db_path); + + // Overwrite with identical content multiple times + for (int i = 0; i < 5; ++i) { + hyperpage::writer writer(db_path.string()); + writer.store(test_page_content); // Same content each time + } + + size_t final_size = std::filesystem::file_size(db_path); + + // The database size should not grow significantly when overwriting with identical content + // Allow for some variance due to SQLite overhead, but it shouldn't grow substantially + MAXTEST_ASSERT(final_size <= initial_size * 1.1); // Max 10% growth tolerance + + // Verify content is still correct + hyperpage::reader reader(db_path.string()); + auto loaded_page = reader.load("/test.html"); + MAXTEST_ASSERT(loaded_page != nullptr); + MAXTEST_ASSERT(loaded_page->get_path() == "/test.html"); + std::string expected_content = "Test Content"; + MAXTEST_ASSERT(loaded_page->get_length() == expected_content.size()); + MAXTEST_ASSERT(match_buffers(loaded_page->get_content(), loaded_page->get_length(), + reinterpret_cast(expected_content.data()), expected_content.size())); + }; } \ No newline at end of file