Skip to content

Commit 4f183c1

Browse files
committed
Add test cases for POSCatalogFullSyncService changes.
1 parent 527ad51 commit 4f183c1

File tree

3 files changed

+111
-3
lines changed

3 files changed

+111
-3
lines changed

Modules/Tests/YosemiteTests/Mocks/MockPOSCatalogPersistenceService.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
import Foundation
33

44
final class MockPOSCatalogPersistenceService: POSCatalogPersistenceServiceProtocol {
5+
// MARK: - replaceAllCatalogData tracking
6+
private(set) var replaceAllCatalogDataCallCount = 0
7+
private(set) var replaceAllCatalogDataLastPersistedCatalog: POSCatalog?
8+
var replaceAllCatalogDataError: Error?
9+
510
// MARK: - persistIncrementalCatalogData tracking
611
private(set) var persistIncrementalCatalogDataCallCount = 0
712
private(set) var persistIncrementalCatalogDataLastPersistedCatalog: POSCatalog?
@@ -10,7 +15,12 @@ final class MockPOSCatalogPersistenceService: POSCatalogPersistenceServiceProtoc
1015
// MARK: - Protocol Implementation
1116

1217
func replaceAllCatalogData(_ catalog: POSCatalog, siteID: Int64) async throws {
13-
// Not used in current tests
18+
replaceAllCatalogDataCallCount += 1
19+
replaceAllCatalogDataLastPersistedCatalog = catalog
20+
21+
if let error = replaceAllCatalogDataError {
22+
throw error
23+
}
1424
}
1525

1626
func persistIncrementalCatalogData(_ catalog: POSCatalog, siteID: Int64) async throws {

Modules/Tests/YosemiteTests/Mocks/MockPOSCatalogSyncRemote.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ final class MockPOSCatalogSyncRemote: POSCatalogSyncRemoteProtocol {
88
private(set) var incrementalProductResults: [Int: Result<PagedItems<POSProduct>, Error>] = [:]
99
private(set) var incrementalVariationResults: [Int: Result<PagedItems<POSProductVariation>, Error>] = [:]
1010

11+
var catalogRequestResult: Result<POSCatalogRequestResponse, Error> = .success(.init(status: .complete, downloadURL: "https://example.com/catalog.json"))
12+
var catalogDownloadResult: Result<POSCatalogResponse, Error> = .success(.init(products: [], variations: []))
13+
1114
let loadProductsCallCount = Counter()
1215
let loadProductVariationsCallCount = Counter()
1316
let loadIncrementalProductsCallCount = Counter()
@@ -129,11 +132,21 @@ final class MockPOSCatalogSyncRemote: POSCatalogSyncRemoteProtocol {
129132
// MARK: - Protocol Methods - Catalog API
130133

131134
func requestCatalogGeneration(for siteID: Int64, forceGeneration: Bool) async throws -> POSCatalogRequestResponse {
132-
.init(status: .pending, downloadURL: nil)
135+
switch catalogRequestResult {
136+
case .success(let response):
137+
return response
138+
case .failure(let error):
139+
throw error
140+
}
133141
}
134142

135143
func downloadCatalog(for siteID: Int64, downloadURL: String) async throws -> POSCatalogResponse {
136-
.init(products: [], variations: [])
144+
switch catalogDownloadResult {
145+
case .success(let response):
146+
return response
147+
case .failure(let error):
148+
throw error
149+
}
137150
}
138151

139152
// MARK: - Protocol Methods - Catalog size

Modules/Tests/YosemiteTests/Tools/POS/POSCatalogFullSyncServiceTests.swift

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,89 @@ struct POSCatalogFullSyncServiceTests {
180180
#expect(await mockSyncRemote.loadProductsCallCount.value == 5)
181181
#expect(await mockSyncRemote.loadProductVariationsCallCount.value == 5)
182182
}
183+
184+
// MARK: - Catalog API Tests
185+
186+
@Test func startFullSync_with_catalog_API_downloads_and_persists_catalog() async throws {
187+
// Given
188+
let expectedProduct = POSProduct.fake().copy(siteID: sampleSiteID, productID: 1)
189+
let expectedVariation = POSProductVariation.fake().copy(siteID: sampleSiteID, productID: 1, productVariationID: 1)
190+
191+
mockSyncRemote.catalogRequestResult = .success(.init(status: .complete, downloadURL: "https://example.com/catalog.json"))
192+
mockSyncRemote.catalogDownloadResult = .success(.init(products: [expectedProduct], variations: [expectedVariation]))
193+
194+
let sut = POSCatalogFullSyncService(
195+
syncRemote: mockSyncRemote,
196+
batchSize: 2,
197+
persistenceService: mockPersistenceService,
198+
usesCatalogAPI: true
199+
)
200+
201+
// When
202+
let result = try await sut.startFullSync(for: sampleSiteID)
203+
204+
// Then
205+
#expect(result.products.count == 1)
206+
#expect(result.variations.count == 1)
207+
#expect(mockPersistenceService.replaceAllCatalogDataCallCount == 1)
208+
#expect(mockPersistenceService.replaceAllCatalogDataLastPersistedCatalog?.products.count == 1)
209+
#expect(mockPersistenceService.replaceAllCatalogDataLastPersistedCatalog?.variations.count == 1)
210+
}
211+
212+
@Test func startFullSync_with_catalog_API_propagates_catalog_request_error() async throws {
213+
// Given
214+
let expectedError = NSError(domain: "catalog", code: 500, userInfo: [NSLocalizedDescriptionKey: "Catalog request failed"])
215+
mockSyncRemote.catalogRequestResult = .failure(expectedError)
216+
217+
let sut = POSCatalogFullSyncService(
218+
syncRemote: mockSyncRemote,
219+
batchSize: 2,
220+
persistenceService: mockPersistenceService,
221+
usesCatalogAPI: true
222+
)
223+
224+
// When/Then
225+
await #expect(throws: expectedError) {
226+
_ = try await sut.startFullSync(for: sampleSiteID)
227+
}
228+
}
229+
230+
@Test func startFullSync_with_catalog_API_propagates_catalog_download_error() async throws {
231+
// Given
232+
let expectedError = NSError(domain: "catalog", code: 404, userInfo: [NSLocalizedDescriptionKey: "Catalog download failed"])
233+
mockSyncRemote.catalogRequestResult = .success(.init(status: .complete, downloadURL: "https://example.com/catalog.json"))
234+
mockSyncRemote.catalogDownloadResult = .failure(expectedError)
235+
236+
let sut = POSCatalogFullSyncService(
237+
syncRemote: mockSyncRemote,
238+
batchSize: 2,
239+
persistenceService: mockPersistenceService,
240+
usesCatalogAPI: true
241+
)
242+
243+
// When/Then
244+
await #expect(throws: expectedError) {
245+
_ = try await sut.startFullSync(for: sampleSiteID)
246+
}
247+
}
248+
249+
@Test func startFullSync_with_catalog_API_propagates_persistence_error() async throws {
250+
// Given
251+
let expectedError = NSError(domain: "persistence", code: 1000, userInfo: [NSLocalizedDescriptionKey: "Persistence failed"])
252+
mockSyncRemote.catalogRequestResult = .success(.init(status: .complete, downloadURL: "https://example.com/catalog.json"))
253+
mockSyncRemote.catalogDownloadResult = .success(.init(products: [], variations: []))
254+
mockPersistenceService.replaceAllCatalogDataError = expectedError
255+
256+
let sut = POSCatalogFullSyncService(
257+
syncRemote: mockSyncRemote,
258+
batchSize: 2,
259+
persistenceService: mockPersistenceService,
260+
usesCatalogAPI: true
261+
)
262+
263+
// When/Then
264+
await #expect(throws: expectedError) {
265+
_ = try await sut.startFullSync(for: sampleSiteID)
266+
}
267+
}
183268
}

0 commit comments

Comments
 (0)