-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodoRepositoryImplTests.swift
More file actions
191 lines (159 loc) · 5.82 KB
/
Copy pathTodoRepositoryImplTests.swift
File metadata and controls
191 lines (159 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// TodoRepositoryImplTests.swift
// DevLogDataTests
//
// Created by opfic on 6/1/26.
//
import Combine
import Foundation
import Testing
import DevLogCore
import DevLogDomain
@testable @preconcurrency import DevLogData
struct TodoRepositoryImplTests {
@Test("Todo 변경 성공 시 위젯 동기화와 mutation 이벤트를 발행한다")
func todo_변경_성공_시_위젯_동기화와_mutation_이벤트를_발행한다() async throws {
let fixture = makeFixture()
let todo = makeTodo()
try await fixture.repository.upsertTodo(todo)
try await fixture.repository.deleteTodo(todo.id)
try await fixture.repository.undoDeleteTodo(todo.id)
let events = fixture.widgetSyncEventBus.events
#expect(events == [.syncRequested, .syncRequested, .syncRequested])
let mutationEvents = fixture.todoMutationEventBus.publishedEvents()
#expect(mutationEvents == [.updated(todo.id), .deleted(todo.id), .restored(todo.id)])
}
@Test("Todo 변경 실패 시 위젯 동기화와 mutation 이벤트를 발행하지 않는다")
func todo_변경_실패_시_위젯_동기화와_mutation_이벤트를_발행하지_않는다() async throws {
let fixture = makeFixture()
let todo = makeTodo()
await fixture.todoService.setShouldFail(true)
do {
try await fixture.repository.upsertTodo(todo)
Issue.record("upsertTodo should fail")
} catch {
#expect(error as? TodoRepositoryImplTestsError == .serviceFailed)
}
do {
try await fixture.repository.deleteTodo(todo.id)
Issue.record("deleteTodo should fail")
} catch {
#expect(error as? TodoRepositoryImplTestsError == .serviceFailed)
}
do {
try await fixture.repository.undoDeleteTodo(todo.id)
Issue.record("undoDeleteTodo should fail")
} catch {
#expect(error as? TodoRepositoryImplTestsError == .serviceFailed)
}
let syncEvents = fixture.widgetSyncEventBus.events
#expect(syncEvents.isEmpty)
let mutationEvents = fixture.todoMutationEventBus.publishedEvents()
#expect(mutationEvents.isEmpty)
}
private func makeFixture() -> Fixture {
let todoService = TodoServiceSpy()
let todoCategoryService = TodoCategoryServiceSpy()
let widgetSyncEventBus = WidgetSyncEventBusSpy()
let todoMutationEventBus = TodoMutationEventBusSpy()
let repository = TodoRepositoryImpl(
todoService: todoService,
todoCategoryService: todoCategoryService,
widgetSyncEventBus: widgetSyncEventBus,
todoMutationEventBus: todoMutationEventBus
)
return Fixture(
repository: repository,
todoService: todoService,
widgetSyncEventBus: widgetSyncEventBus,
todoMutationEventBus: todoMutationEventBus
)
}
private func makeTodo() -> Todo {
Todo(
id: "todo-id",
isPinned: false,
isCompleted: false,
isChecked: false,
number: 1,
title: "title",
content: "content",
createdAt: .now,
updatedAt: .now,
completedAt: nil,
deletedAt: nil,
dueDate: nil,
tags: [],
category: .system(.feature)
)
}
}
private struct Fixture {
let repository: TodoRepositoryImpl
let todoService: TodoServiceSpy
let widgetSyncEventBus: WidgetSyncEventBusSpy
let todoMutationEventBus: TodoMutationEventBusSpy
}
private actor TodoServiceSpy: TodoService {
private var shouldFail = false
func setShouldFail(_ shouldFail: Bool) {
self.shouldFail = shouldFail
}
func fetchTodos(_ query: TodoQuery, cursor: TodoCursorDTO?) async throws -> TodoPageResponse {
throw TodoRepositoryImplTestsError.unexpectedCall
}
func upsertTodo(request: TodoRequest) async throws {
guard shouldFail == false else {
throw TodoRepositoryImplTestsError.serviceFailed
}
}
func deleteTodo(todoId: String) async throws {
guard shouldFail == false else {
throw TodoRepositoryImplTestsError.serviceFailed
}
}
func undoDeleteTodo(todoId: String) async throws {
guard shouldFail == false else {
throw TodoRepositoryImplTestsError.serviceFailed
}
}
func fetchTodo(todoId: String) async throws -> TodoResponse {
throw TodoRepositoryImplTestsError.unexpectedCall
}
func fetchReferences(_ numbers: [Int]) async throws -> [Int: TodoReferenceResponse] {
throw TodoRepositoryImplTestsError.unexpectedCall
}
}
private struct TodoCategoryServiceSpy: TodoCategoryService {
func fetchPreferences() async throws -> [TodoCategoryPreferenceResponse] {
[]
}
func updatePreferences(_ preferences: [TodoCategoryPreferenceResponse]) async throws {
throw TodoRepositoryImplTestsError.unexpectedCall
}
}
private final class WidgetSyncEventBusSpy: WidgetSyncEventBus {
private(set) var events = [WidgetSyncEvent]()
func observe() -> AnyPublisher<WidgetSyncEvent, Never> {
Empty().eraseToAnyPublisher()
}
func publish(_ event: WidgetSyncEvent) {
events.append(event)
}
}
private final class TodoMutationEventBusSpy: TodoMutationEventBus {
private var capturedEvents = [TodoMutationEvent]()
func publish(_ event: TodoMutationEvent) {
capturedEvents.append(event)
}
func publishedEvents() -> [TodoMutationEvent] {
capturedEvents
}
func observe() -> AnyPublisher<TodoMutationEvent, Never> {
Empty().eraseToAnyPublisher()
}
}
private enum TodoRepositoryImplTestsError: Error, Equatable {
case serviceFailed
case unexpectedCall
}