Compare commits

...
1 Commits
Author SHA1 Message Date
Gwendal Roué 389b14ede0 Tests for super encoders WIP 2024-03-18 19:07:07 +01:00
2 changed files with 113 additions and 0 deletions

No files matched your search

@@ -340,3 +340,51 @@ extension DatabaseValueConvertibleEncodableTests {
}
}
}
// MARK: - Super Encoders
extension DatabaseValueConvertibleEncodableTests {
func testSuperEncoder() throws {
// Gets compiler-synthesized `Codable` impl
class CodableSup: Codable, DatabaseValueConvertible {
var x: Int
init(_ x: Int) { self.x = x }
}
// We have to override init(from:) and encode(to:) to avoid inheriting them
class CodableSub: CodableSup {
var y: Int
init(_ x: Int, _ y: Int) { self.y = y; super.init(x) }
private enum CodingKeys: String, CodingKey {
case y
}
required init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
y = try container.decode(Int.self, forKey: .y)
// This will effectively fetch a nested container for key "super", then return a `Decoder` wrapping it
let superDecoder = try container.superDecoder()
try super.init(from: superDecoder)
}
override func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(y, forKey: .y)
// This will effectively create a nested container for key "super", then return an `Encoder` wrapping it
let superEncoder = container.superEncoder()
try super.encode(to: superEncoder)
}
}
do {
let dbValue = CodableSup(42).databaseValue
XCTAssertEqual(dbValue.storage.value as! String, #"{"x":42}"#)
let dbValue2 = CodableSub(42, 500).databaseValue
XCTAssertEqual(dbValue2.storage.value as! String, #"{"super":{"x":42},"y":500}"#)
}
}
}
@@ -844,3 +844,68 @@ extension MutablePersistableRecordEncodableTests {
fileprivate extension CodingUserInfoKey {
static let testKey = CodingUserInfoKey(rawValue: "correct")!
}
// MARK: - Super Encoders
extension MutablePersistableRecordEncodableTests {
func testSuperEncoder() throws {
// Gets compiler-synthesized `Codable` impl
class CodableSup: Codable {
var x: Int
init(_ x: Int) { self.x = x }
}
// We have to override init(from:) and encode(to:) to avoid inheriting them
class CodableSub: CodableSup {
var y: Int
init(_ x: Int, _ y: Int) { self.y = y; super.init(x) }
private enum CodingKeys: String, CodingKey {
case y
}
required init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
y = try container.decode(Int.self, forKey: .y)
// This will effectively fetch a nested container for key "super", then return a `Decoder` wrapping it
let superDecoder = try container.superDecoder()
try super.init(from: superDecoder)
}
override func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(y, forKey: .y)
// This will effectively create a nested container for key "super", then return an `Encoder` wrapping it
let superEncoder = container.superEncoder()
try super.encode(to: superEncoder)
}
}
struct Record: PersistableRecord, Encodable {
var id: String
var value: CodableSup
}
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "record") { t in
t.primaryKey("id", .text)
t.column("value", .jsonText)
}
do {
try Record(id: "sup", value: CodableSup(42)).insert(db)
let value = try String.fetchOne(db, sql: "SELECT value FROM record WHERE id = 'sup'")
XCTAssertEqual(value, #"{"x":42}"#)
}
do {
try Record(id: "sub", value: CodableSub(42, 500)).insert(db)
let value = try String.fetchOne(db, sql: "SELECT value FROM record WHERE id = 'sub'")
XCTAssertEqual(value, #"{"super":{"x":42},"y":500}"#)
}
}
}
}