Skip to content

Commit 32642d5

Browse files
committed
Test cast function
1 parent 1f4d5ac commit 32642d5

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

Tests/GRDBTests/AssociationAggregateTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,30 @@ class AssociationAggregateTests: GRDBTestCase {
15111511
}
15121512
}
15131513

1514+
func testCast() throws {
1515+
let dbQueue = try makeDatabaseQueue()
1516+
try dbQueue.read { db in
1517+
do {
1518+
let request = Team.annotated(with: cast(Team.players.count, as: .real))
1519+
try assertEqualSQL(db, request, """
1520+
SELECT "team".*, CAST(COUNT(DISTINCT "player"."id") AS REAL) AS "playerCount" \
1521+
FROM "team" \
1522+
LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
1523+
GROUP BY "team"."id"
1524+
""")
1525+
}
1526+
do {
1527+
let request = Team.annotated(with: cast(Team.players.count, as: .real).forKey("foo"))
1528+
try assertEqualSQL(db, request, """
1529+
SELECT "team".*, CAST(COUNT(DISTINCT "player"."id") AS REAL) AS "foo" \
1530+
FROM "team" \
1531+
LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
1532+
GROUP BY "team"."id"
1533+
""")
1534+
}
1535+
}
1536+
}
1537+
15141538
func testLength() throws {
15151539
let dbQueue = try makeDatabaseQueue()
15161540
try dbQueue.read { db in

Tests/GRDBTests/QueryInterfaceExpressionsTests.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,15 @@ class QueryInterfaceExpressionsTests: GRDBTestCase {
15261526
sql(dbQueue, tableRequest.select(average(Col.age / 2, filter: Col.age > 0))),
15271527
"SELECT AVG(\"age\" / 2) FILTER (WHERE \"age\" > 0) FROM \"readers\"")
15281528
}
1529-
1529+
1530+
func testCastExpression() throws {
1531+
let dbQueue = try makeDatabaseQueue()
1532+
1533+
XCTAssertEqual(
1534+
sql(dbQueue, tableRequest.select(cast(Col.name, as: .blob))),
1535+
"SELECT CAST(\"name\" AS BLOB) FROM \"readers\"")
1536+
}
1537+
15301538
func testLengthExpression() throws {
15311539
let dbQueue = try makeDatabaseQueue()
15321540

Tests/GRDBTests/SQLExpressionIsConstantTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ class SQLExpressionIsConstantTests: GRDBTestCase {
274274
XCTAssertFalse((Column("a") - 2.databaseValue).isConstantInRequest)
275275
XCTAssertFalse((1.databaseValue - Column("a")).isConstantInRequest)
276276

277+
// CAST
278+
XCTAssertTrue(cast(1.databaseValue, as: .real).isConstantInRequest)
279+
XCTAssertFalse(cast(Column("a"), as: .real).isConstantInRequest)
280+
277281
// SQLExpressionCollate
278282
XCTAssertTrue("foo".databaseValue.collating(.binary).isConstantInRequest)
279283
XCTAssertFalse(Column("a").collating(.binary).isConstantInRequest)

0 commit comments

Comments
 (0)