|
1 | 1 | import XCTest
|
2 | 2 | import SQLite
|
3 | 3 |
|
| 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 | + |
4 | 14 | class StatementTests: SQLiteTestCase {
|
5 | 15 | override func setUpWithError() throws {
|
6 | 16 | try super.setUpWithError()
|
@@ -37,43 +47,29 @@ class StatementTests: SQLiteTestCase {
|
37 | 47 |
|
38 | 48 | /// Check that a statement reset will close the implicit transaction, allowing wal file to checkpoint
|
39 | 49 | 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 |
| - |
59 | 50 | // 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") |
62 | 52 |
|
63 | 53 | // prepare a statement and read a single row. This will incremeent the cursor which
|
64 | 54 | // prevents the implicit transaction from closing.
|
65 | 55 | // https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions
|
66 | 56 | let statement = try db.prepare("SELECT email FROM users")
|
67 | 57 | _ = try statement.step()
|
68 | 58 |
|
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 | + } |
71 | 67 |
|
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 |
73 | 69 | statement.reset()
|
74 | 70 |
|
75 | 71 | // truncate succeeds
|
76 |
| - try db.run("pragma wal_checkpoint(truncate)") |
| 72 | + try db.run("DROP TABLE users") |
77 | 73 | }
|
78 | 74 |
|
79 | 75 | }
|
0 commit comments