trailblazer-macro 2.1.11 → 2.1.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,6 +31,24 @@ class DocsMacroTest < Minitest::Spec
31
31
  it { Trailblazer::Developer.railway(Create).must_equal %{[>my_policy.manager]} }
32
32
  end
33
33
 
34
+
35
+ class MacroAssignVariableTest < Minitest::Spec
36
+ it do
37
+ my_exec_context = Class.new do
38
+ def my_dataset(ctx, my_array:, **)
39
+ my_array.reverse
40
+ end
41
+ end.new
42
+
43
+ dataset_task = Trailblazer::Macro.task_adapter_for_decider(:my_dataset, variable_name: :dataset)
44
+
45
+ signal, (ctx, _) = dataset_task.([{my_array: [1,2]}, {}], exec_context: my_exec_context)
46
+
47
+ assert_equal signal, Trailblazer::Activity::Right
48
+ assert_equal ctx.inspect, %{{:my_array=>[1, 2], :dataset=>[2, 1]}}
49
+ end
50
+ end
51
+
34
52
  # injectable option
35
53
  # nested pipe
36
54
  # using macros in macros
@@ -15,122 +15,149 @@ class DocsModelTest < Minitest::Spec
15
15
  end
16
16
 
17
17
  #:op
18
- class Create < Trailblazer::Operation
19
- step Model(Song, :new)
20
- # ..
18
+ module Song::Activity
19
+ class Create < Trailblazer::Activity::Railway
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
21
27
  end
22
28
  #:op end
23
29
 
30
+ #:update
31
+ module Song::Activity
32
+ class Update < Trailblazer::Activity::Railway
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
24
42
 
25
43
  it "defaults {:params} to empty hash when not passed" do
26
- result = Create.({})
27
- assert_equal true, result.success?
28
- assert_equal %{#<struct DocsModelTest::Song id=nil, title=nil>}, result[:model].inspect
44
+ assert_invoke Song::Activity::Create, seq: "[:validate, :save]",
45
+ expected_ctx_variables: {model: Song.new}
29
46
 
30
- result = Update.({})
31
- assert_equal false, result.success?
32
- assert_equal "nil", result[:model].inspect
47
+ assert_invoke Song::Activity::Update, seq: "[]",
48
+ terminus: :failure
33
49
  end
34
50
 
51
+ #~ctx_to_result
35
52
  it do
36
53
  #:create
37
- result = Create.(params: {})
38
- result[:model] #=> #<struct Song id=nil, title=nil>
54
+ signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, seq: [])
55
+ puts ctx[:model] #=> #<struct Song id=nil, title=nil>
39
56
  #:create end
40
57
 
41
- result[:model].inspect.must_equal %{#<struct DocsModelTest::Song id=nil, title=nil>}
58
+ assert_invoke Song::Activity::Create, params: {},
59
+ seq: "[:validate, :save]", expected_ctx_variables: {model: Song.new}
42
60
  end
43
61
 
62
+ it do
63
+ #:update-ok
64
+ signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: 1}, seq: [])
65
+ ctx[:model] #=> #<Song id=1, ...>
66
+ puts signal #=> #<Trailblazer::Activity::End semantic=:success>
67
+ #:update-ok end
44
68
 
45
- #:update
46
- class Update < Trailblazer::Operation
47
- step Model( Song, :find_by )
48
- # ..
69
+ assert_equal ctx[:model].inspect, %{#<struct #{Song} id=1, title=nil>}
70
+ assert_equal signal.to_h[:semantic], :success
49
71
  end
50
- #:update end
51
72
 
52
- #:update-with-find-by-key
53
- class UpdateWithFindByKey < Trailblazer::Operation
54
- step Model( Song, :find_by, :title )
55
- # ..
56
- end
57
- #:update-with-find-by-key end
73
+ it do
74
+ #:update-fail
75
+ signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {})
76
+ ctx[:model] #=> nil
77
+ puts signal #=> #<Trailblazer::Activity::End semantic=:failure>
78
+ #:update-fail end
58
79
 
59
- #:update-with-not-found-end
60
- class UpdateFailureWithModelNotFound < Trailblazer::Operation
61
- step Model( Song, :find_by, not_found_terminus: true )
62
- # ..
80
+ assert_equal ctx[:model].inspect, %{nil}
81
+ assert_equal signal.to_h[:semantic], :failure
63
82
  end
64
- #:update-with-not-found-end end
83
+ #~ctx_to_result end
84
+ end
65
85
 
66
- it do
67
- #:update-ok
68
- result = Update.(params: { id: 1 })
69
- result[:model] #=> #<struct Song id=1, title="nil">
70
- #:update-ok end
86
+ class DocsModelFindByTitleTest < Minitest::Spec
87
+ Song = Class.new(DocsModelTest::Song)
71
88
 
