|
1 | 1 | import Foundation |
2 | 2 | import Testing |
3 | 3 | @testable import Yosemite |
4 | | -import Storage |
| 4 | +@testable import Storage |
5 | 5 |
|
6 | 6 | struct POSCatalogSyncCoordinatorTests { |
7 | 7 | private let mockSyncService: MockPOSCatalogFullSyncService |
8 | 8 | private let mockSettingsStore: MockSiteSpecificAppSettingsStoreMethods |
| 9 | + private let grdbManager: GRDBManager |
9 | 10 | private let sut: POSCatalogSyncCoordinator |
10 | 11 | private let sampleSiteID: Int64 = 134 |
11 | 12 |
|
12 | | - init() { |
| 13 | + init() throws { |
13 | 14 | self.mockSyncService = MockPOSCatalogFullSyncService() |
14 | 15 | self.mockSettingsStore = MockSiteSpecificAppSettingsStoreMethods() |
| 16 | + self.grdbManager = try GRDBManager() |
15 | 17 | self.sut = POSCatalogSyncCoordinator( |
16 | 18 | syncService: mockSyncService, |
17 | | - settingsStore: mockSettingsStore |
| 19 | + settingsStore: mockSettingsStore, |
| 20 | + grdbManager: grdbManager |
18 | 21 | ) |
19 | 22 | } |
20 | 23 |
|
@@ -128,17 +131,55 @@ struct POSCatalogSyncCoordinatorTests { |
128 | 131 | #expect(shouldSyncB == true) // No previous sync |
129 | 132 | } |
130 | 133 |
|
131 | | - @Test func shouldPerformFullSync_with_zero_maxAge_always_returns_true() { |
| 134 | + @Test func shouldPerformFullSync_with_zero_maxAge_always_returns_true() throws { |
132 | 135 | // Given - previous sync was just now |
133 | 136 | let justNow = Date() |
134 | 137 | mockSettingsStore.storedDates[sampleSiteID] = justNow |
| 138 | + try createSiteInDatabase(siteID: sampleSiteID) |
135 | 139 |
|
136 | 140 | // When - max age is 0 (always sync) |
137 | 141 | let shouldSync = sut.shouldPerformFullSync(for: sampleSiteID, maxAge: 0) |
138 | 142 |
|
139 | 143 | // Then |
140 | 144 | #expect(shouldSync == true) |
141 | 145 | } |
| 146 | + |
| 147 | + // MARK: - Database Check Tests |
| 148 | + |
| 149 | + @Test func shouldPerformFullSync_returns_true_when_site_not_in_database() { |
| 150 | + // Given - site does not exist in database, but has recent sync date |
| 151 | + let recentSyncDate = Date().addingTimeInterval(-30 * 60) // 30 minutes ago |
| 152 | + mockSettingsStore.storedDates[sampleSiteID] = recentSyncDate |
| 153 | + // Note: not creating site in database so it won't exist |
| 154 | + |
| 155 | + // When - max age is 1 hour (normally wouldn't sync) |
| 156 | + let shouldSync = sut.shouldPerformFullSync(for: sampleSiteID, maxAge: 60 * 60) |
| 157 | + |
| 158 | + // Then - should sync because site doesn't exist in database |
| 159 | + #expect(shouldSync == true) |
| 160 | + } |
| 161 | + |
| 162 | + @Test func shouldPerformFullSync_respects_time_when_site_exists_in_database() throws { |
| 163 | + // Given - site exists in database with recent sync date |
| 164 | + let recentSyncDate = Date().addingTimeInterval(-30 * 60) // 30 minutes ago |
| 165 | + mockSettingsStore.storedDates[sampleSiteID] = recentSyncDate |
| 166 | + try createSiteInDatabase(siteID: sampleSiteID) |
| 167 | + |
| 168 | + // When - max age is 1 hour |
| 169 | + let shouldSync = sut.shouldPerformFullSync(for: sampleSiteID, maxAge: 60 * 60) |
| 170 | + |
| 171 | + // Then - should not sync because site exists and time hasn't passed |
| 172 | + #expect(shouldSync == false) |
| 173 | + } |
| 174 | + |
| 175 | + // MARK: - Helper Methods |
| 176 | + |
| 177 | + private func createSiteInDatabase(siteID: Int64) throws { |
| 178 | + try grdbManager.databaseConnection.write { db in |
| 179 | + let site = PersistedSite(id: siteID) |
| 180 | + try site.insert(db) |
| 181 | + } |
| 182 | + } |
142 | 183 | } |
143 | 184 |
|
144 | 185 | // MARK: - Mock Services |
|
0 commit comments