@@ -36,9 +36,9 @@ defmodule CommandexTest do
3636
3737 test "handles string-key map params correctly" do
3838 params = % {
39- email: @ email ,
40- password: @ password ,
41- agree_tos: @ agree_tos
39+ " email" => @ email ,
40+ " password" => @ password ,
41+ " agree_tos" => @ agree_tos
4242 }
4343
4444 command = RegisterUser . new ( params )
@@ -91,35 +91,32 @@ defmodule CommandexTest do
9191
9292 describe "pipeline/1 macro" do
9393 test "accepts valid pipeline arguments" do
94- try do
95- defmodule ExamplePipelineValid do
96- import Commandex
97-
98- command do
99- pipeline :example
100- pipeline { ExamplePipelineValid , :example }
101- pipeline { ExamplePipelineValid , :example_args , [ "test" ] }
102- pipeline & ExamplePipelineValid . example_single / 1
103- pipeline & ExamplePipelineValid . example / 3
104- end
94+ defmodule ExamplePipelineValid do
95+ import Commandex
10596
106- def example ( command , _params , _data ) do
107- command
108- end
97+ command do
98+ pipeline :example
99+ pipeline { ExamplePipelineValid , :example }
100+ pipeline { ExamplePipelineValid , :example_args , [ "test" ] }
101+ pipeline & ExamplePipelineValid . example_single / 1
102+ pipeline & ExamplePipelineValid . example / 3
103+ end
109104
110- def example_single ( command ) do
111- command
112- end
105+ def example ( command , _params , _data ) do
106+ command
107+ end
113108
114- def example_args ( command , _params , _data , _custom_value ) do
115- command
116- end
109+ def example_single ( command ) do
110+ command
117111 end
118112
119- ExamplePipelineValid . run ( )
120- rescue
121- FunctionClauseError -> flunk ( "Should not raise." )
113+ def example_args ( command , _params , _data , _custom_value ) do
114+ command
115+ end
122116 end
117+
118+ command = ExamplePipelineValid . run ( )
119+ assert command . success
123120 end
124121
125122 test "raises if invalid argument defined" do
@@ -135,15 +132,31 @@ defmodule CommandexTest do
135132 end
136133 end
137134
135+ describe "halt/1" do
136+ test "sets halted to true and success to false" do
137+ command = Commandex . halt ( RegisterUser . new ( ) )
138+
139+ assert command . halted
140+ refute command . success
141+ end
142+ end
143+
138144 describe "halt/2" do
145+ test "sets success to true when option given" do
146+ command = Commandex . halt ( RegisterUser . new ( ) , success: true )
147+
148+ assert command . halted
149+ assert command . success
150+ end
151+
139152 test "ignores remaining pipelines" do
140153 command = RegisterUser . run ( % { agree_tos: false } )
141154
142155 refute command . success
143156 assert command . errors === % { tos: :not_accepted }
144157 end
145158
146- test "handles :success option" do
159+ test "handles :success option in pipeline " do
147160 command = RegisterUser . run ( % { email: "exists@test.com" } )
148161
149162 assert command . success
@@ -166,10 +179,87 @@ defmodule CommandexTest do
166179 end
167180 end
168181
182+ describe "new/1" do
183+ test "uses defaults when no params given" do
184+ command = RegisterUser . new ( )
185+ assert command . params . email == "test@test.com"
186+ refute command . params . password
187+ refute command . params . agree_tos
188+ end
189+ end
190+
191+ describe "put_data/3" do
192+ test "sets data field on command" do
193+ command = RegisterUser . new ( )
194+ updated = Commandex . put_data ( command , :user , % { id: 1 } )
195+ assert updated . data . user == % { id: 1 }
196+ end
197+
198+ test "overwrites existing data field" do
199+ command = RegisterUser . new ( )
200+
201+ updated =
202+ command
203+ |> Commandex . put_data ( :user , % { id: 1 } )
204+ |> Commandex . put_data ( :user , % { id: 2 } )
205+
206+ assert updated . data . user == % { id: 2 }
207+ end
208+ end
209+
210+ describe "put_error/3" do
211+ test "sets error on command" do
212+ command = RegisterUser . new ( )
213+ updated = Commandex . put_error ( command , :email , :invalid )
214+ assert updated . errors . email == :invalid
215+ end
216+
217+ test "overwrites existing error for same key" do
218+ command = RegisterUser . new ( )
219+
220+ updated =
221+ command
222+ |> Commandex . put_error ( :email , :invalid )
223+ |> Commandex . put_error ( :email , :taken )
224+
225+ assert updated . errors . email == :taken
226+ end
227+ end
228+
229+ describe "run/1" do
230+ test "succeeds when all pipelines pass" do
231+ command = RegisterUser . run ( % { email: "new@test.com" , password: "pass" , agree_tos: true } )
232+
233+ assert command . success
234+ refute command . halted
235+ assert command . data . user == % { email: "new@test.com" }
236+ assert command . data . auth == true
237+ assert command . errors == % { }
238+ end
239+
240+ test "accepts a pre-built struct" do
241+ command =
242+ RegisterUser . new ( % { email: "new@test.com" , password: "pass" , agree_tos: true } )
243+ |> RegisterUser . run ( )
244+
245+ assert command . success
246+ assert command . data . user == % { email: "new@test.com" }
247+ end
248+
249+ test "halted command does not run subsequent pipelines" do
250+ command = RegisterUser . run ( % { agree_tos: false } )
251+
252+ refute command . success
253+ assert command . halted
254+ assert command . errors == % { tos: :not_accepted }
255+ refute command . data . user
256+ refute command . data . auth
257+ end
258+ end
259+
169260 defp assert_params ( command ) do
170261 assert command . params . email == @ email
171262 assert command . params . password == @ password
172- # Don't use refute here because nil fails the test.
173263 assert command . params . agree_tos == @ agree_tos
174264 end
175265end
0 commit comments