Skip to content

Commit 210303f

Browse files
committed
Use in memory database from test setup, verify error when statement isn't reset, update comments
1 parent da0cd09 commit 210303f

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

Tests/SQLiteTests/StatementTests.swift

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import XCTest
22
import SQLite
33

4+
#if SQLITE_SWIFT_STANDALONE
5+
import sqlite3
6+
#elseif SQLITE_SWIFT_SQLCIPHER
7+
import SQLCipher
8+
#elseif os(Linux)
9+
import CSQLite
10+
#else
11+
import SQLite3
12+
#endif
13+
414
class StatementTests: SQLiteTestCase {
515
override func setUpWithError() throws {
616
try super.setUpWithError()
@@ -37,43 +47,29 @@ class StatementTests: SQLiteTestCase {
3747

3848
/// Check that a statement reset will close the implicit transaction, allowing wal file to checkpoint
3949
func test_reset_statement() throws {
40-
// create new db on disk in wal mode
41-
let path = temporaryFile() + ".sqlite3"
42-
let db = try Connection(.uri(path))
43-
44-
// create users table
45-
try db.execute("""
46-
CREATE TABLE users (
47-
id INTEGER PRIMARY KEY,
48-
email TEXT NOT NULL UNIQUE,
49-
age INTEGER,
50-
salary REAL,
51-
admin BOOLEAN NOT NULL DEFAULT 0 CHECK (admin IN (0, 1)),
52-
manager_id INTEGER,
53-
created_at DATETIME,
54-
FOREIGN KEY(manager_id) REFERENCES users(id)
55-
)
56-
"""
57-
)
58-
5950
// insert single row
60-
try db.run("INSERT INTO \"users\" (email, age, admin) values (?, ?, ?)",
61-
"[email protected]", 1.datatypeValue, false.datatypeValue)
51+
try insertUsers("bob")
6252

6353
// prepare a statement and read a single row. This will incremeent the cursor which
6454
// prevents the implicit transaction from closing.
6555
// https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions
6656
let statement = try db.prepare("SELECT email FROM users")
6757
_ = try statement.step()
6858

69-
// verify that the transaction is not closed, which prevents wal_checkpoints (both explicit and auto)
70-
XCTAssertThrowsError(try db.run("pragma wal_checkpoint(truncate)"))
59+
// verify implicit transaction is not closed, and the users table is still locked
60+
XCTAssertThrowsError(try db.run("DROP TABLE users")) { error in
61+
if case let Result.error(_, code, _) = error {
62+
XCTAssertEqual(code, SQLITE_LOCKED)
63+
} else {
64+
XCTFail("unexpected error")
65+
}
66+
}
7167

72-
// reset the prepared statement, allowing the implicit transaction to close
68+
// reset the prepared statement, unlocking the table and allowing the implicit transaction to close
7369
statement.reset()
7470

7571
// truncate succeeds
76-
try db.run("pragma wal_checkpoint(truncate)")
72+
try db.run("DROP TABLE users")
7773
}
7874

7975
}

0 commit comments

Comments
 (0)