mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Database schema cache tests for DatabasePool and DatabaseQueue
This commit is contained in:
1 parent
5ac0eda8a6
commit
526e065e7d
7 files changed
+192
-57
No files matched your search
@@ -117,6 +117,10 @@
|
||||
5695310F1C9067DC00CF1A2B /* StatementCrashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569531011C9067CC00CF1A2B /* StatementCrashTests.swift */; };
|
||||
5695311F1C907A8C00CF1A2B /* DatabaseSchemaCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */; };
|
||||
569531201C907A8C00CF1A2B /* DatabaseSchemaCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */; };
|
||||
569531261C9087B600CF1A2B /* DatabaseQueueSchemaCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569531231C90878D00CF1A2B /* DatabaseQueueSchemaCacheTests.swift */; };
|
||||
569531271C9087B700CF1A2B /* DatabaseQueueSchemaCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569531231C90878D00CF1A2B /* DatabaseQueueSchemaCacheTests.swift */; };
|
||||
569531291C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569531281C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift */; };
|
||||
5695312A1C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569531281C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift */; };
|
||||
56A238371B9C74A90082EB20 /* DatabaseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238131B9C74A90082EB20 /* DatabaseTests.swift */; };
|
||||
56A238381B9C74A90082EB20 /* DatabaseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238131B9C74A90082EB20 /* DatabaseTests.swift */; };
|
||||
56A238391B9C74A90082EB20 /* InMemoryDatabaseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238141B9C74A90082EB20 /* InMemoryDatabaseTests.swift */; };
|
||||
@@ -369,6 +373,8 @@
|
||||
569531001C9067CC00CF1A2B /* StatementColumnConvertibleCrashTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatementColumnConvertibleCrashTests.swift; sourceTree = "<group>"; };
|
||||
569531011C9067CC00CF1A2B /* StatementCrashTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatementCrashTests.swift; sourceTree = "<group>"; };
|
||||
5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSchemaCache.swift; sourceTree = "<group>"; };
|
||||
569531231C90878D00CF1A2B /* DatabaseQueueSchemaCacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueSchemaCacheTests.swift; sourceTree = "<group>"; };
|
||||
569531281C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePoolSchemaCacheTests.swift; sourceTree = "<group>"; };
|
||||
56A238131B9C74A90082EB20 /* DatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseTests.swift; sourceTree = "<group>"; };
|
||||
56A238141B9C74A90082EB20 /* InMemoryDatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InMemoryDatabaseTests.swift; sourceTree = "<group>"; };
|
||||
56A238161B9C74A90082EB20 /* DatabaseErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseErrorTests.swift; sourceTree = "<group>"; };
|
||||
@@ -658,6 +664,8 @@
|
||||
569978D31B539038005EBEED /* Private */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
569531231C90878D00CF1A2B /* DatabaseQueueSchemaCacheTests.swift */,
|
||||
569531281C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift */,
|
||||
56EB0AB11BCD787300A3DC55 /* NSDataMemoryTests.swift */,
|
||||
5605F1861C69111300235C62 /* StatementInformationTests.swift */,
|
||||
);
|
||||
@@ -1394,6 +1402,7 @@
|
||||
56A238661B9C74A90082EB20 /* RecordFetchTests.swift in Sources */,
|
||||
56EE573E1BB317B7007A6A95 /* SQLiteStatementConversionTests.swift in Sources */,
|
||||
56A238481B9C74A90082EB20 /* DetachedRowTests.swift in Sources */,
|
||||
569531271C9087B700CF1A2B /* DatabaseQueueSchemaCacheTests.swift in Sources */,
|
||||
56A238541B9C74A90082EB20 /* PrimaryKeyMultipleTests.swift in Sources */,
|
||||
56FDECE31BB32DFD009AD709 /* MetalRowTests.swift in Sources */,
|
||||
56DE7B121C3D93ED00861EB8 /* StatementArgumentsTests.swift in Sources */,
|
||||
@@ -1415,6 +1424,7 @@
|
||||
56E8CE0E1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */,
|
||||
56A238581B9C74A90082EB20 /* PrimaryKeyRowIDTests.swift in Sources */,
|
||||
56A238621B9C74A90082EB20 /* RecordEditedTests.swift in Sources */,
|
||||
5695312A1C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift in Sources */,
|
||||
56A2385C1B9C74A90082EB20 /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
|
||||
56A238401B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
|
||||
56A2383C1B9C74A90082EB20 /* DatabaseErrorTests.swift in Sources */,
|
||||
@@ -1459,6 +1469,7 @@
|
||||
56EE573D1BB317B7007A6A95 /* SQLiteStatementConversionTests.swift in Sources */,
|
||||
56A238651B9C74A90082EB20 /* RecordFetchTests.swift in Sources */,
|
||||
56A238471B9C74A90082EB20 /* DetachedRowTests.swift in Sources */,
|
||||
569531261C9087B600CF1A2B /* DatabaseQueueSchemaCacheTests.swift in Sources */,
|
||||
56FDECE21BB32DFD009AD709 /* MetalRowTests.swift in Sources */,
|
||||
56A238531B9C74A90082EB20 /* PrimaryKeyMultipleTests.swift in Sources */,
|
||||
56DE7B111C3D93ED00861EB8 /* StatementArgumentsTests.swift in Sources */,
|
||||
@@ -1480,6 +1491,7 @@
|
||||
56A2385B1B9C74A90082EB20 /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
|
||||
565B0FEF1BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */,
|
||||
56A2383F1B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
|
||||
569531291C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift in Sources */,
|
||||
56E8CE0D1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */,
|
||||
56A2383B1B9C74A90082EB20 /* DatabaseErrorTests.swift in Sources */,
|
||||
56E8CE101BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift in Sources */,
|
||||
|
||||
@@ -30,7 +30,7 @@ public final class Database {
|
||||
private var functions = Set<DatabaseFunction>()
|
||||
private var collations = Set<DatabaseCollation>()
|
||||
|
||||
private var schemaCache: DatabaseSchemaCacheType
|
||||
var schemaCache: DatabaseSchemaCacheType // internal so that it can be tested
|
||||
|
||||
/// See setupTransactionHooks(), updateStatementDidFail(), updateStatementDidExecute()
|
||||
private var transactionState: TransactionState = .WaitForTransactionCompletion
|
||||
|
||||
+55
-16
@@ -2,36 +2,75 @@ import XCTest
|
||||
import GRDB
|
||||
|
||||
class GRDBTestCase: XCTestCase {
|
||||
var databasePath: String!
|
||||
var dbQueue: DatabaseQueue!
|
||||
var dbConfiguration = Configuration()
|
||||
|
||||
var dbQueuePath: String = {
|
||||
let dbQueueFileName = "GRDBTestCase-\(NSProcessInfo.processInfo().globallyUniqueString).sqlite"
|
||||
return (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent(dbQueueFileName)
|
||||
}()
|
||||
var _dbQueue: DatabaseQueue?
|
||||
var dbQueue: DatabaseQueue {
|
||||
get {
|
||||
if let _dbQueue = _dbQueue {
|
||||
return _dbQueue
|
||||
} else {
|
||||
_dbQueue = try! DatabaseQueue(path: dbQueuePath, configuration: dbConfiguration)
|
||||
return _dbQueue!
|
||||
}
|
||||
}
|
||||
set {
|
||||
_dbQueue = newValue
|
||||
}
|
||||
}
|
||||
|
||||
var dbPoolDirectoryPath: String = {
|
||||
let dbPoolDirectoryName = "GRDBTestCase-\(NSProcessInfo.processInfo().globallyUniqueString)"
|
||||
return (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent(dbPoolDirectoryName)
|
||||
}()
|
||||
var _dbPool: DatabasePool?
|
||||
var dbPool: DatabasePool {
|
||||
get {
|
||||
if let _dbPool = _dbPool {
|
||||
return _dbPool
|
||||
} else {
|
||||
try! NSFileManager.defaultManager().createDirectoryAtPath(dbPoolDirectoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
let dbPoolPath = (dbPoolDirectoryPath as NSString).stringByAppendingPathComponent("db.sqlite")
|
||||
_dbPool = try! DatabasePool(path: dbPoolPath, configuration: dbConfiguration)
|
||||
return _dbPool!
|
||||
}
|
||||
}
|
||||
set {
|
||||
_dbPool = newValue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var sqlQueries: [String]!
|
||||
var lastSQLQuery: String!
|
||||
var dbConfiguration: Configuration {
|
||||
var dbConfiguration = Configuration()
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
dbConfiguration.trace = { (sql) in
|
||||
self.sqlQueries.append(sql)
|
||||
self.lastSQLQuery = sql
|
||||
// LogSQL(sql) // Uncomment for verbose tests
|
||||
}
|
||||
return dbConfiguration
|
||||
}
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
sqlQueries = []
|
||||
lastSQLQuery = nil
|
||||
|
||||
let databaseFileName = "GRDBTestCase-\(NSProcessInfo.processInfo().globallyUniqueString).sqlite"
|
||||
databasePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent(databaseFileName)
|
||||
do { try NSFileManager.defaultManager().removeItemAtPath(databasePath) } catch { }
|
||||
dbQueue = try! DatabaseQueue(path: databasePath, configuration: dbConfiguration)
|
||||
do { try NSFileManager.defaultManager().removeItemAtPath(dbQueuePath) } catch { }
|
||||
do { try NSFileManager.defaultManager().removeItemAtPath(dbPoolDirectoryPath) } catch { }
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
super.tearDown()
|
||||
|
||||
dbQueue = nil
|
||||
try! NSFileManager.defaultManager().removeItemAtPath(databasePath)
|
||||
_dbQueue = nil
|
||||
_dbPool = nil
|
||||
|
||||
do { try NSFileManager.defaultManager().removeItemAtPath(dbQueuePath) } catch { }
|
||||
do { try NSFileManager.defaultManager().removeItemAtPath(dbPoolDirectoryPath) } catch { }
|
||||
}
|
||||
|
||||
func assertNoError(@noescape test: (Void) throws -> Void) {
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import XCTest
|
||||
@testable import GRDB
|
||||
|
||||
class DatabasePoolSchemaCacheTests : GRDBTestCase {
|
||||
|
||||
func testCache() {
|
||||
assertNoError {
|
||||
try dbPool.write { db in
|
||||
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
}
|
||||
|
||||
dbPool.write { db in
|
||||
// Assert that the writer cache is empty
|
||||
XCTAssertTrue(db.schemaCache.primaryKey(tableName: "items") == nil)
|
||||
}
|
||||
|
||||
dbPool.read { db in
|
||||
// Assert that a reader cache is empty
|
||||
XCTAssertTrue(db.schemaCache.primaryKey(tableName: "items") == nil)
|
||||
}
|
||||
|
||||
try dbPool.read { db in
|
||||
// Warm cache in a reader
|
||||
let primaryKey = try db.primaryKey("items")
|
||||
switch primaryKey {
|
||||
case .RowID(let columnName):
|
||||
XCTAssertEqual(columnName, "id")
|
||||
default:
|
||||
XCTFail()
|
||||
}
|
||||
|
||||
// Assert that reader cache is warmed
|
||||
XCTAssertTrue(db.schemaCache.primaryKey(tableName: "items") != nil)
|
||||
}
|
||||
|
||||
dbPool.write { db in
|
||||
// Assert that writer cache is warmed
|
||||
XCTAssertTrue(db.schemaCache.primaryKey(tableName: "items") != nil)
|
||||
}
|
||||
|
||||
try dbPool.write { db in
|
||||
// Empty cache after schema change
|
||||
try db.execute("DROP TABLE items")
|
||||
|
||||
// Assert that the writer cache is empty
|
||||
XCTAssertTrue(db.schemaCache.primaryKey(tableName: "items") == nil)
|
||||
}
|
||||
|
||||
dbPool.read { db in
|
||||
// Assert that a reader cache is empty
|
||||
XCTAssertTrue(db.schemaCache.primaryKey(tableName: "items") == nil)
|
||||
}
|
||||
|
||||
try dbPool.read { 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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import XCTest
|
||||
@testable import GRDB
|
||||
|
||||
class DatabaseQueueSchemaCacheTests : GRDBTestCase {
|
||||
|
||||
func testCache() {
|
||||
assertNoError {
|
||||
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")
|
||||
switch primaryKey {
|
||||
case .RowID(let columnName):
|
||||
XCTAssertEqual(columnName, "id")
|
||||
default:
|
||||
XCTFail()
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,45 +1,7 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
class DatabasePoolTests: XCTestCase {
|
||||
var databaseDirectoryPath: String!
|
||||
var dbPool: DatabasePool!
|
||||
var sqlQueries: [String]!
|
||||
var lastSQLQuery: String!
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
let databaseDirectoryName = "GRDBDatabasePoolTests-\(NSProcessInfo.processInfo().globallyUniqueString)"
|
||||
databaseDirectoryPath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent(databaseDirectoryName)
|
||||
do { try NSFileManager.defaultManager().removeItemAtPath(databaseDirectoryPath) } catch { }
|
||||
try! NSFileManager.defaultManager().createDirectoryAtPath(databaseDirectoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
|
||||
sqlQueries = []
|
||||
var configuration = Configuration()
|
||||
configuration.trace = { (sql) in
|
||||
self.sqlQueries.append(sql)
|
||||
self.lastSQLQuery = sql
|
||||
// LogSQL(sql) // Uncomment for verbose tests
|
||||
}
|
||||
|
||||
let databasePath = (databaseDirectoryPath as NSString).stringByAppendingPathComponent("db.sqlite")
|
||||
dbPool = try! DatabasePool(path: databasePath, configuration: configuration)
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
super.tearDown()
|
||||
|
||||
dbPool = nil
|
||||
try! NSFileManager.defaultManager().removeItemAtPath(databaseDirectoryPath)
|
||||
}
|
||||
|
||||
func assertNoError(@noescape test: (Void) throws -> Void) {
|
||||
do {
|
||||
try test()
|
||||
} catch {
|
||||
XCTFail("unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
class DatabasePoolTests: GRDBTestCase {
|
||||
|
||||
func testBasicWriteRead() {
|
||||
assertNoError {
|
||||
@@ -53,4 +15,5 @@ class DatabasePoolTests: XCTestCase {
|
||||
XCTAssertEqual(id, 1)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -307,7 +307,7 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
assertNoError {
|
||||
var configuration = Configuration()
|
||||
configuration.readonly = true
|
||||
dbQueue = try DatabaseQueue(path: databasePath, configuration: configuration)
|
||||
dbQueue = try DatabaseQueue(path: dbQueuePath, configuration: configuration)
|
||||
let statement = try dbQueue.inDatabase { db in
|
||||
try db.updateStatement("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user