feat: Working on mocks and mock storage.
This commit is contained in:
@@ -15,7 +15,6 @@ public extension DatabaseClient {
|
||||
public enum FetchRequest: Equatable {
|
||||
case all
|
||||
case `for`(vendorID: Vendor.ID)
|
||||
case withVendor
|
||||
}
|
||||
|
||||
public func fetchAll() async throws -> [VendorBranch] {
|
||||
@@ -32,3 +31,61 @@ extension DatabaseClient.VendorBranches.FetchRequest: Content {}
|
||||
extension DatabaseClient.VendorBranches: TestDependencyKey {
|
||||
public static let testValue: DatabaseClient.VendorBranches = Self()
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
typealias VendorBranchMockStorage = MockStorage<
|
||||
VendorBranch,
|
||||
VendorBranch.Create,
|
||||
DatabaseClient.VendorBranches.FetchRequest,
|
||||
Void,
|
||||
VendorBranch.Update
|
||||
>
|
||||
|
||||
private extension VendorBranchMockStorage {
|
||||
|
||||
init(_ mocks: [VendorBranch]) {
|
||||
@Dependency(\.date.now) var now
|
||||
@Dependency(\.uuid) var uuid
|
||||
|
||||
self.init(
|
||||
mocks,
|
||||
create: {
|
||||
VendorBranch(id: uuid(), name: $0.name, vendorID: $0.vendorID, createdAt: now, updatedAt: now)
|
||||
},
|
||||
fetch: { request in
|
||||
switch request {
|
||||
case .all:
|
||||
return { _ in true }
|
||||
case let .for(vendorID):
|
||||
return { $0.vendorID == vendorID }
|
||||
}
|
||||
},
|
||||
update: { branch, updates in
|
||||
let model = VendorBranch(
|
||||
id: branch.id,
|
||||
name: updates.name ?? branch.name,
|
||||
vendorID: branch.vendorID,
|
||||
createdAt: branch.createdAt,
|
||||
updatedAt: now
|
||||
)
|
||||
|
||||
branch = model
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public extension DatabaseClient.VendorBranches {
|
||||
static func mock(_ mocks: [VendorBranch] = []) -> Self {
|
||||
let storage = VendorBranchMockStorage(mocks)
|
||||
return .init(
|
||||
create: { try await storage.create($0) },
|
||||
delete: { try await storage.delete($0) },
|
||||
fetchAll: { try await storage.fetchAll($0) },
|
||||
get: { try await storage.get($0) },
|
||||
update: { try await storage.update($0, $1) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user