|
5 | 5 | import pytest |
6 | 6 |
|
7 | 7 | from dissect.database.sqlite3 import sqlite3 |
| 8 | +from tests._util import absolute_path |
8 | 9 |
|
9 | 10 | if TYPE_CHECKING: |
10 | 11 | from pathlib import Path |
@@ -162,3 +163,39 @@ def _assert_checkpoint_3(s: sqlite3.SQLite3) -> None: |
162 | 163 | assert rows[9].id == 11 |
163 | 164 | assert rows[9].name == "second checkpoint" |
164 | 165 | assert rows[9].value == 101 |
| 166 | + |
| 167 | + |
| 168 | +def test_wal_page_count() -> None: |
| 169 | + """Test if we count the page numbers in the SQLite3 and WAL correctly. |
| 170 | +
|
| 171 | + Test data generated using: |
| 172 | +
|
| 173 | + $ sqlite3 tests/_data/sqlite3/page_count.db |
| 174 | + SQLite version 3.45.1 2024-01-30 16:01:20 |
| 175 | + Enter ".help" for usage hints. |
| 176 | + sqlite> PRAGMA journal_mode = WAL; |
| 177 | + wal |
| 178 | + sqlite> CREATE TABLE t1 (a, b); |
| 179 | + sqlite> .quit # commits wal |
| 180 | +
|
| 181 | + $ python |
| 182 | + >>> import sqlite3 |
| 183 | + >>> con = sqlite3.connect("tests/_data/sqlite3/page_count.db") |
| 184 | + ... cur = con.cursor() |
| 185 | + >>> cur.execute("INSERT INTO t1 VALUES (1, ?)", ("A" * 8192,)) |
| 186 | + >>> con.commit() |
| 187 | + # Copy page_count.db* files before closing |
| 188 | + """ |
| 189 | + |
| 190 | + db = sqlite3.SQLite3(absolute_path("_data/sqlite3/page_count.db")) |
| 191 | + table = db.table("t1") |
| 192 | + assert table.sql == "CREATE TABLE t1 (a, b)" |
| 193 | + |
| 194 | + row = next(table.rows()) |
| 195 | + assert row.a == 1 |
| 196 | + assert row.b == "A" * 8192 |
| 197 | + |
| 198 | + assert db.wal |
| 199 | + assert db.wal.highest_page_num == 4 |
| 200 | + assert db.header.page_count == 2 |
| 201 | + assert db.page_count == 4 |
0 commit comments