Skip to content

Commit

Permalink
Test cast function
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Mar 23, 2024
1 parent 1f4d5ac commit 32642d5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
24 changes: 24 additions & 0 deletions Tests/GRDBTests/AssociationAggregateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,30 @@ class AssociationAggregateTests: GRDBTestCase {
}
}

func testCast() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
do {
let request = Team.annotated(with: cast(Team.players.count, as: .real))
try assertEqualSQL(db, request, """
SELECT "team".*, CAST(COUNT(DISTINCT "player"."id") AS REAL) AS "playerCount" \
FROM "team" \
LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
GROUP BY "team"."id"
""")
}
do {
let request = Team.annotated(with: cast(Team.players.count, as: .real).forKey("foo"))
try assertEqualSQL(db, request, """
SELECT "team".*, CAST(COUNT(DISTINCT "player"."id") AS REAL) AS "foo" \
FROM "team" \
LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
GROUP BY "team"."id"
""")
}
}
}

func testLength() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
Expand Down
10 changes: 9 additions & 1 deletion Tests/GRDBTests/QueryInterfaceExpressionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,15 @@ class QueryInterfaceExpressionsTests: GRDBTestCase {
sql(dbQueue, tableRequest.select(average(Col.age / 2, filter: Col.age > 0))),
"SELECT AVG(\"age\" / 2) FILTER (WHERE \"age\" > 0) FROM \"readers\"")
}


func testCastExpression() throws {
let dbQueue = try makeDatabaseQueue()

XCTAssertEqual(
sql(dbQueue, tableRequest.select(cast(Col.name, as: .blob))),
"SELECT CAST(\"name\" AS BLOB) FROM \"readers\"")
}

func testLengthExpression() throws {
let dbQueue = try makeDatabaseQueue()

Expand Down
4 changes: 4 additions & 0 deletions Tests/GRDBTests/SQLExpressionIsConstantTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ class SQLExpressionIsConstantTests: GRDBTestCase {
XCTAssertFalse((Column("a") - 2.databaseValue).isConstantInRequest)
XCTAssertFalse((1.databaseValue - Column("a")).isConstantInRequest)

// CAST
XCTAssertTrue(cast(1.databaseValue, as: .real).isConstantInRequest)
XCTAssertFalse(cast(Column("a"), as: .real).isConstantInRequest)

// SQLExpressionCollate
XCTAssertTrue("foo".databaseValue.collating(.binary).isConstantInRequest)
XCTAssertFalse(Column("a").collating(.binary).isConstantInRequest)
Expand Down

0 comments on commit 32642d5

Please sign in to comment.