Skip to content

Commit

Permalink
Failing test for #1565
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Jul 9, 2024
1 parent 78545f2 commit 5999227
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Tests/GRDBTests/MutablePersistableRecordEncodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,47 @@ extension MutablePersistableRecordEncodableTests {
XCTAssertEqual(string, "foo (MutablePersistableRecord)")
}
}

// Regression test for <https://github.com/groue/GRDB.swift/issues/1565>
func testSingleValueContainer() throws {
struct Struct: Encodable {
let value: String
}

struct Wrapper<Model: Encodable>: MutablePersistableRecord, Encodable {
static var databaseTableName: String { "t1" }
var model: Model
var otherValue: String

enum CodingKeys: String, CodingKey {
case otherValue
}

func encode(to encoder: any Encoder) throws {
var modelContainer = encoder.singleValueContainer()
try modelContainer.encode(model)

var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(otherValue, forKey: .otherValue)
}
}

let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.create(table: "t1") { t in
t.column("value", .text)
t.column("otherValue", .text)
}

var value = Wrapper(model: Struct(value: "foo"), otherValue: "bar")
try assert(value, isEncodedIn: ["value": "foo", "otherValue": "bar"])

try value.insert(db)
let row = try Row.fetchOne(db, sql: "SELECT value, otherValue FROM t1")!
XCTAssertEqual(row[0], "foo")
XCTAssertEqual(row[1], "bar")
}
}
}

// MARK: - Different kinds of single-value properties
Expand Down

0 comments on commit 5999227

Please sign in to comment.