From 88fbec723e355293f532caae11039d971b23595e Mon Sep 17 00:00:00 2001 From: bwdmr Date: Sun, 19 Jan 2025 15:31:27 +0100 Subject: [PATCH] fix test cases: move soft delete check into JOIN clause for better SQL optimization incorporate changes in test cases --- .../AsyncTests/AsyncFluentKitTests.swift | 10 +++++----- .../AsyncTests/AsyncQueryBuilderTests.swift | 2 +- Tests/FluentKitTests/FluentKitTests.swift | 12 ++++++------ Tests/FluentKitTests/QueryBuilderTests.swift | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Tests/FluentKitTests/AsyncTests/AsyncFluentKitTests.swift b/Tests/FluentKitTests/AsyncTests/AsyncFluentKitTests.swift index 3fcf9199..83172fd1 100644 --- a/Tests/FluentKitTests/AsyncTests/AsyncFluentKitTests.swift +++ b/Tests/FluentKitTests/AsyncTests/AsyncFluentKitTests.swift @@ -75,23 +75,23 @@ final class AsyncFluentKitTests: XCTestCase { let db = DummyDatabaseForTestSQLSerializer() _ = try await Planet.query(on: db).join(child: \Planet.$governor).all() XCTAssertEqual(db.sqlSerializers.count, 1) - XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "governors" ON "planets"."id" = "governors"."planet_id"#), true) + XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "governors" ON "planets"."id" = "governors"."planet_id" AND ("governors"."deleted_at" IS NULL OR "governors"."deleted_at" > $1)"#), true) db.reset() _ = try await Planet.query(on: db).join(children: \Planet.$moons).all() XCTAssertEqual(db.sqlSerializers.count, 1) - XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "moons" ON "planets"."id" = "moons"."planet_id"#), true) + XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "moons" ON "planets"."id" = "moons"."planet_id" AND ("moons"."deleted_at" IS NULL OR "moons"."deleted_at" > $1)"#), true) db.reset() _ = try await Planet.query(on: db).join(parent: \Planet.$star).all() XCTAssertEqual(db.sqlSerializers.count, 1) - XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "stars" ON "planets"."star_id" = "stars"."id"#), true) + XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "stars" ON "planets"."star_id" = "stars"."id" AND ("stars"."deleted_at" IS NULL OR "stars"."deleted_at" > $1)"#), true) db.reset() _ = try await Planet.query(on: db).join(siblings: \Planet.$tags).all() XCTAssertEqual(db.sqlSerializers.count, 1) - XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "planet+tag" ON "planet+tag"."planet_id" = "planets"."id""#), true) - XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "tags" ON "planet+tag"."tag_id" = "tags"."id""#), true) + XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "planet+tag" ON "planet+tag"."planet_id" = "planets"."id" AND ("planet+tag"."deleted_at" IS NULL OR "planet+tag"."deleted_at" > $1)"#), true) + XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "tags" ON "planet+tag"."tag_id" = "tags"."id" AND ("tags"."deleted_at" IS NULL OR "tags"."deleted_at" > $1)"#), true) db.reset() } diff --git a/Tests/FluentKitTests/AsyncTests/AsyncQueryBuilderTests.swift b/Tests/FluentKitTests/AsyncTests/AsyncQueryBuilderTests.swift index d9da3716..3b68d73f 100644 --- a/Tests/FluentKitTests/AsyncTests/AsyncQueryBuilderTests.swift +++ b/Tests/FluentKitTests/AsyncTests/AsyncQueryBuilderTests.swift @@ -304,6 +304,6 @@ final class AsyncQueryBuilderTests: XCTestCase { .join(Star.self, on: \Star.$id == \Planet.$star.$id && \Star.$name != \Planet.$name) .all() XCTAssertEqual(db.sqlSerializers.count, 1) - XCTAssertEqual(try db.sqlSerializers.xctAt(0).sql, #"SELECT "planets"."id" AS "planets_id", "planets"."name" AS "planets_name", "planets"."star_id" AS "planets_star_id", "planets"."possible_star_id" AS "planets_possible_star_id", "planets"."deleted_at" AS "planets_deleted_at", "stars"."id" AS "stars_id", "stars"."name" AS "stars_name", "stars"."galaxy_id" AS "stars_galaxy_id", "stars"."deleted_at" AS "stars_deleted_at" FROM "planets" INNER JOIN "stars" ON "stars"."id" = "planets"."star_id" AND "stars"."name" <> "planets"."name" WHERE ("planets"."deleted_at" IS NULL OR "planets"."deleted_at" > $1) AND ("stars"."deleted_at" IS NULL OR "stars"."deleted_at" > $2)"#) + XCTAssertEqual(try db.sqlSerializers.xctAt(0).sql, #"SELECT "planets"."id" AS "planets_id", "planets"."name" AS "planets_name", "planets"."star_id" AS "planets_star_id", "planets"."possible_star_id" AS "planets_possible_star_id", "planets"."deleted_at" AS "planets_deleted_at", "stars"."id" AS "stars_id", "stars"."name" AS "stars_name", "stars"."galaxy_id" AS "stars_galaxy_id", "stars"."deleted_at" AS "stars_deleted_at" FROM "planets" INNER JOIN "stars" ON "stars"."id" = "planets"."star_id" AND "stars"."name" <> "planets"."name" AND ("stars"."deleted_at" IS NULL OR "stars"."deleted_at" > $1) WHERE ("planets"."deleted_at" IS NULL OR "planets"."deleted_at" > $2) AND ("stars"."deleted_at" IS NULL OR "stars"."deleted_at" > $3)"#) } } diff --git a/Tests/FluentKitTests/FluentKitTests.swift b/Tests/FluentKitTests/FluentKitTests.swift index 54439af3..e0811fab 100644 --- a/Tests/FluentKitTests/FluentKitTests.swift +++ b/Tests/FluentKitTests/FluentKitTests.swift @@ -129,28 +129,28 @@ final class FluentKitTests: XCTestCase { let db = DummyDatabaseForTestSQLSerializer() _ = try Planet.query(on: db).join(child: \Planet.$governor).all().wait() XCTAssertEqual(db.sqlSerializers.count, 1) - XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "governors" ON "planets"."id" = "governors"."planet_id"#), true) + XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "governors" ON "planets"."id" = "governors"."planet_id" AND ("governors"."deleted_at" IS NULL OR "governors"."deleted_at" > $1)"#), true) db.reset() _ = try Planet.query(on: db).join(children: \Planet.$moons).all().wait() XCTAssertEqual(db.sqlSerializers.count, 1) - XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "moons" ON "planets"."id" = "moons"."planet_id"#), true) + XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "moons" ON "planets"."id" = "moons"."planet_id" AND ("moons"."deleted_at" IS NULL OR "moons"."deleted_at" > $1)"#), true) db.reset() _ = try Planet.query(on: db).join(parent: \Planet.$star).all().wait() XCTAssertEqual(db.sqlSerializers.count, 1) - XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "stars" ON "planets"."star_id" = "stars"."id"#), true) + XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "stars" ON "planets"."star_id" = "stars"."id" AND ("stars"."deleted_at" IS NULL OR "stars"."deleted_at" > $1)"#), true) db.reset() _ = try Planet.query(on: db).join(parent: \Planet.$possibleStar).all().wait() XCTAssertEqual(db.sqlSerializers.count, 1) - XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "stars" ON "planets"."possible_star_id" = "stars"."id"#), true) + XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "stars" ON "planets"."possible_star_id" = "stars"."id" AND ("stars"."deleted_at" IS NULL OR "stars"."deleted_at" > $1)"#), true) db.reset() _ = try Planet.query(on: db).join(siblings: \Planet.$tags).all().wait() XCTAssertEqual(db.sqlSerializers.count, 1) - XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "planet+tag" ON "planet+tag"."planet_id" = "planets"."id""#), true, db.sqlSerializers.first?.sql ?? "") - XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "tags" ON "planet+tag"."tag_id" = "tags"."id""#), true) + XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "planet+tag" ON "planet+tag"."planet_id" = "planets"."id" AND ("planet+tag"."deleted_at" IS NULL OR "planet+tag"."deleted_at" > $1)"#), true, db.sqlSerializers.first?.sql ?? "") + XCTAssertEqual(db.sqlSerializers.first?.sql.contains(#"INNER JOIN "tags" ON "planet+tag"."tag_id" = "tags"."id" AND ("tags"."deleted_at" IS NULL OR "tags"."deleted_at" > $1)"#), true) db.reset() } diff --git a/Tests/FluentKitTests/QueryBuilderTests.swift b/Tests/FluentKitTests/QueryBuilderTests.swift index 6d5b7dd0..ec35bb4f 100644 --- a/Tests/FluentKitTests/QueryBuilderTests.swift +++ b/Tests/FluentKitTests/QueryBuilderTests.swift @@ -196,7 +196,7 @@ final class QueryBuilderTests: XCTestCase { on: .custom(#"LEFT JOIN "stars" ON "stars"."id" = "planets"."id" AND "stars"."name" = 'Sol'"#)) .all().wait() XCTAssertEqual(db.sqlSerializers.count, 1) - XCTAssertEqual(db.sqlSerializers.first?.sql, #"SELECT "planets"."id" AS "planets_id", "planets"."name" AS "planets_name", "planets"."star_id" AS "planets_star_id", "planets"."possible_star_id" AS "planets_possible_star_id", "planets"."deleted_at" AS "planets_deleted_at", "stars"."id" AS "stars_id", "stars"."name" AS "stars_name", "stars"."galaxy_id" AS "stars_galaxy_id", "stars"."deleted_at" AS "stars_deleted_at" FROM "planets" LEFT JOIN "stars" ON "stars"."id" = "planets"."id" AND "stars"."name" = 'Sol' WHERE ("planets"."deleted_at" IS NULL OR "planets"."deleted_at" > $1) AND ("stars"."deleted_at" IS NULL OR "stars"."deleted_at" > $2)"#) + XCTAssertEqual(db.sqlSerializers.first?.sql, #"SELECT "planets"."id" AS "planets_id", "planets"."name" AS "planets_name", "planets"."star_id" AS "planets_star_id", "planets"."possible_star_id" AS "planets_possible_star_id", "planets"."deleted_at" AS "planets_deleted_at", "stars"."id" AS "stars_id", "stars"."name" AS "stars_name", "stars"."galaxy_id" AS "stars_galaxy_id", "stars"."deleted_at" AS "stars_deleted_at" FROM "planets" LEFT JOIN "stars" ON "stars"."id" = "planets"."id" AND "stars"."name" = 'Sol' AND ("stars"."deleted_at" IS NULL OR "stars"."deleted_at" > $1) WHERE ("planets"."deleted_at" IS NULL OR "planets"."deleted_at" > $2) AND ("stars"."deleted_at" IS NULL OR "stars"."deleted_at" > $3)"#) } func testComplexJoinOperators() throws {