From 8fc23e177113df5277f1e65a8d0e68c3ced9fef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 2 Mar 2019 15:48:17 +0100 Subject: [PATCH] Complete AssociationHasOneThroughSQLTests --- .../AssociationHasOneThroughSQLTests.swift | 595 ++++++++++++++++++ 1 file changed, 595 insertions(+) diff --git a/Tests/GRDBTests/AssociationHasOneThroughSQLTests.swift b/Tests/GRDBTests/AssociationHasOneThroughSQLTests.swift index 33dd6e80f..5944252e8 100644 --- a/Tests/GRDBTests/AssociationHasOneThroughSQLTests.swift +++ b/Tests/GRDBTests/AssociationHasOneThroughSQLTests.swift @@ -732,23 +732,618 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase { } func testBelongsToBelongsToHasOne() throws { + struct A: TableRecord { + static let b = belongsTo(B.self) + static let c = hasOne(B.c, through: b) + static let d1 = hasOne(C.d, through: c) + static let d2 = hasOne(B.d, through: b) + } + struct B: TableRecord { + static let c = belongsTo(C.self) + static let d = hasOne(C.d, through: c) + } + struct C: TableRecord { + static let d = hasOne(D.self) + } + struct D: TableRecord { + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { db in + try db.create(table: "c") { t in + t.autoIncrementedPrimaryKey("id") + } + try db.create(table: "d") { t in + t.autoIncrementedPrimaryKey("id") + t.column("cId").references("c") + } + try db.create(table: "b") { t in + t.autoIncrementedPrimaryKey("id") + t.column("cId").references("c") + } + try db.create(table: "a") { t in + t.autoIncrementedPrimaryKey("id") + t.column("bId").references("b") + } + + do { + for request in [ + A.including(optional: A.d1), + A.including(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."id" = "a"."bId") \ + LEFT JOIN "c" ON ("c"."id" = "b"."cId") \ + LEFT JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + for request in [ + A.including(required: A.d1), + A.including(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + for request in [ + A.joining(optional: A.d1), + A.joining(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."id" = "a"."bId") \ + LEFT JOIN "c" ON ("c"."id" = "b"."cId") \ + LEFT JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + for request in [ + A.joining(required: A.d1), + A.joining(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + } + } } func testBelongsToHasOneBelongsTo() throws { + struct A: TableRecord { + static let b = belongsTo(B.self) + static let c = hasOne(B.c, through: b) + static let d1 = hasOne(C.d, through: c) + static let d2 = hasOne(B.d, through: b) + } + struct B: TableRecord { + static let c = hasOne(C.self) + static let d = hasOne(C.d, through: c) + } + struct C: TableRecord { + static let d = belongsTo(D.self) + } + struct D: TableRecord { + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { db in + try db.create(table: "d") { t in + t.autoIncrementedPrimaryKey("id") + } + try db.create(table: "b") { t in + t.autoIncrementedPrimaryKey("id") + } + try db.create(table: "c") { t in + t.autoIncrementedPrimaryKey("id") + t.column("bId").references("b") + t.column("dId").references("d") + } + try db.create(table: "a") { t in + t.autoIncrementedPrimaryKey("id") + t.column("bId").references("b") + } + + do { + for request in [ + A.including(optional: A.d1), + A.including(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."id" = "a"."bId") \ + LEFT JOIN "c" ON ("c"."bId" = "b"."id") \ + LEFT JOIN "d" ON ("d"."id" = "c"."dId") + """) + } + for request in [ + A.including(required: A.d1), + A.including(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."bId" = "b"."id") \ + JOIN "d" ON ("d"."id" = "c"."dId") + """) + } + for request in [ + A.joining(optional: A.d1), + A.joining(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."id" = "a"."bId") \ + LEFT JOIN "c" ON ("c"."bId" = "b"."id") \ + LEFT JOIN "d" ON ("d"."id" = "c"."dId") + """) + } + for request in [ + A.joining(required: A.d1), + A.joining(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."bId" = "b"."id") \ + JOIN "d" ON ("d"."id" = "c"."dId") + """) + } + } + } } func testBelongsToHasOneHasOne() throws { + struct A: TableRecord { + static let b = belongsTo(B.self) + static let c = hasOne(B.c, through: b) + static let d1 = hasOne(C.d, through: c) + static let d2 = hasOne(B.d, through: b) + } + struct B: TableRecord { + static let c = hasOne(C.self) + static let d = hasOne(C.d, through: c) + } + struct C: TableRecord { + static let d = hasOne(D.self) + } + struct D: TableRecord { + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { db in + try db.create(table: "b") { t in + t.autoIncrementedPrimaryKey("id") + } + try db.create(table: "c") { t in + t.autoIncrementedPrimaryKey("id") + t.column("bId").references("b") + } + try db.create(table: "d") { t in + t.autoIncrementedPrimaryKey("id") + t.column("cId").references("c") + } + try db.create(table: "a") { t in + t.autoIncrementedPrimaryKey("id") + t.column("bId").references("b") + } + + do { + for request in [ + A.including(optional: A.d1), + A.including(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."id" = "a"."bId") \ + LEFT JOIN "c" ON ("c"."bId" = "b"."id") \ + LEFT JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + for request in [ + A.including(required: A.d1), + A.including(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."bId" = "b"."id") \ + JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + for request in [ + A.joining(optional: A.d1), + A.joining(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."id" = "a"."bId") \ + LEFT JOIN "c" ON ("c"."bId" = "b"."id") \ + LEFT JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + for request in [ + A.joining(required: A.d1), + A.joining(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."bId" = "b"."id") \ + JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + } + } } func testHasOneBelongsToBelongsTo() throws { + struct A: TableRecord { + static let b = hasOne(B.self) + static let c = hasOne(B.c, through: b) + static let d1 = hasOne(C.d, through: c) + static let d2 = hasOne(B.d, through: b) + } + struct B: TableRecord { + static let c = belongsTo(C.self) + static let d = hasOne(C.d, through: c) + } + struct C: TableRecord { + static let d = belongsTo(D.self) + } + struct D: TableRecord { + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { db in + try db.create(table: "a") { t in + t.autoIncrementedPrimaryKey("id") + } + try db.create(table: "d") { t in + t.autoIncrementedPrimaryKey("id") + } + try db.create(table: "c") { t in + t.autoIncrementedPrimaryKey("id") + t.column("dId").references("d") + } + try db.create(table: "b") { t in + t.autoIncrementedPrimaryKey("id") + t.column("aId").references("a") + t.column("cId").references("c") + } + + do { + for request in [ + A.including(optional: A.d1), + A.including(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."aId" = "a"."id") \ + LEFT JOIN "c" ON ("c"."id" = "b"."cId") \ + LEFT JOIN "d" ON ("d"."id" = "c"."dId") + """) + } + for request in [ + A.including(required: A.d1), + A.including(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + JOIN "b" ON ("b"."aId" = "a"."id") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + JOIN "d" ON ("d"."id" = "c"."dId") + """) + } + for request in [ + A.joining(optional: A.d1), + A.joining(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."aId" = "a"."id") \ + LEFT JOIN "c" ON ("c"."id" = "b"."cId") \ + LEFT JOIN "d" ON ("d"."id" = "c"."dId") + """) + } + for request in [ + A.joining(required: A.d1), + A.joining(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + JOIN "b" ON ("b"."aId" = "a"."id") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + JOIN "d" ON ("d"."id" = "c"."dId") + """) + } + } + } } func testHasOneBelongsToHasOne() throws { + struct A: TableRecord { + static let b = hasOne(B.self) + static let c = hasOne(B.c, through: b) + static let d1 = hasOne(C.d, through: c) + static let d2 = hasOne(B.d, through: b) + } + struct B: TableRecord { + static let c = belongsTo(C.self) + static let d = hasOne(C.d, through: c) + } + struct C: TableRecord { + static let d = hasOne(D.self) + } + struct D: TableRecord { + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { db in + try db.create(table: "a") { t in + t.autoIncrementedPrimaryKey("id") + } + try db.create(table: "c") { t in + t.autoIncrementedPrimaryKey("id") + } + try db.create(table: "d") { t in + t.autoIncrementedPrimaryKey("id") + t.column("cId").references("c") + } + try db.create(table: "b") { t in + t.autoIncrementedPrimaryKey("id") + t.column("aId").references("a") + t.column("cId").references("c") + } + + do { + for request in [ + A.including(optional: A.d1), + A.including(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."aId" = "a"."id") \ + LEFT JOIN "c" ON ("c"."id" = "b"."cId") \ + LEFT JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + for request in [ + A.including(required: A.d1), + A.including(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + JOIN "b" ON ("b"."aId" = "a"."id") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + for request in [ + A.joining(optional: A.d1), + A.joining(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."aId" = "a"."id") \ + LEFT JOIN "c" ON ("c"."id" = "b"."cId") \ + LEFT JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + for request in [ + A.joining(required: A.d1), + A.joining(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + JOIN "b" ON ("b"."aId" = "a"."id") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + } + } } func testHasOneHasOneBelongsTo() throws { + struct A: TableRecord { + static let b = hasOne(B.self) + static let c = hasOne(B.c, through: b) + static let d1 = hasOne(C.d, through: c) + static let d2 = hasOne(B.d, through: b) + } + struct B: TableRecord { + static let c = hasOne(C.self) + static let d = hasOne(C.d, through: c) + } + struct C: TableRecord { + static let d = belongsTo(D.self) + } + struct D: TableRecord { + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { db in + try db.create(table: "a") { t in + t.autoIncrementedPrimaryKey("id") + } + try db.create(table: "d") { t in + t.autoIncrementedPrimaryKey("id") + } + try db.create(table: "b") { t in + t.autoIncrementedPrimaryKey("id") + t.column("aId").references("a") + } + try db.create(table: "c") { t in + t.autoIncrementedPrimaryKey("id") + t.column("bId").references("b") + t.column("dId").references("d") + } + + do { + for request in [ + A.including(optional: A.d1), + A.including(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."aId" = "a"."id") \ + LEFT JOIN "c" ON ("c"."bId" = "b"."id") \ + LEFT JOIN "d" ON ("d"."id" = "c"."dId") + """) + } + for request in [ + A.including(required: A.d1), + A.including(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + JOIN "b" ON ("b"."aId" = "a"."id") \ + JOIN "c" ON ("c"."bId" = "b"."id") \ + JOIN "d" ON ("d"."id" = "c"."dId") + """) + } + for request in [ + A.joining(optional: A.d1), + A.joining(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."aId" = "a"."id") \ + LEFT JOIN "c" ON ("c"."bId" = "b"."id") \ + LEFT JOIN "d" ON ("d"."id" = "c"."dId") + """) + } + for request in [ + A.joining(required: A.d1), + A.joining(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + JOIN "b" ON ("b"."aId" = "a"."id") \ + JOIN "c" ON ("c"."bId" = "b"."id") \ + JOIN "d" ON ("d"."id" = "c"."dId") + """) + } + } + } } func testHasOneHasOneHasOne() throws { + struct A: TableRecord { + static let b = hasOne(B.self) + static let c = hasOne(B.c, through: b) + static let d1 = hasOne(C.d, through: c) + static let d2 = hasOne(B.d, through: b) + } + struct B: TableRecord { + static let c = hasOne(C.self) + static let d = hasOne(C.d, through: c) + } + struct C: TableRecord { + static let d = hasOne(D.self) + } + struct D: TableRecord { + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { db in + try db.create(table: "a") { t in + t.autoIncrementedPrimaryKey("id") + } + try db.create(table: "b") { t in + t.autoIncrementedPrimaryKey("id") + t.column("aId").references("a") + } + try db.create(table: "c") { t in + t.autoIncrementedPrimaryKey("id") + t.column("bId").references("b") + } + try db.create(table: "d") { t in + t.autoIncrementedPrimaryKey("id") + t.column("cId").references("c") + } + + do { + for request in [ + A.including(optional: A.d1), + A.including(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."aId" = "a"."id") \ + LEFT JOIN "c" ON ("c"."bId" = "b"."id") \ + LEFT JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + for request in [ + A.including(required: A.d1), + A.including(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".*, "d".* \ + FROM "a" \ + JOIN "b" ON ("b"."aId" = "a"."id") \ + JOIN "c" ON ("c"."bId" = "b"."id") \ + JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + for request in [ + A.joining(optional: A.d1), + A.joining(optional: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + LEFT JOIN "b" ON ("b"."aId" = "a"."id") \ + LEFT JOIN "c" ON ("c"."bId" = "b"."id") \ + LEFT JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + for request in [ + A.joining(required: A.d1), + A.joining(required: A.d2)] + { + try assertEqualSQL(db, request, """ + SELECT "a".* \ + FROM "a" \ + JOIN "b" ON ("b"."aId" = "a"."id") \ + JOIN "c" ON ("c"."bId" = "b"."id") \ + JOIN "d" ON ("d"."cId" = "c"."id") + """) + } + } + } } }