From e3d8c06ad0e2a18d6c8ff8d03f065f2fefa00549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 3 Apr 2016 16:55:04 +0200 Subject: [PATCH] Refactor tests so that DatabasePool can be configured at the target level. We are preparing tests for encrypted databases. --- CHANGELOG.md | 2 +- GRDB/Core/DatabasePool.swift | 8 +++ GRDB/Core/DatabaseQueue.swift | 30 +++++------ GRDB/Core/DatabaseStore.swift | 37 ++------------ Tests/GRDBTestCase.swift | 47 +++++++---------- .../DatabasePoolReleaseMemoryTests.swift | 45 +++++++++-------- .../DatabasePoolSchemaCacheTests.swift | 2 + .../DatabasePoolCollationTests.swift | 2 + .../DatabasePoolConcurrencyTests.swift | 45 ++++++++++------- .../DatabasePoolFileAttributesTests.swift | 50 +++++++++++-------- .../DatabasePoolFunctionTests.swift | 2 + .../DatabasePoolReadOnlyTests.swift | 15 +++--- .../DatabaseReader/DatabaseReaderTests.swift | 5 ++ .../DatabasePoolMutablePersistableTests.swift | 33 +++++++++--- .../DatabasePoolPersistableTests.swift | 35 ++++++++++--- .../Migrations/DatabaseMigratorTests.swift | 37 +++++++++++++- 16 files changed, 230 insertions(+), 165 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8384cdfe1..b8bb1294f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ Release Notes **New** -- `DatabaseQueue.path` is the path to the database. +- `DatabaseQueue.path` and `DatabasePool.path` give the path to the database. **Breaking Changes** diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 1efb7134b..7ac4743a3 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -91,6 +91,14 @@ public final class DatabasePool { #endif + // MARK: - Configuration + + /// The path to the database. + public var path: String { + return store.path + } + + // MARK: - WAL Management /// Runs a WAL checkpoint diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index e0a212744..fa3befc06 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -7,19 +7,6 @@ import UIKit /// A DatabaseQueue serializes access to an SQLite database. public final class DatabaseQueue { - // MARK: - Configuration - - /// The database configuration - public var configuration: Configuration { - return serializedDatabase.configuration - } - - /// The path to the database. - /// - /// It is nil for in-memory databases. - public let path: String! - - // MARK: - Initializers /// Opens the SQLite database at path *path*. @@ -33,7 +20,6 @@ public final class DatabaseQueue { /// - configuration: A configuration. /// - throws: A DatabaseError whenever an SQLite error occurs. public init(path: String, configuration: Configuration = Configuration()) throws { - self.path = path store = try DatabaseStore(path: path, attributes: configuration.fileAttributes) serializedDatabase = try SerializedDatabase( path: path, @@ -49,7 +35,6 @@ public final class DatabaseQueue { /// /// - parameter configuration: A configuration. public init(configuration: Configuration = Configuration()) { - path = nil store = nil serializedDatabase = try! SerializedDatabase( path: ":memory:", @@ -68,6 +53,21 @@ public final class DatabaseQueue { #endif + // MARK: - Configuration + + /// The database configuration + public var configuration: Configuration { + return serializedDatabase.configuration + } + + /// The path to the database. + /// + /// It is nil for in-memory databases. + public var path: String! { + return store?.path + } + + // MARK: - Database access /// Synchronously executes a block a protected dispatch queue, and returns diff --git a/GRDB/Core/DatabaseStore.swift b/GRDB/Core/DatabaseStore.swift index e97185fc8..54a62c887 100644 --- a/GRDB/Core/DatabaseStore.swift +++ b/GRDB/Core/DatabaseStore.swift @@ -4,10 +4,13 @@ import Foundation /// all database files (db.sqlite, db.sqlite-wal, db.sqlite-shm, /// db.sqlite-journal, etc.) class DatabaseStore { + let path: String private let source: dispatch_source_t? private let queue: dispatch_queue_t? init(path: String, attributes: [String: AnyObject]?) throws { + self.path = path + guard let attributes = attributes else { self.queue = nil self.source = nil @@ -54,40 +57,6 @@ class DatabaseStore { dispatch_resume(source) } - private init() { - self.queue = nil - self.source = nil - } - - private init(directoryPath: String, databaseFileName: String, directoryDescriptor: CInt, attributes: [String: AnyObject]) { - let queue = dispatch_queue_create("GRDB.DatabaseStore", nil) - let source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, UInt(directoryDescriptor), DISPATCH_VNODE_WRITE, queue) - - // Configure dispatch source - dispatch_source_set_event_handler(source) { - // Directory has been modified: apply file attributes on unprocessed files - DatabaseStore.setFileAttributes( - directoryPath: directoryPath, - databaseFileName: databaseFileName, - attributes: attributes) - } - dispatch_source_set_cancel_handler(source) { - close(directoryDescriptor) - } - - self.queue = queue - self.source = source - - // Apply file attributes on existing files - DatabaseStore.setFileAttributes( - directoryPath: directoryPath, - databaseFileName: databaseFileName, - attributes: attributes) - - // Wait for directory modifications - dispatch_resume(source) - } - deinit { if let source = source { dispatch_source_cancel(source) diff --git a/Tests/GRDBTestCase.swift b/Tests/GRDBTestCase.swift index 732225cef..9d5d15de0 100644 --- a/Tests/GRDBTestCase.swift +++ b/Tests/GRDBTestCase.swift @@ -5,46 +5,36 @@ class GRDBTestCase: XCTestCase { // The default configuration for tests var dbConfiguration: Configuration! - // Builds a database queue + // Builds a database queue based on dbConfiguration func makeDatabaseQueue(filename: String = "db.sqlite") throws -> DatabaseQueue { try! NSFileManager.defaultManager().createDirectoryAtPath(dbDirectoryPath, withIntermediateDirectories: true, attributes: nil) - let dbQueuePath = (dbDirectoryPath as NSString).stringByAppendingPathComponent(filename) - let dbQueue = try DatabaseQueue(path: dbQueuePath, configuration: dbConfiguration) + let dbPath = (dbDirectoryPath as NSString).stringByAppendingPathComponent(filename) + let dbQueue = try DatabaseQueue(path: dbPath, configuration: dbConfiguration) try setUpDatabase(dbQueue) return dbQueue } + // Builds a database pool based on dbConfiguration + func makeDatabasePool(filename: String = "db.sqlite") throws -> DatabasePool { + try! NSFileManager.defaultManager().createDirectoryAtPath(dbDirectoryPath, withIntermediateDirectories: true, attributes: nil) + let dbPath = (dbDirectoryPath as NSString).stringByAppendingPathComponent(filename) + let dbPool = try DatabasePool(path: dbPath, configuration: dbConfiguration) + try setUpDatabase(dbPool) + return dbPool + } + // Subclasses can override + // Default implementation is empty. func setUpDatabase(dbWriter: DatabaseWriter) throws { } // The default path for database pool directory - var dbDirectoryPath: String! - - // The default path for database pool - var dbPoolPath: String { - return (dbDirectoryPath as NSString).stringByAppendingPathComponent("db.sqlite") - } - - // The default database pool - var dbPool: DatabasePool! { - get { - if let _dbPool = _dbPool { - return _dbPool - } else { - try! NSFileManager.defaultManager().createDirectoryAtPath(dbDirectoryPath, withIntermediateDirectories: true, attributes: nil) - _dbPool = try! DatabasePool(path: dbPoolPath, configuration: dbConfiguration) - return _dbPool! - } - } - set { - _dbPool = newValue - } - } - var _dbPool: DatabasePool? - + private var dbDirectoryPath: String! + // Populated by default configuration var sqlQueries: [String]! + + // Populated by default configuration var lastSQLQuery: String! override func setUp() { @@ -67,9 +57,6 @@ class GRDBTestCase: XCTestCase { override func tearDown() { super.tearDown() - - _dbPool = nil - do { try NSFileManager.defaultManager().removeItemAtPath(dbDirectoryPath) } catch { } } diff --git a/Tests/Private/DatabasePoolReleaseMemoryTests.swift b/Tests/Private/DatabasePoolReleaseMemoryTests.swift index c7c2d8f9d..533dd3214 100644 --- a/Tests/Private/DatabasePoolReleaseMemoryTests.swift +++ b/Tests/Private/DatabasePoolReleaseMemoryTests.swift @@ -24,14 +24,14 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { // write & read - try dbPool.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") - for _ in 0..<2 { - try dbPool.execute("INSERT INTO items (id) VALUES (NULL)") + do { + // Create and release DatabasePool + let dbPool = try makeDatabasePool() + // Writer connection + try dbPool.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") + // Reader connection + _ = Int.fetchOne(dbPool, "SELECT COUNT(*) FROM items") } - _ = Int.fetchOne(dbPool, "SELECT COUNT(*) FROM items") - - // Release - dbPool = nil // One reader, one writer XCTAssertEqual(totalOpenConnectionCount, 2) @@ -60,6 +60,7 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { } } + let dbPool = try makeDatabasePool() try dbPool.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") for _ in 0..<2 { try dbPool.execute("INSERT INTO items (id) VALUES (NULL)") @@ -80,7 +81,7 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { // end end releaseMemory let block1 = { () in - self.dbPool.read { db in + dbPool.read { db in let generator = Row.fetch(db, "SELECT * FROM items").generate() XCTAssertTrue(generator.next() != nil) dispatch_semaphore_signal(s1) @@ -92,7 +93,7 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { } let block2 = { () in dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) - self.dbPool.read { db in + dbPool.read { db in let generator = Row.fetch(db, "SELECT * FROM items").generate() XCTAssertTrue(generator.next() != nil) dispatch_semaphore_signal(s2) @@ -102,7 +103,7 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { } let block3 = { () in dispatch_semaphore_wait(s3, DISPATCH_TIME_FOREVER) - self.dbPool.releaseMemory() + dbPool.releaseMemory() } let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT) dispatch_apply(3, queue) { index in @@ -136,8 +137,6 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { } } - try self.dbPool.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") - // Block 1 Block 2 // read { // > @@ -149,12 +148,14 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { // } let (block1, block2) = { () -> (() -> (), () -> ()) in + var dbPool: DatabasePool? = try! makeDatabasePool() + try! dbPool!.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") + let block1 = { () in dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) - self.dbPool = nil + dbPool = nil dispatch_semaphore_signal(s2) } - let dbPool = self.dbPool let block2 = { [weak dbPool] () in if let dbPool = dbPool { dbPool.read { db in @@ -201,10 +202,6 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { } } - try self.dbPool.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") - try self.dbPool.execute("INSERT INTO items (id) VALUES (NULL)") - try self.dbPool.execute("INSERT INTO items (id) VALUES (NULL)") - // Block 1 Block 2 // write { // SELECT @@ -219,12 +216,16 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { // } let (block1, block2) = { () -> (() -> (), () -> ()) in + var dbPool: DatabasePool? = try! makeDatabasePool() + try! dbPool!.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") + try! dbPool!.execute("INSERT INTO items (id) VALUES (NULL)") + try! dbPool!.execute("INSERT INTO items (id) VALUES (NULL)") + let block1 = { () in dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) - self.dbPool = nil + dbPool = nil dispatch_semaphore_signal(s2) } - let dbPool = self.dbPool let block2 = { [weak dbPool] () in weak var connection: Database? = nil var generator: DatabaseGenerator? = nil @@ -277,12 +278,12 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { // dbPool is nil let (block1, block2) = { () -> (() -> (), () -> ()) in + var dbPool: DatabasePool? = try! makeDatabasePool() let block1 = { () in dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) - self.dbPool = nil + dbPool = nil dispatch_semaphore_signal(s2) } - let dbPool = self.dbPool let block2 = { [weak dbPool] () in var statement: UpdateStatement? = nil do { diff --git a/Tests/Private/DatabasePoolSchemaCacheTests.swift b/Tests/Private/DatabasePoolSchemaCacheTests.swift index a518a1c4f..f390fc3e8 100644 --- a/Tests/Private/DatabasePoolSchemaCacheTests.swift +++ b/Tests/Private/DatabasePoolSchemaCacheTests.swift @@ -5,6 +5,8 @@ class DatabasePoolSchemaCacheTests : GRDBTestCase { func testCache() { assertNoError { + let dbPool = try makeDatabasePool() + try dbPool.write { db in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") } diff --git a/Tests/Public/Core/DatabasePool/DatabasePoolCollationTests.swift b/Tests/Public/Core/DatabasePool/DatabasePoolCollationTests.swift index 660e06707..a738c2495 100644 --- a/Tests/Public/Core/DatabasePool/DatabasePoolCollationTests.swift +++ b/Tests/Public/Core/DatabasePool/DatabasePoolCollationTests.swift @@ -5,6 +5,8 @@ class DatabasePoolCollationTests: GRDBTestCase { func testCollationIsSharedBetweenWriterAndReaders() { assertNoError { + let dbPool = try makeDatabasePool() + let collation1 = DatabaseCollation("collation1") { (string1, string2) in return (string1 == string2) ? .OrderedSame : ((string1 < string2) ? .OrderedAscending : .OrderedDescending) } diff --git a/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift b/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift index c55588759..ac2706b0f 100644 --- a/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift +++ b/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift @@ -5,6 +5,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { func testUnwrappedReadWrite() { assertNoError { + let dbPool = try makeDatabasePool() try dbPool.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") try dbPool.execute("INSERT INTO items (id) VALUES (NULL)") let id = Int.fetchOne(dbPool, "SELECT id FROM items")! @@ -14,6 +15,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { func testWrappedReadWrite() { assertNoError { + let dbPool = try makeDatabasePool() try dbPool.write { db in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") try db.execute("INSERT INTO items (id) VALUES (NULL)") @@ -27,6 +29,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { func testConcurrentRead() { assertNoError { + let dbPool = try makeDatabasePool() try dbPool.write { db in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") for _ in 0..<3 { @@ -47,7 +50,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { // end let block1 = { () in - self.dbPool.read { db in + dbPool.read { db in let generator = Row.fetch(db, "SELECT * FROM items").generate() XCTAssertTrue(generator.next() != nil) dispatch_semaphore_signal(s1) @@ -58,7 +61,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { } } let block2 = { () in - self.dbPool.read { db in + dbPool.read { db in let generator = Row.fetch(db, "SELECT * FROM items").generate() XCTAssertTrue(generator.next() != nil) dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) @@ -77,6 +80,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { func testReadMethodIsolationOfStatement() { assertNoError { + let dbPool = try makeDatabasePool() try dbPool.write { db in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") for _ in 0..<2 { @@ -96,7 +100,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { // end let block1 = { () in - self.dbPool.read { db in + dbPool.read { db in let generator = Row.fetch(db, "SELECT * FROM items").generate() XCTAssertTrue(generator.next() != nil) dispatch_semaphore_signal(s1) @@ -109,7 +113,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { do { dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) defer { dispatch_semaphore_signal(s2) } - try self.dbPool.write { db in + try dbPool.write { db in try db.execute("DELETE FROM items") } } catch { @@ -125,6 +129,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { func testReadMethodIsolationOfStatementWithCheckpoint() { assertNoError { + let dbPool = try makeDatabasePool() try dbPool.write { db in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") for _ in 0..<2 { @@ -145,7 +150,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { // end let block1 = { () in - self.dbPool.read { db in + dbPool.read { db in let generator = Row.fetch(db, "SELECT * FROM items").generate() XCTAssertTrue(generator.next() != nil) dispatch_semaphore_signal(s1) @@ -158,10 +163,10 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { do { dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) defer { dispatch_semaphore_signal(s2) } - try self.dbPool.write { db in + try dbPool.write { db in try db.execute("DELETE FROM items") } - try self.dbPool.checkpoint() + try dbPool.checkpoint() } catch { XCTFail("error: \(error)") } @@ -175,6 +180,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { func testReadMethodIsolationOfBlock() { assertNoError { + let dbPool = try makeDatabasePool() try dbPool.write { db in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") } @@ -190,7 +196,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { // SELECT COUNT(*) FROM items let block1 = { () in - self.dbPool.read { db in + dbPool.read { db in XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM items")!, 0) dispatch_semaphore_signal(s1) dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER) @@ -201,10 +207,10 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { do { dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) defer { dispatch_semaphore_signal(s2) } - try self.dbPool.write { db in + try dbPool.write { db in try db.execute("INSERT INTO items (id) VALUES (NULL)") } - try self.dbPool.checkpoint() + try dbPool.checkpoint() } catch { XCTFail("error: \(error)") } @@ -218,6 +224,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { func testNonIsolatedReadMethodIsolationOfStatement() { assertNoError { + let dbPool = try makeDatabasePool() try dbPool.write { db in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") for _ in 0..<2 { @@ -238,7 +245,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { // SELECT COUNT(*) FROM items -> 0 let block1 = { () in - self.dbPool.nonIsolatedRead { db in + dbPool.nonIsolatedRead { db in let generator = Row.fetch(db, "SELECT * FROM items").generate() XCTAssertTrue(generator.next() != nil) dispatch_semaphore_signal(s1) @@ -252,7 +259,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { do { dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) defer { dispatch_semaphore_signal(s2) } - try self.dbPool.write { db in + try dbPool.write { db in try db.execute("DELETE FROM items") } } catch { @@ -268,6 +275,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { func testNonIsolatedReadMethodIsolationOfStatementWithCheckpoint() { assertNoError { + let dbPool = try makeDatabasePool() try dbPool.write { db in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") for _ in 0..<2 { @@ -288,7 +296,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { // end let block1 = { () in - self.dbPool.nonIsolatedRead { db in + dbPool.nonIsolatedRead { db in let generator = Row.fetch(db, "SELECT * FROM items").generate() XCTAssertTrue(generator.next() != nil) dispatch_semaphore_signal(s1) @@ -301,10 +309,10 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { do { dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) defer { dispatch_semaphore_signal(s2) } - try self.dbPool.write { db in + try dbPool.write { db in try db.execute("DELETE FROM items") } - try self.dbPool.checkpoint() + try dbPool.checkpoint() } catch { XCTFail("error: \(error)") } @@ -318,6 +326,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { func testNonIsolatedReadMethodIsolationOfBlock() { assertNoError { + let dbPool = try makeDatabasePool() try dbPool.write { db in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") } @@ -333,7 +342,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { // SELECT COUNT(*) FROM items let block1 = { () in - self.dbPool.nonIsolatedRead { db in + dbPool.nonIsolatedRead { db in XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM items")!, 0) dispatch_semaphore_signal(s1) dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER) @@ -344,10 +353,10 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { do { dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) defer { dispatch_semaphore_signal(s2) } - try self.dbPool.write { db in + try dbPool.write { db in try db.execute("INSERT INTO items (id) VALUES (NULL)") } - try self.dbPool.checkpoint() + try dbPool.checkpoint() } catch { XCTFail("error: \(error)") } diff --git a/Tests/Public/Core/DatabasePool/DatabasePoolFileAttributesTests.swift b/Tests/Public/Core/DatabasePool/DatabasePoolFileAttributesTests.swift index 0773fc302..b171d1557 100644 --- a/Tests/Public/Core/DatabasePool/DatabasePoolFileAttributesTests.swift +++ b/Tests/Public/Core/DatabasePool/DatabasePoolFileAttributesTests.swift @@ -5,14 +5,15 @@ class DatabasePoolFileAttributesTests: GRDBTestCase { func testDefaultFileAttributes() { assertNoError { + let dbPool = try makeDatabasePool() let fm = NSFileManager.defaultManager() try dbPool.execute("CREATE TABLE foo (bar INTEGER)") - var attributes = try fm.attributesOfItemAtPath(dbPoolPath) + var attributes = try fm.attributesOfItemAtPath(dbPool.path) XCTAssertFalse((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) - attributes = try fm.attributesOfItemAtPath(dbPoolPath + "-wal") + attributes = try fm.attributesOfItemAtPath(dbPool.path + "-wal") XCTAssertFalse((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) - attributes = try fm.attributesOfItemAtPath(dbPoolPath + "-shm") + attributes = try fm.attributesOfItemAtPath(dbPool.path + "-shm") XCTAssertFalse((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) } } @@ -21,23 +22,27 @@ class DatabasePoolFileAttributesTests: GRDBTestCase { assertNoError { let fm = NSFileManager.defaultManager() - try dbPool.execute("CREATE TABLE foo (bar INTEGER)") - var attributes = try fm.attributesOfItemAtPath(dbPoolPath) - XCTAssertFalse((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) - attributes = try fm.attributesOfItemAtPath(dbPoolPath + "-wal") - XCTAssertFalse((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) - attributes = try fm.attributesOfItemAtPath(dbPoolPath + "-shm") - XCTAssertFalse((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) - dbPool = nil + do { + let dbPool = try makeDatabasePool() + try dbPool.execute("CREATE TABLE foo (bar INTEGER)") + var attributes = try fm.attributesOfItemAtPath(dbPool.path) + XCTAssertFalse((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) + attributes = try fm.attributesOfItemAtPath(dbPool.path + "-wal") + XCTAssertFalse((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) + attributes = try fm.attributesOfItemAtPath(dbPool.path + "-shm") + XCTAssertFalse((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) + } - dbConfiguration.fileAttributes = [NSFileExtensionHidden: true] - _ = dbPool - attributes = try fm.attributesOfItemAtPath(dbPoolPath) - XCTAssertTrue((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) - attributes = try fm.attributesOfItemAtPath(dbPoolPath + "-wal") - XCTAssertTrue((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) - attributes = try fm.attributesOfItemAtPath(dbPoolPath + "-shm") - XCTAssertTrue((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) + do { + dbConfiguration.fileAttributes = [NSFileExtensionHidden: true] + let dbPool = try makeDatabasePool() + var attributes = try fm.attributesOfItemAtPath(dbPool.path) + XCTAssertTrue((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) + attributes = try fm.attributesOfItemAtPath(dbPool.path + "-wal") + XCTAssertTrue((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) + attributes = try fm.attributesOfItemAtPath(dbPool.path + "-shm") + XCTAssertTrue((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) + } } } @@ -46,15 +51,16 @@ class DatabasePoolFileAttributesTests: GRDBTestCase { let fm = NSFileManager.defaultManager() dbConfiguration.fileAttributes = [NSFileExtensionHidden: true] + let dbPool = try makeDatabasePool() try dbPool.execute("CREATE TABLE foo (bar INTEGER)") // TODO: this test is fragile: we have to wait until the database // store has been notified of file creation. NSThread.sleepForTimeInterval(0.1) - var attributes = try fm.attributesOfItemAtPath(dbPoolPath) + var attributes = try fm.attributesOfItemAtPath(dbPool.path) XCTAssertTrue((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) - attributes = try fm.attributesOfItemAtPath(dbPoolPath + "-wal") + attributes = try fm.attributesOfItemAtPath(dbPool.path + "-wal") XCTAssertTrue((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) - attributes = try fm.attributesOfItemAtPath(dbPoolPath + "-shm") + attributes = try fm.attributesOfItemAtPath(dbPool.path + "-shm") XCTAssertTrue((attributes[NSFileExtensionHidden] as! NSNumber).boolValue) } } diff --git a/Tests/Public/Core/DatabasePool/DatabasePoolFunctionTests.swift b/Tests/Public/Core/DatabasePool/DatabasePoolFunctionTests.swift index 3149d01d1..c04c869b5 100644 --- a/Tests/Public/Core/DatabasePool/DatabasePoolFunctionTests.swift +++ b/Tests/Public/Core/DatabasePool/DatabasePoolFunctionTests.swift @@ -5,6 +5,8 @@ class DatabasePoolFunctionTests: GRDBTestCase { func testFunctionIsSharedBetweenWriterAndReaders() { assertNoError { + let dbPool = try makeDatabasePool() + let function1 = DatabaseFunction("function1", argumentCount: 1, pure: true) { (databaseValues: [DatabaseValue]) in return databaseValues[0] } diff --git a/Tests/Public/Core/DatabasePool/DatabasePoolReadOnlyTests.swift b/Tests/Public/Core/DatabasePool/DatabasePoolReadOnlyTests.swift index 0faf43597..64954647b 100644 --- a/Tests/Public/Core/DatabasePool/DatabasePoolReadOnlyTests.swift +++ b/Tests/Public/Core/DatabasePool/DatabasePoolReadOnlyTests.swift @@ -13,11 +13,11 @@ class DatabasePoolReadOnlyTests: GRDBTestCase { func testConcurrentRead() { assertNoError { + let databaseFileName = "db.sqlite" + // Create a non-WAL database: - let dbQueuePath: String do { - let dbQueue = try makeDatabaseQueue() - dbQueuePath = dbQueue.path + let dbQueue = try makeDatabaseQueue(databaseFileName) try dbQueue.inDatabase { db in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") for _ in 0..<3 { @@ -27,9 +27,8 @@ class DatabasePoolReadOnlyTests: GRDBTestCase { } // Open a read-only pool on that database: - var configuration = Configuration() - configuration.readonly = true - dbPool = try! DatabasePool(path: dbQueuePath, configuration: configuration) + dbConfiguration.readonly = true + let dbPool = try makeDatabasePool(databaseFileName) // Make sure the database is not in WAL mode let mode = String.fetchOne(dbPool, "PRAGMA journal_mode")! @@ -48,7 +47,7 @@ class DatabasePoolReadOnlyTests: GRDBTestCase { // end let block1 = { () in - self.dbPool.read { db in + dbPool.read { db in let generator = Row.fetch(db, "SELECT * FROM items").generate() XCTAssertTrue(generator.next() != nil) dispatch_semaphore_signal(s1) @@ -59,7 +58,7 @@ class DatabasePoolReadOnlyTests: GRDBTestCase { } } let block2 = { () in - self.dbPool.read { db in + dbPool.read { db in let generator = Row.fetch(db, "SELECT * FROM items").generate() XCTAssertTrue(generator.next() != nil) dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) diff --git a/Tests/Public/Core/DatabaseReader/DatabaseReaderTests.swift b/Tests/Public/Core/DatabaseReader/DatabaseReaderTests.swift index 30860bdea..33cda9feb 100644 --- a/Tests/Public/Core/DatabaseReader/DatabaseReaderTests.swift +++ b/Tests/Public/Core/DatabaseReader/DatabaseReaderTests.swift @@ -73,6 +73,7 @@ class DatabaseReaderTests : GRDBTestCase { func testDatabaseValueConvertibleFetch() { assertNoError { let dbQueue = try makeDatabaseQueue("DatabaseQueue.swift") + let dbPool = try makeDatabasePool("DatabasePool.swift") XCTAssertEqual(WrappedInt.fetchOne(dbQueue, "SELECT 123")!.int, 123) XCTAssertEqual(WrappedInt.fetchAll(dbQueue, "SELECT 123 UNION SELECT 321").map { $0.int }, [123, 321]) @@ -90,6 +91,7 @@ class DatabaseReaderTests : GRDBTestCase { func testStatementColumnConvertibleFetch() { assertNoError { let dbQueue = try makeDatabaseQueue("DatabaseQueue.swift") + let dbPool = try makeDatabasePool("DatabasePool.swift") XCTAssertEqual(FastWrappedInt.fetchOne(dbQueue, "SELECT 123")!.int, 123) XCTAssertEqual(FastWrappedInt.fetchAll(dbQueue, "SELECT 123 UNION SELECT 321").map { $0.int }, [123, 321]) @@ -107,6 +109,7 @@ class DatabaseReaderTests : GRDBTestCase { func testRowConvertibleFetch() { assertNoError { let dbQueue = try makeDatabaseQueue("DatabaseQueue.swift") + let dbPool = try makeDatabasePool("DatabasePool.swift") XCTAssertEqual(Item.fetchOne(dbQueue, "SELECT 123 AS id")!.id, 123) XCTAssertEqual(Item.fetchAll(dbQueue, "SELECT 123 AS id UNION SELECT 321").map { $0.id }, [123, 321]) @@ -124,6 +127,7 @@ class DatabaseReaderTests : GRDBTestCase { func testTableMappingFetch() { assertNoError { let dbQueue = try makeDatabaseQueue("DatabaseQueue.swift") + let dbPool = try makeDatabasePool("DatabasePool.swift") let populateDatabase = { (db: Database) -> () in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") @@ -151,6 +155,7 @@ class DatabaseReaderTests : GRDBTestCase { func testFetchRequestFetch() { assertNoError { let dbQueue = try makeDatabaseQueue("DatabaseQueue.swift") + let dbPool = try makeDatabasePool("DatabasePool.swift") let populateDatabase = { (db: Database) -> () in try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") diff --git a/Tests/Public/Core/Persistable/DatabasePoolMutablePersistableTests.swift b/Tests/Public/Core/Persistable/DatabasePoolMutablePersistableTests.swift index a2d273eec..ed79ea33d 100644 --- a/Tests/Public/Core/Persistable/DatabasePoolMutablePersistableTests.swift +++ b/Tests/Public/Core/Persistable/DatabasePoolMutablePersistableTests.swift @@ -86,9 +86,7 @@ private struct MutablePersistableCustomizedCountry : MutablePersistable { class DatabasePoolMutablePersistableTests: GRDBTestCase { - override func setUp() { - super.setUp() - + override func setUpDatabase(dbWriter: DatabaseWriter) throws { var migrator = DatabaseMigrator() migrator.registerMigration("setUp") { db in try db.execute( @@ -102,10 +100,7 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { "name TEXT NOT NULL " + ")") } - - assertNoError { - try migrator.migrate(dbPool) - } + try migrator.migrate(dbWriter) } @@ -113,6 +108,8 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { func testInsertMutablePersistablePerson() { assertNoError { + let dbPool = try makeDatabasePool() + var person = MutablePersistablePerson(id: nil, name: "Arthur") try person.insert(dbPool) @@ -125,6 +122,8 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { func testUpdateMutablePersistablePerson() { assertNoError { + let dbPool = try makeDatabasePool() + var person1 = MutablePersistablePerson(id: nil, name: "Arthur") try person1.insert(dbPool) var person2 = MutablePersistablePerson(id: nil, name: "Barbara") @@ -144,6 +143,8 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { func testDeleteMutablePersistablePerson() { assertNoError { + let dbPool = try makeDatabasePool() + var person1 = MutablePersistablePerson(id: nil, name: "Arthur") try person1.insert(dbPool) var person2 = MutablePersistablePerson(id: nil, name: "Barbara") @@ -160,6 +161,8 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { func testExistsMutablePersistablePerson() { assertNoError { + let dbPool = try makeDatabasePool() + var person = MutablePersistablePerson(id: nil, name: "Arthur") try person.insert(dbPool) XCTAssertTrue(person.exists(dbPool)) @@ -175,6 +178,8 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { func testInsertMutablePersistableCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var country = MutablePersistableCountry(rowID: nil, isoCode: "FR", name: "France") try country.insert(dbPool) @@ -187,6 +192,8 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { func testUpdateMutablePersistableCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var country1 = MutablePersistableCountry(rowID: nil, isoCode: "FR", name: "France") try country1.insert(dbPool) var country2 = MutablePersistableCountry(rowID: nil, isoCode: "US", name: "United States") @@ -206,6 +213,8 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { func testDeleteMutablePersistableCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var country1 = MutablePersistableCountry(rowID: nil, isoCode: "FR", name: "France") try country1.insert(dbPool) var country2 = MutablePersistableCountry(rowID: nil, isoCode: "US", name: "United States") @@ -222,6 +231,8 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { func testExistsMutablePersistableCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var country = MutablePersistableCountry(rowID: nil, isoCode: "FR", name: "France") try country.insert(dbPool) XCTAssertTrue(country.exists(dbPool)) @@ -237,6 +248,8 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { func testInsertMutablePersistableCustomizedCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var insertCount: Int = 0 var updateCount: Int = 0 var saveCount: Int = 0 @@ -268,6 +281,8 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { func testUpdateMutablePersistableCustomizedCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var insertCount: Int = 0 var updateCount: Int = 0 var saveCount: Int = 0 @@ -314,6 +329,8 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { func testDeleteMutablePersistableCustomizedCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var insertCount: Int = 0 var updateCount: Int = 0 var saveCount: Int = 0 @@ -357,6 +374,8 @@ class DatabasePoolMutablePersistableTests: GRDBTestCase { func testExistsMutablePersistableCustomizedCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var insertCount: Int = 0 var updateCount: Int = 0 var saveCount: Int = 0 diff --git a/Tests/Public/Core/Persistable/DatabasePoolPersistableTests.swift b/Tests/Public/Core/Persistable/DatabasePoolPersistableTests.swift index 17f00345a..46dc3fd30 100644 --- a/Tests/Public/Core/Persistable/DatabasePoolPersistableTests.swift +++ b/Tests/Public/Core/Persistable/DatabasePoolPersistableTests.swift @@ -96,9 +96,7 @@ private struct PersistableCustomizedCountry : Persistable { class DatabasePoolPersistableTests: GRDBTestCase { - override func setUp() { - super.setUp() - + override func setUpDatabase(dbWriter: DatabaseWriter) throws { var migrator = DatabaseMigrator() migrator.registerMigration("setUp") { db in try db.execute( @@ -113,10 +111,7 @@ class DatabasePoolPersistableTests: GRDBTestCase { "name TEXT NOT NULL " + ")") } - - assertNoError { - try migrator.migrate(dbPool) - } + try migrator.migrate(dbWriter) } @@ -124,6 +119,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testInsertPersistablePerson() { assertNoError { + let dbPool = try makeDatabasePool() + let person = PersistablePerson(name: "Arthur", age: 42) try person.insert(dbPool) @@ -138,6 +135,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testInsertPersistablePersonClass() { assertNoError { + let dbPool = try makeDatabasePool() + let person = PersistablePersonClass(id: nil, name: "Arthur", age: 42) try person.insert(dbPool) @@ -150,6 +149,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testUpdatePersistablePersonClass() { assertNoError { + let dbPool = try makeDatabasePool() + let person1 = PersistablePersonClass(id: nil, name: "Arthur", age: 42) try person1.insert(dbPool) let person2 = PersistablePersonClass(id: nil, name: "Barbara", age: 39) @@ -169,6 +170,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testDeletePersistablePersonClass() { assertNoError { + let dbPool = try makeDatabasePool() + let person1 = PersistablePersonClass(id: nil, name: "Arthur", age: 42) try person1.insert(dbPool) let person2 = PersistablePersonClass(id: nil, name: "Barbara", age: 39) @@ -185,6 +188,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testExistsPersistablePersonClass() { assertNoError { + let dbPool = try makeDatabasePool() + let person = PersistablePersonClass(id: nil, name: "Arthur", age: 42) try person.insert(dbPool) XCTAssertTrue(person.exists(dbPool)) @@ -200,6 +205,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testInsertPersistableCountry() { assertNoError { + let dbPool = try makeDatabasePool() + let country = PersistableCountry(isoCode: "FR", name: "France") try country.insert(dbPool) @@ -212,6 +219,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testUpdatePersistableCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var country1 = PersistableCountry(isoCode: "FR", name: "France") try country1.insert(dbPool) let country2 = PersistableCountry(isoCode: "US", name: "United States") @@ -231,6 +240,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testDeletePersistableCountry() { assertNoError { + let dbPool = try makeDatabasePool() + let country1 = PersistableCountry(isoCode: "FR", name: "France") try country1.insert(dbPool) let country2 = PersistableCountry(isoCode: "US", name: "United States") @@ -247,6 +258,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testExistsPersistableCountry() { assertNoError { + let dbPool = try makeDatabasePool() + let country = PersistableCountry(isoCode: "FR", name: "France") try country.insert(dbPool) XCTAssertTrue(country.exists(dbPool)) @@ -262,6 +275,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testInsertPersistableCustomizedCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var insertCount: Int = 0 var updateCount: Int = 0 var saveCount: Int = 0 @@ -292,6 +307,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testUpdatePersistableCustomizedCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var insertCount: Int = 0 var updateCount: Int = 0 var saveCount: Int = 0 @@ -336,6 +353,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testDeletePersistableCustomizedCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var insertCount: Int = 0 var updateCount: Int = 0 var saveCount: Int = 0 @@ -377,6 +396,8 @@ class DatabasePoolPersistableTests: GRDBTestCase { func testExistsPersistableCustomizedCountry() { assertNoError { + let dbPool = try makeDatabasePool() + var insertCount: Int = 0 var updateCount: Int = 0 var saveCount: Int = 0 diff --git a/Tests/Public/Migrations/DatabaseMigratorTests.swift b/Tests/Public/Migrations/DatabaseMigratorTests.swift index fbb0be1e9..c367a78bb 100644 --- a/Tests/Public/Migrations/DatabaseMigratorTests.swift +++ b/Tests/Public/Migrations/DatabaseMigratorTests.swift @@ -3,7 +3,7 @@ import GRDB class DatabaseMigratorTests : GRDBTestCase { - func testMigrator() { + func testMigratorDatabaseQueue() { assertNoError { let dbQueue = try makeDatabaseQueue() @@ -41,6 +41,41 @@ class DatabaseMigratorTests : GRDBTestCase { XCTAssertTrue(db.tableExists("persons")) XCTAssertFalse(db.tableExists("pets")) } + } + } + + func testMigratorDatabasePool() { + assertNoError { + let dbPool = try makeDatabasePool() + + var migrator = DatabaseMigrator() + migrator.registerMigration("createPersons") { db in + try db.execute( + "CREATE TABLE persons (" + + "id INTEGER PRIMARY KEY, " + + "name TEXT" + + ")") + } + migrator.registerMigration("createPets") { db in + try db.execute( + "CREATE TABLE pets (" + + "id INTEGER PRIMARY KEY, " + + "masterID INTEGER NOT NULL " + + " REFERENCES persons(id) " + + " ON DELETE CASCADE ON UPDATE CASCADE, " + + "name TEXT" + + ")") + } + + try migrator.migrate(dbPool) + dbPool.read { db in + XCTAssertTrue(db.tableExists("persons")) + XCTAssertTrue(db.tableExists("pets")) + } + + migrator.registerMigration("destroyPersons") { db in + try db.execute("DROP TABLE pets") + } try migrator.migrate(dbPool) dbPool.read { db in