forked from stephencelis/SQLite.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatementTests.swift
74 lines (62 loc) · 2.55 KB
/
StatementTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import XCTest
@testable import SQLite
#if SQLITE_SWIFT_STANDALONE
import sqlite3
#elseif SQLITE_SWIFT_SQLCIPHER
import SQLCipher
#elseif os(Linux)
import CSQLite
#else
import SQLite3
#endif
class StatementTests: SQLiteTestCase {
override func setUpWithError() throws {
try super.setUpWithError()
try createUsersTable()
}
func test_cursor_to_blob() throws {
try insertUsers("alice")
let statement = try db.prepare("SELECT email FROM users")
XCTAssert(try statement.step())
let blob = statement.row[0] as Blob
XCTAssertEqual("[email protected]", String(bytes: blob.bytes, encoding: .utf8)!)
}
func test_zero_sized_blob_returns_null() throws {
let blobs = Table("blobs")
let blobColumn = SQLite.Expression<Blob>("blob_column")
try db.run(blobs.create { $0.column(blobColumn) })
try db.run(blobs.insert(blobColumn <- Blob(bytes: [])))
let blobValue = try db.scalar(blobs.select(blobColumn).limit(1, offset: 0))
XCTAssertEqual([], blobValue.bytes)
}
func test_prepareRowIterator() throws {
let names = ["a", "b", "c"]
try insertUsers(names)
let emailColumn = SQLite.Expression<String>("email")
let statement = try db.prepare("SELECT email FROM users")
let emails = try statement.prepareRowIterator().map { $0[emailColumn] }
XCTAssertEqual(names.map({ "\($0)@example.com" }), emails.sorted())
}
/// Check that a statement reset will close the implicit transaction, allowing wal file to checkpoint
func test_reset_statement() throws {
// insert single row
try insertUsers("bob")
// prepare a statement and read a single row. This will increment the cursor which
// prevents the implicit transaction from closing.
// https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions
let statement = try db.prepare("SELECT email FROM users")
_ = try statement.step()
// verify implicit transaction is not closed, and the users table is still locked
XCTAssertThrowsError(try db.run("DROP TABLE users")) { error in
if case let Result.error(_, code, _) = error {
XCTAssertEqual(code, SQLITE_LOCKED)
} else {
XCTFail("unexpected error")
}
}
// reset the prepared statement, unlocking the table and allowing the implicit transaction to close
statement.reset()
// truncate succeeds
try db.run("DROP TABLE users")
}
}