72
- result[:model].inspect.must_equal %{#<struct DocsModelTest::Song id=1, title=nil>}
89
+ #:update-with-find-by-key
90
+ module Song::Activity
91
+ class Update < Trailblazer::Activity::Railway
92
+ step Model(Song, :find_by, :title) # third positional argument.
93
+ step :validate
94
+ step :save
95
+ #~meths
96
+ include T.def_steps(:validate, :save)
97
+ #~meths end
98
+ end
73
99
  end
100
+ #:update-with-find-by-key end
74
101
 
102
+ #~ctx_to_result
75
103
  it do
76
104
  #:update-with-find-by-key-ok
77
- result = UpdateWithFindByKey.(params: { title: "Test" } )
78
- result[:model] #=> #<struct Song id=2, title="Test">
105
+ signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {title: "Test"}, seq: [])
106
+ ctx[:model] #=> #<struct Song id=2, title="Test">
79
107
  #:update-with-find-by-key-ok end
80
108
 
81
- result[:model].inspect.must_equal %{#<struct DocsModelTest::Song id=2, title="Test">}
109
+ assert_equal ctx[:model].inspect, %{#<struct #{Song} id=2, title="Test">}
82
110
  end
83
111
 
84
112
  it do
85
- #:update-fail
86
- result = Update.(params: {})
87
- result[:model] #=> nil
88
- result.success? #=> false
89
- #:update-fail end
90
- result[:model].must_be_nil
91
- result.success?.must_equal false
92
- end
93
-
94
- it do
95
- #:update-with-find-by-key-fail
96
- result = UpdateWithFindByKey.(params: {title: nil})
97
- result[:model] #=> nil
98
- result.success? #=> false
99
- #:update-with-find-by-key-fail end
100
- result[:model].must_be_nil
101
- result.success?.must_equal false
113
+ #:key-title-fail
114
+ signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {title: nil}, seq: [])
115
+ assert_equal ctx[:model].inspect, %{nil}
116
+ #:key-title-fail end
102
117
  end
118
+ #~ctx_to_result end
119
+ end
103
120
 
104
- it do
105
- #:update-with-not-found-end-use
106
- result = UpdateFailureWithModelNotFound.(params: {title: nil})
107
- result[:model] #=> nil
108
- result.success? #=> false
109
- result.event #=> #<Trailblazer::Activity::End semantic=:not_found>
110
- #:update-with-not-found-end-use end
111
-
112
- result[:model].must_be_nil
113
- result.success?.must_equal false
114
- result.event.inspect.must_equal %{#<Trailblazer::Activity::End semantic=:not_found>}
115
- end
121
+ class DocsModelAccessorTest < Minitest::Spec
122
+ Song = Class.new(DocsModelTest::Song)
116
123
 
117
124
  #:show
118
- class Show < Trailblazer::Operation
119
- step Model( Song, :[] )
120
- # ..
125
+ module Song::Activity
126
+ class Update < Trailblazer::Activity::Railway
127
+ step Model(Song, :[])
128
+ step :validate
129
+ step :save
130
+ #~meths
131
+ include T.def_steps(:validate, :save)
132
+ #~meths end
133
+ end
121
134
  end
122
135
  #:show end
123
136
 
137
+ #~ctx_to_result
124
138
  it do
125
- result = Show.(params: { id: 1 })
126
-
127
139
  #:show-ok
128
- result = Show.(params: { id: 1 })
129
- result[:model] #=> #<struct Song id=1, title="Roxanne">
140
+ signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: 1}, seq: [])
141
+ ctx[:model] #=> #<struct Song id=1, title="Roxanne">
130
142
  #:show-ok end
131
143
 
