Skip to content

Commit a9048a2

Browse files
committed
Demonstrate joined records in variation tests
1 parent 5c8d942 commit a9048a2

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

Modules/Sources/Storage/GRDB/Model/PersistedProductVariation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ extension PersistedProductVariation: FetchableRecord, PersistableRecord {
5656
static let stockStatusKey = Column(CodingKeys.stockStatusKey)
5757
}
5858

59-
public static let attributes = hasMany(PersistedProductVariationAttribute.self)
60-
public static let image = hasOne(PersistedProductVariationImage.self)
59+
public static let attributes = hasMany(PersistedProductVariationAttribute.self).forKey("attributes")
60+
public static let image = hasOne(PersistedProductVariationImage.self).forKey("image")
6161
}
6262

6363

Modules/Tests/YosemiteTests/Storage/PersistedProductVariationTests.swift

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -180,37 +180,50 @@ struct PersistedProductVariationTests {
180180
}
181181

182182
// Test automatic fetching via associations
183-
let fetchedVariation = try db.read { db in
184-
try PersistedProductVariation.filter(PersistedProductVariation.Columns.id == 500).fetchOne(db)
183+
struct DetailedVariation: Decodable, FetchableRecord {
184+
var variation: PersistedProductVariation
185+
var image: PersistedProductVariationImage
186+
var attributes: [PersistedProductVariationAttribute]
187+
188+
enum CodingKeys: CodingKey {
189+
case variation
190+
case image
191+
case attributes
192+
}
185193
}
186194

187-
let variation = try #require(fetchedVariation)
188-
let posVariation = try variation.toPOSProductVariation(db: db)
195+
let fetchedVariation = try db.read { db in
196+
try PersistedProductVariation
197+
.including(optional: PersistedProductVariation.image)
198+
.including(all: PersistedProductVariation.attributes)
199+
.asRequest(of: DetailedVariation.self)
200+
.fetchAll(db)
201+
}.first
189202

190203
// Verify variation fields
191-
#expect(posVariation.productVariationID == 500)
192-
#expect(posVariation.siteID == 2)
193-
#expect(posVariation.productID == 50)
194-
#expect(posVariation.sku == "VAR-SKU")
195-
#expect(posVariation.fullDescription == "Variation description")
196-
#expect(posVariation.price == "15.50")
197-
#expect(posVariation.downloadable == true)
198-
#expect(posVariation.manageStock == false)
199-
#expect(posVariation.stockQuantity == nil)
200-
#expect(posVariation.stockStatusKey == "outofstock")
204+
#expect(fetchedVariation?.variation.id == 500)
205+
#expect(fetchedVariation?.variation.siteID == 2)
206+
#expect(fetchedVariation?.variation.productID == 50)
207+
#expect(fetchedVariation?.variation.sku == "VAR-SKU")
208+
#expect(fetchedVariation?.variation.fullDescription == "Variation description")
209+
#expect(fetchedVariation?.variation.price == "15.50")
210+
#expect(fetchedVariation?.variation.downloadable == true)
211+
#expect(fetchedVariation?.variation.manageStock == false)
212+
#expect(fetchedVariation?.variation.stockQuantity == nil)
213+
#expect(fetchedVariation?.variation.stockStatusKey == "outofstock")
201214

202215
// Verify image was fetched
203-
#expect(posVariation.image?.imageID == 600)
204-
#expect(posVariation.image?.src == "https://example.com/var-img.png")
205-
#expect(posVariation.image?.name == "Variation Image")
206-
#expect(posVariation.image?.alt == "Variation alt text")
216+
#expect(fetchedVariation?.image.id == 600)
217+
#expect(fetchedVariation?.image.src == "https://example.com/var-img.png")
218+
#expect(fetchedVariation?.image.name == "Variation Image")
219+
#expect(fetchedVariation?.image.alt == "Variation alt text")
207220

208221
// Verify attributes were fetched
209-
#expect(posVariation.attributes.count == 2)
210-
let colorAttr = posVariation.attributes.first { $0.name == "Color" }
222+
#expect(fetchedVariation?.attributes.count == 2)
223+
let colorAttr = fetchedVariation?.attributes.first { $0.name == "Color" }
211224
#expect(colorAttr?.option == "Purple")
212225

213-
let materialAttr = posVariation.attributes.first { $0.name == "Material" }
226+
let materialAttr = fetchedVariation?.attributes.first { $0.name == "Material" }
214227
#expect(materialAttr?.option == "Cotton")
215228
}
216229

0 commit comments

Comments
 (0)