@@ -159,7 +159,7 @@ struct POSCatalogPersistenceServiceTests {
159159 }
160160 }
161161
162- @Test func replaceAllCatalogData_removes_related_images_and_attributes ( ) async throws {
162+ @Test func replaceAllCatalogData_removes_related_images_and_attributes_for_products ( ) async throws {
163163 // Given - existing data with relations
164164 let existingProduct = POSProduct . fake ( ) . copy (
165165 siteID: sampleSiteID,
@@ -186,4 +186,39 @@ struct POSCatalogPersistenceServiceTests {
186186 #expect( attributeCount == 0 )
187187 }
188188 }
189+
190+ @Test func replaceAllCatalogData_removes_related_images_and_attributes_for_variations( ) async throws {
191+ // Given - existing data with variation relations
192+ let parentProduct = POSProduct . fake ( ) . copy (
193+ siteID: sampleSiteID,
194+ productID: 10
195+ )
196+ let existingVariation = POSProductVariation . fake ( ) . copy (
197+ siteID: sampleSiteID,
198+ productID: 10 ,
199+ productVariationID: 5 ,
200+ attributes: [ ProductVariationAttribute . fake ( ) ] ,
201+ image: ProductImage . fake ( ) . copy ( imageID: 500 )
202+ )
203+ let existingCatalog = POSCatalog ( products: [ parentProduct] , variations: [ existingVariation] )
204+ try await sut. replaceAllCatalogData ( existingCatalog, siteID: sampleSiteID)
205+
206+ // When - replace with catalog containing only parent product (no variations)
207+ let catalogWithoutVariations = POSCatalog ( products: [ parentProduct] , variations: [ ] )
208+ try await sut. replaceAllCatalogData ( catalogWithoutVariations, siteID: sampleSiteID)
209+
210+ // Then - variation and its related data should be gone
211+ let db = grdbManager. databaseConnection
212+ try await db. read { db in
213+ let productCount = try PersistedProduct . fetchCount ( db)
214+ let variationCount = try PersistedProductVariation . fetchCount ( db)
215+ let variationImageCount = try PersistedProductVariationImage . fetchCount ( db)
216+ let variationAttributeCount = try PersistedProductVariationAttribute . fetchCount ( db)
217+
218+ #expect( productCount == 1 ) // Parent product should remain
219+ #expect( variationCount == 0 ) // Variation should be gone
220+ #expect( variationImageCount == 0 ) // Variation image should be gone
221+ #expect( variationAttributeCount == 0 ) // Variation attributes should be gone
222+ }
223+ }
189224}
0 commit comments