Files
GRDB.swift/Tests/GRDBTests/AssociationHasManyPrefetchingSQLTests.swift
T

201 lines
6.9 KiB
Swift

import XCTest
#if GRDBCUSTOMSQLITE
import GRDBCustomSQLite
#else
import GRDB
#endif
private struct A: TableRecord, FetchableRecord, Decodable {
var cola1: Int64
var cola2: String
}
private struct B: TableRecord, FetchableRecord, Decodable {
var colb1: Int64
var colb2: Int64
var colb3: String
}
private struct C: TableRecord, FetchableRecord, Decodable {
var colc1: Int64
var colc2: String
}
private struct D: TableRecord, FetchableRecord, Decodable {
var cold1: Int64
var cold2: Int64
var cold3: String
}
class AssociationHasManyPrefetchingSQLTests: GRDBTestCase {
override func setup(_ dbWriter: DatabaseWriter) throws {
try dbWriter.write { db in
try db.create(table: "a") { t in
t.autoIncrementedPrimaryKey("cola1")
t.column("cola2", .text)
}
try db.create(table: "b") { t in
t.autoIncrementedPrimaryKey("colb1")
t.column("colb2", .integer).references("a")
t.column("colb3", .text)
}
try db.create(table: "c") { t in
t.autoIncrementedPrimaryKey("colc1")
t.column("colc2", .integer).references("a")
}
try db.create(table: "d") { t in
t.autoIncrementedPrimaryKey("cold1")
t.column("cold2", .integer).references("c")
t.column("cold3", .text)
}
try db.execute(
sql: """
INSERT INTO a (cola1, cola2) VALUES (?, ?);
INSERT INTO a (cola1, cola2) VALUES (?, ?);
INSERT INTO a (cola1, cola2) VALUES (?, ?);
INSERT INTO b (colb1, colb2, colb3) VALUES (?, ?, ?);
INSERT INTO b (colb1, colb2, colb3) VALUES (?, ?, ?);
INSERT INTO b (colb1, colb2, colb3) VALUES (?, ?, ?);
INSERT INTO c (colc1, colc2) VALUES (?, ?);
INSERT INTO c (colc1, colc2) VALUES (?, ?);
INSERT INTO c (colc1, colc2) VALUES (?, ?);
INSERT INTO d (cold1, cold2, cold3) VALUES (?, ?, ?);
INSERT INTO d (cold1, cold2, cold3) VALUES (?, ?, ?);
INSERT INTO d (cold1, cold2, cold3) VALUES (?, ?, ?);
INSERT INTO d (cold1, cold2, cold3) VALUES (?, ?, ?);
""",
arguments: [
1, "a1",
2, "a2",
3, "a3",
4, 1, "b1",
5, 1, "b2",
6, 2, "b3",
7, 1,
8, 2,
9, 2,
10, 7, "d1",
11, 8, "d2",
12, 8, "d3",
13, 9, "d4",
])
}
}
func testHasMany() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
let request = A
.including(all: A
.hasMany(B.self)
.orderByPrimaryKey())
.orderByPrimaryKey()
sqlQueries.removeAll()
_ = try Row.fetchAll(db, request)
let selectQueries = sqlQueries.filter { $0.contains("SELECT") }
XCTAssertEqual(selectQueries, [
"""
SELECT * FROM "a" ORDER BY "cola1"
""",
"""
SELECT *, "colb2" AS "grdb_colb2" \
FROM "b" \
WHERE ("colb2" IN (1, 2, 3)) \
ORDER BY "colb1"
"""])
}
}
func testHasManyThrough() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
let request = A
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self), using: C.hasMany(D.self))
.orderByPrimaryKey())
.orderByPrimaryKey()
sqlQueries.removeAll()
_ = try Row.fetchAll(db, request)
let selectQueries = sqlQueries.filter { $0.contains("SELECT") }
XCTAssertEqual(selectQueries, [
"""
SELECT * FROM "a" ORDER BY "cola1"
""",
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (1, 2, 3))) \
ORDER BY "d"."cold1"
"""])
}
}
func testHasManyMergedWithHasManyThrough() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
let request = A
.including(all: A
.hasMany(C.self)
.orderByPrimaryKey())
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self), using: C.hasMany(D.self))
.orderByPrimaryKey())
.orderByPrimaryKey()
sqlQueries.removeAll()
_ = try Row.fetchAll(db, request)
let selectQueries = sqlQueries.filter { $0.contains("SELECT") }
XCTAssertEqual(selectQueries, [
"""
SELECT * FROM "a" ORDER BY "cola1"
""",
"""
SELECT *, "colc2" AS "grdb_colc2" \
FROM "c" \
WHERE ("colc2" IN (1, 2, 3)) \
ORDER BY "colc1"
""",
"""
SELECT *, "cold2" AS "grdb_cold2" \
FROM "d" \
WHERE ("cold2" IN (7, 8, 9)) \
ORDER BY "cold1"
"""])
}
}
func testAssociationIncludingAll() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
let request = B
.including(optional: B
.belongsTo(A.self)
.including(all: A
.hasMany(C.self)
.orderByPrimaryKey())
)
.orderByPrimaryKey()
sqlQueries.removeAll()
_ = try Row.fetchAll(db, request)
let selectQueries = sqlQueries.filter { $0.contains("SELECT") }
XCTAssertEqual(selectQueries, [
"""
SELECT "b".*, "a".* \
FROM "b" \
LEFT JOIN "a" ON ("a"."cola1" = "b"."colb2") \
ORDER BY "b"."colb1"
""",
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND ("a"."cola1" IN (1, 2))) \
ORDER BY "c"."colc1"
"""])
}
}
}