Skip to content

Commit 991e405

Browse files
committed
Given/When/Then for all tests
1 parent a9048a2 commit 991e405

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

Modules/Tests/YosemiteTests/Storage/PersistedProductTests.swift

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ struct PersistedProductTests {
122122

123123
@Test("A Product's images and attributes are fetched automatically")
124124
func product_with_associations_fetches_related_records() throws {
125+
// Given
125126
let grdbManager = try GRDBManager()
126127
let db = grdbManager.databaseConnection
127128

@@ -136,15 +137,15 @@ struct PersistedProductTests {
136137
siteID: 1,
137138
name: "Test Product",
138139
productTypeKey: "simple",
139-
fullDescription: "Full description",
140-
shortDescription: "Short description",
141-
sku: "TEST-SKU",
142-
globalUniqueID: "GID-100",
140+
fullDescription: nil,
141+
shortDescription: nil,
142+
sku: nil,
143+
globalUniqueID: nil,
143144
price: "29.99",
144145
downloadable: false,
145146
parentID: 0,
146-
manageStock: true,
147-
stockQuantity: 10,
147+
manageStock: false,
148+
stockQuantity: nil,
148149
stockStatusKey: "instock"
149150
)
150151
try product.insert(db)
@@ -190,23 +191,17 @@ struct PersistedProductTests {
190191
try attribute2.insert(db)
191192
}
192193

193-
// Test automatic fetching via associations
194+
// When the product is fetched and converted to a POSProduct
194195
let fetchedProduct = try db.read { db in
195196
try PersistedProduct.filter(PersistedProduct.Columns.id == 100).fetchOne(db)
196197
}
197198

198199
let product = try #require(fetchedProduct)
199200
let posProduct = try product.toPOSProduct(db: db)
200201

201-
// Verify product fields
202+
// Then during conversion, images and attributes are populated via associations
203+
// Verify product
202204
#expect(posProduct.productID == 100)
203-
#expect(posProduct.siteID == 1)
204-
#expect(posProduct.name == "Test Product")
205-
#expect(posProduct.fullDescription == "Full description")
206-
#expect(posProduct.shortDescription == "Short description")
207-
#expect(posProduct.sku == "TEST-SKU")
208-
#expect(posProduct.price == "29.99")
209-
#expect(posProduct.stockQuantity == 10)
210205

211206
// Verify images were fetched
212207
#expect(posProduct.images.count == 2)
@@ -235,6 +230,7 @@ struct PersistedProductTests {
235230

236231
@Test("Product without related records has empty arrays")
237232
func product_without_related_records_creates_empty_arrays() throws {
233+
// Given a product without any images or attributes
238234
let grdbManager = try GRDBManager()
239235
let db = grdbManager.databaseConnection
240236

@@ -243,7 +239,6 @@ struct PersistedProductTests {
243239
let site = PersistedSite(id: 3)
244240
try site.insert(db)
245241

246-
// Insert product without any images or attributes
247242
let product = PersistedProduct(
248243
id: 300,
249244
siteID: 3,
@@ -263,20 +258,19 @@ struct PersistedProductTests {
263258
try product.insert(db)
264259
}
265260

266-
// Test that product with no related records returns empty arrays
261+
// When we retrieve and convert the product to a POSProduct
267262
let fetchedProduct = try db.read { db in
268263
try PersistedProduct.filter(PersistedProduct.Columns.id == 300).fetchOne(db)
269264
}
270265

271266
let product = try #require(fetchedProduct)
272267
let posProduct = try product.toPOSProduct(db: db)
273268

269+
// Then the POSProduct has empty arrays for images and attributes
274270
#expect(posProduct.productID == 300)
275271
#expect(posProduct.name == "Lonely Product")
276272
#expect(posProduct.images.isEmpty)
277273
#expect(posProduct.attributes.isEmpty)
278-
#expect(posProduct.fullDescription == nil)
279-
#expect(posProduct.shortDescription == nil)
280274
}
281275

282276
@Test("PersistedProductAttribute init(from:) and toProductAttribute round-trip")

Modules/Tests/YosemiteTests/Storage/PersistedProductVariationTests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ struct PersistedProductVariationTests {
112112

113113
@Test("ProductVariation with associations fetches attributes and image automatically")
114114
func product_variation_with_associations_fetches_related_records() throws {
115+
// Given a persisted variation with associations
115116
let grdbManager = try GRDBManager()
116117
let db = grdbManager.databaseConnection
117118

@@ -179,7 +180,7 @@ struct PersistedProductVariationTests {
179180
try attr2.insert(db)
180181
}
181182

182-
// Test automatic fetching via associations
183+
// When we fetch it, specifying inclusion of image and attributes
183184
struct DetailedVariation: Decodable, FetchableRecord {
184185
var variation: PersistedProductVariation
185186
var image: PersistedProductVariationImage
@@ -212,6 +213,7 @@ struct PersistedProductVariationTests {
212213
#expect(fetchedVariation?.variation.stockQuantity == nil)
213214
#expect(fetchedVariation?.variation.stockStatusKey == "outofstock")
214215

216+
// Then the image and attributes are included in the fetched data
215217
// Verify image was fetched
216218
#expect(fetchedVariation?.image.id == 600)
217219
#expect(fetchedVariation?.image.src == "https://example.com/var-img.png")
@@ -229,6 +231,7 @@ struct PersistedProductVariationTests {
229231

230232
@Test("ProductVariation without image returns nil image")
231233
func product_variation_without_image_returns_nil() throws {
234+
// Given a persisted variation without an image
232235
let grdbManager = try GRDBManager()
233236
let db = grdbManager.databaseConnection
234237

@@ -280,14 +283,15 @@ struct PersistedProductVariationTests {
280283
try attr.insert(db)
281284
}
282285

283-
// Test that variation with no image returns nil for image
286+
// When we fetch that variation
284287
let fetchedVariation = try db.read { db in
285288
try PersistedProductVariation.filter(PersistedProductVariation.Columns.id == 700).fetchOne(db)
286289
}
287290

288291
let variation = try #require(fetchedVariation)
289292
let posVariation = try variation.toPOSProductVariation(db: db)
290293

294+
// Then the image is nil
291295
#expect(posVariation.productVariationID == 700)
292296
#expect(posVariation.image == nil)
293297
#expect(posVariation.attributes.count == 1)

0 commit comments

Comments
 (0)