mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
CustomDatabaseValueTests
This commit is contained in:
1 parent
8dba912c10
commit
b3cebea0f6
2 files changed
+87
No files matched your search
@@ -0,0 +1,83 @@
|
||||
//
|
||||
// CustomDatabaseValueTests.swift
|
||||
// GRDB
|
||||
//
|
||||
// Created by Gwendal Roué on 02/07/2015.
|
||||
// Copyright © 2015 Gwendal Roué. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
struct DatabaseDate: DatabaseValue {
|
||||
let date: NSDate
|
||||
|
||||
init(_ date: NSDate) {
|
||||
self.date = date
|
||||
}
|
||||
|
||||
func bindInSQLiteStatement(statement: SQLiteStatement, atIndex index:Int) -> Int32 {
|
||||
let timestamp = date.timeIntervalSince1970
|
||||
return timestamp.bindInSQLiteStatement(statement, atIndex: index)
|
||||
}
|
||||
|
||||
static func fromSQLiteValue(value: SQLiteValue) -> DatabaseDate? {
|
||||
switch value {
|
||||
case .Double(let timestamp):
|
||||
return self.init(NSDate(timeIntervalSince1970: timestamp))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class CustomDatabaseValueTests: GRDBTests {
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createPersons") { db in
|
||||
try db.execute(
|
||||
"CREATE TABLE stuffs (" +
|
||||
"ID INTEGER PRIMARY KEY, " +
|
||||
"creationTimestamp DOUBLE" +
|
||||
")")
|
||||
}
|
||||
assertNoError {
|
||||
try migrator.migrate(dbQueue)
|
||||
}
|
||||
}
|
||||
|
||||
func testExample() {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
|
||||
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
|
||||
do {
|
||||
let dateComponents = NSDateComponents()
|
||||
dateComponents.year = 1973
|
||||
dateComponents.month = 09
|
||||
dateComponents.day = 18
|
||||
let date = calendar.dateFromComponents(dateComponents)!
|
||||
try db.execute("INSERT INTO stuffs (creationTimestamp) VALUES (?)", bindings: [DatabaseDate(date)])
|
||||
}
|
||||
|
||||
do {
|
||||
let row = db.fetchOneRow("SELECT creationTimestamp FROM stuffs")!
|
||||
let date: DatabaseDate = row.value(atIndex: 0)!
|
||||
let year = calendar.component(NSCalendarUnit.Year, fromDate: date.date)
|
||||
XCTAssertEqual(year, 1973)
|
||||
}
|
||||
|
||||
do {
|
||||
let date = db.fetchOne(DatabaseDate.self, "SELECT creationTimestamp FROM stuffs")!
|
||||
let year = calendar.component(NSCalendarUnit.Year, fromDate: date.date)
|
||||
XCTAssertEqual(year, 1973)
|
||||
}
|
||||
|
||||
return .Rollback
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
56CC3C991B445DDB00A9A534 /* GRDBTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C981B445DDB00A9A534 /* GRDBTests.swift */; };
|
||||
56CC3C9B1B4464AE00A9A534 /* RowModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C9A1B4464AE00A9A534 /* RowModel.swift */; };
|
||||
56CC3C9D1B44681A00A9A534 /* RowModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C9C1B44681A00A9A534 /* RowModelTests.swift */; };
|
||||
56CC3C9F1B45E84400A9A534 /* CustomDatabaseValueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C9E1B45E84400A9A534 /* CustomDatabaseValueTests.swift */; };
|
||||
56E4BB7F1B452A6200FD324B /* SQLite.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4BB7E1B452A6200FD324B /* SQLite.swift */; };
|
||||
56E4BB811B45373600FD324B /* Bindings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4BB801B45373600FD324B /* Bindings.swift */; };
|
||||
DC2393C81ABE35F8003FF113 /* GRDB-Bridging.h in Headers */ = {isa = PBXBuildFile; fileRef = DC2393C61ABE35F8003FF113 /* GRDB-Bridging.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
@@ -55,6 +56,7 @@
|
||||
56CC3C981B445DDB00A9A534 /* GRDBTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBTests.swift; sourceTree = SOURCE_ROOT; };
|
||||
56CC3C9A1B4464AE00A9A534 /* RowModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModel.swift; sourceTree = SOURCE_ROOT; };
|
||||
56CC3C9C1B44681A00A9A534 /* RowModelTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelTests.swift; sourceTree = SOURCE_ROOT; };
|
||||
56CC3C9E1B45E84400A9A534 /* CustomDatabaseValueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomDatabaseValueTests.swift; sourceTree = SOURCE_ROOT; };
|
||||
56E1524B1B43FD5B00EE27AA /* TODO.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = TODO.md; sourceTree = "<group>"; };
|
||||
56E4BB7E1B452A6200FD324B /* SQLite.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLite.swift; sourceTree = "<group>"; };
|
||||
56E4BB801B45373600FD324B /* Bindings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Bindings.swift; sourceTree = "<group>"; };
|
||||
@@ -99,6 +101,7 @@
|
||||
56CC3C981B445DDB00A9A534 /* GRDBTests.swift */,
|
||||
56CC3C9C1B44681A00A9A534 /* RowModelTests.swift */,
|
||||
DC37740319C8CBB3004FCF85 /* Supporting Files */,
|
||||
56CC3C9E1B45E84400A9A534 /* CustomDatabaseValueTests.swift */,
|
||||
);
|
||||
path = "GRDB Tests";
|
||||
sourceTree = "<group>";
|
||||
@@ -298,6 +301,7 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
56CC3C9F1B45E84400A9A534 /* CustomDatabaseValueTests.swift in Sources */,
|
||||
56CC3C991B445DDB00A9A534 /* GRDBTests.swift in Sources */,
|
||||
56CC3C971B44598200A9A534 /* DatabaseMigratorTests.swift in Sources */,
|
||||
56CC3C9D1B44681A00A9A534 /* RowModelTests.swift in Sources */,
|
||||
|
||||
Reference in new issue
Block a user