From c4e04d6e074f16032e1988355d1fdcfd23a57100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 16 Dec 2016 19:46:46 +0100 Subject: [PATCH] Tests for FetchedRecordsController error handling --- TODO.md | 2 +- .../FetchedRecordsControllerTests.swift | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 85c518945..4165c5b6c 100644 --- a/TODO.md +++ b/TODO.md @@ -6,7 +6,7 @@ - [ ] Request.bound(to:adapter:) (requires adapters to be able to adapt an already adapted row) - [ ] Think about supporting Cursor's underestimatedCount, which could speed up Array(cursor) and fetchAll() -- [ ] FetchedRecordsController: handle fetch errors +- [ ] FetchedRecordsController: document how to handle errors - [ ] Swift 3.0.2 (Xcode 8.2): "Type inference will properly unwrap optionals when used with generics and implicitly-unwrapped optionals." Maybe this fixes `row.value(named: "foo") as? Int`? - [ ] Attach databases (this could be the support for fetched records controller caches). Interesting question: what happens when one attaches a non-WAL db to a databasePool? - [ ] SQLCipher: sqlite3_rekey is discouraged (https://github.com/ccgus/fmdb/issues/547#issuecomment-259219320) diff --git a/Tests/Public/FetchedRecordsController/FetchedRecordsControllerTests.swift b/Tests/Public/FetchedRecordsController/FetchedRecordsControllerTests.swift index a555379d9..239948ff3 100644 --- a/Tests/Public/FetchedRecordsController/FetchedRecordsControllerTests.swift +++ b/Tests/Public/FetchedRecordsController/FetchedRecordsControllerTests.swift @@ -591,6 +591,37 @@ class FetchedRecordsControllerTests: GRDBTestCase { XCTAssertEqual(recorder.countAfterChanges!, 2) } } + + func testFetchErrors() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + let controller = try FetchedRecordsController(dbQueue, request: Person.all()) + + let expectation = self.expectation(description: "expectation") + var error: Error? + controller.trackErrors { + error = $1 + expectation.fulfill() + } + controller.trackChanges { _ in } + try controller.performFetch() + try dbQueue.inTransaction { db in + try synchronizePersons(db, [ + Person(id: 1, name: "Arthur")]) + try db.drop(table: "persons") + return .commit + } + waitForExpectations(timeout: 1, handler: nil) + if let error = error as? DatabaseError { + XCTAssertEqual(error.code, 1) // SQLITE_ERROR + XCTAssertEqual(error.message, "no such table: persons") + XCTAssertEqual(error.sql!, "SELECT * FROM \"persons\"") + XCTAssertEqual(error.description, "SQLite error 1 with statement `SELECT * FROM \"persons\"`: no such table: persons") + } else { + XCTFail("Expected DatabaseError") + } + } + } }