-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommandex_test.exs
More file actions
175 lines (145 loc) · 4.07 KB
/
commandex_test.exs
File metadata and controls
175 lines (145 loc) · 4.07 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
defmodule CommandexTest do
use ExUnit.Case
doctest Commandex
@email "example@example.com"
@password "test1234"
@agree_tos false
describe "struct assembly" do
test "sets :params map" do
for key <- [:email, :password, :agree_tos] do
assert Map.has_key?(%RegisterUser{}.params, key)
end
end
test "sets param default if specified" do
assert %RegisterUser{}.params.email == "test@test.com"
end
test "sets :data map" do
for key <- [:user, :auth] do
assert Map.has_key?(%RegisterUser{}.data, key)
end
end
test "handles atom-key map params correctly" do
params = %{
email: @email,
password: @password,
agree_tos: @agree_tos
}
command = RegisterUser.new(params)
assert_params(command)
end
test "handles string-key map params correctly" do
params = %{
email: @email,
password: @password,
agree_tos: @agree_tos
}
command = RegisterUser.new(params)
assert_params(command)
end
test "handles keyword list params correctly" do
params = [
email: @email,
password: @password,
agree_tos: @agree_tos
]
command = RegisterUser.new(params)
assert_params(command)
end
end
describe "param/2 macro" do
test "raises if duplicate defined" do
assert_raise ArgumentError, fn ->
defmodule ExampleParamInvalid do
import Commandex
command do
param :key_1
param :key_2
param :key_1
end
end
end
end
end
describe "data/1 macro" do
test "raises if duplicate defined" do
assert_raise ArgumentError, fn ->
defmodule ExampleDataInvalid do
import Commandex
command do
data :key_1
data :key_2
data :key_1
end
end
end
end
end
describe "pipeline/1 macro" do
test "accepts valid pipeline arguments" do
try do
defmodule ExamplePipelineValid do
import Commandex
command do
pipeline :example
pipeline {ExamplePipelineValid, :example}
pipeline {ExamplePipelineValid, :example_args, ["test"]}
pipeline &ExamplePipelineValid.example_single/1
pipeline &ExamplePipelineValid.example/3
end
def example(command, _params, _data) do
command
end
def example_single(command) do
command
end
def example_args(command, _params, _data, _custom_value) do
command
end
end
ExamplePipelineValid.run()
rescue
FunctionClauseError -> flunk("Should not raise.")
end
end
test "raises if invalid argument defined" do
assert_raise ArgumentError, fn ->
defmodule ExamplePipelineInvalid do
import Commandex
command do
pipeline 1234
end
end
end
end
end
describe "halt/2" do
test "ignores remaining pipelines" do
command = RegisterUser.run(%{agree_tos: false})
refute command.success
assert command.errors === %{tos: :not_accepted}
end
test "handles :success option" do
command = RegisterUser.run(%{email: "exists@test.com"})
assert command.success
assert command.errors === %{user: :already_exists}
end
end
describe "run/0" do
test "is defined if no params are defined" do
assert Kernel.function_exported?(GenerateReport, :run, 0)
command = GenerateReport.run()
assert command.success
assert command.data.total_valid > 0
assert command.data.total_invalid > 0
end
test "is not defined if params are defined" do
refute Kernel.function_exported?(RegisterUser, :run, 0)
end
end
defp assert_params(command) do
assert command.params.email == @email
assert command.params.password == @password
# Don't use refute here because nil fails the test.
assert command.params.agree_tos == @agree_tos
end
end