From e43bbe79bdc099cfa06ad9915713ce2088ea0332 Mon Sep 17 00:00:00 2001 From: Pascal Edmond Date: Fri, 18 Dec 2015 13:14:24 +0100 Subject: [PATCH] WIP Tests --- .../FetchedRecordsControllerTests.swift | 98 +++++++++++++++++-- 1 file changed, 90 insertions(+), 8 deletions(-) diff --git a/GRDB Tests/Experimental/FetchedRecordsControllerTests.swift b/GRDB Tests/Experimental/FetchedRecordsControllerTests.swift index 16c2dcf9a..e1171aa20 100644 --- a/GRDB Tests/Experimental/FetchedRecordsControllerTests.swift +++ b/GRDB Tests/Experimental/FetchedRecordsControllerTests.swift @@ -1,20 +1,102 @@ import XCTest import GRDB -class FetchedRecordsControllerTests: GRDBTestCase { + +extension Person : Hashable { + + var hashValue: Int { + return self.id.hashValue + } +} + +func ==(lhs: Person, rhs: Person) -> Bool { + return lhs.id == rhs.id +} + +func ==(lhs: FetchedRecordsUpdate, rhs: FetchedRecordsUpdate) -> Bool { + switch (lhs, rhs) { + case (.Inserted(let lhsPerson, let lhsIndexPath), .Inserted(let rhsPerson, let rhsIndexPath)) where lhsPerson == rhsPerson && lhsIndexPath == rhsIndexPath : return true + case (.Deleted(let lhsPerson, let lhsIndexPath), .Deleted(let rhsPerson, let rhsIndexPath)) where lhsPerson == rhsPerson && lhsIndexPath == rhsIndexPath : return true + case (.Moved(let lhsPerson, let lhsFromIndexPath, let lhsToIndexPath), .Moved(let rhsPerson, let rhsFromIndexPath, let rhsToIndexPath)) where lhsPerson == rhsPerson && lhsFromIndexPath == rhsFromIndexPath && lhsToIndexPath == rhsToIndexPath : return true + case (.Updated(let lhsPerson, let lhsIndexPath, _), .Updated(let rhsPerson, let rhsIndexPath, _)) where lhsPerson == rhsPerson && lhsIndexPath == rhsIndexPath : return true + default: return false + } +} + + +class FetchedRecordsControllerTests: GRDBTestCase, FetchedRecordsControllerDelegate { + + var fetchedRecordsController: FetchedRecordsController! + + var willUpdateExpectation: XCTestExpectation? + var updates = [FetchedRecordsUpdate]() + var didFinishUpdatesExpectation: XCTestExpectation? override func setUp() { super.setUp() - // Put setup code here. This method is called before the invocation of each test method in the class. + + var migrator = DatabaseMigrator() + migrator.registerMigration("createPersons", migrate: Person.setupInDatabase) + assertNoError { + try migrator.migrate(dbQueue) + } + + fetchedRecordsController = FetchedRecordsController(sql: "SELECT * FROM persons ORDER BY LOWER(name)", databaseQueue: dbQueue) + fetchedRecordsController.delegate = self + fetchedRecordsController.performFetch() } - override func tearDown() { - // Put teardown code here. This method is called after the invocation of each test method in the class. - super.tearDown() + // MARK: - FetchedRecordsControllerDelegate + + func controllerWillUpdate(controller: FetchedRecordsController) { + willUpdateExpectation?.fulfill() } - 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 controllerUpdate(controller: FetchedRecordsController, update: FetchedRecordsUpdate) { + updates.append(update) + } + + func controllerDidFinishUpdates(controller: FetchedRecordsController) { + didFinishUpdatesExpectation?.fulfill() + } + + // MARK: - Tests + + func testNotificationsOnInsertion() { + + let person = Person() + person.name = "Pascal" + person.age = 27 + + willUpdateExpectation = expectationWithDescription("Did receive controllerWillUpdate:") + didFinishUpdatesExpectation = expectationWithDescription("Did receive controllerDidFinishUpdates:") + + // Save person + assertNoError { + try! dbQueue.inDatabase { db in + try person.save(db) + } + } + + // Did received notifications + waitForExpectationsWithTimeout(2, handler: nil) + } + + func testUpdateOnInsertion() { + + let person = Person() + person.name = "Pascal" + person.age = 27 + + // Save person + assertNoError { + try! dbQueue.inDatabase { db in + try person.save(db) + } + } + + // Update + XCTAssert(updates.count == 1) } } +