Skip to content

Commit 1dd2103

Browse files
committed
Recover parts of incremental sync service test cases.
1 parent 9f8289f commit 1dd2103

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import Foundation
2+
import Testing
3+
@testable import Networking
4+
@testable import Yosemite
5+
6+
struct POSCatalogIncrementalSyncServiceTests {
7+
private let sut: POSCatalogIncrementalSyncService
8+
private let mockSyncRemote: MockPOSCatalogSyncRemote
9+
private let sampleSiteID: Int64 = 134
10+
11+
init() {
12+
self.mockSyncRemote = MockPOSCatalogSyncRemote()
13+
self.sut = POSCatalogIncrementalSyncService(syncRemote: mockSyncRemote, batchSize: 2)
14+
}
15+
16+
// MARK: - Basic Incremental Sync Tests
17+
18+
@Test func startIncrementalSync_uses_lastFullSyncDate_as_modifiedAfter_date_when_not_synced_before() async throws {
19+
// Given
20+
let lastFullSyncDate = Date(timeIntervalSince1970: 1000)
21+
let expectedProducts = [POSProduct.fake(), POSProduct.fake()]
22+
let expectedVariations = [POSProductVariation.fake()]
23+
24+
mockSyncRemote.setIncrementalProductResult(pageNumber: 1, result: .success(PagedItems(items: expectedProducts, hasMorePages: false, totalItems: 0)))
25+
mockSyncRemote.setIncrementalVariationResult(pageNumber: 1, result: .success(PagedItems(items: expectedVariations, hasMorePages: false, totalItems: 0)))
26+
27+
// When
28+
try await sut.startIncrementalSync(for: sampleSiteID, lastFullSyncDate: lastFullSyncDate)
29+
30+
// Then
31+
#expect(mockSyncRemote.loadIncrementalProductsCallCount == 2)
32+
#expect(mockSyncRemote.loadIncrementalProductVariationsCallCount == 2)
33+
#expect(mockSyncRemote.lastIncrementalProductsModifiedAfter == lastFullSyncDate)
34+
#expect(mockSyncRemote.lastIncrementalVariationsModifiedAfter == lastFullSyncDate)
35+
}
36+
37+
@Test func startIncrementalSync_uses_last_incremental_sync_date_as_modifiedAfter_date_when_available() async throws {
38+
// Given
39+
let lastFullSyncDate = Date(timeIntervalSince1970: 1000)
40+
let lastIncrementalDate = Date(timeIntervalSince1970: 2000)
41+
42+
// First sync to establish incremental date.
43+
mockSyncRemote.setIncrementalProductResult(pageNumber: 1, result: .success(PagedItems(items: [], hasMorePages: false, totalItems: 0)))
44+
mockSyncRemote.setIncrementalVariationResult(pageNumber: 1, result: .success(PagedItems(items: [], hasMorePages: false, totalItems: 0)))
45+
try await sut.startIncrementalSync(for: sampleSiteID, lastFullSyncDate: lastIncrementalDate)
46+
47+
// When
48+
try await sut.startIncrementalSync(for: sampleSiteID, lastFullSyncDate: lastFullSyncDate)
49+
50+
// Then
51+
#expect(mockSyncRemote.lastIncrementalProductsModifiedAfter != lastFullSyncDate)
52+
#expect(mockSyncRemote.lastIncrementalVariationsModifiedAfter != lastFullSyncDate)
53+
}
54+
55+
// MARK: - Pagination Tests
56+
57+
@Test func startIncrementalSync_handles_paginated_products_correctly() async throws {
58+
// Given - 3 pages of products
59+
let lastFullSyncDate = Date(timeIntervalSince1970: 1000)
60+
let page1Products = [POSProduct.fake()]
61+
let page2Products = [POSProduct.fake()]
62+
let page3Products = [POSProduct.fake()]
63+
64+
mockSyncRemote.setIncrementalProductResults([
65+
PagedItems(items: page1Products, hasMorePages: true, totalItems: 3),
66+
PagedItems(items: page2Products, hasMorePages: true, totalItems: 3),
67+
PagedItems(items: page3Products, hasMorePages: false, totalItems: 3)
68+
])
69+
mockSyncRemote.setIncrementalVariationResult(pageNumber: 1, result: .success(PagedItems(items: [], hasMorePages: false, totalItems: 0)))
70+
71+
// When
72+
try await sut.startIncrementalSync(for: sampleSiteID, lastFullSyncDate: lastFullSyncDate)
73+
74+
// Then
75+
#expect(mockSyncRemote.loadIncrementalProductsCallCount == 4)
76+
}
77+
78+
@Test func startIncrementalSync_handles_paginated_variations_correctly() async throws {
79+
// Given - 2 pages of variations
80+
let lastFullSyncDate = Date(timeIntervalSince1970: 1000)
81+
let page1Variations = [POSProductVariation.fake()]
82+
let page2Variations = [POSProductVariation.fake()]
83+
84+
mockSyncRemote.setIncrementalProductResult(pageNumber: 1, result: .success(PagedItems(items: [], hasMorePages: false, totalItems: 0)))
85+
mockSyncRemote.setIncrementalVariationResults([
86+
PagedItems(items: page1Variations, hasMorePages: true, totalItems: 2),
87+
PagedItems(items: page2Variations, hasMorePages: false, totalItems: 2)
88+
])
89+
90+
// When
91+
try await sut.startIncrementalSync(for: sampleSiteID, lastFullSyncDate: lastFullSyncDate)
92+
93+
// Then
94+
#expect(mockSyncRemote.loadIncrementalProductVariationsCallCount == 2)
95+
}
96+
97+
// MARK: - Error Handling Tests
98+
99+
@Test func startIncrementalSync_throws_error_when_product_loading_fails() async throws {
100+
// Given
101+
let lastFullSyncDate = Date(timeIntervalSince1970: 1000)
102+
let expectedError = NSError(domain: "test", code: 500, userInfo: nil)
103+
104+
mockSyncRemote.setIncrementalProductResult(pageNumber: 1, result: .failure(expectedError))
105+
mockSyncRemote.setIncrementalVariationResult(pageNumber: 1, result: .success(PagedItems(items: [], hasMorePages: false, totalItems: 0)))
106+
107+
// When/Then
108+
await #expect(throws: expectedError) {
109+
try await sut.startIncrementalSync(for: sampleSiteID, lastFullSyncDate: lastFullSyncDate)
110+
}
111+
112+
// When attempting a second sync
113+
mockSyncRemote.setIncrementalProductResult(pageNumber: 1, result: .success(PagedItems(items: [], hasMorePages: false, totalItems: 0)))
114+
try await sut.startIncrementalSync(for: sampleSiteID, lastFullSyncDate: lastFullSyncDate)
115+
116+
// Then it uses lastFullSyncDate since no incremental date was stored due to previous failure
117+
#expect(mockSyncRemote.lastIncrementalProductsModifiedAfter == lastFullSyncDate)
118+
}
119+
120+
// MARK: - Per-Site Behavior Tests
121+
122+
@Test func startIncrementalSync_manages_sync_dates_per_site() async throws {
123+
// Given
124+
let site1ID: Int64 = 123
125+
let site2ID: Int64 = 456
126+
let lastFullSyncDate = Date(timeIntervalSince1970: 1000)
127+
128+
mockSyncRemote.setIncrementalProductResult(pageNumber: 1, result: .success(PagedItems(items: [], hasMorePages: false, totalItems: 0)))
129+
mockSyncRemote.setIncrementalVariationResult(pageNumber: 1, result: .success(PagedItems(items: [], hasMorePages: false, totalItems: 0)))
130+
131+
// When - Sync site 1
132+
try await sut.startIncrementalSync(for: site1ID, lastFullSyncDate: lastFullSyncDate)
133+
let site1ModifiedAfter = try #require(mockSyncRemote.lastIncrementalProductsModifiedAfter)
134+
135+
// When - Sync site 2
136+
try await sut.startIncrementalSync(for: site2ID, lastFullSyncDate: lastFullSyncDate)
137+
let site2ModifiedAfter = try #require(mockSyncRemote.lastIncrementalProductsModifiedAfter)
138+
139+
#expect(site1ModifiedAfter == lastFullSyncDate)
140+
#expect(site2ModifiedAfter == lastFullSyncDate)
141+
}
142+
}

0 commit comments

Comments
 (0)