132
- result.success?.must_equal true
133
- result[:model].inspect.must_equal %{#<struct DocsModelTest::Song id=100, title=nil>}
144
+ assert_equal ctx[:model].inspect, %{#<struct #{Song} id=100, title=nil>}
145
+ end
146
+ #~ctx_to_result end
147
+ end
148
+
149
+ class DocsModelDependencyInjectionTest < Minitest::Spec
150
+ Song = Class.new(DocsModelTest::Song)
151
+
152
+ module Song::Activity
153
+ class Create < Trailblazer::Activity::Railway
154
+ step Model(Song, :new)
155
+ step :validate
156
+ step :save
157
+ #~meths
158
+ include T.def_steps(:validate, :save)
159
+ #~meths end
160
+ end
134
161
  end
135
162
 
136
163
  it "allows injecting {:model.class} and friends" do
@@ -138,37 +165,126 @@ class DocsModelTest < Minitest::Spec
138
165
  end
139
166
 
140
167
  #:di-model-class
141
- result = Create.(params: {}, :"model.class" => Hit)
168
+ signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, :"model.class" => Hit, seq: [])
142
169
  #:di-model-class end
143
170
 
144
- result[:model].inspect.must_equal %{#<struct DocsModelTest::Hit id=nil, title=nil>}
171
+ assert_equal ctx[:model].inspect, %{#<struct #{Hit} id=nil, title=nil>}
145
172
 
146
173
  # inject all variables
147
174
  #:di-all
148
- result = Create.(
175
+ signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create,
149
176
  params: {title: "Olympia"}, # some random variable.
150
177
  "model.class": Hit,
151
178
  "model.action": :find_by,
152
- "model.find_by_key": :title
179
+ "model.find_by_key": :title, seq: []
153
180
  )
154
181
  #:di-all end
155
182
 
156
- result[:model].inspect.must_equal %{#<struct DocsModelTest::Hit id=2, title="Olympia">}
183
+ assert_equal ctx[:model].inspect, %{#<struct #{Hit} id=2, title="Olympia">}
184
+ end
157
185
 
158
186
  # use empty Model() and inject {model.class} and {model.action}
159
- module A
160
- #:op-model-empty
187
+ class DocsModelEmptyDITest < Minitest::Spec
188
+ Song = Class.new(DocsModelTest::Song)
189
+ Hit = Class.new(Song)
190
+
191
+ #:op-model-empty
192
+ module Song::Activity
193
+ class Create < Trailblazer::Activity::Railway
194
+ step Model()
195
+ step :validate
196
+ step :save
197
+ #~meths
198
+ include T.def_steps(:validate, :save)
199
+ #~meths end
200
+ end
201
+ #:op-model-empty end
202
+ end
203
+
204
+ it do
205
+ signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, :"model.class" => Hit, seq: [])
206
+ assert_equal ctx[:model].inspect, %{#<struct #{Hit} id=nil, title=nil>}
207
+ end
208
+ end
209
+
210
+ class DocsModelIOTest < Minitest::Spec
211
+ Song = Class.new(DocsModelTest::Song)
212
+ Hit = Class.new(Song)
213
+
214
+ it "allows to use composable I/O with macros" do
215
+ #:in
216
+ module Song::Activity
161
217
  class Create < Trailblazer::Operation
162
- step Model()
163
- # ..
218
+ step Model(Song, :find_by),
219
+ In() => ->(ctx, my_id:, **) { ctx.merge(params: {id: my_id}) } # Model() needs {params[:id]}.
220
+ # ...
164
221
  end
165
- #:op-model-empty end
166
- end # A
222
+ end
223
+ #:in end
224
+
225
+ signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, my_id: 1, :"model.class" => Hit)
226
+ assert_equal ctx[:model].inspect, %{#<struct #{Hit} id=1, title=nil>}
227
+ =begin
228
+ #:in-call
229
+ result = Create.(my_id: 1)
230
+ #:in-call end
231
+ =end
232
+ end
233
+ end
234
+ end
167
235
 
168
- result = A::Create.(params: {}, :"model.class" => Hit)
236
+ class Model404TerminusTest < Minitest::Spec
237
+ Song = Class.new(DocsModelTest::Song)
238
+ #:update-with-not-found-end
239
+ module Song::Activity
240
+ class Update < Trailblazer::Activity::Railway
241
+ step Model(Song, :find_by, not_found_terminus: true)
242
+ step :validate
243
+ step :save
244
+ #~meths
245
+ include T.def_steps(:validate, :save)
246
+ #~meths end
247
+ end
248
+ end
249
+ #:update-with-not-found-end end
169
250
 
170
- result[:model].inspect.must_equal %{#<struct DocsModelTest::Hit id=nil, title=nil>}
251
+ it do
252
+ assert_invoke Song::Activity::Update, params: {id: 1},
253
+ seq: "[:validate, :save]", expected_ctx_variables: {model: Song.find_by(id: 1)}
254
+ assert_invoke Song::Activity::Update, params: {id: nil}, terminus: :not_found
255
+
256
+ #:not_found
257
+ signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: nil})
258
+ puts signal #=> #<Trailblazer::Activity::End semantic=:not_found>
259
+ #:not_found end
260
+ end
261
+ end
171
262
 
263
+ class Model404TerminusTest < Minitest::Spec
264
+ Song = Class.new(DocsModelTest::Song)
265
+ #:update-with-not-found-end
266
+ class Song
267
+ module Activity
268
+ class Update < Trailblazer::Activity::Railway
269
+ step Model(Song, :find_by, not_found_terminus: true)
270
+ step :validate
271
+ step :save
272
+ #~meths
273
+ include T.def_steps(:validate, :save)
274
+ #~meths end
275
+ end
276
+ end
277
+ end
278
+ #:update-with-not-found-end end
172
279
 
280
+ it do
281
+ assert_invoke Song::Activity::Update, params: {id: 1},
282
+ seq: "[:validate, :save]", expected_ctx_variables: {model: Song.find_by(id: 1)}
283
+ assert_invoke Song::Activity::Update, params: {id: nil}, terminus: :not_found
284
+
285
+ #:not_found
286
+ signal, (ctx, _) = Trailblazer::Activity::TaskWrap.invoke(Song::Activity::Update, [{params: {id: nil}},{}])
287
+ puts signal #=> #<Trailblazer::Activity::End semantic=:not_found>
288
+ #:not_found end
173
289
  end
174
290
  end