From 526e065e7d1b105fcbeb3f0e2c513c0ffdac5ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 9 Mar 2016 18:15:18 +0100 Subject: [PATCH] Database schema cache tests for DatabasePool and DatabaseQueue --- GRDB.xcodeproj/project.pbxproj | 12 ++++ GRDB/Core/Database.swift | 2 +- Tests/GRDBTestCase.swift | 71 ++++++++++++++----- .../DatabasePoolSchemaCacheTests.swift | 68 ++++++++++++++++++ .../DatabaseQueueSchemaCacheTests.swift | 53 ++++++++++++++ .../Core/DatabasePool/DatabasePoolTests.swift | 41 +---------- .../Core/Statement/UpdateStatementTests.swift | 2 +- 7 files changed, 192 insertions(+), 57 deletions(-) create mode 100644 Tests/Private/DatabasePoolSchemaCacheTests.swift create mode 100644 Tests/Private/DatabaseQueueSchemaCacheTests.swift diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 2bc096787..c1c9328fc 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -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 = ""; }; 569531011C9067CC00CF1A2B /* StatementCrashTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatementCrashTests.swift; sourceTree = ""; }; 5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSchemaCache.swift; sourceTree = ""; }; + 569531231C90878D00CF1A2B /* DatabaseQueueSchemaCacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueSchemaCacheTests.swift; sourceTree = ""; }; + 569531281C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePoolSchemaCacheTests.swift; sourceTree = ""; }; 56A238131B9C74A90082EB20 /* DatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseTests.swift; sourceTree = ""; }; 56A238141B9C74A90082EB20 /* InMemoryDatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InMemoryDatabaseTests.swift; sourceTree = ""; }; 56A238161B9C74A90082EB20 /* DatabaseErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseErrorTests.swift; sourceTree = ""; }; @@ -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 */, diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 05a75941b..700546673 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -30,7 +30,7 @@ public final class Database { private var functions = Set() private var collations = Set() - private var schemaCache: DatabaseSchemaCacheType + var schemaCache: DatabaseSchemaCacheType // internal so that it can be tested /// See setupTransactionHooks(), updateStatementDidFail(), updateStatementDidExecute() private var transactionState: TransactionState = .WaitForTransactionCompletion diff --git a/Tests/GRDBTestCase.swift b/Tests/GRDBTestCase.swift index 28cfb7127..bc9b0ba58 100644 --- a/Tests/GRDBTestCase.swift +++ b/Tests/GRDBTestCase.swift @@ -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) { diff --git a/Tests/Private/DatabasePoolSchemaCacheTests.swift b/Tests/Private/DatabasePoolSchemaCacheTests.swift new file mode 100644 index 000000000..a518a1c4f --- /dev/null +++ b/Tests/Private/DatabasePoolSchemaCacheTests.swift @@ -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") + } + } + } + } +} diff --git a/Tests/Private/DatabaseQueueSchemaCacheTests.swift b/Tests/Private/DatabaseQueueSchemaCacheTests.swift new file mode 100644 index 000000000..c6dacccff --- /dev/null +++ b/Tests/Private/DatabaseQueueSchemaCacheTests.swift @@ -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") + } + } + } + } +} diff --git a/Tests/Public/Core/DatabasePool/DatabasePoolTests.swift b/Tests/Public/Core/DatabasePool/DatabasePoolTests.swift index d91ee6264..a17e7b7cf 100644 --- a/Tests/Public/Core/DatabasePool/DatabasePoolTests.swift +++ b/Tests/Public/Core/DatabasePool/DatabasePoolTests.swift @@ -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) } } + } diff --git a/Tests/Public/Core/Statement/UpdateStatementTests.swift b/Tests/Public/Core/Statement/UpdateStatementTests.swift index 3922e6454..637b174a6 100644 --- a/Tests/Public/Core/Statement/UpdateStatementTests.swift +++ b/Tests/Public/Core/Statement/UpdateStatementTests.swift @@ -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)") }