feat: Working on mocks and mock storage.
This commit is contained in:
@@ -28,3 +28,66 @@ extension User.Update: Content {}
|
||||
extension DatabaseClient.Users: TestDependencyKey {
|
||||
public static let testValue: DatabaseClient.Users = Self()
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
typealias UserMockStorage = MockStorage<
|
||||
User,
|
||||
User.Create,
|
||||
Void,
|
||||
Void,
|
||||
User.Update
|
||||
>
|
||||
|
||||
private extension UserMockStorage {
|
||||
static func make(_ mocks: [User]) -> Self {
|
||||
@Dependency(\.date.now) var now
|
||||
@Dependency(\.uuid) var uuid
|
||||
|
||||
return .init(
|
||||
create: { model in
|
||||
User(
|
||||
id: uuid(),
|
||||
email: model.email,
|
||||
username: model.username,
|
||||
createdAt: now,
|
||||
updatedAt: now
|
||||
)
|
||||
},
|
||||
update: { model, updates in
|
||||
let user = User(
|
||||
id: model.id,
|
||||
email: updates.email ?? model.email,
|
||||
username: updates.username ?? model.username,
|
||||
createdAt: model.createdAt,
|
||||
updatedAt: now
|
||||
)
|
||||
model = user
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public extension User.Token {
|
||||
static func mock(id: User.ID) -> Self {
|
||||
.init(id: UUID(0), userID: id, value: "test")
|
||||
}
|
||||
}
|
||||
|
||||
public extension DatabaseClient.Users {
|
||||
|
||||
static func mock(_ mocks: [User]) -> Self {
|
||||
let storage = UserMockStorage.make(mocks)
|
||||
return .init(
|
||||
count: { try await storage.count() },
|
||||
create: { try await storage.create($0) },
|
||||
delete: { try await storage.delete($0) },
|
||||
fetchAll: { try await storage.fetchAll() },
|
||||
get: { try await storage.get($0) },
|
||||
login: { _ in .mock(id: UUID(0)) },
|
||||
logout: { _ in },
|
||||
token: { .mock(id: $0) },
|
||||
update: { try await storage.update($0, $1) }
|
||||
)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user