Skip to content

Commit

Permalink
Add async setup to TestCacheProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed Sep 27, 2024
1 parent 71bd705 commit e18d225
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 209 deletions.
27 changes: 13 additions & 14 deletions Tests/ApolloInternalTestHelpers/SQLiteTestCacheProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@ import Foundation
import Apollo
import ApolloSQLite

public class SQLiteTestCacheProvider: TestCacheProvider {
/// Execute a test block rather than return a cache synchronously, since cache setup may be
/// asynchronous at some point.
public static func withCache(initialRecords: RecordSet? = nil, fileURL: URL? = nil, execute test: (any NormalizedCache) throws -> ()) throws {
let fileURL = fileURL ?? temporarySQLiteFileURL()
let cache = try! SQLiteNormalizedCache(fileURL: fileURL)
if let initialRecords = initialRecords {
_ = try cache.merge(records: initialRecords)
}
try test(cache)
public class SQLiteTestCacheProvider: TestCacheProvider {
public static func makeNormalizedCache() async -> TestDependency<any NormalizedCache> {
await makeNormalizedCache(fileURL: temporarySQLiteFileURL())
}

public static func makeNormalizedCache(_ completionHandler: (Result<TestDependency<any NormalizedCache>, any Error>) -> ()) {
let fileURL = temporarySQLiteFileURL()

public static func makeNormalizedCache(fileURL: URL) async -> TestDependency<any NormalizedCache> {
let cache = try! SQLiteNormalizedCache(fileURL: fileURL)
completionHandler(.success((cache, nil)))
let tearDownHandler = { @Sendable in
try Self.deleteCache(at: fileURL)
}
return (cache, tearDownHandler)
}

public static func temporarySQLiteFileURL() -> URL {
Expand All @@ -30,4 +25,8 @@ public class SQLiteTestCacheProvider: TestCacheProvider {

return folder.appendingPathComponent("db.sqlite3")
}

static func deleteCache(at url: URL) throws {
try FileManager.default.removeItem(at: url)
}
}
46 changes: 15 additions & 31 deletions Tests/ApolloInternalTestHelpers/TestCacheProvider.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import XCTest
import Apollo

public typealias TearDownHandler = () throws -> ()
public typealias TearDownHandler = @Sendable () throws -> ()
public typealias TestDependency<Resource> = (Resource, TearDownHandler?)

public protocol TestCacheProvider: AnyObject {
static func makeNormalizedCache(_ completionHandler: (Result<TestDependency<any NormalizedCache>, any Error>) -> ())
static func makeNormalizedCache() async -> TestDependency<any NormalizedCache>
}

public class InMemoryTestCacheProvider: TestCacheProvider {
public static func makeNormalizedCache(_ completionHandler: (Result<TestDependency<any NormalizedCache>, any Error>) -> ()) {
public static func makeNormalizedCache() async -> TestDependency<any NormalizedCache> {
let cache = InMemoryNormalizedCache()
completionHandler(.success((cache, nil)))
return (cache, nil)
}
}

Expand All @@ -21,36 +21,20 @@ public protocol CacheDependentTesting {
}

extension CacheDependentTesting where Self: XCTestCase {
public func makeNormalizedCache() throws -> any NormalizedCache {
var result: Result<any NormalizedCache, any Error> = .failure(XCTestError(.timeoutWhileWaiting))

let expectation = XCTestExpectation(description: "Initialized normalized cache")

cacheType.makeNormalizedCache() { [weak self] testDependencyResult in
guard let self = self else { return }

result = testDependencyResult.map { testDependency in
let (cache, tearDownHandler) = testDependency

if let tearDownHandler = tearDownHandler {
self.addTeardownBlock {
do {
try tearDownHandler()
} catch {
self.record(error)
}
}
public func makeNormalizedCache() async throws -> any NormalizedCache {
let (cache, tearDownHandler) = await cacheType.makeNormalizedCache()

if let tearDownHandler = tearDownHandler {
self.addTeardownBlock {
do {
try tearDownHandler()
} catch {
self.record(error)
}

return cache
}

expectation.fulfill()
}

wait(for: [expectation], timeout: 1)

return try result.get()

return cache
}

public func mergeRecordsIntoCache(_ records: RecordSet) {
Expand Down
8 changes: 4 additions & 4 deletions Tests/ApolloTests/Cache/CacheDependentInterceptorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class CacheDependentInterceptorTests: XCTestCase, CacheDependentTesting {
var cache: (any NormalizedCache)!
var store: ApolloStore!

override func setUpWithError() throws {
try super.setUpWithError()
cache = try makeNormalizedCache()
override func setUp() async throws {
try await super.setUp()

cache = try await makeNormalizedCache()
store = ApolloStore(cache: cache)
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/ApolloTests/Cache/DeferOperationCacheReadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ class DeferOperationCacheReadTests: XCTestCase, CacheDependentTesting {
var server: MockGraphQLServer!
var client: ApolloClient!

override func setUpWithError() throws {
try super.setUpWithError()
override func setUp() async throws {
try await super.setUp()

cache = try makeNormalizedCache()
cache = try await makeNormalizedCache()
let store = ApolloStore(cache: cache)

server = MockGraphQLServer()
Expand Down
6 changes: 3 additions & 3 deletions Tests/ApolloTests/Cache/DeferOperationCacheWriteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ class DeferOperationCacheWriteTests: XCTestCase, CacheDependentTesting, StoreLoa
var cache: (any NormalizedCache)!
var store: ApolloStore!

override func setUpWithError() throws {
try super.setUpWithError()
override func setUp() async throws {
try await super.setUp()

cache = try makeNormalizedCache()
cache = try await makeNormalizedCache()
store = ApolloStore(cache: cache)
}

Expand Down
8 changes: 4 additions & 4 deletions Tests/ApolloTests/Cache/FetchQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class FetchQueryTests: XCTestCase, CacheDependentTesting {
var server: MockGraphQLServer!
var client: ApolloClient!

override func setUpWithError() throws {
try super.setUpWithError()
cache = try makeNormalizedCache()
override func setUp() async throws {
try await super.setUp()

cache = try await makeNormalizedCache()
let store = ApolloStore(cache: cache)

server = MockGraphQLServer()
Expand Down
8 changes: 4 additions & 4 deletions Tests/ApolloTests/Cache/LoadQueryFromStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class LoadQueryFromStoreTests: XCTestCase, CacheDependentTesting, StoreLoading {
var cache: (any NormalizedCache)!
var store: ApolloStore!

override func setUpWithError() throws {
try super.setUpWithError()
cache = try makeNormalizedCache()
override func setUp() async throws {
try await super.setUp()

cache = try await makeNormalizedCache()
store = ApolloStore(cache: cache)
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/ApolloTests/Cache/ReadWriteFromStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class ReadWriteFromStoreTests: XCTestCase, CacheDependentTesting, StoreLoading {
var cache: (any NormalizedCache)!
var store: ApolloStore!

override func setUpWithError() throws {
try super.setUpWithError()
override func setUp() async throws {
try await super.setUp()

cache = try makeNormalizedCache()
cache = try await makeNormalizedCache()
store = ApolloStore(cache: cache)
}

Expand Down
Loading

0 comments on commit e18d225

Please sign in to comment.