From 8038a9db05eaf80ef3f3b0ccdbc47e36051668cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 16 May 2019 22:21:52 +0200 Subject: [PATCH] Tests for consumption of prefetched rows into sets --- GRDB/Core/Row.swift | 44 ++++++ ...ciationPrefetchingCodableRecordTests.swift | 99 ++++++++++---- ...ationPrefetchingFetchableRecordTests.swift | 126 +++++++++++++----- 3 files changed, 204 insertions(+), 65 deletions(-) diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 9d96b960d..35e46bca3 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -582,6 +582,50 @@ extension Row { } return rows.map(Record.init(row:)) } + + /// Returns the set of records encoded in the given prefetched rows. + /// + /// struct Author: FetchableRecord, TableRecord { + /// static let books = hasMany(Book.self) + /// ... + /// } + /// struct Book: FetchableRecord, TableRecord, Hashable { + /// ... + /// } + /// + /// struct AuthorInfo { + /// var author: Author + /// var books: Set + /// + /// init(row: Row) { + /// author = Author(row: row) + /// books = row["books"] // <--- + /// } + /// } + /// + /// try dbQueue.read { db in + /// // Fetch authors along with their books: + /// let request = Author.including(all: Author.books) + /// let authorInfos = try AuthorInfo.fetchAll(db, request) + /// + /// for authorInfo in authorInfos { + /// print("\(authorInfo.author.name) wrote:") + /// for book in authorInfo.books { + /// print("- \(book.title)") + /// } + /// } + /// } + public subscript(_ key: String) -> Set { + guard let rows = prefetches[key] else { + // Programmer error + fatalError("no such prefetched rows: \(key)") + } + var set = Set(minimumCapacity: rows.count) + for row in rows { + set.insert(Record(row: row)) + } + return set + } } extension Row { diff --git a/Tests/GRDBTests/AssociationPrefetchingCodableRecordTests.swift b/Tests/GRDBTests/AssociationPrefetchingCodableRecordTests.swift index b2eec947c..76583c018 100644 --- a/Tests/GRDBTests/AssociationPrefetchingCodableRecordTests.swift +++ b/Tests/GRDBTests/AssociationPrefetchingCodableRecordTests.swift @@ -9,7 +9,7 @@ private struct A: TableRecord, FetchableRecord, Decodable, Equatable { var cola1: Int64 var cola2: String } -private struct B: TableRecord, FetchableRecord, Decodable, Equatable { +private struct B: TableRecord, FetchableRecord, Decodable, Hashable { var colb1: Int64 var colb2: Int64? var colb3: String @@ -93,41 +93,84 @@ class AssociationPrefetchingCodableRecordTests: GRDBTestCase { .forKey("bs")) // TODO: auto-pluralization .orderByPrimaryKey() - struct Record: FetchableRecord, Decodable, Equatable { - var a: A - var bs: [B] - } - - // Record.fetchAll + // Array do { - let records = try Record.fetchAll(db, request) - XCTAssertEqual(records, [ - Record( + struct Record: FetchableRecord, Decodable, Equatable { + var a: A + var bs: [B] + } + + // Record.fetchAll + do { + let records = try Record.fetchAll(db, request) + XCTAssertEqual(records, [ + Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: [ + B(row: ["colb1": 4, "colb2": 1, "colb3": "b1"]), + B(row: ["colb1": 5, "colb2": 1, "colb3": "b2"]), + ]), + Record( + a: A(row: ["cola1": 2, "cola2": "a2"]), + bs: [ + B(row: ["colb1": 6, "colb2": 2, "colb3": "b3"]), + ]), + Record( + a: A(row: ["cola1": 3, "cola2": "a3"]), + bs: []), + ]) + } + + // Record.fetchOne + do { + let record = try Record.fetchOne(db, request)! + XCTAssertEqual(record, Record( a: A(row: ["cola1": 1, "cola2": "a1"]), bs: [ B(row: ["colb1": 4, "colb2": 1, "colb3": "b1"]), B(row: ["colb1": 5, "colb2": 1, "colb3": "b2"]), - ]), - Record( - a: A(row: ["cola1": 2, "cola2": "a2"]), - bs: [ - B(row: ["colb1": 6, "colb2": 2, "colb3": "b3"]), - ]), - Record( - a: A(row: ["cola1": 3, "cola2": "a3"]), - bs: []), - ]) + ])) + } } - // Record.fetchOne + // Set do { - let record = try Record.fetchOne(db, request)! - XCTAssertEqual(record, Record( - a: A(row: ["cola1": 1, "cola2": "a1"]), - bs: [ - B(row: ["colb1": 4, "colb2": 1, "colb3": "b1"]), - B(row: ["colb1": 5, "colb2": 1, "colb3": "b2"]), - ])) + struct Record: FetchableRecord, Decodable, Equatable { + var a: A + var bs: Set + } + + // Record.fetchAll + do { + let records = try Record.fetchAll(db, request) + XCTAssertEqual(records, [ + Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: [ + B(row: ["colb1": 4, "colb2": 1, "colb3": "b1"]), + B(row: ["colb1": 5, "colb2": 1, "colb3": "b2"]), + ]), + Record( + a: A(row: ["cola1": 2, "cola2": "a2"]), + bs: [ + B(row: ["colb1": 6, "colb2": 2, "colb3": "b3"]), + ]), + Record( + a: A(row: ["cola1": 3, "cola2": "a3"]), + bs: []), + ]) + } + + // Record.fetchOne + do { + let record = try Record.fetchOne(db, request)! + XCTAssertEqual(record, Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: [ + B(row: ["colb1": 4, "colb2": 1, "colb3": "b1"]), + B(row: ["colb1": 5, "colb2": 1, "colb3": "b2"]), + ])) + } } } diff --git a/Tests/GRDBTests/AssociationPrefetchingFetchableRecordTests.swift b/Tests/GRDBTests/AssociationPrefetchingFetchableRecordTests.swift index e934c8330..be7393a8e 100644 --- a/Tests/GRDBTests/AssociationPrefetchingFetchableRecordTests.swift +++ b/Tests/GRDBTests/AssociationPrefetchingFetchableRecordTests.swift @@ -14,7 +14,7 @@ private struct A: TableRecord, FetchableRecord, Equatable { cola2 = row["cola2"] } } -private struct B: TableRecord, FetchableRecord, Equatable { +private struct B: TableRecord, FetchableRecord, Hashable { var colb1: Int64 var colb2: Int64? var colb3: String @@ -115,50 +115,102 @@ class AssociationPrefetchingFetchableRecordTests: GRDBTestCase { .forKey("bs")) // TODO: auto-pluralization .orderByPrimaryKey() - struct Record: FetchableRecord, Equatable { - var a: A - var bs: [B] - - init(a: A, bs: [B]) { - self.a = a - self.bs = bs - } - - init(row: Row) { - self.init(a: A(row: row), bs: row["bs"]) - } - } - - // Record.fetchAll + // Array do { - let records = try Record.fetchAll(db, request) - XCTAssertEqual(records, [ - Record( + struct Record: FetchableRecord, Equatable { + var a: A + var bs: [B] + + init(a: A, bs: [B]) { + self.a = a + self.bs = bs + } + + init(row: Row) { + self.init(a: A(row: row), bs: row["bs"]) + } + } + + // Record.fetchAll + do { + let records = try Record.fetchAll(db, request) + XCTAssertEqual(records, [ + Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: [ + B(row: ["colb1": 4, "colb2": 1, "colb3": "b1"]), + B(row: ["colb1": 5, "colb2": 1, "colb3": "b2"]), + ]), + Record( + a: A(row: ["cola1": 2, "cola2": "a2"]), + bs: [ + B(row: ["colb1": 6, "colb2": 2, "colb3": "b3"]), + ]), + Record( + a: A(row: ["cola1": 3, "cola2": "a3"]), + bs: []), + ]) + } + + // Record.fetchOne + do { + let record = try Record.fetchOne(db, request)! + XCTAssertEqual(record, Record( a: A(row: ["cola1": 1, "cola2": "a1"]), bs: [ B(row: ["colb1": 4, "colb2": 1, "colb3": "b1"]), B(row: ["colb1": 5, "colb2": 1, "colb3": "b2"]), - ]), - Record( - a: A(row: ["cola1": 2, "cola2": "a2"]), - bs: [ - B(row: ["colb1": 6, "colb2": 2, "colb3": "b3"]), - ]), - Record( - a: A(row: ["cola1": 3, "cola2": "a3"]), - bs: []), - ]) + ])) + } } - // Record.fetchOne + // Set do { - let record = try Record.fetchOne(db, request)! - XCTAssertEqual(record, Record( - a: A(row: ["cola1": 1, "cola2": "a1"]), - bs: [ - B(row: ["colb1": 4, "colb2": 1, "colb3": "b1"]), - B(row: ["colb1": 5, "colb2": 1, "colb3": "b2"]), - ])) + struct Record: FetchableRecord, Equatable { + var a: A + var bs: Set + + init(a: A, bs: Set) { + self.a = a + self.bs = bs + } + + init(row: Row) { + self.init(a: A(row: row), bs: row["bs"]) + } + } + + // Record.fetchAll + do { + let records = try Record.fetchAll(db, request) + XCTAssertEqual(records, [ + Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: [ + B(row: ["colb1": 4, "colb2": 1, "colb3": "b1"]), + B(row: ["colb1": 5, "colb2": 1, "colb3": "b2"]), + ]), + Record( + a: A(row: ["cola1": 2, "cola2": "a2"]), + bs: [ + B(row: ["colb1": 6, "colb2": 2, "colb3": "b3"]), + ]), + Record( + a: A(row: ["cola1": 3, "cola2": "a3"]), + bs: []), + ]) + } + + // Record.fetchOne + do { + let record = try Record.fetchOne(db, request)! + XCTAssertEqual(record, Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: [ + B(row: ["colb1": 4, "colb2": 1, "colb3": "b1"]), + B(row: ["colb1": 5, "colb2": 1, "colb3": "b2"]), + ])) + } } }