Fix failing tests for consumption of prefetched rows

This commit is contained in:
Gwendal Roué committed 2019-05-05 19:31:43 +02:00
1 parent bd0884503e
commit a45065c340
3 files changed
+19 -16

No files matched your search

+14 -11
View File
@@ -566,7 +566,7 @@ extension Row {
/// row.scopes["bar"] // [bar:2]
/// row.scopes["baz"] // nil
public var scopes: ScopesView {
return impl.scopes
return impl.scopes(prefetches: prefetches)
}
/// Returns a view on the scopes tree defined by row adapters.
@@ -1154,19 +1154,21 @@ extension Row {
public typealias Index = Dictionary<String, LayoutedRowAdapter>.Index
private let row: Row
private let scopes: [String: LayoutedRowAdapter]
private let prefetches: [String: Row.Prefetch]
/// The scopes defined on this row.
public var names: Dictionary<String, LayoutedRowAdapter>.Keys {
return scopes.keys
}
init() {
self.init(row: Row(), scopes: [:])
self.init(row: Row(), scopes: [:], prefetches: [:])
}
init(row: Row, scopes: [String: LayoutedRowAdapter]) {
init(row: Row, scopes: [String: LayoutedRowAdapter], prefetches: [String: Row.Prefetch]) {
self.row = row
self.scopes = scopes
self.prefetches = prefetches
}
/// :nodoc:
@@ -1187,16 +1189,17 @@ extension Row {
/// :nodoc:
public subscript(position: Index) -> (name: String, row: Row) {
let (name, adapter) = scopes[position]
return (name: name, row: Row(base: row, adapter: adapter))
let adaptedRow = Row(base: row, adapter: adapter)
if let prefetch = prefetches[name] {
adaptedRow.prefetches = prefetch.prefetches
}
return (name: name, row: adaptedRow)
}
/// Returns the row associated with the given scope, or nil if the
/// scope is not defined.
public subscript(_ name: String) -> Row? {
guard let adapter = scopes[name] else {
return nil
}
return Row(base: row, adapter: adapter)
return scopes.index(forKey: name).map { self[$0].row }
}
}
}
@@ -1296,7 +1299,7 @@ extension Row {
protocol RowImpl {
var count: Int { get }
var isFetched: Bool { get }
var scopes: Row.ScopesView { get }
func scopes(prefetches: [String: Row.Prefetch]) -> Row.ScopesView
func columnName(atUncheckedIndex index: Int) -> String
func hasNull(atUncheckedIndex index:Int) -> Bool
func databaseValue(atUncheckedIndex index: Int) -> DatabaseValue
@@ -1329,7 +1332,7 @@ extension RowImpl {
return row
}
var scopes: Row.ScopesView {
func scopes(prefetches: [String: Row.Prefetch]) -> Row.ScopesView {
// unless customized, assume unuscoped row (see AdaptedRowImpl for customization)
return Row.ScopesView()
}
+2 -2
View File
@@ -489,8 +489,8 @@ struct AdaptedRowImpl : RowImpl {
return base.isFetched
}
var scopes: Row.ScopesView {
return Row.ScopesView(row: base, scopes: adapter.scopes)
func scopes(prefetches: [String: Row.Prefetch]) -> Row.ScopesView {
return Row.ScopesView(row: base, scopes: adapter.scopes, prefetches: prefetches)
}
func hasNull(atUncheckedIndex index: Int) -> Bool {
@@ -1042,7 +1042,7 @@ class AssociationPrefetchingTests: GRDBTestCase {
XCTAssertEqual(rows[0].scopes["a"]!.prefetchTree.keys, ["cs"])
XCTAssertEqual(rows[0].scopes["a"]!.prefetchTree["cs"]!.count, 1)
XCTAssertEqual(rows[0].scopes["a"]!.prefetchTree["cs"]![0], ["colc1": 7, "colc2": 1, "grdb_cola1": 1]) // TODO: remove grdb_ column?
XCTAssertEqual(rows[1].unscoped, ["colb1": 5, "colb2": 1, "colb3": "b2"])
XCTAssertEqual(rows[1].prefetchTree.keys, ["cs"])
XCTAssertEqual(rows[1].prefetchTree["cs"]!.count, 1)
@@ -1052,7 +1052,7 @@ class AssociationPrefetchingTests: GRDBTestCase {
XCTAssertEqual(rows[1].scopes["a"]!.prefetchTree.keys, ["cs"])
XCTAssertEqual(rows[1].scopes["a"]!.prefetchTree["cs"]!.count, 1)
XCTAssertEqual(rows[1].scopes["a"]!.prefetchTree["cs"]![0], ["colc1": 7, "colc2": 1, "grdb_cola1": 1]) // TODO: remove grdb_ column?
XCTAssertEqual(rows[2].unscoped, ["colb1": 6, "colb2": 2, "colb3": "b3"])
XCTAssertEqual(rows[2].prefetchTree.keys, ["cs"])
XCTAssertEqual(rows[2].prefetchTree["cs"]!.count, 2)
@@ -1064,7 +1064,7 @@ class AssociationPrefetchingTests: GRDBTestCase {
XCTAssertEqual(rows[2].scopes["a"]!.prefetchTree["cs"]!.count, 2)
XCTAssertEqual(rows[2].scopes["a"]!.prefetchTree["cs"]![0], ["colc1": 8, "colc2": 2, "grdb_cola1": 2]) // TODO: remove grdb_ column?
XCTAssertEqual(rows[2].scopes["a"]!.prefetchTree["cs"]![1], ["colc1": 9, "colc2": 2, "grdb_cola1": 2]) // TODO: remove grdb_ column?
XCTAssertEqual(rows[3].unscoped, ["colb1": 14, "colb2": nil, "colb3": "b4"])
XCTAssertEqual(rows[3].prefetchTree.keys, ["cs"])
XCTAssertEqual(rows[3].prefetchTree["cs"]!.count, 0)