@@ -152,6 +152,37 @@ class QueryIntegrationTests: SQLiteTestCase {
152
152
XCTAssertEqual ( values. count, 2 )
153
153
}
154
154
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
+
155
186
func test_upsert( ) throws {
156
187
try XCTSkipUnless ( db. satisfiesMinimumVersion ( minor: 24 ) )
157
188
let fetchAge = { ( ) throws -> Int ? in
0 commit comments