Skip to content

Commit

Permalink
Regression tests for #1512
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Mar 21, 2024
1 parent ac8c4ae commit a49fd85
Showing 1 changed file with 119 additions and 3 deletions.
122 changes: 119 additions & 3 deletions Tests/GRDBTests/FetchableRecordDecodableTests.swift
Expand Up @@ -263,14 +263,130 @@ extension FetchableRecordDecodableTests {

extension FetchableRecordDecodableTests {

func testStructWithData() throws {
struct StructWithData : FetchableRecord, Decodable {
let data: Data
}

let dbQueue = try makeDatabaseQueue()

do {
let data = "foo".data(using: .utf8)

do {
let value = try StructWithData(row: ["data": data])
XCTAssertEqual(value.data, data)
}

do {
let value = try dbQueue.read {
try StructWithData.fetchOne($0, sql: "SELECT ? AS data", arguments: [data])!
}
XCTAssertEqual(value.data, data)
}
}
do {
do {
_ = try StructWithData(row: ["data": nil])
XCTFail("Expected Error")
} catch let error as RowDecodingError {
switch error {
case .valueMismatch:
XCTAssertEqual(error.description, """
could not decode Data from database value NULL - \
column: "data", \
column index: 0, \
row: [data:NULL]
""")
default:
XCTFail("Unexpected Error")
}
}

do {
try dbQueue.read {
_ = try StructWithData.fetchOne($0, sql: "SELECT NULL AS data")
}
XCTFail("Expected Error")
} catch let error as RowDecodingError {
switch error {
case .valueMismatch:
XCTAssertEqual(error.description, """
could not decode Data from database value NULL - \
column: "data", \
column index: 0, \
row: [data:NULL], \
sql: `SELECT NULL AS data`, \
arguments: []
""")
default:
XCTFail("Unexpected Error")
}
}
}
}

func testStructWithDate() throws {
struct StructWithDate : FetchableRecord, Decodable {
let date: Date
}

let date = Date()
let value = try StructWithDate(row: ["date": date])
XCTAssert(abs(value.date.timeIntervalSince(date)) < 0.001)
let dbQueue = try makeDatabaseQueue()

do {
let date = Date()

do {
let value = try StructWithDate(row: ["date": date])
XCTAssert(abs(value.date.timeIntervalSince(date)) < 0.001)
}

do {
let value = try dbQueue.read {
try StructWithDate.fetchOne($0, sql: "SELECT ? AS date", arguments: [date])!
}
XCTAssert(abs(value.date.timeIntervalSince(date)) < 0.001)
}
}
do {
do {
_ = try StructWithDate(row: ["date": nil])
XCTFail("Expected Error")
} catch let error as RowDecodingError {
switch error {
case .valueMismatch:
XCTAssertEqual(error.description, """
could not decode Date from database value NULL - \
column: "date", \
column index: 0, \
row: [date:NULL]
""")
default:
XCTFail("Unexpected Error")
}
}

do {
try dbQueue.read {
_ = try StructWithDate.fetchOne($0, sql: "SELECT NULL AS date")
}
XCTFail("Expected Error")
} catch let error as RowDecodingError {
switch error {
case .valueMismatch:
XCTAssertEqual(error.description, """
could not decode Date from database value NULL - \
column: "date", \
column index: 0, \
row: [date:NULL], \
sql: `SELECT NULL AS date`, \
arguments: []
""")
default:
XCTFail("Unexpected Error")
}
}
}
}

func testStructWithURL() throws {
Expand Down

0 comments on commit a49fd85

Please sign in to comment.