More passing prefetch tests

This commit is contained in:
Gwendal Roué committed 2019-05-04 21:28:59 +02:00
1 parent 73d622a074
commit 7ef5669710
1 file changed
+252 -58
@@ -104,6 +104,23 @@ class AssociationHasManyPrefetchingSQLTests: GRDBTestCase {
ORDER BY "colb1"
"""])
}
do {
let request = A
.filter(false)
.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\" WHERE 0 ORDER BY \"cola1\"
"""])
}
do {
let request = A
.filter(Column("cola1") != 1)
@@ -179,6 +196,32 @@ class AssociationHasManyPrefetchingSQLTests: GRDBTestCase {
ORDER BY "cold1"
"""])
}
do {
let request = A
.including(all: A
.hasMany(C.self)
.filter(false)
.including(all: C
.hasMany(D.self)
.orderByPrimaryKey())
.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 (0 AND ("colc2" IN (1, 2, 3))) \
ORDER BY "colc1"
"""])
}
do {
let request = A
.filter(Column("cola1") != 1)
@@ -302,6 +345,11 @@ class AssociationHasManyPrefetchingSQLTests: GRDBTestCase {
.filter(Column("cold1") != 11)
.orderByPrimaryKey()
.forKey("d2"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self).forKey("c2"), using: C.hasMany(D.self))
.filter(Column("cold1") == 11)
.orderByPrimaryKey()
.forKey("d3"))
.orderByPrimaryKey()
sqlQueries.removeAll()
@@ -326,6 +374,13 @@ class AssociationHasManyPrefetchingSQLTests: GRDBTestCase {
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (2, 3))) \
WHERE ("d"."cold1" <> 11) \
ORDER BY "d"."cold1"
""",
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (2, 3))) \
WHERE ("d"."cold1" = 11) \
ORDER BY "d"."cold1"
"""])
}
}
@@ -334,67 +389,206 @@ class AssociationHasManyPrefetchingSQLTests: GRDBTestCase {
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)
do {
let request = A
.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"
"""])
.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"
"""])
}
do {
let request = A
.filter(Column("cola1") != 1)
.including(all: A
.hasMany(C.self)
.filter(Column("colc1") != 8)
.orderByPrimaryKey()
.forKey("c1"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self).forKey("c1"), using: C.hasMany(D.self))
.filter(Column("cold1") != 11)
.orderByPrimaryKey()
.forKey("d1"))
.including(all: A
.hasMany(C.self)
.filter(Column("colc1") != 9)
.orderByPrimaryKey()
.forKey("c2"))
.including(all: A
.hasMany(D.self, through: A.hasMany(C.self).forKey("c2"), using: C.hasMany(D.self))
.filter(Column("cold1") == 11)
.orderByPrimaryKey()
.forKey("d2"))
.orderByPrimaryKey()
sqlQueries.removeAll()
_ = try Row.fetchAll(db, request)
let selectQueries = sqlQueries.filter { $0.contains("SELECT") }
XCTAssertEqual(selectQueries, [
"""
SELECT * \
FROM "a" \
WHERE ("cola1" <> 1) \
ORDER BY "cola1"
""",
"""
SELECT *, "colc2" AS "grdb_colc2" \
FROM "c" \
WHERE (("colc1" <> 8) AND ("colc2" IN (2, 3))) \
ORDER BY "colc1"
""",
"""
SELECT *, "cold2" AS "grdb_cold2" \
FROM "d" \
WHERE (("cold1" <> 11) AND ("cold2" = 9)) \
ORDER BY "cold1"
""",
"""
SELECT *, "colc2" AS "grdb_colc2" \
FROM "c" \
WHERE (("colc1" <> 9) AND ("colc2" IN (2, 3))) \
ORDER BY "colc1"
""",
"""
SELECT *, "cold2" AS "grdb_cold2" \
FROM "d" \
WHERE (("cold1" = 11) AND ("cold2" = 8)) \
ORDER BY "cold1"
"""])
}
}
}
func testBelongsToHasMany() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
do {
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"
"""])
}
do {
let request = B
.including(optional: B
.belongsTo(A.self)
.filter(Column("cola1") == 1)
.including(all: A
.hasMany(C.self)
.filter(Column("colc1") == 9)
.orderByPrimaryKey()
.forKey("c1"))
.including(all: A
.hasMany(C.self)
.filter(Column("colc1") != 9)
.orderByPrimaryKey()
.forKey("c2"))
.forKey("a1"))
.including(optional: B
.belongsTo(A.self)
.filter(Column("cola1") == 2)
.including(all: A
.hasMany(C.self)
.filter(Column("colc1") == 9)
.orderByPrimaryKey()
.forKey("c1"))
.including(all: A
.hasMany(C.self)
.filter(Column("colc1") != 9)
.orderByPrimaryKey()
.forKey("c2"))
.forKey("a2"))
.orderByPrimaryKey()
sqlQueries.removeAll()
_ = try Row.fetchAll(db, request)
let selectQueries = sqlQueries.filter { $0.contains("SELECT") }
// TODO: ("a"."cola1" IN (1, 2)) conditions are all wrong
XCTAssertEqual(selectQueries, [
"""
SELECT "b".*, "a1".*, "a2".* \
FROM "b" \
LEFT JOIN "a" "a1" ON (("a1"."cola1" = "b"."colb2") AND ("a1"."cola1" = 1)) \
LEFT JOIN "a" "a2" ON (("a2"."cola1" = "b"."colb2") AND ("a2"."cola1" = 2)) \
ORDER BY "b"."colb1"
""",
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND (("a"."cola1" = 1) AND ("a"."cola1" IN (1, 2)))) \
WHERE ("c"."colc1" = 9) \
ORDER BY "c"."colc1"
""",
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND (("a"."cola1" = 1) AND ("a"."cola1" IN (1, 2)))) \
WHERE ("c"."colc1" <> 9) \
ORDER BY "c"."colc1"
""",
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND (("a"."cola1" = 2) AND ("a"."cola1" IN (1, 2)))) \
WHERE ("c"."colc1" = 9) \
ORDER BY "c"."colc1"
""",
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND (("a"."cola1" = 2) AND ("a"."cola1" IN (1, 2)))) \
WHERE ("c"."colc1" <> 9) \
ORDER BY "c"."colc1"
"""])
}
}
}
}