Skip to content

Commit

Permalink
fix test cases: move soft delete check into JOIN clause for better SQ…
Browse files Browse the repository at this point in the history
…L optimization

incorporate changes in test cases
  • Loading branch information
bwdmr committed Jan 19, 2025
1 parent ff2d10b commit 88fbec7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions Tests/FluentKitTests/AsyncTests/AsyncFluentKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)"#)
}
}
12 changes: 6 additions & 6 deletions Tests/FluentKitTests/FluentKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/FluentKitTests/QueryBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 88fbec7

Please sign in to comment.