migration
This commit is contained in:
37
PediFoodsTests/ApiPizzaModelsDecodingTests.swift
Normal file
37
PediFoodsTests/ApiPizzaModelsDecodingTests.swift
Normal file
@@ -0,0 +1,37 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable import PediFoods
|
||||
|
||||
private func decode<T: Decodable>(_ type: T.Type, _ json: String) throws -> T {
|
||||
try JSONDecoder().decode(T.self, from: Data(json.utf8))
|
||||
}
|
||||
|
||||
@Test("StorePizzaConfig defaults every list to empty when fields are missing")
|
||||
func storePizzaConfigDefaultsToEmptyLists() throws {
|
||||
let config = try decode(StorePizzaConfig.self, "{}")
|
||||
#expect(config.sizes.isEmpty)
|
||||
#expect(config.doughs.isEmpty)
|
||||
#expect(config.crusts.isEmpty)
|
||||
}
|
||||
|
||||
@Test("StorePizzaCrust falls back to a placeholder id when missing")
|
||||
func storePizzaCrustPlaceholderId() throws {
|
||||
let crust = try decode(StorePizzaCrust.self, "{}")
|
||||
#expect(crust.id.isEmpty == false)
|
||||
#expect(crust.priceModifier == nil)
|
||||
}
|
||||
|
||||
@Test("StorePizzaCrust coerces priceModifier from Double, Int, or comma-decimal String")
|
||||
func storePizzaCrustPriceModifierCoercion() throws {
|
||||
let asDouble = try decode(StorePizzaCrust.self, #"{"id":"1","priceModifier":3.5}"#)
|
||||
#expect(asDouble.priceModifier == 3.5)
|
||||
|
||||
let asInt = try decode(StorePizzaCrust.self, #"{"id":"1","priceModifier":4}"#)
|
||||
#expect(asInt.priceModifier == 4.0)
|
||||
|
||||
let asString = try decode(StorePizzaCrust.self, #"{"id":"1","priceModifier":"3,50"}"#)
|
||||
#expect(asString.priceModifier == 3.5)
|
||||
|
||||
let malformed = try decode(StorePizzaCrust.self, #"{"id":"1","priceModifier":"not-a-number"}"#)
|
||||
#expect(malformed.priceModifier == nil)
|
||||
}
|
||||
Reference in New Issue
Block a user