Skip to content

Commit dd9ee72

Browse files
committed
Tests for saving POS models to GRDB
1 parent bded87a commit dd9ee72

File tree

2 files changed

+155
-20
lines changed

2 files changed

+155
-20
lines changed

Modules/Tests/YosemiteTests/Storage/PersistedProductTests.swift

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,6 @@ struct PersistedProductTests {
1111
// Given
1212
let siteID: Int64 = 1
1313
let productID: Int64 = 10
14-
let images: [Yosemite.ProductImage] = [
15-
ProductImage(imageID: 100,
16-
dateCreated: Date(timeIntervalSince1970: 1),
17-
dateModified: nil,
18-
src: "https://example.com/1.png",
19-
name: "img1",
20-
alt: "alt1"),
21-
ProductImage(imageID: 101,
22-
dateCreated: Date(timeIntervalSince1970: 2),
23-
dateModified: Date(timeIntervalSince1970: 3),
24-
src: "https://example.com/2.png",
25-
name: "img2",
26-
alt: nil)
27-
]
28-
let attributes: [Yosemite.ProductAttribute] = [
29-
ProductAttribute(siteID: siteID, attributeID: 0, name: "Color", position: 0, visible: true, variation: true, options: ["Red", "Blue"]),
30-
ProductAttribute(siteID: siteID, attributeID: 0, name: "Size", position: 1, visible: false, variation: false, options: ["S", "M"])
31-
]
3214
let posProduct = POSProduct(
3315
siteID: siteID,
3416
productID: productID,
@@ -41,8 +23,8 @@ struct PersistedProductTests {
4123
price: "9.99",
4224
downloadable: false,
4325
parentID: 0,
44-
images: images,
45-
attributes: attributes,
26+
images: [],
27+
attributes: [],
4628
manageStock: true,
4729
stockQuantity: 5,
4830
stockStatusKey: "instock"
@@ -361,4 +343,70 @@ struct PersistedProductTests {
361343
#expect(back.name == image.name)
362344
#expect(back.alt == image.alt)
363345
}
346+
347+
@Test("POSProduct.save() persists complete product with relationships")
348+
func test_save_persists_complete_pos_product() throws {
349+
let grdbManager = try GRDBManager()
350+
let db = grdbManager.databaseConnection
351+
352+
// Setup site
353+
try db.write { db in
354+
let site = PersistedSite(id: 1)
355+
try site.insert(db)
356+
}
357+
358+
// Given a complete POSProduct
359+
let posProduct = POSProduct(
360+
siteID: 1,
361+
productID: 123,
362+
name: "Complete Product",
363+
productTypeKey: "variable",
364+
fullDescription: "Complete description",
365+
shortDescription: "Short",
366+
sku: "COMPLETE-SKU",
367+
globalUniqueID: "GID-123",
368+
price: "99.99",
369+
downloadable: true,
370+
parentID: 0,
371+
images: [
372+
ProductImage(imageID: 1001, dateCreated: Date(), dateModified: nil, src: "https://example.com/1.png", name: "img1", alt: "alt1"),
373+
ProductImage(imageID: 1002, dateCreated: Date(), dateModified: nil, src: "https://example.com/2.png", name: "img2", alt: nil)
374+
],
375+
attributes: [
376+
ProductAttribute(siteID: 1, attributeID: 0, name: "Color", position: 0, visible: true, variation: true, options: ["Red", "Blue"]),
377+
ProductAttribute(siteID: 1, attributeID: 0, name: "Size", position: 1, visible: true, variation: false, options: ["S", "M", "L"])
378+
],
379+
manageStock: true,
380+
stockQuantity: 50,
381+
stockStatusKey: "instock"
382+
)
383+
384+
// When saving and loading back
385+
try posProduct.save(to: db)
386+
387+
let fetchedProduct = try db.read { db in
388+
try PersistedProduct.filter(PersistedProduct.Columns.id == 123).fetchOne(db)
389+
}
390+
let product = try #require(fetchedProduct)
391+
let loadedPOSProduct = try product.toPOSProduct(db: db)
392+
393+
// Then all data should be preserved
394+
#expect(loadedPOSProduct.productID == posProduct.productID)
395+
#expect(loadedPOSProduct.name == posProduct.name)
396+
#expect(loadedPOSProduct.fullDescription == posProduct.fullDescription)
397+
#expect(loadedPOSProduct.shortDescription == posProduct.shortDescription)
398+
#expect(loadedPOSProduct.sku == posProduct.sku)
399+
#expect(loadedPOSProduct.images.count == 2)
400+
#expect(loadedPOSProduct.attributes.count == 2)
401+
402+
// Verify images preserved
403+
let img1 = loadedPOSProduct.images.first { $0.imageID == 1001 }
404+
#expect(img1?.name == "img1")
405+
#expect(img1?.alt == "alt1")
406+
407+
// Verify attributes preserved
408+
let colorAttr = loadedPOSProduct.attributes.first { $0.name == "Color" }
409+
#expect(colorAttr?.options == ["Red", "Blue"])
410+
#expect(colorAttr?.variation == true)
411+
}
364412
}

Modules/Tests/YosemiteTests/Storage/PersistedProductVariationTests.swift

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,91 @@ struct PersistedProductVariationTests {
332332
#expect(back.name == image.name)
333333
#expect(back.alt == image.alt)
334334
}
335+
336+
@Test("POSProductVariation.save() persists complete variation with relationships")
337+
func test_save_persists_complete_pos_product_variation() throws {
338+
let grdbManager = try GRDBManager()
339+
let db = grdbManager.databaseConnection
340+
341+
// Setup site and parent product
342+
try db.write { db in
343+
let site = PersistedSite(id: 1)
344+
try site.insert(db)
345+
346+
let parentProduct = PersistedProduct(
347+
id: 200,
348+
siteID: 1,
349+
name: "Parent Product",
350+
productTypeKey: "variable",
351+
fullDescription: nil,
352+
shortDescription: nil,
353+
sku: "PARENT-SKU",
354+
globalUniqueID: nil,
355+
price: "0.00",
356+
downloadable: false,
357+
parentID: 0,
358+
manageStock: false,
359+
stockQuantity: nil,
360+
stockStatusKey: "instock"
361+
)
362+
try parentProduct.insert(db)
363+
}
364+
365+
// Given a complete POSProductVariation
366+
let posVariation = POSProductVariation(
367+
siteID: 1,
368+
productID: 200,
369+
productVariationID: 456,
370+
attributes: [
371+
ProductVariationAttribute(id: 0, name: "Color", option: "Red"),
372+
ProductVariationAttribute(id: 0, name: "Size", option: "Large")
373+
],
374+
image: ProductImage(imageID: 2001,
375+
dateCreated: Date(),
376+
dateModified: nil,
377+
src: "https://example.com/var.png",
378+
name: "var-img",
379+
alt: "variation image"),
380+
fullDescription: "Complete variation description",
381+
sku: "VAR-COMPLETE-SKU",
382+
globalUniqueID: "GID-456",
383+
price: "149.99",
384+
downloadable: false,
385+
manageStock: true,
386+
stockQuantity: 25,
387+
stockStatusKey: "instock"
388+
)
389+
390+
// When saving and loading back
391+
try posVariation.save(to: db)
392+
393+
let fetchedVariation = try db.read { db in
394+
try PersistedProductVariation.filter(PersistedProductVariation.Columns.id == 456).fetchOne(db)
395+
}
396+
let variation = try #require(fetchedVariation)
397+
let loadedPOSVariation = try variation.toPOSProductVariation(db: db)
398+
399+
// Then all data should be preserved
400+
#expect(loadedPOSVariation.productVariationID == posVariation.productVariationID)
401+
#expect(loadedPOSVariation.productID == posVariation.productID)
402+
#expect(loadedPOSVariation.siteID == posVariation.siteID)
403+
#expect(loadedPOSVariation.fullDescription == posVariation.fullDescription)
404+
#expect(loadedPOSVariation.sku == posVariation.sku)
405+
#expect(loadedPOSVariation.price == posVariation.price)
406+
#expect(loadedPOSVariation.stockQuantity == posVariation.stockQuantity)
407+
#expect(loadedPOSVariation.attributes.count == 2)
408+
#expect(loadedPOSVariation.image != nil)
409+
410+
// Verify image preserved
411+
#expect(loadedPOSVariation.image?.imageID == 2001)
412+
#expect(loadedPOSVariation.image?.name == "var-img")
413+
#expect(loadedPOSVariation.image?.alt == "variation image")
414+
415+
// Verify attributes preserved
416+
let colorAttr = loadedPOSVariation.attributes.first { $0.name == "Color" }
417+
#expect(colorAttr?.option == "Red")
418+
419+
let sizeAttr = loadedPOSVariation.attributes.first { $0.name == "Size" }
420+
#expect(sizeAttr?.option == "Large")
421+
}
335422
}

0 commit comments

Comments
 (0)