WIP Tests

This commit is contained in:
Pascal Edmond committed 2015-12-18 13:14:24 +01:00
1 parent cec026495f
commit e43bbe79bd
1 file changed
+90 -8
@@ -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<Person>, rhs: FetchedRecordsUpdate<Person>) -> 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<Person>!
var willUpdateExpectation: XCTestExpectation?
var updates = [FetchedRecordsUpdate<Person>]()
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<T>(controller: FetchedRecordsController<T>) {
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<T>(controller: FetchedRecordsController<T>, update: FetchedRecordsUpdate<T>) {
updates.append(update)
}
func controllerDidFinishUpdates<T>(controller: FetchedRecordsController<T>) {
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)
}
}