Skip to content

Commit d5b2adc

Browse files
Adding failing test that includes an optional struct
This test passes with release 0.14.1, but fails starting with 0.15
1 parent e78ae02 commit d5b2adc

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Tests/SQLiteTests/Typed/QueryIntegrationTests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,37 @@ class QueryIntegrationTests: SQLiteTestCase {
152152
XCTAssertEqual(values.count, 2)
153153
}
154154

155+
func test_insert_custom_encodable_type() throws {
156+
struct TestTypeWithOptionalStruct: Codable {
157+
var myInt: Int
158+
var myString: String
159+
var myOptionalStruct: TestOptionalStruct?
160+
}
161+
struct TestOptionalStruct: Codable {
162+
var myDouble: Double
163+
var anotherDouble: Double
164+
}
165+
let table = Table("custom_codable")
166+
try db.run(table.create { builder in
167+
builder.column(Expression<Int?>("myInt"))
168+
builder.column(Expression<String?>("myString"))
169+
builder.column(Expression<String?>("myOptionalStruct"))
170+
})
171+
172+
let secondCustomType = TestOptionalStruct(myDouble: 13.42, anotherDouble: 0.0)
173+
let customType = TestTypeWithOptionalStruct(myInt: 13, myString: "foo", myOptionalStruct: secondCustomType)
174+
try db.run(table.insert(customType))
175+
let rows = try db.prepare(table)
176+
let values: [TestTypeWithOptionalStruct] = try rows.map({ try $0.decode() })
177+
XCTAssertEqual(values.count, 1, "return one optional custom type")
178+
179+
let customTypeWithNil = TestTypeWithOptionalStruct(myInt: 123, myString: "String", myOptionalStruct: nil)
180+
try db.run(table.insert(customTypeWithNil))
181+
let rowsNil = try db.prepare(table)
182+
let valuesNil: [TestTypeWithOptionalStruct] = try rowsNil.map({ try $0.decode() })
183+
XCTAssertEqual(valuesNil.count, 2, "return two custom objects, including one that contains a nil optional")
184+
}
185+
155186
func test_upsert() throws {
156187
try XCTSkipUnless(db.satisfiesMinimumVersion(minor: 24))
157188
let fetchAge = { () throws -> Int? in

0 commit comments

Comments
 (0)