Skip to content

Instantly share code, notes, and snippets.

@davetroy
Created May 15, 2016 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davetroy/e05f0d20bc950b8c18cfdc886d6c0429 to your computer and use it in GitHub Desktop.
Save davetroy/e05f0d20bc950b8c18cfdc886d6c0429 to your computer and use it in GitHub Desktop.
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 = "test@user.com"
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" : "bobby@testuser.com", "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