mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Explicit tests for DatabaseValueConvertible interpolation
This commit is contained in:
1 parent
18e09cda11
commit
24c25dfd04
2 files changed
+49
No files matched your search
@@ -79,6 +79,31 @@ class SQLInterpolationTests: GRDBTestCase {
|
||||
XCTAssertEqual(sql.arguments, [1, 2, 1, 1, 2])
|
||||
}
|
||||
|
||||
func testDatabaseValueConvertibleInterpolation() {
|
||||
func test<V: DatabaseValueConvertible>(value: V, isInterpolatedAs dbValue: DatabaseValue) {
|
||||
var sql = SQLInterpolation(literalCapacity: 0, interpolationCount: 1)
|
||||
sql.appendInterpolation(value)
|
||||
XCTAssertEqual(sql.sql, "?")
|
||||
XCTAssertEqual(sql.arguments, [dbValue])
|
||||
}
|
||||
|
||||
struct V: DatabaseValueConvertible {
|
||||
var databaseValue: DatabaseValue {
|
||||
return "V".databaseValue
|
||||
}
|
||||
|
||||
static func fromDatabaseValue(_ dbValue: DatabaseValue) -> V? {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
test(value: 42, isInterpolatedAs: 42.databaseValue)
|
||||
test(value: 1.23, isInterpolatedAs: 1.23.databaseValue)
|
||||
test(value: "foo", isInterpolatedAs: "foo".databaseValue)
|
||||
test(value: "foo".data(using: .utf8)!, isInterpolatedAs: "foo".data(using: .utf8)!.databaseValue)
|
||||
test(value: V(), isInterpolatedAs: "V".databaseValue)
|
||||
}
|
||||
|
||||
func testQualifiedExpressionInterpolation() {
|
||||
var sql = SQLInterpolation(literalCapacity: 0, interpolationCount: 1)
|
||||
|
||||
|
||||
@@ -210,6 +210,30 @@ extension SQLLiteralTests {
|
||||
XCTAssertEqual(query.arguments, [1, 2, 1, 1, 2])
|
||||
}
|
||||
|
||||
func testDatabaseValueConvertibleInterpolation() {
|
||||
func test<V: DatabaseValueConvertible>(value: V, isInterpolatedAs dbValue: DatabaseValue) {
|
||||
let query: SQLLiteral = "SELECT \(value)"
|
||||
XCTAssertEqual(query.sql, "SELECT ?")
|
||||
XCTAssertEqual(query.arguments, [dbValue])
|
||||
}
|
||||
|
||||
struct V: DatabaseValueConvertible {
|
||||
var databaseValue: DatabaseValue {
|
||||
return "V".databaseValue
|
||||
}
|
||||
|
||||
static func fromDatabaseValue(_ dbValue: DatabaseValue) -> V? {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
test(value: 42, isInterpolatedAs: 42.databaseValue)
|
||||
test(value: 1.23, isInterpolatedAs: 1.23.databaseValue)
|
||||
test(value: "foo", isInterpolatedAs: "foo".databaseValue)
|
||||
test(value: "foo".data(using: .utf8)!, isInterpolatedAs: "foo".data(using: .utf8)!.databaseValue)
|
||||
test(value: V(), isInterpolatedAs: "V".databaseValue)
|
||||
}
|
||||
|
||||
func testQualifiedExpressionInterpolation() {
|
||||
let query: SQLLiteral = """
|
||||
SELECT \(Column("name").aliased("foo"))
|
||||
|
||||
Reference in new issue
Block a user