-
Notifications
You must be signed in to change notification settings - Fork 33
/
result_transitions_test.rb
222 lines (155 loc) · 4.95 KB
/
result_transitions_test.rb
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require 'test_helper'
class Micro::Cases::Safe::Flow::ResultTransitionsTest < Minitest::Test
require 'digest'
require 'securerandom'
class User < InactiveRecord::Base
attr_reader :password_hash
attr_accessor :name
def initialize(options = {})
@name = options[:name]
@password = options[:password]
end
def invalid?
name.empty? || @password.empty?
end
def save
return false if invalid?
self.name = name
save_new_record do
@password_hash = Digest::SHA256.hexdigest(@password)
end
end
def wrong_password?(value)
password_hash != Digest::SHA256.hexdigest(value)
end
end
class Todo < InactiveRecord::Base
attr_accessor :description, :done, :user_id
def self.find_by_id_and_user_id(id, user_id)
all.find { |todo| todo.id == id && todo.user_id && user_id }
end
def initialize(options = {})
@user_id = options[:user_id]
@description = options[:description]
end
def invalid?
description.empty? || user_id.empty?
end
def save
return false if invalid?
self.description = description
save_new_record { @done = done? }
end
def pending?; !done; end
def done?; !pending?; end
end
module Users
class Create < Micro::Case::Strict::Safe
attributes :name, :password, :password_confirmation
def call!
return Failure(:invalid_password) if password != password_confirmation
user = User.new(attributes(:name, :password))
return Failure(:validation_error) unless user.save
Success { { user: user } }
end
end
class Fetch < Micro::Case::Strict::Safe
attribute :user_id
def call!
user = User.find_by_id(user_id)
return Success { { user: user } } if user
Failure(:user_not_found)
end
end
class CheckPassword < Micro::Case::Strict::Safe
attributes :user, :password
def call!
return Failure(:user_must_be_persisted) if user.new_record?
return Failure(:wrong_password) if user.wrong_password?(password)
return Success { attributes(:user) }
end
end
Authenticate = Micro::Cases.safe_flow([
Fetch,
CheckPassword
])
end
module Todos
class Create < Micro::Case::Strict::Safe
attributes :user, :description
def call!
todo = Todo.new(user_id: user.id, description: description)
return Failure(:validation_error) unless todo.save
Success { { todo: todo } }
end
end
class FetchByUser < Micro::Case::Strict::Safe
attributes :user, :todo_id
def call!
todo = Todo.find_by_id_and_user_id(todo_id, user.id)
return Success { { todo: todo } } if todo
Failure(:todo_not_found)
end
end
class SetToDone < Micro::Case::Strict::Safe
attribute :todo
def call!
if todo.pending?
todo.done = true
todo.save
end
return Success { attributes(:todo) }
end
end
end
module UserTodos
Create = Micro::Cases.safe_flow([Users::Authenticate, Todos::Create])
class MarkAsDone < Micro::Case
flow Users::Authenticate,
Todos::FetchByUser,
Todos::SetToDone
end
end
def setup
[User, Todo].each(&:delete_all)
end
def test_the_todo_creation_and_its_marking_as_done
user_password = '123456'
user_created =
Users::Create.call(name: 'Rodrigo', password: user_password, password_confirmation: user_password)
assert_success_result(user_created)
user = user_created.value[:user]
refute_nil(user.id)
refute_predicate(user, :new_record?)
todo_created =
UserTodos::Create.call(user_id: user.id, password: user_password, description: 'Buy milk')
assert_success_result(todo_created)
todo = todo_created.value[:todo]
refute_nil(todo.id)
refute_predicate(todo, :new_record?)
assert_equal(user.id, todo.user_id)
result =
UserTodos::MarkAsDone.call(user_id: user.id, password: user_password, todo_id: todo.id)
todo_updated = result.value[:todo]
assert_equal(todo.id, todo_updated.id)
assert_predicate(todo_updated, :done?)
assert_equal(1, User.count)
assert_equal(1, Todo.count)
end
def test_the_some_todo_creation_failures
user_password = '123456'
user_created =
Users::Create.call(name: 'Rodrigo', password: user_password, password_confirmation: user_password)
assert_success_result(user_created)
user = user_created.value[:user]
result1 =
UserTodos::Create.call(user_id: user.id, password: '', description: 'Buy beer')
assert_failure_result(result1, type: :wrong_password)
result2 =
UserTodos::Create.call(user_id: user.id, password: user_password, description: '')
assert_failure_result(result2, type: :validation_error)
assert_instance_of(Todos::Create, result2.use_case)
assert_equal(1, User.count)
assert_equal(0, Todo.count)
end
end