From 75258afb6c4392b43affcb729dbb2bb4bf5cf1e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 19 May 2016 08:26:28 +0200 Subject: [PATCH] Issue #50: Tests for FetchedRecordsController(...:adapter:) --- GRDB.xcodeproj/project.pbxproj | 22 +++-- TODO.md | 1 - ...ft => FetchedRecordsControllerTests.swift} | 84 +++++++++++++++++-- 3 files changed, 91 insertions(+), 16 deletions(-) rename Tests/Public/FetchedRecordsController/{FetchedRecordsControllerOSXTests.swift => FetchedRecordsControllerTests.swift} (86%) diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 70fb0cc07..1e09910b9 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -227,9 +227,9 @@ 563445061C4A7FD7003D3DC6 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563445041C4A7FD7003D3DC6 /* Utils.swift */; }; 564A50C71BFF4B7F00B3A3A2 /* CollationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564A50C61BFF4B7F00B3A3A2 /* CollationTests.swift */; }; 564A50C81BFF4B7F00B3A3A2 /* CollationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564A50C61BFF4B7F00B3A3A2 /* CollationTests.swift */; }; - 565049051CE32543000A97D8 /* FetchedRecordsControllerOSXTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565049041CE32543000A97D8 /* FetchedRecordsControllerOSXTests.swift */; }; - 565049061CE32543000A97D8 /* FetchedRecordsControllerOSXTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565049041CE32543000A97D8 /* FetchedRecordsControllerOSXTests.swift */; }; - 565049071CE32543000A97D8 /* FetchedRecordsControllerOSXTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565049041CE32543000A97D8 /* FetchedRecordsControllerOSXTests.swift */; }; + 565049051CE32543000A97D8 /* FetchedRecordsControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565049041CE32543000A97D8 /* FetchedRecordsControllerTests.swift */; }; + 565049061CE32543000A97D8 /* FetchedRecordsControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565049041CE32543000A97D8 /* FetchedRecordsControllerTests.swift */; }; + 565049071CE32543000A97D8 /* FetchedRecordsControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565049041CE32543000A97D8 /* FetchedRecordsControllerTests.swift */; }; 56553C101C3E906C00522B5C /* GRDBTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5623E0901B4AFACC00B20B7F /* GRDBTestCase.swift */; }; 56553C121C3E906C00522B5C /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 56BB86301BA98AB0001F9168 /* libsqlite3.tbd */; }; 56553C131C3E906C00522B5C /* GRDB.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DC3773F319C8CBB3004FCF85 /* GRDB.framework */; }; @@ -337,6 +337,9 @@ 568E1CB21CB037BA008D97A6 /* sqlite3.h in Headers */ = {isa = PBXBuildFile; fileRef = 568BE5191CB035A900270F93 /* sqlite3.h */; settings = {ATTRIBUTES = (Public, ); }; }; 568E1CB91CB03847008D97A6 /* GRDBCipher-Bridging.h in Headers */ = {isa = PBXBuildFile; fileRef = 568E1CB71CB03847008D97A6 /* GRDBCipher-Bridging.h */; settings = {ATTRIBUTES = (Public, ); }; }; 568E1CBA1CB03847008D97A6 /* GRDBCipher.h in Headers */ = {isa = PBXBuildFile; fileRef = 568E1CB81CB03847008D97A6 /* GRDBCipher.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 569178391CED8E0A00E179EA /* FetchedRecordsControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565049041CE32543000A97D8 /* FetchedRecordsControllerTests.swift */; }; + 569178411CED8E0C00E179EA /* FetchedRecordsControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565049041CE32543000A97D8 /* FetchedRecordsControllerTests.swift */; }; + 569178421CED8E0D00E179EA /* FetchedRecordsControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565049041CE32543000A97D8 /* FetchedRecordsControllerTests.swift */; }; 569531091C9067DC00CF1A2B /* DatabaseQueueCrashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569530FB1C9067CC00CF1A2B /* DatabaseQueueCrashTests.swift */; }; 5695310A1C9067DC00CF1A2B /* DatabaseValueConvertibleCrashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569530FC1C9067CC00CF1A2B /* DatabaseValueConvertibleCrashTests.swift */; }; 5695310B1C9067DC00CF1A2B /* GRDBCrashTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569530FD1C9067CC00CF1A2B /* GRDBCrashTestCase.swift */; }; @@ -881,7 +884,7 @@ 563363F81C960711000BE133 /* PerformanceRealmTests.realm */ = {isa = PBXFileReference; lastKnownFileType = file; path = PerformanceRealmTests.realm; sourceTree = ""; }; 563445041C4A7FD7003D3DC6 /* Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = ""; }; 564A50C61BFF4B7F00B3A3A2 /* CollationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollationTests.swift; sourceTree = ""; }; - 565049041CE32543000A97D8 /* FetchedRecordsControllerOSXTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchedRecordsControllerOSXTests.swift; sourceTree = ""; }; + 565049041CE32543000A97D8 /* FetchedRecordsControllerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchedRecordsControllerTests.swift; sourceTree = ""; }; 56553C181C3E906C00522B5C /* GRDBOSXCrashTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBOSXCrashTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 565B0FEE1BBC7D980098DE03 /* RowConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowConvertibleTests.swift; sourceTree = ""; }; 565D5D701BBC694D00DC9BD4 /* Row+FoundationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Row+FoundationTests.swift"; sourceTree = ""; }; @@ -1502,7 +1505,7 @@ isa = PBXGroup; children = ( 56B15D0A1CD4C35100A24C8B /* FetchedRecordsControlleriOSTests.swift */, - 565049041CE32543000A97D8 /* FetchedRecordsControllerOSXTests.swift */, + 565049041CE32543000A97D8 /* FetchedRecordsControllerTests.swift */, ); path = FetchedRecordsController; sourceTree = ""; @@ -2290,7 +2293,7 @@ 560FC5891CB00B880014AA8E /* TransactionObserverTests.swift in Sources */, 560FC58A1CB00B880014AA8E /* DatabasePoolReleaseMemoryTests.swift in Sources */, 560FC58B1CB00B880014AA8E /* DatabaseValueTests.swift in Sources */, - 565049061CE32543000A97D8 /* FetchedRecordsControllerOSXTests.swift in Sources */, + 565049061CE32543000A97D8 /* FetchedRecordsControllerTests.swift in Sources */, 567156171CB142AA007DC145 /* ReadOnlyDatabaseTests.swift in Sources */, 560FC58D1CB00B880014AA8E /* Row+FoundationTests.swift in Sources */, 567156141CB141D0007DC145 /* InMemoryDatabaseTests.swift in Sources */, @@ -2386,7 +2389,7 @@ 567156451CB16729007DC145 /* DatabasePoolConcurrencyTests.swift in Sources */, 567156461CB16729007DC145 /* TransactionObserverTests.swift in Sources */, 567156471CB16729007DC145 /* DatabasePoolReleaseMemoryTests.swift in Sources */, - 565049071CE32543000A97D8 /* FetchedRecordsControllerOSXTests.swift in Sources */, + 565049071CE32543000A97D8 /* FetchedRecordsControllerTests.swift in Sources */, 567156481CB16729007DC145 /* DatabaseValueTests.swift in Sources */, 5671564A1CB16729007DC145 /* ReadOnlyDatabaseTests.swift in Sources */, 5671564B1CB16729007DC145 /* Row+FoundationTests.swift in Sources */, @@ -2515,6 +2518,7 @@ 56AFCA561CB1AA9900F48B96 /* DatabasePoolConcurrencyTests.swift in Sources */, 56AFCA571CB1AA9900F48B96 /* TransactionObserverTests.swift in Sources */, 56AFCA581CB1AA9900F48B96 /* Row+FoundationTests.swift in Sources */, + 569178411CED8E0C00E179EA /* FetchedRecordsControllerTests.swift in Sources */, 56AFCA5A1CB1AA9900F48B96 /* RecordSubClassTests.swift in Sources */, 56AFCA5B1CB1AA9900F48B96 /* DatabaseValueTests.swift in Sources */, 56AFCA5C1CB1AA9900F48B96 /* ReadOnlyDatabaseTests.swift in Sources */, @@ -2588,6 +2592,7 @@ 56AFCAA81CB1ABC800F48B96 /* NSDataMemoryTests.swift in Sources */, 56AFCAA91CB1ABC800F48B96 /* RecordCopyTests.swift in Sources */, 56AFCAAA1CB1ABC800F48B96 /* ManagedDataControllerTests.swift in Sources */, + 569178421CED8E0D00E179EA /* FetchedRecordsControllerTests.swift in Sources */, 56AFCAAB1CB1ABC800F48B96 /* PersistableTests.swift in Sources */, 56AFCAAC1CB1ABC800F48B96 /* DatabaseValueConversionTests.swift in Sources */, 56AFCAAD1CB1ABC800F48B96 /* MappingTests.swift in Sources */, @@ -2725,6 +2730,7 @@ 560A37AC1C90085D00949E71 /* DatabasePoolConcurrencyTests.swift in Sources */, 5607EFD41BB827FD00605DE3 /* TransactionObserverTests.swift in Sources */, 565D5D721BBC694D00DC9BD4 /* Row+FoundationTests.swift in Sources */, + 569178391CED8E0A00E179EA /* FetchedRecordsControllerTests.swift in Sources */, 56A2386A1B9C74A90082EB20 /* RecordSubClassTests.swift in Sources */, 56A2383E1B9C74A90082EB20 /* DatabaseValueTests.swift in Sources */, 567156181CB142AA007DC145 /* ReadOnlyDatabaseTests.swift in Sources */, @@ -2806,7 +2812,7 @@ 563363D01C943D13000BE133 /* DatabasePoolReleaseMemoryTests.swift in Sources */, 56A2383D1B9C74A90082EB20 /* DatabaseValueTests.swift in Sources */, 565D5D711BBC694D00DC9BD4 /* Row+FoundationTests.swift in Sources */, - 565049051CE32543000A97D8 /* FetchedRecordsControllerOSXTests.swift in Sources */, + 565049051CE32543000A97D8 /* FetchedRecordsControllerTests.swift in Sources */, 567156161CB142AA007DC145 /* ReadOnlyDatabaseTests.swift in Sources */, 56A238571B9C74A90082EB20 /* PrimaryKeyRowIDTests.swift in Sources */, 56EA86941C91DFE7002BB4DF /* DatabaseReaderTests.swift in Sources */, diff --git a/TODO.md b/TODO.md index f4d0b4975..970783c41 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ -- [ ] RowAdapter: test FetchedRecordsController(...:adapter:) - [ ] RowAdapter: document awakeFromFetch for properties initialized from subrows (see RowConvertibleTests:Person.awakeFromFetch) - [ ] GRDBCipher: remove limitations on iOS or OS X versions - [ ] FetchedRecordsController: take inspiration from https://github.com/jflinter/Dwifft diff --git a/Tests/Public/FetchedRecordsController/FetchedRecordsControllerOSXTests.swift b/Tests/Public/FetchedRecordsController/FetchedRecordsControllerTests.swift similarity index 86% rename from Tests/Public/FetchedRecordsController/FetchedRecordsControllerOSXTests.swift rename to Tests/Public/FetchedRecordsController/FetchedRecordsControllerTests.swift index 97c88b6c3..1aff0c3b3 100644 --- a/Tests/Public/FetchedRecordsController/FetchedRecordsControllerOSXTests.swift +++ b/Tests/Public/FetchedRecordsController/FetchedRecordsControllerTests.swift @@ -60,7 +60,19 @@ private class Person : Record { } } -class FetchedRecordsControllerOSXTests: GRDBTestCase { +struct Book : RowConvertible { + var id: Int64 + var authorID: Int64 + var title: String + + init(_ row: Row) { + id = row.value(named: "id") + authorID = row.value(named: "authorID") + title = row.value(named: "title") + } +} + +class FetchedRecordsControllerTests: GRDBTestCase { override func setUpDatabase(dbWriter: DatabaseWriter) throws { try dbWriter.write { db in @@ -72,7 +84,7 @@ class FetchedRecordsControllerOSXTests: GRDBTestCase { try db.execute( "CREATE TABLE books (" + "id INTEGER PRIMARY KEY, " + - "ownerId INTEGER NOT NULL REFERENCES persons(id) ON DELETE CASCADE ON UPDATE CASCADE," + + "authorId INTEGER NOT NULL REFERENCES persons(id) ON DELETE CASCADE ON UPDATE CASCADE," + "title TEXT" + ")") try db.execute( @@ -83,6 +95,64 @@ class FetchedRecordsControllerOSXTests: GRDBTestCase { } } + func testControllerFromSQL() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + let authorId: Int64 = try dbQueue.inDatabase { db in + let plato = Person(name: "Plato") + try plato.insert(db) + try db.execute("INSERT INTO books (authorID, title) VALUES (?, ?)", arguments: [plato.id, "Symposium"]) + let cervantes = Person(name: "Cervantes") + try cervantes.insert(db) + try db.execute("INSERT INTO books (authorID, title) VALUES (?, ?)", arguments: [cervantes.id, "Don Quixote"]) + return cervantes.id! + } + + let controller = FetchedRecordsController(dbQueue, sql: "SELECT * FROM books WHERE authorID = ?", arguments: [authorId]) + controller.performFetch() + XCTAssertEqual(controller.fetchedRecords!.count, 1) + XCTAssertEqual(controller.fetchedRecords![0].title, "Don Quixote") + } + } + + func testControllerFromSQLWithAdapter() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + let authorId: Int64 = try dbQueue.inDatabase { db in + let plato = Person(name: "Plato") + try plato.insert(db) + try db.execute("INSERT INTO books (authorID, title) VALUES (?, ?)", arguments: [plato.id, "Symposium"]) + let cervantes = Person(name: "Cervantes") + try cervantes.insert(db) + try db.execute("INSERT INTO books (authorID, title) VALUES (?, ?)", arguments: [cervantes.id, "Don Quixote"]) + return cervantes.id! + } + + let adapter = RowAdapter(mapping: ["id": "_id", "authorId": "_authorId", "title": "_title"]) + let controller = FetchedRecordsController(dbQueue, sql: "SELECT id AS _id, authorId AS _authorId, title AS _title FROM books WHERE authorID = ?", arguments: [authorId], adapter: adapter) + controller.performFetch() + XCTAssertEqual(controller.fetchedRecords!.count, 1) + XCTAssertEqual(controller.fetchedRecords![0].title, "Don Quixote") + } + } + + func testControllerFromFetchRequest() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try Person(name: "Plato").insert(db) + try Person(name: "Cervantes").insert(db) + } + + let request = Person.order(SQLColumn("name")) + let controller = FetchedRecordsController(dbQueue, request: request) + controller.performFetch() + XCTAssertEqual(controller.fetchedRecords!.count, 2) + XCTAssertEqual(controller.fetchedRecords![0].name, "Cervantes") + XCTAssertEqual(controller.fetchedRecords![1].name, "Plato") + } + } + func testRecordsAreNotLoadedUntilPerformFetch() { assertNoError { let dbQueue = try makeDatabaseQueue() @@ -296,7 +366,7 @@ class FetchedRecordsControllerOSXTests: GRDBTestCase { dbQueue, sql: ("SELECT persons.*, COUNT(books.id) AS bookCount " + "FROM persons " + - "LEFT JOIN books ON books.ownerId = persons.id " + + "LEFT JOIN books ON books.authorID = persons.id " + "GROUP BY persons.id " + "ORDER BY persons.name"), compareRecordsByPrimaryKey: true) @@ -309,8 +379,8 @@ class FetchedRecordsControllerOSXTests: GRDBTestCase { // Insert recorder.transactionExpectation = expectationWithDescription("expectation") try dbQueue.inTransaction { db in - try db.execute("INSERT INTO persons (name) VALUES (?)", arguments: ["Arthur"]) - try db.execute("INSERT INTO books (ownerId, title) VALUES (?, ?)", arguments: [1, "Moby Dick"]) + try db.execute("INSERT INTO persons (name) VALUES (?)", arguments: ["Herman Melville"]) + try db.execute("INSERT INTO books (authorID, title) VALUES (?, ?)", arguments: [1, "Moby-Dick"]) return .Commit } waitForExpectationsWithTimeout(1, handler: nil) @@ -324,10 +394,10 @@ class FetchedRecordsControllerOSXTests: GRDBTestCase { waitForExpectationsWithTimeout(1, handler: nil) XCTAssertEqual(recorder.recordsBeforeChanges.count, 1) - XCTAssertEqual(recorder.recordsBeforeChanges.map { $0.name }, ["Arthur"]) + XCTAssertEqual(recorder.recordsBeforeChanges.map { $0.name }, ["Herman Melville"]) XCTAssertEqual(recorder.recordsBeforeChanges.map { $0.bookCount! }, [1]) XCTAssertEqual(recorder.recordsAfterChanges.count, 1) - XCTAssertEqual(recorder.recordsAfterChanges.map { $0.name }, ["Arthur"]) + XCTAssertEqual(recorder.recordsAfterChanges.map { $0.name }, ["Herman Melville"]) XCTAssertEqual(recorder.recordsAfterChanges.map { $0.bookCount! }, [0]) } }