Created
May 15, 2016 21:24
-
-
Save davetroy/e05f0d20bc950b8c18cfdc886d6c0429 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
import TLIndexPathTools | |
@testable import NewApp | |
class TLToolsTests: CoreDataTestCase, TLIndexPathControllerDelegate { | |
var expectedProperties: [String:AnyObject]? = nil | |
var changeExpectation: XCTestExpectation? | |
var deleteExpectation: XCTestExpectation? | |
var insertExpectation: XCTestExpectation? | |
var indexPathController: TLIndexPathController! | |
func updateAndWait(object: NSManagedObject, changes: [String:AnyObject]) { | |
object.setValuesForKeysWithDictionary(changes) | |
object.willAccessValueForKey(nil) | |
self.expectedProperties = changes | |
self.changeExpectation = expectationWithDescription("properties are correct") | |
NSLog("-- waiting for update to '\(changes)' --") | |
waitForExpectationsWithTimeout(1, handler: nil) | |
} | |
override func setUp() { | |
super.setUp() | |
let request = Address.fetch(predicate: nil, sortAttribute: "messageCount", ascending: true) | |
indexPathController = TLIndexPathController(fetchRequest: request, managedObjectContext: mainContext, sectionNameKeyPath: nil, identifierKeyPath: nil, cacheName: nil) | |
indexPathController.delegate = self | |
indexPathController.ignoreFetchedResultsChanges = false | |
indexPathController.ignoreDataModelChanges = false | |
try! indexPathController.performFetch() | |
} | |
var itemCount: Int { return indexPathController.dataModel!.numberOfRowsInSection(0) } | |
func testIndexPathController() { | |
// check counts | |
XCTAssertEqual(itemCount, 0) | |
// insert | |
let address = Address.createEntity(inContext: mainContext) as! Address | |
address.mailbox = "[email protected]" | |
address.messageCount = 0 | |
insertExpectation = expectationWithDescription("item is inserted") | |
try! mainContext.save() | |
waitForExpectationsWithTimeout(1, handler: nil) | |
XCTAssertEqual(itemCount, 1) | |
// updates | |
updateAndWait(address, changes: ["messageCount" : 10]) | |
updateAndWait(address, changes: ["messageCount" : 12]) | |
updateAndWait(address, changes: ["isContact" : true]) | |
updateAndWait(address, changes: ["messageCount" : 119]) | |
updateAndWait(address, changes: ["messageCount" : 103]) | |
updateAndWait(address, changes: ["mailbox" : "[email protected]", "isContact" : false]) | |
// delete | |
deleteExpectation = expectationWithDescription("item is deleted") | |
mainContext.deleteObject(address) | |
waitForExpectationsWithTimeout(1, handler: nil) | |
// check counts | |
XCTAssertEqual(itemCount, 0) | |
} | |
func controller(controller: TLIndexPathController, didUpdateDataModel updates: TLIndexPathUpdates) { | |
NSLog("updateModifiedItems: \(updates.updateModifiedItems)") | |
NSLog("has changes: \(updates.hasChanges)") | |
NSLog("inserted: \(updates.insertedItems)") | |
NSLog("modified: \(updates.modifiedItems)") | |
NSLog("deleted: \(updates.deletedItems)") | |
NSLog("moved: \(updates.movedItems)") | |
if updates.insertedItems.count > 0 { | |
insertExpectation?.fulfill() | |
} | |
if updates.modifiedItems.count > 0 { | |
changeExpectation?.fulfill() | |
} | |
if updates.movedItems.count > 0 { | |
changeExpectation?.fulfill() | |
} | |
if updates.deletedItems.count > 0 { | |
deleteExpectation?.fulfill() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment