@@ -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}
0 commit comments