From 359d1d8262270266b9740a220a6a08037873d1dd Mon Sep 17 00:00:00 2001 From: Brian Hackett Date: Tue, 7 Nov 2023 16:37:51 +0000 Subject: [PATCH] Updated storage fake, fixed some warnings --- .../CacheDataInteractorTests.swift | 4 +-- .../CacheDataTests.swift | 20 ++++++------- .../Helpers/CacheDataUtility.swift | 2 +- .../Helpers/StorageMock.swift | 28 +++++++++++++------ 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/Tests/ConfidenceProviderTests/CacheDataInteractorTests.swift b/Tests/ConfidenceProviderTests/CacheDataInteractorTests.swift index ab6a1249..30ba8ace 100644 --- a/Tests/ConfidenceProviderTests/CacheDataInteractorTests.swift +++ b/Tests/ConfidenceProviderTests/CacheDataInteractorTests.swift @@ -34,7 +34,7 @@ final class CacheDataInteractorTests: XCTestCase { Task { // When cache data add method is called - await cacheDataInteractor.add(resolveToken: "token", flagName: "name", applyTime: Date()) + _ = await cacheDataInteractor.add(resolveToken: "token", flagName: "name", applyTime: Date()) // Then event is added with let cache = await cacheDataInteractor.cache @@ -49,7 +49,7 @@ final class CacheDataInteractorTests: XCTestCase { Task { // When cache data add method is called - await cacheDataInteractor.add(resolveToken: "token", flagName: "name", applyTime: Date()) + _ = await cacheDataInteractor.add(resolveToken: "token", flagName: "name", applyTime: Date()) // Then event is added with let cache = await cacheDataInteractor.cache diff --git a/Tests/ConfidenceProviderTests/CacheDataTests.swift b/Tests/ConfidenceProviderTests/CacheDataTests.swift index 34fb789a..38ef7789 100644 --- a/Tests/ConfidenceProviderTests/CacheDataTests.swift +++ b/Tests/ConfidenceProviderTests/CacheDataTests.swift @@ -11,7 +11,7 @@ final class CacheDataTests: XCTestCase { // When add event is called let applyTime = Date() - cacheData.add(resolveToken: "token1", flagName: "flagName", applyTime: applyTime) + _ = cacheData.add(resolveToken: "token1", flagName: "flagName", applyTime: applyTime) // Then cache data has one resolve event XCTAssertEqual(cacheData.resolveEvents.count, 1) @@ -29,7 +29,7 @@ final class CacheDataTests: XCTestCase { var cacheData = CacheData(resolveToken: "token1", events: []) // When add event is called with token that already exist in cache data - cacheData.add(resolveToken: "token1", flagName: "flagName", applyTime: applyTime) + _ = cacheData.add(resolveToken: "token1", flagName: "flagName", applyTime: applyTime) // Then cache data has one resolve event XCTAssertEqual(cacheData.resolveEvents.count, 1) @@ -46,9 +46,9 @@ final class CacheDataTests: XCTestCase { var cacheData = try CacheDataUtility.prefilledCacheData() // When add event is called 3 times with token that already exist in cache data - cacheData.add(resolveToken: "token0", flagName: "flagName", applyTime: Date()) - cacheData.add(resolveToken: "token0", flagName: "flagName2", applyTime: Date()) - cacheData.add(resolveToken: "token0", flagName: "flagName3", applyTime: Date()) + _ = cacheData.add(resolveToken: "token0", flagName: "flagName", applyTime: Date()) + _ = cacheData.add(resolveToken: "token0", flagName: "flagName2", applyTime: Date()) + _ = cacheData.add(resolveToken: "token0", flagName: "flagName3", applyTime: Date()) // Then cache data has 6 apply events XCTAssertEqual(cacheData.resolveEvents.first?.events.count, 6) @@ -58,11 +58,11 @@ final class CacheDataTests: XCTestCase { // Given pre filled cache data let applyTime = Date(timeIntervalSince1970: 1000) var cacheData = CacheData(resolveToken: "token1", events: []) - cacheData.add(resolveToken: "token1", flagName: "flagName", applyTime: applyTime) + _ = cacheData.add(resolveToken: "token1", flagName: "flagName", applyTime: applyTime) // When add event is called with token and flagName that already exist in cache let applyTimeOther = Date(timeIntervalSince1970: 3000) - cacheData.add(resolveToken: "token1", flagName: "flagName", applyTime: applyTimeOther) + _ = cacheData.add(resolveToken: "token1", flagName: "flagName", applyTime: applyTimeOther) // Then apply record is not overriden let applyEvent = try XCTUnwrap(cacheData.resolveEvents.first?.events.first) @@ -75,9 +75,9 @@ final class CacheDataTests: XCTestCase { let date = Date(timeIntervalSince1970: 2000) // When add event is called 3 times with different tokens - cacheData.add(resolveToken: "token1", flagName: "prefilled", applyTime: date) - cacheData.add(resolveToken: "token2", flagName: "prefilled", applyTime: date) - cacheData.add(resolveToken: "token3", flagName: "prefilled", applyTime: date) + _ = cacheData.add(resolveToken: "token1", flagName: "prefilled", applyTime: date) + _ = cacheData.add(resolveToken: "token2", flagName: "prefilled", applyTime: date) + _ = cacheData.add(resolveToken: "token3", flagName: "prefilled", applyTime: date) // Then cache data has 4 resolve event XCTAssertEqual(cacheData.resolveEvents.count, 4) diff --git a/Tests/ConfidenceProviderTests/Helpers/CacheDataUtility.swift b/Tests/ConfidenceProviderTests/Helpers/CacheDataUtility.swift index 99aea167..025dee3a 100644 --- a/Tests/ConfidenceProviderTests/Helpers/CacheDataUtility.swift +++ b/Tests/ConfidenceProviderTests/Helpers/CacheDataUtility.swift @@ -26,7 +26,7 @@ enum CacheDataUtility { let applyEvent = FlagApply(name: flagName, applyTime: date) applyEvents.append(applyEvent) } else { - cacheData.add(resolveToken: resolveToken, flagName: flagName, applyTime: date) + _ = cacheData.add(resolveToken: resolveToken, flagName: flagName, applyTime: date) } } diff --git a/Tests/ConfidenceProviderTests/Helpers/StorageMock.swift b/Tests/ConfidenceProviderTests/Helpers/StorageMock.swift index a883248f..bc98ec1f 100644 --- a/Tests/ConfidenceProviderTests/Helpers/StorageMock.swift +++ b/Tests/ConfidenceProviderTests/Helpers/StorageMock.swift @@ -6,8 +6,8 @@ import XCTest class StorageMock: Storage { var data = "" - var saveExpectation: XCTestExpectation? + private let storageQueue = DispatchQueue(label: "com.confidence.storagemock") convenience init(data: Encodable) throws { self.init() @@ -15,20 +15,32 @@ class StorageMock: Storage { } func save(data: Encodable) throws { - let dataB = try JSONEncoder().encode(data) - self.data = String(data: dataB, encoding: .utf8) ?? "" + try storageQueue.sync { + let dataB = try JSONEncoder().encode(data) + self.data = String(data: dataB, encoding: .utf8) ?? "" - saveExpectation?.fulfill() + saveExpectation?.fulfill() + } } func load(defaultValue: T) throws -> T where T: Decodable { - if data.isEmpty { - return defaultValue + try storageQueue.sync { + if data.isEmpty { + return defaultValue + } + return try JSONDecoder().decode(T.self, from: data.data) } - return try JSONDecoder().decode(T.self, from: data.data) } func clear() throws { - data = "" + storageQueue.sync { + data = "" + } + } + + func isEmpty() -> Bool { + storageQueue.sync { + return data.isEmpty + } } }