From fbb40e1232f4b10bd5e61bdb4f7a7bbd8537981c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 11 May 2016 10:45:48 +0200 Subject: [PATCH] FetchedRecordsController: tests that trackChanges { ... } trailing closure is invoked after records have been changed. --- .../FetchedRecordsControllerOSXTests.swift | 22 +++++++++++++++++++ .../FetchedRecordsControlleriOSTests.swift | 22 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/Tests/Public/FetchedRecordsController/FetchedRecordsControllerOSXTests.swift b/Tests/Public/FetchedRecordsController/FetchedRecordsControllerOSXTests.swift index 3b9325f11..b156f0755 100644 --- a/Tests/Public/FetchedRecordsController/FetchedRecordsControllerOSXTests.swift +++ b/Tests/Public/FetchedRecordsController/FetchedRecordsControllerOSXTests.swift @@ -435,6 +435,28 @@ class FetchedRecordsControllerOSXTests: GRDBTestCase { XCTAssertEqual(recorder.recordsAfterChanges.map { $0.name }, ["Arthur"]) } } + + func testTrailingClosureCallback() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + let controller = FetchedRecordsController(dbQueue, request: Person.order(SQLColumn("name")), compareRecordsByPrimaryKey: true) + var persons: [Person] = [] + controller.performFetch() + + let expectation = expectationWithDescription("expectation") + controller.trackChanges { + persons = $0.fetchedRecords! + expectation.fulfill() + } + try dbQueue.inTransaction { db in + try synchronizePersons(db, [ + Person(id: 1, name: "Arthur")]) + return .Commit + } + waitForExpectationsWithTimeout(1, handler: nil) + XCTAssertEqual(persons.map { $0.name }, ["Arthur"]) + } + } } diff --git a/Tests/Public/FetchedRecordsController/FetchedRecordsControlleriOSTests.swift b/Tests/Public/FetchedRecordsController/FetchedRecordsControlleriOSTests.swift index 7d5e14d10..0c501e729 100644 --- a/Tests/Public/FetchedRecordsController/FetchedRecordsControlleriOSTests.swift +++ b/Tests/Public/FetchedRecordsController/FetchedRecordsControlleriOSTests.swift @@ -693,6 +693,28 @@ class FetchedRecordsControlleriOSTests: GRDBTestCase { } } } + + func testTrailingClosureCallback() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + let controller = FetchedRecordsController(dbQueue, request: Person.order(SQLColumn("name")), compareRecordsByPrimaryKey: true) + var persons: [Person] = [] + controller.performFetch() + + let expectation = expectationWithDescription("expectation") + controller.trackChanges { + persons = $0.fetchedRecords! + expectation.fulfill() + } + try dbQueue.inTransaction { db in + try synchronizePersons(db, [ + Person(id: 1, name: "Arthur")]) + return .Commit + } + waitForExpectationsWithTimeout(1, handler: nil) + XCTAssertEqual(persons.map { $0.name }, ["Arthur"]) + } + } }