// // GRDB.swift // https://github.com/groue/GRDB.swift // Copyright (c) 2015 Gwendal Roué // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. import XCTest import GRDB struct DBDate: SQLiteValueConvertible { // MARK: - DBDate <-> NSDate conversion let date: NSDate // Define a failable initializer in order to consistently use nil as the // NULL marker throughout the conversions NSDate <-> DBDate <-> SQLite init?(_ date: NSDate?) { if let date = date { self.date = date } else { return nil } } // MARK: - DBDate <-> SQLiteValue conversion var sqliteValue: SQLiteValue { return .Real(date.timeIntervalSince1970) } init?(sqliteValue: SQLiteValue) { // Don't handle the raw SQLiteValue unless you know what you do. // It is recommended to use GRDB built-in conversions instead: if let timestamp = Double(sqliteValue: sqliteValue) { self.init(NSDate(timeIntervalSince1970: timestamp)) } else { return nil } } } class GRDBTestCase: XCTestCase { var databasePath: String! var dbQueue: DatabaseQueue! override func setUp() { super.setUp() self.databasePath = "/tmp/GRDB.sqlite" do { try NSFileManager.defaultManager().removeItemAtPath(databasePath) } catch { } let configuration = Configuration(trace: Configuration.logSQL) self.dbQueue = try! DatabaseQueue(path: databasePath, configuration: configuration) } override func tearDown() { super.tearDown() self.dbQueue = nil try! NSFileManager.defaultManager().removeItemAtPath(databasePath) } func assertNoError(@noescape test: (Void) throws -> Void) { do { try test() } catch let error as SQLiteError { fatalError(error.description) } catch { fatalError("error: \(error)") } } }