Skip to content

Commit

Permalink
Merge pull request #2718 from cloudflare/shrima/add-sqlite_test_large…
Browse files Browse the repository at this point in the history
…_readwrite

Add Sqlite test for read/write row counters for large rows
  • Loading branch information
shrima-cf authored Sep 17, 2024
2 parents c70a208 + a33e1a8 commit 563d72f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/workerd/util/sqlite-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,40 @@ KJ_TEST("SQLite write row counters (basic)") {
}
}

KJ_TEST("SQLite read/write row counters (large row insert)") {
// This is used to verify reading/writing a large row (bigger than the size of one page in sqlite)
// results only in 1 read/row count as returned by the DB

auto dir = kj::newInMemoryDirectory(kj::nullClock());
SqliteDatabase::Vfs vfs(*dir);
SqliteDatabase db(vfs, kj::Path({"foo"}), kj::WriteMode::CREATE | kj::WriteMode::MODIFY);

db.run("CREATE TABLE large_things (id INTEGER PRIMARY KEY, large_value TEXT)");

// SQLite's default page size is 4096 bytes
// So create a string significantly larger than that
KJ_EXPECT(db.run("PRAGMA page_size").getInt(0) == 4096);
kj::String largeValue = kj::str(kj::repeat('A', 100000));

// Insert the large row
RowCounts insertStats = countRowsTouched(
db, "INSERT INTO large_things (id, large_value) VALUES (?, ?)", 1, kj::mv(largeValue));

KJ_EXPECT(insertStats.found == 0);
KJ_EXPECT(insertStats.read == 1);
KJ_EXPECT(insertStats.written == 1);

// Verify the insert
auto verifyStmt = db.prepare("SELECT COUNT(*) FROM large_things");
KJ_EXPECT(verifyStmt.run().getInt(0) == 1);

// Read the large row
RowCounts readStats = countRowsTouched(db, "SELECT * FROM large_things WHERE id = ?", 1);
KJ_EXPECT(readStats.found == 1);
KJ_EXPECT(readStats.read == 1);
KJ_EXPECT(readStats.written == 0);
}

KJ_TEST("SQLite row counters with triggers") {
auto dir = kj::newInMemoryDirectory(kj::nullClock());
SqliteDatabase::Vfs vfs(*dir);
Expand Down

0 comments on commit 563d72f

Please sign in to comment.