mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
WIP tests
This commit is contained in:
1 parent
32f73fb816
commit
e8d5d6a190
3 files changed
+84
-21
No files matched your search
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// FetchedRecordsControllerTests.swift
|
||||
// GRDB
|
||||
//
|
||||
// Created by Pascal Edmond on 16/12/2015.
|
||||
// Copyright © 2015 Gwendal Roué. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
|
||||
class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createPersons") { db in
|
||||
try db.execute(
|
||||
"CREATE TABLE persons (" +
|
||||
"id INTEGER PRIMARY KEY, " +
|
||||
"firstName TEXT, " +
|
||||
"lastName TEXT " +
|
||||
")")
|
||||
}
|
||||
migrator.registerMigration("addPersons") { db in
|
||||
try Person(firstName: "Arthur", lastName: "Miller").insert(db)
|
||||
try Person(firstName: "Barbara", lastName: "Streisand").insert(db)
|
||||
try Person(firstName: "Cinderella").insert(db)
|
||||
}
|
||||
try! migrator.migrate(dbQueue)
|
||||
}
|
||||
|
||||
func testExample() {
|
||||
// This is an example of a functional test case.
|
||||
// Use XCTAssert and related functions to verify your tests produce the correct results.
|
||||
}
|
||||
|
||||
func testPerformanceExample() {
|
||||
// This is an example of a performance test case.
|
||||
self.measureBlock {
|
||||
// Put the code you want to measure the time of here.
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,6 +7,8 @@
|
||||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
315013971C21BDA400951DE3 /* FetchedRecordsControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 315013961C21BDA400951DE3 /* FetchedRecordsControllerTests.swift */; };
|
||||
315013981C21BDA400951DE3 /* FetchedRecordsControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 315013961C21BDA400951DE3 /* FetchedRecordsControllerTests.swift */; };
|
||||
31AEFE3F1C185E63003DB703 /* FetchedRecordsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31AEFE3E1C185E63003DB703 /* FetchedRecordsController.swift */; };
|
||||
31AEFE421C185E63003DB703 /* FetchedRecordsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31AEFE3E1C185E63003DB703 /* FetchedRecordsController.swift */; };
|
||||
5606AF911C0ED37000D20487 /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 56BB86301BA98AB0001F9168 /* libsqlite3.tbd */; };
|
||||
@@ -299,7 +301,8 @@
|
||||
/* End PBXContainerItemProxy section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
31AEFE3E1C185E63003DB703 /* FetchedRecordsController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchedRecordsController.swift; sourceTree = "<group>"; };
|
||||
315013961C21BDA400951DE3 /* FetchedRecordsControllerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FetchedRecordsControllerTests.swift; path = FetchedRecordsController/FetchedRecordsControllerTests.swift; sourceTree = "<group>"; };
|
||||
31AEFE3E1C185E63003DB703 /* FetchedRecordsController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FetchedRecordsController.swift; path = Record/FetchedRecordsController.swift; sourceTree = "<group>"; };
|
||||
5607EFD21BB8254800605DE3 /* TransactionObserverTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionObserverTests.swift; sourceTree = "<group>"; };
|
||||
560B3FAB1C19E01000C58EC7 /* DatabasePersistableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePersistableTests.swift; sourceTree = "<group>"; };
|
||||
560C97C61BFD0B8400BF8471 /* FunctionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FunctionTests.swift; sourceTree = "<group>"; };
|
||||
@@ -472,6 +475,22 @@
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
315013941C21BCE600951DE3 /* FetchedRecordsController */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
31AEFE3E1C185E63003DB703 /* FetchedRecordsController.swift */,
|
||||
);
|
||||
name = FetchedRecordsController;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
315013951C21BD6200951DE3 /* FetchedRecordsController */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
315013961C21BDA400951DE3 /* FetchedRecordsControllerTests.swift */,
|
||||
);
|
||||
name = FetchedRecordsController;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
5607EFD11BB8253300605DE3 /* TransactionObserver */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -529,6 +548,7 @@
|
||||
56CB92EC1BA453E500DFD5FC /* CrashTests.swift */,
|
||||
56A238111B9C74A90082EB20 /* Core */,
|
||||
56B7F4281BE149F600E39BBF /* CoreGraphics */,
|
||||
315013951C21BD6200951DE3 /* FetchedRecordsController */,
|
||||
56F0B98C1B6001C600A2F135 /* Foundation */,
|
||||
56A238231B9C74A90082EB20 /* Migrations */,
|
||||
56A238251B9C74A90082EB20 /* Record */,
|
||||
@@ -722,7 +742,6 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56A238A11B9C753B0082EB20 /* Record.swift */,
|
||||
31AEFE3E1C185E63003DB703 /* FetchedRecordsController.swift */,
|
||||
);
|
||||
path = Record;
|
||||
sourceTree = "<group>";
|
||||
@@ -901,6 +920,7 @@
|
||||
children = (
|
||||
56A2386F1B9C75030082EB20 /* Core */,
|
||||
56B7F4251BE148FE00E39BBF /* CoreGraphics */,
|
||||
315013941C21BCE600951DE3 /* FetchedRecordsController */,
|
||||
56A238001B9C6A1E0082EB20 /* Foundation */,
|
||||
56A238911B9C750B0082EB20 /* Migrations */,
|
||||
56A2389F1B9C753B0082EB20 /* Record */,
|
||||
@@ -1287,6 +1307,7 @@
|
||||
565B0FF01BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */,
|
||||
56E8CE0E1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */,
|
||||
56A238581B9C74A90082EB20 /* PrimaryKeyRowIDTests.swift in Sources */,
|
||||
315013981C21BDA400951DE3 /* FetchedRecordsControllerTests.swift in Sources */,
|
||||
56A238621B9C74A90082EB20 /* RecordEditedTests.swift in Sources */,
|
||||
56A2385C1B9C74A90082EB20 /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
|
||||
56A238401B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
|
||||
@@ -1345,6 +1366,7 @@
|
||||
56A238611B9C74A90082EB20 /* RecordEditedTests.swift in Sources */,
|
||||
56A2385B1B9C74A90082EB20 /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
|
||||
565B0FEF1BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */,
|
||||
315013971C21BDA400951DE3 /* FetchedRecordsControllerTests.swift in Sources */,
|
||||
56A2383F1B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
|
||||
56E8CE0D1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */,
|
||||
56A2383B1B9C74A90082EB20 /* DatabaseErrorTests.swift in Sources */,
|
||||
|
||||
@@ -100,10 +100,8 @@ public class FetchedRecordsController<T: protocol<RowConvertible, DatabaseTableM
|
||||
if let _ = currentIndexPathForItem[item] {
|
||||
// item was there
|
||||
} else {
|
||||
// Insert
|
||||
guard let newIndexPath = finalIndexPathForItem[item] else {
|
||||
print("WTF?!")
|
||||
break
|
||||
fatalError()
|
||||
}
|
||||
|
||||
let update = FetchedRecordsUpdate.Insert(item: item, at: newIndexPath)
|
||||
@@ -114,37 +112,35 @@ public class FetchedRecordsController<T: protocol<RowConvertible, DatabaseTableM
|
||||
|
||||
// 3 - DELETES, MOVES & RELOAD
|
||||
for oldItem: T in currentRecords {
|
||||
|
||||
guard let oldIndexPath = currentIndexPathForItem[oldItem] else {
|
||||
print("WTF?!")
|
||||
break
|
||||
fatalError()
|
||||
}
|
||||
|
||||
if let index = finalIndexPathForItem.indexForKey(oldItem) {
|
||||
|
||||
let (newItem, newIndexPath) = finalIndexPathForItem[index]
|
||||
|
||||
if oldIndexPath == newIndexPath {
|
||||
|
||||
var changes: [String: (old: DatabaseValue?, new: DatabaseValue)]?
|
||||
|
||||
if let oldRecord = oldItem as? Record, let newRecord = oldItem as? Record {
|
||||
let recordCopy = newRecord.copy()
|
||||
recordCopy.referenceRow = oldRecord.referenceRow
|
||||
changes = recordCopy.databaseChanges
|
||||
let changes = recordCopy.persistentChangedValues
|
||||
if changes.count > 0 {
|
||||
let update = FetchedRecordsUpdate.Update(item: newItem, at: newIndexPath, changes: changes)
|
||||
updates.append(update)
|
||||
apply(update)
|
||||
}
|
||||
} else {
|
||||
// Not a record
|
||||
let update = FetchedRecordsUpdate.Update(item: newItem, at: newIndexPath, changes: nil)
|
||||
updates.append(update)
|
||||
apply(update)
|
||||
}
|
||||
|
||||
// Not a record
|
||||
let update = FetchedRecordsUpdate.Update(item: newItem, at: newIndexPath, changes: changes)
|
||||
updates.append(update)
|
||||
apply(update)
|
||||
|
||||
} else {
|
||||
// item moved
|
||||
let update = FetchedRecordsUpdate.Move(item: newItem, from: oldIndexPath, to: newIndexPath)
|
||||
updates.append(update)
|
||||
apply(update)
|
||||
}
|
||||
|
||||
} else {
|
||||
// item deleted
|
||||
let update = FetchedRecordsUpdate.Delete(item: oldItem, at: oldIndexPath)
|
||||
@@ -205,7 +201,7 @@ public enum FetchedRecordsUpdate<T> {
|
||||
case Insert(item:T, at: NSIndexPath)
|
||||
case Delete(item:T, at: NSIndexPath)
|
||||
case Move(item:T, from: NSIndexPath, to: NSIndexPath)
|
||||
case Update(item:T, at: NSIndexPath, changes: [String: (old: DatabaseValue?, new: DatabaseValue)]?)
|
||||
case Update(item:T, at: NSIndexPath, changes: [String: DatabaseValue?]?)
|
||||
|
||||
var description: String {
|
||||
switch self {
|
||||
|
||||
Reference in new issue
Block a user