trailblazer-macro 2.1.11 → 2.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,263 @@
1
+ require "test_helper"
2
+ module Autogenerated
3
+ class DocsModelTest < Minitest::Spec
4
+ Song = Struct.new(:id, :title) do
5
+ def self.find_by(args)
6
+ key, value = args.flatten
7
+ return nil if value.nil?
8
+ return new(value) if key == :id
9
+ new(2, value) if key == :title
10
+ end
11
+
12
+ def self.[](id)
13
+ id.nil? ? nil : new(id+99)
14
+ end
15
+ end
16
+
17
+ #:op
18
+ module Song::Operation
19
+ class Create < Trailblazer::Operation
20
+ step Model(Song, :new)
21
+ step :validate
22
+ step :save
23
+ #~meths
24
+ include T.def_steps(:validate, :save)
25
+ #~meths end
26
+ end
27
+ end
28
+ #:op end
29
+
30
+ #:update
31
+ module Song::Operation
32
+ class Update < Trailblazer::Operation
33
+ step Model(Song, :find_by)
34
+ step :validate
35
+ step :save
36
+ #~meths
37
+ include T.def_steps(:validate, :save)
38
+ #~meths end
39
+ end
40
+ end
41
+ #:update end
42
+
43
+ it "defaults {:params} to empty hash when not passed" do
44
+ assert_invoke Song::Operation::Create, seq: "[:validate, :save]",
45
+ expected_ctx_variables: {model: Song.new}
46
+
47
+ assert_invoke Song::Operation::Update, seq: "[]",
48
+ terminus: :failure
49
+ end
50
+
51
+ #~ctx_to_result
52
+ it do
53
+ #:create
54
+ result = Song::Operation::Create.(params: {}, seq: [])
55
+ puts result[:model] #=> #<struct Song id=nil, title=nil>
56
+ #:create end
57
+
58
+ assert_invoke Song::Operation::Create, params: {},
59
+ seq: "[:validate, :save]", expected_ctx_variables: {model: Song.new}
60
+ end
61
+
62
+ it do
63
+ #:update-ok
64
+ result = Song::Operation::Update.(params: {id: 1}, seq: [])
65
+ result[:model] #=> #<Song id=1, ...>
66
+ result.success? # => true
67
+ #:update-ok end
68
+
69
+ assert_equal result[:model].inspect, %{#<struct #{Song} id=1, title=nil>}
70
+ assert_equal result.event.to_h[:semantic], :success
71
+ end
72
+
73
+ it do
74
+ #:update-fail
75
+ result = Song::Operation::Update.(params: {})
76
+ result[:model] #=> nil
77
+ result.success? # => false
78
+ #:update-fail end
79
+
80
+
81
+ assert_equal result[:model].inspect, %{nil}
82
+ assert_equal result.event.to_h[:semantic], :failure
83
+ end
84
+ #~ctx_to_result end
85
+ end
86
+
87
+ class DocsModelFindByTitleTest < Minitest::Spec
88
+ Song = Class.new(DocsModelTest::Song)
89
+
90
+ #:update-with-find-by-key
91
+ module Song::Operation
92
+ class Update < Trailblazer::Operation
93
+ step Model(Song, :find_by, :title) # third positional argument.
94
+ step :validate
95
+ step :save
96
+ #~meths
97
+ include T.def_steps(:validate, :save)
98
+ #~meths end
99
+ end
100
+ end
101
+ #:update-with-find-by-key end
102
+
103
+ #~ctx_to_result
104
+ it do
105
+ #:update-with-find-by-key-ok
106
+ result = Song::Operation::Update.(params: {title: "Test"}, seq: [])
107
+ result[:model] #=> #<struct Song id=2, title="Test">
108
+ #:update-with-find-by-key-ok end
109
+
110
+ assert_equal result[:model].inspect, %{#<struct #{Song} id=2, title="Test">}
111
+ end
112
+
113
+ it do
114
+ #:key-title-fail
115
+ result = Song::Operation::Update.(params: {title: nil}, seq: [])
116
+ assert_equal result[:model].inspect, %{nil}
117
+ #:key-title-fail end
118
+ end
119
+ #~ctx_to_result end
120
+ end
121
+
122
+ class DocsModelAccessorTest < Minitest::Spec
123
+ Song = Class.new(DocsModelTest::Song)
124
+
125
+ #:show
126
+ module Song::Operation
127
+ class Update < Trailblazer::Operation
128
+ step Model(Song, :[])
129
+ step :validate
130
+ step :save
131
+ #~meths
132
+ include T.def_steps(:validate, :save)
133
+ #~meths end
134
+ end
135
+ end
136
+ #:show end
137
+
138
+ #~ctx_to_result
139
+ it do
140
+ #:show-ok
141
+ result = Song::Operation::Update.(params: {id: 1}, seq: [])
142
+ result[:model] #=> #<struct Song id=1, title="Roxanne">
143
+ #:show-ok end
144
+
145
+ assert_equal result[:model].inspect, %{#<struct #{Song} id=100, title=nil>}
146
+ end
147
+ #~ctx_to_result end
148
+ end
149
+
150
+ class DocsModelDependencyInjectionTest < Minitest::Spec
151
+ Song = Class.new(DocsModelTest::Song)
152
+
153
+ module Song::Operation
154
+ class Create < Trailblazer::Operation
155
+ step Model(Song, :new)
156
+ step :validate
157
+ step :save
158
+ #~meths
159
+ include T.def_steps(:validate, :save)
160
+ #~meths end
161
+ end
162
+ end
163
+
164
+ it "allows injecting {:model.class} and friends" do
165
+ class Hit < Song
166
+ end
167
+
168
+ #:di-model-class
169
+ result = Song::Operation::Create.(params: {}, :"model.class" => Hit, seq: [])
170
+ #:di-model-class end
171
+
172
+ assert_equal result[:model].inspect, %{#<struct #{Hit} id=nil, title=nil>}
173
+
174
+ # inject all variables
175
+ #:di-all
176
+ result = Song::Operation::Create.(
177
+ params: {title: "Olympia"}, # some random variable.
178
+ "model.class": Hit,
179
+ "model.action": :find_by,
180
+ "model.find_by_key": :title, seq: []
181
+ )
182
+ #:di-all end
183
+
184
+ assert_equal result[:model].inspect, %{#<struct #{Hit} id=2, title="Olympia">}
185
+ end
186
+
187
+ # use empty Model() and inject {model.class} and {model.action}
188
+ class DocsModelEmptyDITest < Minitest::Spec
189
+ Song = Class.new(DocsModelTest::Song)
190
+ Hit = Class.new(Song)
191
+
192
+ #:op-model-empty
193
+ module Song::Operation
194
+ class Create < Trailblazer::Operation
195
+ step Model()
196
+ step :validate
197
+ step :save
198
+ #~meths
199
+ include T.def_steps(:validate, :save)
200
+ #~meths end
201
+ end
202
+ #:op-model-empty end
203
+ end
204
+
205
+ it do
206
+ result = Song::Operation::Create.(params: {}, :"model.class" => Hit, seq: [])
207
+ assert_equal result[:model].inspect, %{#<struct #{Hit} id=nil, title=nil>}
208
+ end
209
+ end
210
+
211
+ class DocsModelIOTest < Minitest::Spec
212
+ Song = Class.new(DocsModelTest::Song)
213
+ Hit = Class.new(Song)
214
+
215
+ it "allows to use composable I/O with macros" do
216
+ #:in
217
+ module Song::Operation
218
+ class Create < Trailblazer::Operation
219
+ step Model(Song, :find_by),
220
+ In() => ->(ctx, my_id:, **) { ctx.merge(params: {id: my_id}) } # Model() needs {params[:id]}.
221
+ # ...
222
+ end
223
+ end
224
+ #:in end
225
+
226
+ result = Song::Operation::Create.(params: {}, my_id: 1, :"model.class" => Hit)
227
+ assert_equal result[:model].inspect, %{#<struct #{Hit} id=1, title=nil>}
228
+ =begin
229
+ #:in-call
230
+ result = Create.(my_id: 1)
231
+ #:in-call end
232
+ =end
233
+ end
234
+ end
235
+ end
236
+
237
+ class Model404TerminusTest < Minitest::Spec
238
+ Song = Class.new(DocsModelTest::Song)
239
+ #:update-with-not-found-end
240
+ module Song::Operation
241
+ class Update < Trailblazer::Operation
242
+ step Model(Song, :find_by, not_found_terminus: true)
243
+ step :validate
244
+ step :save
245
+ #~meths
246
+ include T.def_steps(:validate, :save)
247
+ #~meths end
248
+ end
249
+ end
250
+ #:update-with-not-found-end end
251
+
252
+ it do
253
+ assert_invoke Song::Operation::Update, params: {id: 1},
254
+ seq: "[:validate, :save]", expected_ctx_variables: {model: Song.find_by(id: 1)}
255
+ assert_invoke Song::Operation::Update, params: {id: nil}, terminus: :not_found
256
+
257
+ #:not_found
258
+ result = Song::Operation::Update.(params: {id: nil})
259
+ result.success? # => false
260
+ #:not_found end
261
+ end
262
+ end
263
+ end