mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
603 lines
24 KiB
Swift
603 lines
24 KiB
Swift
import XCTest
|
|
#if GRDBCIPHER
|
|
import GRDBCipher
|
|
#elseif GRDBCUSTOMSQLITE
|
|
import GRDBCustomSQLite
|
|
#else
|
|
import GRDB
|
|
#endif
|
|
|
|
private struct Team: Codable, FetchableRecord, PersistableRecord {
|
|
static let players = hasMany(Player.self)
|
|
static let customPlayers = hasMany(Player.self, key: "custom")
|
|
var id: Int64
|
|
var name: String
|
|
}
|
|
|
|
private struct Player: Codable, FetchableRecord, PersistableRecord {
|
|
var id: Int64
|
|
var teamId: Int64?
|
|
var name: String
|
|
var score: Int
|
|
}
|
|
|
|
private struct TeamInfo: Decodable, FetchableRecord {
|
|
var team: Team
|
|
var averagePlayerScore: Double?
|
|
var playerCount: Int?
|
|
var maxPlayerScore: Int?
|
|
var minPlayerScore: Int?
|
|
var playerScoreSum: Int?
|
|
}
|
|
|
|
private struct CustomTeamInfo: Decodable, FetchableRecord {
|
|
var team: Team
|
|
var averageCustomScore: Double?
|
|
var customCount: Int?
|
|
var maxCustomScore: Int?
|
|
var minCustomScore: Int?
|
|
var customScoreSum: Int?
|
|
}
|
|
|
|
class AnnotationTests: GRDBTestCase {
|
|
|
|
override func setup(_ dbWriter: DatabaseWriter) throws {
|
|
try dbWriter.write { db in
|
|
try db.create(table: "team") { t in
|
|
t.column("id", .integer).primaryKey()
|
|
t.column("name", .text)
|
|
}
|
|
try db.create(table: "player") { t in
|
|
t.column("id", .integer).primaryKey()
|
|
t.column("teamId", .integer).references("team")
|
|
t.column("name", .text)
|
|
t.column("score", .integer)
|
|
}
|
|
|
|
try Team(id: 1, name: "Reds").insert(db)
|
|
try Player(id: 1, teamId: 1, name: "Arthur", score: 100).insert(db)
|
|
try Player(id: 2, teamId: 1, name: "Barbara", score: 1000).insert(db)
|
|
try Team(id: 2, name: "Blues").insert(db)
|
|
try Player(id: 3, teamId: 2, name: "Craig", score: 200).insert(db)
|
|
try Player(id: 4, teamId: 2, name: "David", score: 500).insert(db)
|
|
try Player(id: 5, teamId: 2, name: "Elise", score: 800).insert(db)
|
|
try Team(id: 3, name: "Greens").insert(db)
|
|
}
|
|
}
|
|
|
|
func testDefaultAvg() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.players.average(Column("score")))
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: TeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, AVG("player"."score") AS "averagePlayerScore" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].averagePlayerScore, 550)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].averagePlayerScore, 500)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertNil(teamInfos[2].averagePlayerScore)
|
|
}
|
|
}
|
|
|
|
func testDefaultCount() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.players.count())
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: TeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, COUNT(DISTINCT "player"."rowid") AS "playerCount" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].playerCount, 2)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].playerCount, 3)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertEqual(teamInfos[2].playerCount, 0)
|
|
}
|
|
}
|
|
|
|
func testDefaultMax() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.players.max(Column("score")))
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: TeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, MAX("player"."score") AS "maxPlayerScore" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].maxPlayerScore, 1000)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].maxPlayerScore, 800)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertNil(teamInfos[2].maxPlayerScore)
|
|
}
|
|
}
|
|
|
|
func testDefaultMin() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.players.min(Column("score")))
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: TeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, MIN("player"."score") AS "minPlayerScore" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].minPlayerScore, 100)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].minPlayerScore, 200)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertNil(teamInfos[2].minPlayerScore)
|
|
}
|
|
}
|
|
|
|
func testDefaultSum() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.players.sum(Column("score")))
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: TeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, SUM("player"."score") AS "playerScoreSum" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].playerScoreSum, 1100)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].playerScoreSum, 1500)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertNil(teamInfos[2].playerScoreSum)
|
|
}
|
|
}
|
|
|
|
func testDefaultMultipleAnnotations() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.players.average(Column("score")))
|
|
.annotated(with: Team.players.count())
|
|
.annotated(with: Team.players.max(Column("score")))
|
|
.annotated(with: Team.players.min(Column("score")))
|
|
.annotated(with: Team.players.sum(Column("score")))
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: TeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, \
|
|
AVG("player"."score") AS "averagePlayerScore", \
|
|
COUNT(DISTINCT "player"."rowid") AS "playerCount", \
|
|
MAX("player"."score") AS "maxPlayerScore", \
|
|
MIN("player"."score") AS "minPlayerScore", \
|
|
SUM("player"."score") AS "playerScoreSum" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].averagePlayerScore, 550)
|
|
XCTAssertEqual(teamInfos[0].playerCount, 2)
|
|
XCTAssertEqual(teamInfos[0].maxPlayerScore, 1000)
|
|
XCTAssertEqual(teamInfos[0].minPlayerScore, 100)
|
|
XCTAssertEqual(teamInfos[0].playerScoreSum, 1100)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].averagePlayerScore, 500)
|
|
XCTAssertEqual(teamInfos[1].playerCount, 3)
|
|
XCTAssertEqual(teamInfos[1].maxPlayerScore, 800)
|
|
XCTAssertEqual(teamInfos[1].minPlayerScore, 200)
|
|
XCTAssertEqual(teamInfos[1].playerScoreSum, 1500)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertNil(teamInfos[2].averagePlayerScore)
|
|
XCTAssertEqual(teamInfos[2].playerCount, 0)
|
|
XCTAssertNil(teamInfos[2].maxPlayerScore)
|
|
XCTAssertNil(teamInfos[2].minPlayerScore)
|
|
XCTAssertNil(teamInfos[2].playerScoreSum)
|
|
}
|
|
}
|
|
|
|
func testCustomAvg() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.customPlayers.average(Column("score")))
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: CustomTeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, AVG("player"."score") AS "averageCustomScore" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].averageCustomScore, 550)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].averageCustomScore, 500)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertNil(teamInfos[2].averageCustomScore)
|
|
}
|
|
}
|
|
|
|
func testCustomCount() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.customPlayers.count())
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: CustomTeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, COUNT(DISTINCT "player"."rowid") AS "customCount" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].customCount, 2)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].customCount, 3)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertEqual(teamInfos[2].customCount, 0)
|
|
}
|
|
}
|
|
|
|
func testCustomMax() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.customPlayers.max(Column("score")))
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: CustomTeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, MAX("player"."score") AS "maxCustomScore" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].maxCustomScore, 1000)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].maxCustomScore, 800)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertNil(teamInfos[2].maxCustomScore)
|
|
}
|
|
}
|
|
|
|
func testCustomMin() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.customPlayers.min(Column("score")))
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: CustomTeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, MIN("player"."score") AS "minCustomScore" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].minCustomScore, 100)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].minCustomScore, 200)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertNil(teamInfos[2].minCustomScore)
|
|
}
|
|
}
|
|
|
|
func testCustomSum() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.customPlayers.sum(Column("score")))
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: CustomTeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, SUM("player"."score") AS "customScoreSum" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].customScoreSum, 1100)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].customScoreSum, 1500)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertNil(teamInfos[2].customScoreSum)
|
|
}
|
|
}
|
|
|
|
func testCustomMultipleAnnotations() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.customPlayers.average(Column("score")))
|
|
.annotated(with: Team.customPlayers.count())
|
|
.annotated(with: Team.customPlayers.max(Column("score")))
|
|
.annotated(with: Team.customPlayers.min(Column("score")))
|
|
.annotated(with: Team.customPlayers.sum(Column("score")))
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: CustomTeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, \
|
|
AVG("player"."score") AS "averageCustomScore", \
|
|
COUNT(DISTINCT "player"."rowid") AS "customCount", \
|
|
MAX("player"."score") AS "maxCustomScore", \
|
|
MIN("player"."score") AS "minCustomScore", \
|
|
SUM("player"."score") AS "customScoreSum" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].averageCustomScore, 550)
|
|
XCTAssertEqual(teamInfos[0].customCount, 2)
|
|
XCTAssertEqual(teamInfos[0].maxCustomScore, 1000)
|
|
XCTAssertEqual(teamInfos[0].minCustomScore, 100)
|
|
XCTAssertEqual(teamInfos[0].customScoreSum, 1100)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].averageCustomScore, 500)
|
|
XCTAssertEqual(teamInfos[1].customCount, 3)
|
|
XCTAssertEqual(teamInfos[1].maxCustomScore, 800)
|
|
XCTAssertEqual(teamInfos[1].minCustomScore, 200)
|
|
XCTAssertEqual(teamInfos[1].customScoreSum, 1500)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertNil(teamInfos[2].averageCustomScore)
|
|
XCTAssertEqual(teamInfos[2].customCount, 0)
|
|
XCTAssertNil(teamInfos[2].maxCustomScore)
|
|
XCTAssertNil(teamInfos[2].minCustomScore)
|
|
XCTAssertNil(teamInfos[2].customScoreSum)
|
|
}
|
|
}
|
|
|
|
func testAnnotationAlias() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.players.average(Column("score"), aliased: "a1"))
|
|
.annotated(with: Team.players.count(aliased: "a2"))
|
|
.annotated(with: Team.players.max(Column("score"), aliased: "a3"))
|
|
.annotated(with: Team.players.min(Column("score"), aliased: "a4"))
|
|
.annotated(with: Team.players.sum(Column("score"), aliased: "a5"))
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: CustomTeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, \
|
|
AVG("player"."score") AS "a1", \
|
|
COUNT(DISTINCT "player"."rowid") AS "a2", \
|
|
MAX("player"."score") AS "a3", \
|
|
MIN("player"."score") AS "a4", \
|
|
SUM("player"."score") AS "a5" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
}
|
|
}
|
|
|
|
func testAnnotationExpression() throws {
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.players.average(Column("score") * Column("score"), aliased: "a1"))
|
|
.annotated(with: Team.players.max(Column("score") * 10, aliased: "a3"))
|
|
.annotated(with: Team.players.min(-Column("score"), aliased: "a4"))
|
|
.annotated(with: Team.players.sum(Column("score") * Column("score"), aliased: "a5"))
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: CustomTeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, \
|
|
AVG(("player"."score" * "player"."score")) AS "a1", \
|
|
MAX(("player"."score" * 10)) AS "a3", \
|
|
MIN(-"player"."score") AS "a4", \
|
|
SUM(("player"."score" * "player"."score")) AS "a5" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
}
|
|
}
|
|
|
|
func testMultipleCount() throws {
|
|
struct TeamInfo: Decodable, FetchableRecord {
|
|
var team: Team
|
|
var lowPlayerCount: Int
|
|
var highPlayerCount: Int
|
|
}
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.read { db in
|
|
let request = Team
|
|
.annotated(with: Team.players.filter(Column("score") < 500).forKey("lowPlayer").count())
|
|
.annotated(with: Team.players.filter(Column("score") >= 500).forKey("highPlayer").count())
|
|
.orderByPrimaryKey()
|
|
.asRequest(of: TeamInfo.self)
|
|
|
|
try assertEqualSQL(db, request, """
|
|
SELECT "team".*, \
|
|
COUNT(DISTINCT "player1"."rowid") AS "lowPlayerCount", \
|
|
COUNT(DISTINCT "player2"."rowid") AS "highPlayerCount" \
|
|
FROM "team" \
|
|
LEFT JOIN "player" "player1" ON (("player1"."teamId" = "team"."id") AND ("player1"."score" < 500)) \
|
|
LEFT JOIN "player" "player2" ON (("player2"."teamId" = "team"."id") AND ("player2"."score" >= 500)) \
|
|
GROUP BY "team"."id" \
|
|
ORDER BY "team"."id"
|
|
""")
|
|
|
|
let teamInfos = try request.fetchAll(db)
|
|
XCTAssertEqual(teamInfos.count, 3)
|
|
|
|
XCTAssertEqual(teamInfos[0].team.id, 1)
|
|
XCTAssertEqual(teamInfos[0].team.name, "Reds")
|
|
XCTAssertEqual(teamInfos[0].lowPlayerCount, 1)
|
|
XCTAssertEqual(teamInfos[0].highPlayerCount, 1)
|
|
|
|
XCTAssertEqual(teamInfos[1].team.id, 2)
|
|
XCTAssertEqual(teamInfos[1].team.name, "Blues")
|
|
XCTAssertEqual(teamInfos[1].lowPlayerCount, 1)
|
|
XCTAssertEqual(teamInfos[1].highPlayerCount, 2)
|
|
|
|
XCTAssertEqual(teamInfos[2].team.id, 3)
|
|
XCTAssertEqual(teamInfos[2].team.name, "Greens")
|
|
XCTAssertEqual(teamInfos[2].lowPlayerCount, 0)
|
|
XCTAssertEqual(teamInfos[2].highPlayerCount, 0)
|
|
}
|
|
}
|
|
}
|