trailblazer 2.1.0.rc1 → 2.1.0.rc12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +0,0 @@
1
- require "test_helper"
2
-
3
- class DocsInputOutputTest < Minitest::Spec
4
- class NoMappingTest < Minitest::Spec
5
- Memo = Class.new(Memo)
6
- end
7
- end
@@ -1,407 +0,0 @@
1
- require "test_helper"
2
-
3
- class DocsOperationExampleTest < Minitest::Spec
4
- Song = Struct.new(:id, :title, :created_by) do
5
- def save; true; end
6
- end
7
- User = Struct.new(:name)
8
-
9
- #:invocation-dep
10
- class Create < Trailblazer::Operation
11
- step Model( Song, :new )
12
- step :assign_current_user!
13
- # ..
14
- def assign_current_user!(options, **)
15
- options[:model].created_by = options[:current_user]
16
- end
17
- end
18
- #:invocation-dep end
19
-
20
- it do
21
- current_user = User.new("Ema")
22
- #:invocation-dep-call
23
- result = Create.( params: { title: "Roxanne" }, :current_user => current_user )
24
- #:invocation-dep-call end
25
-
26
- #:invocation-dep-res
27
- result[:current_user] #=> #<User name="Ema">
28
- result[:model] #=> #<Song id=nil, title=nil, created_by=#<User name="Ema">>
29
- #:invocation-dep-res end
30
- end
31
-
32
- it { Create.(params: { title: "Roxanne" }, :current_user => Module).inspect(:model).must_equal %{<Result:true [#<struct DocsOperationExampleTest::Song id=nil, title=nil, created_by=Module>] >} }
33
-
34
- #:op
35
- class Song::Create < Trailblazer::Operation
36
- class Form < Reform::Form
37
- property :title
38
- validates :title, presence: true
39
- end
40
-
41
- step Model( Song, :new )
42
- step :assign_current_user!
43
- step Contract::Build( constant: Form )
44
- step Contract::Validate( )
45
- failure :log_error!
46
- step Contract::Persist( )
47
-
48
- def log_error!(options, **)
49
- # ..
50
- end
51
-
52
- def assign_current_user!(options, **)
53
- options[:model].created_by =
54
- options[:current_user]
55
- end
56
- end
57
- #:op end
58
-
59
- it { Song::Create.(params: { }).inspect(:model).must_equal %{<Result:false [#<struct DocsOperationExampleTest::Song id=nil, title=nil, created_by=nil>] >} }
60
- it { Song::Create.(params: { title: "Nothin'" }, :current_user=>Module).inspect(:model).must_equal %{<Result:true [#<struct DocsOperationExampleTest::Song id=nil, title="Nothin'", created_by=Module>] >} }
61
- end
62
-
63
- class DndTest < Minitest::Spec
64
- class Create < Trailblazer::Operation
65
- step :authorize!
66
- failure :auth_err!
67
- step :save!
68
- failure Wrap
69
- end
70
- end
71
-
72
- class DocsResultTest < Minitest::Spec
73
- Song = Struct.new(:id, :title, :created_by) do
74
- def save; true; end
75
- end
76
-
77
- #:step-options
78
- class Song::Create < Trailblazer::Operation
79
- step :model!
80
- step :assign!
81
- step :validate!
82
-
83
- def model!(options, current_user:, **)
84
- options[:model] = Song.new
85
- options[:model].created_by = current_user
86
- end
87
-
88
- def assign!(*, params:, model:, **)
89
- model.title= params[:title]
90
- end
91
-
92
- #:step-val
93
- def validate!(options, model:, **)
94
- options["result.validate"] = ( model.created_by && model.title )
95
- end
96
- #:step-val end
97
- end
98
- #:step-options end
99
-
100
- it do
101
- current_user = Struct.new(:email).new("nick@trailblazer.to")
102
- #:step-res
103
- result = Song::Create.(params: { title: "Roxanne" }, :current_user => current_user)
104
-
105
- result[:model] #=> #<Song title="Roxanne", "created_by"=<User ...>
106
- result["result.validate"] #=> true
107
- #:step-res end
108
-
109
- result.inspect(:current_user, :model).must_equal %{<Result:true [#<struct email=\"nick@trailblazer.to\">, #<struct DocsResultTest::Song id=nil, title="Roxanne", created_by=#<struct email=\"nick@trailblazer.to\">>] >}
110
-
111
- #:step-binary
112
- result.success? #=> true
113
- result.failure? #=> falsee
114
- #:step-binary end
115
-
116
- #:step-dep
117
- result[:current_user] #=> <User ...>
118
- #:step-dep end
119
-
120
- #:step-inspect
121
- result.inspect(:current_user, :model) #=> "<Result:true [#<User email=\"nick@tra... "
122
- #:step-inspect end
123
- end
124
- end
125
-
126
- class DocsDependencyTest < Minitest::Spec
127
- Song = Struct.new(:id, :title, :created_by) do
128
- def save; true; end
129
- end
130
- Hit = Struct.new(:id)
131
-
132
- #:dep-op
133
- class Song::Create < Trailblazer::Operation
134
- extend ClassDependencies
135
- self["my.model.class"] = Song
136
-
137
- #~dep-pipe
138
- step :model!
139
-
140
- def model!(options, **)
141
- options["my.model"] = # setting runtime data.
142
- options["my.model.class"].new # reading class data at runtime.
143
- end
144
- #~dep-pipe end
145
- end
146
- #:dep-op end
147
-
148
- it do
149
- #:dep-op-class
150
- Song::Create["my.model.class"] #=> Song
151
- #:dep-op-class end
152
-
153
- #:dep-op-res
154
- result = Song::Create.({})
155
-
156
- result["my.model.class"] #=> Song
157
- result["my.model"] #=> #<Song title=nil>
158
- #:dep-op-res end
159
-
160
- Song::Create["my.model.class"].must_equal Song
161
- result["my.model.class"].must_equal Song
162
- result["my.model"].inspect.must_equal %{#<struct DocsDependencyTest::Song id=nil, title=nil, created_by=nil>}
163
- end
164
-
165
- it do
166
- #:dep-di
167
- result = Song::Create.({}, "my.model.class" => Hit)
168
-
169
- result["my.model"] #=> #<Hit id=nil>
170
- #:dep-di end
171
- result["my.model"].inspect.must_equal %{#<struct DocsDependencyTest::Hit id=nil>}
172
- end
173
- end
174
-
175
-
176
-
177
- class DocsOperationAPIExampleTest < Minitest::Spec
178
- Song = Struct.new(:id, :title, :created_by) do
179
- def save; true; end
180
- end
181
-
182
- class MyContract < Reform::Form
183
- property :title
184
- validates :title, presence: true
185
- end
186
-
187
- #:op-api
188
- class Song::Create < Trailblazer::Operation
189
- step Model( Song, :new )
190
- step :assign_current_user!
191
- step Contract::Build( constant: MyContract )
192
- step Contract::Validate()
193
- failure :log_error!
194
- step Contract::Persist()
195
-
196
- def log_error!(options, **)
197
- # ..
198
- end
199
-
200
- def assign_current_user!(options, **)
201
- options[:model].created_by =
202
- options[:current_user]
203
- end
204
- end
205
- #:op-api end
206
-
207
- it { Song::Create.(params: {}).inspect(:model).must_equal %{<Result:false [#<struct DocsOperationAPIExampleTest::Song id=nil, title=nil, created_by=nil>] >} }
208
- it { Song::Create.(params: { title: "Nothin'" }, :current_user=>Module).inspect(:model).must_equal %{<Result:true [#<struct DocsOperationAPIExampleTest::Song id=nil, title="Nothin'", created_by=Module>] >} }
209
- end
210
-
211
-
212
- class DocsOperationInheritanceTest < Minitest::Spec
213
- Song = Struct.new(:id, :title, :created_by) do
214
- def save; true; end
215
- end
216
-
217
- class MyContract < Reform::Form
218
- property :title
219
- validates :title, presence: true
220
- end
221
-
222
- #:inh-new
223
- class New < Trailblazer::Operation
224
- step Model( Song, :new )
225
- step Contract::Build( constant: MyContract )
226
- end
227
- #:inh-new end
228
-
229
- puts Trailblazer::Operation::Inspect.(New, style: :row)
230
- =begin
231
- #:inh-new-pipe
232
- 0 =======================>>operation.new
233
- 1 ==========================&model.build
234
- 2 =======================>contract.build
235
- #:inh-new-pipe end
236
- =end
237
-
238
- #:inh-create
239
- class Create < New
240
- step Contract::Validate()
241
- step Contract::Persist()
242
- end
243
- #:inh-create end
244
-
245
- puts Trailblazer::Operation::Inspect.(Create, style: :row)
246
- =begin
247
- #:inh-create-pipe
248
- 0 =======================>>operation.new
249
- 1 ==========================&model.build
250
- 2 =======================>contract.build
251
- 3 ==============&contract.default.params
252
- 4 ============&contract.default.validate
253
- 5 =========================&persist.save
254
- #:inh-create-pipe end
255
- =end
256
-
257
- module MyApp
258
- end
259
-
260
- #:override-app
261
- module MyApp::Operation
262
- class New < Trailblazer::Operation
263
- extend Contract::DSL
264
-
265
- contract do
266
- property :title
267
- end
268
-
269
- step Model( nil, :new )
270
- step Contract::Build()
271
- end
272
- end
273
- #:override-app end
274
-
275
- #:override-new
276
- class Song::New < MyApp::Operation::New
277
- step Model( Song, :new ), override: true
278
- end
279
- #:override-new end
280
-
281
- puts Trailblazer::Operation::Inspect.(Song::New, style: :row)
282
- =begin
283
- #:override-pipe
284
- Song::New["pipetree"].inspect(style: :row)
285
- 0 =======================>>operation.new
286
- 1 ==========================&model.build
287
- 2 =======================>contract.build
288
- #:override-pipe end
289
- =end
290
-
291
- it do
292
- Trailblazer::Operation::Inspect.(Song::New).must_equal %{[>model.build,>contract.build]}
293
- Song::New.(params: {}).inspect(:model).must_equal %{<Result:true [#<struct DocsOperationInheritanceTest::Song id=nil, title=nil, created_by=nil>] >}
294
- end
295
- end
296
-
297
- class DocsOperationStepOptionsTest < Minitest::Spec
298
- Song = Struct.new(:title) do
299
- def self.find_by(*)
300
- nil
301
- end
302
- end
303
-
304
- class AutomaticNameTest < Minitest::Spec
305
- #:name-auto
306
- class New < Trailblazer::Operation
307
- step Model( Song, :new )
308
- end
309
- #:name-auto end
310
-
311
- puts Trailblazer::Operation::Inspect.(New, style: :row)
312
- =begin
313
- #:name-auto-pipe
314
- 0 =======================>>operation.new
315
- 1 ==========================&model.build
316
- #:name-auto-pipe end
317
- =end
318
-
319
- #:replace-inh
320
- class Update < New
321
- step Model(Song, :find_by), replace: "model.build"
322
- end
323
- #:replace-inh end
324
-
325
- puts Trailblazer::Operation::Inspect.(Update, style: :row)
326
- =begin
327
- #:replace-inh-pipe
328
- 0 =======================>>operation.new
329
- 2 ==========================&model.build
330
- #:replace-inh-pipe end
331
- =end
332
-
333
- it { Update.(params: {}).inspect(:model).must_equal %{<Result:false [nil] >} }
334
-
335
-
336
- # #:delete-inh
337
- # class Noop < New
338
- # step nil, delete: "model.build"
339
- # end
340
- # #:delete-inh end
341
-
342
- # puts "yo"
343
- # puts Update["pipetree"].inspect(style: :row)
344
- # =begin
345
- # #:delete-inh-pipe
346
- # 0 =======================>>operation.new
347
- # 2 ==========================&model.build
348
- # #:delete-inh-pipe end
349
- # =end
350
-
351
- # it { Noop.({}).inspect(:model).must_equal %{<Result:false [nil] >} }
352
- end
353
-
354
- class ManualNameTest < Minitest::Spec
355
- #:name-manu
356
- class New < Trailblazer::Operation
357
- step(Model( Song, :new ), {name: "build.song.model"})
358
- step :validate_params!, name: "my.params.validate"
359
- # ..
360
- end
361
- #:name-manu end
362
-
363
- puts Trailblazer::Operation::Inspect.(New, style: :row)
364
- =begin
365
- #:name-manu-pipe
366
- 0 =======================>>operation.new
367
- 1 =====================&build.song.model
368
- 2 ===================&my.params.validate
369
- #:name-manu-pipe end
370
- =end
371
- end
372
-
373
- class BeforeTest < Minitest::Spec
374
- #:pos-before
375
- class New < Trailblazer::Operation
376
- step Model( Song, :new )
377
- step :validate_params!, before: "model.build"
378
- # ..
379
- end
380
- #:pos-before end
381
-
382
- puts Trailblazer::Operation::Inspect.(New, style: :row)
383
- =begin
384
- #:pos-before-pipe
385
- 0 =======================>>operation.new
386
- 1 =====================&validate_params!
387
- 2 ==========================&model.build
388
- #:pos-before-pipe end
389
- =end
390
-
391
- #:pos-inh
392
- class Create < New
393
- step :policy!, before: "model.build"
394
- end
395
- #:pos-inh end
396
-
397
- puts Trailblazer::Operation::Inspect.(Create, style: :row)
398
- =begin
399
- #:pos-inh-pipe
400
- 0 =======================>>operation.new
401
- 1 ==============================&policy!
402
- 2 =====================&validate_params!
403
- 3 ==========================&model.build
404
- #:pos-inh-pipe end
405
- =end
406
- end
407
- end
@@ -1,83 +0,0 @@
1
- require "test_helper"
2
-
3
- class DocsTraceTest < Minitest::Spec
4
- Song = Struct.new(:id, :title)
5
-
6
- class MyContract < Reform::Form
7
- property :title
8
- end
9
-
10
- class Create < Trailblazer::Operation
11
- class Present < Trailblazer::Operation
12
- step Model( Song, :new )
13
- step Contract::Build( constant: MyContract )
14
- end
15
-
16
- step Nested( Present )
17
- step Contract::Validate( key: :song )
18
- step Contract::Persist()
19
- step :notify_band
20
-
21
- def notify_band(options, **)
22
- true
23
- end
24
- end
25
-
26
- let(:params) { {} }
27
- let(:current_user) { Module }
28
-
29
- it do
30
- #:trace
31
- result = Create::Present.trace( params: params, current_user: current_user )
32
- puts result.wtf?
33
-
34
- # =>
35
- # |-- Start.default
36
- # |-- model.build
37
- # |-- contract.build
38
- # `-- End.success
39
- #:trace end
40
-
41
- result.wtf.gsub(/0x\w+/, "").must_equal %{`-- DocsTraceTest::Create::Present
42
- |-- Start.default
43
- |-- model.build
44
- |-- contract.build
45
- `-- End.success}
46
- end
47
-
48
- it do
49
- #:trace-cpx
50
- result = Create.trace( params: params, current_user: current_user )
51
- puts result.wtf?
52
-
53
- # =>
54
- # |-- #<Trailblazer::Activity::Nested:0x000000031960d8>
55
- # | |-- Start.default
56
- # | |-- model.build
57
- # | |-- contract.build
58
- # | |-- End.success
59
- # | `-- #<Trailblazer::Activity::Nested:0x000000031960d8>
60
- # |-- #<Trailblazer::Activity::Nested:0x0000000311f3e8>
61
- # | |-- Start.default
62
- # | |-- contract.default.params
63
- # | |-- End.failure
64
- # | `-- #<Trailblazer::Activity::Nested:0x0000000311f3e8>
65
- # `-- #<Trailblazer::Operation::Railway::End::Failure:0x00000003201fb8>
66
- #:trace-cpx end
67
-
68
- # result.wtf?.must_equal %{|-- Start.default
69
- # |-- model.build
70
- # |-- contract.build
71
- # `-- End.success}
72
- end
73
-
74
- # operation = ->(*args) { Create.__call__(*args) }
75
-
76
- # stack, _ = Trailblazer::Circuit::Trace.(
77
- # operation,
78
- # nil,
79
- # options={ a_return: true },
80
- # )
81
-
82
- # puts output = Circuit::Trace::Present.tree(stack)
83
- end