Files
GRDB.swift/Tests/Private/DatabaseQueueSchemaCacheTests.swift
T
swiftlyfalling 30bc7e4f6c Replace most uses of #if SQLITE_HAS_CODEC with the appropriate USING_BUILTIN_SQLITE or USING_SQLCIPHER defines.
This enables easier compilation of a custom version of SQLite that
itself uses the SQLITE_HAS_CODEC define. (ex. If building SQLite with
the SQLite Encryption Extension.)
The SQLITE_HAS_CODEC define is now only used to guard use of the SQLite
encryption APIs.
GRDBCipher builds define USING_BUILTIN_SQLITE and USING_SQLCIPHER.
2016-05-30 18:19:45 -04:00

57 lines
1.9 KiB
Swift

import XCTest
#if USING_SQLCIPHER
@testable import GRDBCipher
#elseif USING_CUSTOMSQLITE
@testable import GRDBCustomSQLite
#else
@testable import GRDB
#endif
class DatabaseQueueSchemaCacheTests : GRDBTestCase {
func testCache() {
assertNoError {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
}
dbQueue.inDatabase { db in
// Assert that cache is empty
XCTAssertTrue(db.schemaCache.primaryKey(tableName: "items") == nil)
}
try dbQueue.inDatabase { db in
// Warm cache
let primaryKey = try db.primaryKey("items")!
XCTAssertEqual(primaryKey.rowIDColumn, "id")
// Assert that cache is warmed
XCTAssertTrue(db.schemaCache.primaryKey(tableName: "items") != nil)
}
try dbQueue.inDatabase { db in
// Empty cache after schema change
try db.execute("DROP TABLE items")
// Assert that cache is empty
XCTAssertTrue(db.schemaCache.primaryKey(tableName: "items") == nil)
}
try dbQueue.inDatabase { db in
do {
// Assert that cache is used: we expect an error now that
// the cache is empty.
_ = try db.primaryKey("items")
XCTFail()
} catch let error as DatabaseError {
XCTAssertEqual(error.code, 1) // SQLITE_ERROR
XCTAssertEqual(error.message!, "no such table: items")
XCTAssertEqual(error.description, "SQLite error 1: no such table: items")
}
}
}
}
}