trailblazer-macro 2.1.14 → 2.1.16
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.
- checksums.yaml +4 -4
- data/CHANGES.md +12 -0
- data/Gemfile +3 -2
- data/lib/trailblazer/macro/each.rb +7 -2
- data/lib/trailblazer/macro/model/find.rb +175 -0
- data/lib/trailblazer/macro/rescue.rb +8 -6
- data/lib/trailblazer/macro/strategy.rb +1 -1
- data/lib/trailblazer/macro/version.rb +1 -1
- data/lib/trailblazer/macro.rb +14 -3
- data/test/docs/autogenerated/operation_each_test.rb +2 -0
- data/test/docs/autogenerated/operation_model_test.rb +4 -1
- data/test/docs/each_test.rb +12 -4
- data/test/docs/guard_test.rb +17 -15
- data/test/docs/macro_test.rb +1 -1
- data/test/docs/model/find_test.rb +679 -0
- data/test/docs/model_test.rb +3 -0
- data/test/docs/nested_static_test.rb +3 -1
- data/test/docs/pundit_test.rb +19 -19
- data/test/docs/rescue_test.rb +100 -28
- data/test/docs/wrap_test.rb +45 -27
- data/test/operation/integration_test.rb +5 -7
- data/test/operation/pundit_test.rb +31 -25
- data/test/test_helper.rb +9 -2
- data/trailblazer-macro.gemspec +2 -2
- metadata +15 -6
data/test/docs/pundit_test.rb
CHANGED
@@ -29,29 +29,29 @@ class DocsPunditProcTest < Minitest::Spec
|
|
29
29
|
end
|
30
30
|
#:pundit end
|
31
31
|
|
32
|
-
it { Trailblazer::Developer.railway(Create)
|
33
|
-
it { Create.(params: {}, current_user: Module).inspect(:model)
|
34
|
-
it { Create.(params: {}
|
32
|
+
it { assert_equal Trailblazer::Developer.railway(Create), %{[>model.build,>policy.default.eval]} }
|
33
|
+
it { assert_equal Create.(params: {}, current_user: Module).inspect(:model), %{<Result:true [#<struct DocsPunditProcTest::Song id=nil>] >} }
|
34
|
+
it { assert_equal Create.(params: {}).inspect(:model), %{<Result:false [#<struct DocsPunditProcTest::Song id=nil>] >} }
|
35
35
|
|
36
36
|
it do
|
37
37
|
#:pundit-result
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
result = Create.(params: {}, current_user: Module)
|
39
|
+
result[:"result.policy.default"].success? #=> true
|
40
|
+
result[:"result.policy.default"][:policy] #=> #<MyPolicy ...>
|
41
41
|
#:pundit-result end
|
42
|
-
result[:"result.policy.default"].success
|
43
|
-
result[:"result.policy.default"][:policy].is_a?(MyPolicy)
|
42
|
+
assert_equal result[:"result.policy.default"].success?, true
|
43
|
+
assert_equal result[:"result.policy.default"][:policy].is_a?(MyPolicy), true
|
44
44
|
end
|
45
45
|
|
46
46
|
#---
|
47
47
|
#- override
|
48
48
|
class New < Create
|
49
|
-
step Policy::Pundit( MyPolicy, :new? ),
|
49
|
+
step Policy::Pundit( MyPolicy, :new? ), replace: :"policy.default.eval"
|
50
50
|
end
|
51
51
|
|
52
|
-
it { Trailblazer::Developer.railway(New)
|
53
|
-
it { New.(params: {}, current_user: Class ).inspect(:model)
|
54
|
-
it { New.(params: {}, current_user: nil ).inspect(:model)
|
52
|
+
it { assert_equal Trailblazer::Developer.railway(New), %{[>model.build,>policy.default.eval]} }
|
53
|
+
it { assert_equal New.(params: {}, current_user: Class ).inspect(:model), %{<Result:true [#<struct DocsPunditProcTest::Song id=nil>] >} }
|
54
|
+
it { assert_equal New.(params: {}, current_user: nil ).inspect(:model), %{<Result:false [#<struct DocsPunditProcTest::Song id=nil>] >} }
|
55
55
|
|
56
56
|
#---
|
57
57
|
#- override with :name
|
@@ -64,10 +64,10 @@ class DocsPunditProcTest < Minitest::Spec
|
|
64
64
|
step Policy::Pundit( MyPolicy, :new?, name: "first" ), override: true
|
65
65
|
end
|
66
66
|
|
67
|
-
it { Trailblazer::Developer.railway(Edit)
|
68
|
-
it { Edit.(params: {}, current_user: Class).inspect(:model)
|
69
|
-
it { Trailblazer::Developer.railway(Update)
|
70
|
-
it { Update.(params: {}, current_user: Class).inspect(:model)
|
67
|
+
it { assert_equal Trailblazer::Developer.railway(Edit), %{[>policy.first.eval,>policy.second.eval]} }
|
68
|
+
it { assert_equal Edit.(params: {}, current_user: Class).inspect(:model), %{<Result:false [nil] >} }
|
69
|
+
it { assert_equal Trailblazer::Developer.railway(Update), %{[>policy.first.eval,>policy.second.eval]} }
|
70
|
+
it { assert_equal Update.(params: {}, current_user: Class).inspect(:model), %{<Result:true [nil] >} }
|
71
71
|
|
72
72
|
#---
|
73
73
|
# dependency injection
|
@@ -85,7 +85,7 @@ class DocsPunditProcTest < Minitest::Spec
|
|
85
85
|
:"policy.default.eval" => Trailblazer::Operation::Policy::Pundit.build(AnotherPolicy, :create?)
|
86
86
|
)
|
87
87
|
#:di-call end
|
88
|
-
|
88
|
+
assert_equal result.inspect(""), %{<Result:true [nil] >} }
|
89
89
|
end
|
90
90
|
|
91
91
|
#-
|
@@ -106,7 +106,7 @@ class PunditWithNameTest < Minitest::Spec
|
|
106
106
|
result = Create.(params: {}, current_user: Module)
|
107
107
|
result[:"result.policy.after_model"].success? #=> true
|
108
108
|
#:name-call end
|
109
|
-
|
109
|
+
assert_equal result[:"result.policy.after_model"].success?, true }
|
110
110
|
end
|
111
111
|
|
112
112
|
#---
|
@@ -123,7 +123,7 @@ end
|
|
123
123
|
# #:class-level end
|
124
124
|
|
125
125
|
# it { Create.(); Create["result.policy"].must_be_nil }
|
126
|
-
# it { Create.(params: {}, current_user: Module)["x"]
|
126
|
+
# it { Create.(params: {}, current_user: Module)["x"], true }
|
127
127
|
# it { Create.(params: {} )["x"].must_be_nil }
|
128
128
|
# end
|
129
129
|
|
data/test/docs/rescue_test.rb
CHANGED
@@ -23,18 +23,18 @@ class NestedRescueTest < Minitest::Spec
|
|
23
23
|
fail ->(options, **) { options["outer-err"] = true }, id: "nested/failure"
|
24
24
|
end
|
25
25
|
|
26
|
-
it {
|
27
|
-
it { NestedInsanity.().inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err")
|
28
|
-
it { NestedInsanity.( "raise-y" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err")
|
29
|
-
it { NestedInsanity.( "raise-a" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err")
|
26
|
+
it { assert_match /\[>Rescue\/.{1,3},>nested/, Trailblazer::Developer.railway(NestedInsanity) } # FIXME: better introspect tests for all id-generating macros.
|
27
|
+
it { assert_equal NestedInsanity.().inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err"), %{<Result:true [true, true, true, true, true, true, nil, nil] >} }
|
28
|
+
it { assert_equal NestedInsanity.( "raise-y" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err"), %{<Result:false [true, true, nil, nil, nil, nil, true, true] >} }
|
29
|
+
it { assert_equal NestedInsanity.( "raise-a" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err"), %{<Result:false [true, true, true, true, nil, nil, nil, true] >} }
|
30
30
|
|
31
31
|
#-
|
32
32
|
# inheritance
|
33
33
|
class UbernestedInsanity < NestedInsanity
|
34
34
|
end
|
35
35
|
|
36
|
-
it { UbernestedInsanity.().inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err")
|
37
|
-
it { UbernestedInsanity.( "raise-a" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err")
|
36
|
+
it { assert_equal UbernestedInsanity.().inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err"), %{<Result:true [true, true, true, true, true, true, nil, nil] >} }
|
37
|
+
it { assert_equal UbernestedInsanity.( "raise-a" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err"), %{<Result:false [true, true, true, true, nil, nil, nil, true] >} }
|
38
38
|
end
|
39
39
|
|
40
40
|
class RescueTest < Minitest::Spec
|
@@ -43,31 +43,37 @@ class RescueTest < Minitest::Spec
|
|
43
43
|
plain Rescue()
|
44
44
|
=end
|
45
45
|
class RescueWithoutHandlerTest < Minitest::Spec
|
46
|
-
|
46
|
+
Song = Class.new
|
47
|
+
module Song::Activity; end
|
47
48
|
|
48
|
-
|
49
|
-
|
49
|
+
#:no-args
|
50
|
+
class Song::Activity::Create < Trailblazer::Activity::Railway
|
51
|
+
step :create_model
|
50
52
|
step Rescue() {
|
51
|
-
step :
|
53
|
+
step :upload
|
52
54
|
step :rehash
|
53
55
|
}
|
54
56
|
step :notify
|
55
57
|
fail :log_error
|
56
58
|
#~methods
|
57
|
-
include T.def_steps(:
|
59
|
+
include T.def_steps(:create_model, :upload, :notify, :log_error)
|
58
60
|
include Rehash
|
59
61
|
#~methods end
|
60
62
|
end
|
63
|
+
#:no-args end
|
61
64
|
|
62
|
-
it {
|
63
|
-
it {
|
65
|
+
it { assert_invoke Song::Activity::Create, seq: "[:create_model, :upload, :rehash, :notify]" }
|
66
|
+
it { assert_invoke Song::Activity::Create, rehash_raise: RuntimeError, terminus: :failure, seq: "[:create_model, :upload, :rehash, :log_error]", exception_class: RuntimeError }
|
67
|
+
it { assert_invoke Song::Activity::Create, rehash_raise: :bla, terminus: :failure, seq: "[:create_model, :upload, :rehash, :log_error]", exception_class: :bla }
|
68
|
+
it { assert_invoke Song::Activity::Create, rehash_raise: NoMethodError, terminus: :failure, seq: "[:create_model, :upload, :rehash, :log_error]", exception_class: NoMethodError }
|
64
69
|
end
|
65
70
|
|
66
71
|
=begin
|
67
|
-
Rescue( handler: X )
|
72
|
+
Rescue( SPECIFIC_EXCEPTION, handler: X )
|
68
73
|
=end
|
69
74
|
class RescueWithClassHandlerTest < Minitest::Spec
|
70
|
-
|
75
|
+
Song = Class.new
|
76
|
+
module Song::Activity; end
|
71
77
|
|
72
78
|
#:rescue-handler
|
73
79
|
class MyHandler
|
@@ -78,23 +84,29 @@ Rescue( handler: X )
|
|
78
84
|
#:rescue-handler end
|
79
85
|
|
80
86
|
#:rescue
|
81
|
-
class
|
82
|
-
step :
|
83
|
-
step Rescue(
|
84
|
-
step :
|
87
|
+
class Song::Activity::Create < Trailblazer::Activity::Railway
|
88
|
+
step :create_model
|
89
|
+
step Rescue(RuntimeError, handler: MyHandler) {
|
90
|
+
step :upload
|
85
91
|
step :rehash
|
86
92
|
}
|
87
93
|
step :notify
|
88
94
|
fail :log_error
|
89
95
|
#~methods
|
90
|
-
include T.def_steps(:
|
96
|
+
include T.def_steps(:create_model, :upload, :notify, :log_error)
|
91
97
|
include Rehash
|
92
98
|
#~methods end
|
93
99
|
end
|
94
100
|
#:rescue end
|
95
101
|
|
96
|
-
it {
|
97
|
-
it {
|
102
|
+
it { assert_invoke Song::Activity::Create, seq: "[:create_model, :upload, :rehash, :notify]" }
|
103
|
+
it { assert_invoke Song::Activity::Create, rehash_raise: RuntimeError, terminus: :failure, seq: "[:create_model, :upload, :rehash, :log_error]", exception_class: RuntimeError }
|
104
|
+
it do
|
105
|
+
# Since we don't catch NoMethodError, execution stops.
|
106
|
+
assert_raises NoMethodError do
|
107
|
+
Song::Activity::Create.invoke([{seq: {}, rehash_raise: NoMethodError}])
|
108
|
+
end
|
109
|
+
end
|
98
110
|
end
|
99
111
|
|
100
112
|
class RescueWithModuleHandlerTest < Minitest::Spec
|
@@ -118,8 +130,8 @@ Rescue( handler: X )
|
|
118
130
|
include Rehash
|
119
131
|
end
|
120
132
|
|
121
|
-
it { Memo::Create.( { seq: [], } ).inspect(:seq, :exception_class)
|
122
|
-
it { Memo::Create.( { seq: [], rehash_raise:
|
133
|
+
it { assert_equal Memo::Create.( { seq: [], } ).inspect(:seq, :exception_class), %{<Result:true [[:find_model, :update, :rehash, :notify], nil] >} }
|
134
|
+
it { assert_equal Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq, :exception_class), %{<Result:false [[:find_model, :update, :rehash, :log_error], RuntimeError] >} }
|
123
135
|
end
|
124
136
|
|
125
137
|
=begin
|
@@ -148,8 +160,8 @@ Rescue( handler: :instance_method )
|
|
148
160
|
end
|
149
161
|
#:rescue-method end
|
150
162
|
|
151
|
-
it { Memo::Create.( { seq: [], } ).inspect(:seq, :exception_class)
|
152
|
-
it { Memo::Create.( { seq: [], rehash_raise:
|
163
|
+
it { assert_equal Memo::Create.( { seq: [], } ).inspect(:seq, :exception_class), %{<Result:true [[:find_model, :update, :rehash, :notify], nil] >} }
|
164
|
+
it { assert_equal Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq, :exception_class), %{<Result:false [[:find_model, :update, :rehash, :log_error], RuntimeError] >} }
|
153
165
|
end
|
154
166
|
|
155
167
|
=begin
|
@@ -173,7 +185,67 @@ Rescue(), fast_track: true {}
|
|
173
185
|
include T.def_steps(:find_model, :update, :notify, :log_error, :rehash)
|
174
186
|
end
|
175
187
|
|
176
|
-
it { Memo::Create.( { seq: [], } ).inspect(:seq)
|
177
|
-
it { Memo::Create.( { seq: [], update: false } ).inspect(:seq)
|
188
|
+
it { assert_equal Memo::Create.( { seq: [], } ).inspect(:seq), %{<Result:true [[:find_model, :update, :rehash, :notify]] >} }
|
189
|
+
it { assert_equal Memo::Create.( { seq: [], update: false } ).inspect(:seq), %{<Result:false [[:find_model, :update]] >} }
|
190
|
+
end
|
191
|
+
|
192
|
+
class RescueIDTest < Minitest::Spec
|
193
|
+
class Validate
|
194
|
+
def self.call(*)
|
195
|
+
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
it "assigns ID via {Macro.id_for}" do
|
200
|
+
activity = Class.new(Trailblazer::Activity::Railway) do
|
201
|
+
step Rescue() {}
|
202
|
+
step Rescue(handler: Validate) {}
|
203
|
+
step Rescue(handler: :instance_method) {}
|
204
|
+
step Rescue() {}, id: "Rescue-1"
|
205
|
+
step Rescue(id: "Rescue-2") {}
|
206
|
+
# test identical configuration.
|
207
|
+
step Rescue() {}
|
208
|
+
step Rescue(handler: Validate) {}
|
209
|
+
end
|
210
|
+
|
211
|
+
# assert_equal Trailblazer::Developer::Introspect.find_path(activity, ["Each/EachIDTest::Validate"])[0].id, "Each/EachIDTest::Validate"
|
212
|
+
# assert_equal Trailblazer::Developer::Introspect.find_path(activity, ["Each-1"])[0].id, "Each-1"
|
213
|
+
# assert_equal Trailblazer::Developer::Introspect.find_path(activity, ["Each/composers_for_each"])[0].id, "Each/composers_for_each"
|
214
|
+
|
215
|
+
assert_match (/Rescue\/\d+/), Trailblazer::Activity::Introspect::Nodes(activity).values[1].id
|
216
|
+
assert_match (/Rescue\/\d+/), Trailblazer::Activity::Introspect::Nodes(activity).values[2].id
|
217
|
+
assert_match (/Rescue\/\d+/), Trailblazer::Activity::Introspect::Nodes(activity).values[3].id
|
218
|
+
assert_equal "Rescue-1", Trailblazer::Activity::Introspect::Nodes(activity).values[4].id
|
219
|
+
assert_equal "Rescue-2", Trailblazer::Activity::Introspect::Nodes(activity).values[5].id
|
220
|
+
assert_match (/Rescue\/\d+/), Trailblazer::Activity::Introspect::Nodes(activity).values[6].id
|
221
|
+
assert_match (/Rescue\/\d+/), Trailblazer::Activity::Introspect::Nodes(activity).values[7].id
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
class ComplianceTest < Minitest::Spec
|
226
|
+
it "tracing" do
|
227
|
+
activity = Class.new(Trailblazer::Activity::Railway) do
|
228
|
+
step Rescue(id: "Rescue/1") {
|
229
|
+
step :validate
|
230
|
+
}
|
231
|
+
|
232
|
+
def validate(ctx, validate: false, seq:, **)
|
233
|
+
seq << :validate
|
234
|
+
raise unless validate
|
235
|
+
validate
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
ctx = {validate: false}
|
240
|
+
|
241
|
+
output, _ = trace activity, ctx
|
242
|
+
|
243
|
+
assert_equal output, %(TOP
|
244
|
+
|-- Start.default
|
245
|
+
|-- Rescue/1
|
246
|
+
| |-- Start.default
|
247
|
+
| `-- validate
|
248
|
+
`-- End.failure)
|
249
|
+
end
|
178
250
|
end
|
179
251
|
end
|
data/test/docs/wrap_test.rb
CHANGED
@@ -169,9 +169,9 @@ When raise: return {Railway.fail!}
|
|
169
169
|
#@ happy days
|
170
170
|
assert_invoke Memo::Create, seq: "[:model, :update, :rehash, :notify]"
|
171
171
|
#@ rehash raises
|
172
|
-
assert_invoke Memo::Create, rehash_raise:
|
172
|
+
assert_invoke Memo::Create, rehash_raise: RuntimeError, seq: "[:model, :update, :rehash, :log_error]",
|
173
173
|
terminus: :failure,
|
174
|
-
expected_ctx_variables: {exception: "
|
174
|
+
expected_ctx_variables: {exception: "RuntimeError"}
|
175
175
|
end
|
176
176
|
|
177
177
|
=begin
|
@@ -204,8 +204,8 @@ When raise: return {Railway.fail!}, but wire Wrap() to {fail_fast: true}
|
|
204
204
|
#~methods end
|
205
205
|
end
|
206
206
|
|
207
|
-
it { Memo::Create.( { seq: [] } ).inspect(:seq)
|
208
|
-
it { Memo::Create.( { seq: [], rehash_raise:
|
207
|
+
it { assert_equal Memo::Create.( { seq: [] } ).inspect(:seq), %{<Result:true [[:model, :update, :rehash, :notify]] >} }
|
208
|
+
it { assert_equal Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq), %{<Result:false [[:model, :update, :rehash]] >} }
|
209
209
|
end
|
210
210
|
|
211
211
|
=begin
|
@@ -241,8 +241,8 @@ When raise: return {Railway.fail_fast!} and configure Wrap() to {fast_track: t
|
|
241
241
|
end
|
242
242
|
#:fail-fast end
|
243
243
|
|
244
|
-
it { Memo::Create.( { seq: [] } ).inspect(:seq)
|
245
|
-
it { Memo::Create.( { seq: [], rehash_raise:
|
244
|
+
it { assert_equal Memo::Create.( { seq: [] } ).inspect(:seq), %{<Result:true [[:model, :update, :rehash, :notify]] >} }
|
245
|
+
it { assert_equal Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq), %{<Result:false [[:model, :update, :rehash]] >} }
|
246
246
|
end
|
247
247
|
|
248
248
|
=begin
|
@@ -284,14 +284,16 @@ When raise: return {Railway.fail!} or {Railway.pass!}
|
|
284
284
|
|
285
285
|
it do
|
286
286
|
result = Memo::Create.( { seq: [] } )
|
287
|
-
|
288
|
-
result.
|
287
|
+
|
288
|
+
assert_equal result.inspect(:seq), %{<Result:false [[:model, :update, :rehash]] >}
|
289
|
+
assert_equal result.event.inspect, %{#<Trailblazer::Activity::End semantic=:transaction_worked>}
|
289
290
|
end
|
290
291
|
|
291
292
|
it do
|
292
|
-
result = Memo::Create.( { seq: [], rehash_raise:
|
293
|
-
|
294
|
-
result.
|
293
|
+
result = Memo::Create.( { seq: [], rehash_raise: RuntimeError } )
|
294
|
+
|
295
|
+
assert_equal result.inspect(:seq), %{<Result:false [[:model, :update, :rehash]] >}
|
296
|
+
assert_equal result.event.inspect, %{#<Trailblazer::Activity::End semantic=:transaction_failed>}
|
295
297
|
end
|
296
298
|
end
|
297
299
|
|
@@ -325,8 +327,8 @@ When raise: return {Railway.pass!} and go "successful"
|
|
325
327
|
#~methods end
|
326
328
|
end
|
327
329
|
|
328
|
-
it { Memo::Create.( { seq: [] } ).inspect(:seq)
|
329
|
-
it { Memo::Create.( { seq: [], rehash_raise:
|
330
|
+
it { assert_equal Memo::Create.( { seq: [] } ).inspect(:seq), %{<Result:true [[:model, :update, :rehash, :notify]] >} }
|
331
|
+
it { assert_equal Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq), %{<Result:true [[:model, :update, :rehash, :notify]] >} }
|
330
332
|
end
|
331
333
|
|
332
334
|
=begin
|
@@ -361,9 +363,10 @@ You can return boolean true in wrap.
|
|
361
363
|
end
|
362
364
|
|
363
365
|
it "translates true returned form a wrap to a signal with a `success` semantic" do
|
364
|
-
result = Memo::Create.( { seq: [], rehash_raise:
|
365
|
-
|
366
|
-
result.
|
366
|
+
result = Memo::Create.( { seq: [], rehash_raise: RuntimeError } )
|
367
|
+
|
368
|
+
assert_equal result.inspect(:seq), %{<Result:true [[:model, :update, :rehash, :notify]] >}
|
369
|
+
assert_equal result.event.inspect, %{#<Trailblazer::Activity::Railway::End::Success semantic=:success>}
|
367
370
|
end
|
368
371
|
end
|
369
372
|
|
@@ -399,9 +402,10 @@ You can return boolean false in wrap.
|
|
399
402
|
end
|
400
403
|
|
401
404
|
it "translates false returned form a wrap to a signal with a `failure` semantic" do
|
402
|
-
result = Memo::Create.( { seq: [], rehash_raise:
|
403
|
-
|
404
|
-
result.
|
405
|
+
result = Memo::Create.( { seq: [], rehash_raise: RuntimeError } )
|
406
|
+
|
407
|
+
assert_equal result.inspect(:seq), %{<Result:false [[:model, :update, :rehash, :log_error]] >}
|
408
|
+
assert_equal result.event.inspect, %{#<Trailblazer::Activity::Railway::End::Failure semantic=:failure>}
|
405
409
|
end
|
406
410
|
end
|
407
411
|
|
@@ -437,9 +441,10 @@ You can return nil in wrap.
|
|
437
441
|
end
|
438
442
|
|
439
443
|
it "translates nil returned form a wrap to a signal with a `failure` semantic" do
|
440
|
-
result = Memo::Create.( { seq: [], rehash_raise:
|
441
|
-
|
442
|
-
result.
|
444
|
+
result = Memo::Create.( { seq: [], rehash_raise: RuntimeError } )
|
445
|
+
|
446
|
+
assert_equal result.inspect(:seq), %{<Result:false [[:model, :update, :rehash, :log_error]] >}
|
447
|
+
assert_equal result.event.inspect, %{#<Trailblazer::Activity::Railway::End::Failure semantic=:failure>}
|
443
448
|
end
|
444
449
|
end
|
445
450
|
|
@@ -453,7 +458,7 @@ This one is mostly to show how one could wrap steps in a transaction
|
|
453
458
|
|
454
459
|
module Sequel
|
455
460
|
def self.transaction
|
456
|
-
|
461
|
+
_end_event, (_ctx, _flow_options) = yield
|
457
462
|
end
|
458
463
|
end
|
459
464
|
|
@@ -483,8 +488,8 @@ This one is mostly to show how one could wrap steps in a transaction
|
|
483
488
|
end
|
484
489
|
#:transaction end
|
485
490
|
|
486
|
-
it { Memo::Create.( { seq: [] } ).inspect(:seq)
|
487
|
-
it { Memo::Create.( { seq: [], rehash_raise:
|
491
|
+
it { assert_equal Memo::Create.( { seq: [] } ).inspect(:seq), %{<Result:true [[:model, :update, :rehash, :notify]] >} }
|
492
|
+
it { assert_equal Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq), %{<Result:false [[:model, :update, :rehash, :log_error]] >} }
|
488
493
|
end
|
489
494
|
|
490
495
|
=begin
|
@@ -523,8 +528,8 @@ This one is mostly to show how one could evaluate Wrap()'s return value based on
|
|
523
528
|
end
|
524
529
|
#:transaction end
|
525
530
|
|
526
|
-
it { Memo::Create.( { seq: [] } ).inspect(:seq)
|
527
|
-
it { Memo::Create.( { seq: [], update: false } ).inspect(:seq)
|
531
|
+
it { assert_equal Memo::Create.( { seq: [] } ).inspect(:seq), %{<Result:true [[:model, :update]] >} }
|
532
|
+
it { assert_equal Memo::Create.( { seq: [], update: false } ).inspect(:seq), %{<Result:false [[:model, :update, :log_error]] >} }
|
528
533
|
end
|
529
534
|
|
530
535
|
|
@@ -661,6 +666,19 @@ class WrapUnitTest < Minitest::Spec
|
|
661
666
|
|
662
667
|
#@ the patched version only runs {mock_validation}.
|
663
668
|
assert_invoke patched_activity, seq: %{[:mock_validation]}
|
669
|
+
|
670
|
+
# Patch Wrap/Subprocess/step, put step after {validate}
|
671
|
+
patched_activity = Trailblazer::Activity::DSL::Linear::Patch.call(
|
672
|
+
activity,
|
673
|
+
["Wrap/WrapUnitTest::HandleUnsafeProcess", :validation],
|
674
|
+
-> { step mock_validation }
|
675
|
+
)
|
676
|
+
|
677
|
+
#@ the original activity with Wrap is unchanged.
|
678
|
+
assert_invoke activity, seq: %{[:validate]}
|
679
|
+
|
680
|
+
#@ the patched version only runs {mock_validation}.
|
681
|
+
assert_invoke patched_activity, seq: %{[:validate, :mock_validation]}
|
664
682
|
end
|
665
683
|
end
|
666
684
|
|
@@ -30,12 +30,12 @@ class IntegrationTest < Minitest::Spec
|
|
30
30
|
end
|
31
31
|
|
32
32
|
class SongSpecialCreate < Trailblazer::Operation
|
33
|
-
step
|
34
|
-
step
|
33
|
+
step Subprocess(ArtistCreate)
|
34
|
+
step Subprocess(SongCreate)
|
35
35
|
end
|
36
36
|
|
37
37
|
it "create Artist and Song" do
|
38
|
-
result = SongSpecialCreate.
|
38
|
+
result = SongSpecialCreate.wtf?(
|
39
39
|
params: {
|
40
40
|
artist: {
|
41
41
|
name: "My Artist"
|
@@ -46,10 +46,8 @@ class IntegrationTest < Minitest::Spec
|
|
46
46
|
}
|
47
47
|
)
|
48
48
|
|
49
|
-
puts result.wtf?
|
50
|
-
|
51
49
|
# this should return song
|
52
|
-
result[:model].name
|
53
|
-
result[:model].artist_name
|
50
|
+
assert_equal result[:model].name, "My Song"
|
51
|
+
assert_equal result[:model].artist_name, "My Artist"
|
54
52
|
end
|
55
53
|
end
|
@@ -27,23 +27,24 @@ class PolicyTest < Minitest::Spec
|
|
27
27
|
# successful.
|
28
28
|
it do
|
29
29
|
result = Create.(params: {}, current_user: Module)
|
30
|
-
|
30
|
+
|
31
|
+
assert_equal result[:process], true
|
31
32
|
#- result object, policy
|
32
|
-
result[:"result.policy.default"].success
|
33
|
-
result[:"result.policy.default"][:message]
|
33
|
+
assert_equal result[:"result.policy.default"].success?, true
|
34
|
+
assert_nil result[:"result.policy.default"][:message]
|
34
35
|
# result[:valid].must_be_nil
|
35
|
-
result[:"policy.default"].inspect
|
36
|
+
assert_equal result[:"policy.default"].inspect, %{<Auth: user:Module, model:nil>}
|
36
37
|
end
|
37
38
|
# breach.
|
38
39
|
it do
|
39
40
|
result = Create.(params: {}, current_user: nil)
|
40
|
-
result[:process]
|
41
|
+
assert_nil result[:process]
|
41
42
|
#- result object, policy
|
42
|
-
result[:"result.policy.default"].success
|
43
|
-
result[:"result.policy.default"][:message]
|
43
|
+
assert_equal result[:"result.policy.default"].success?, false
|
44
|
+
assert_equal result[:"result.policy.default"][:message], "Breach"
|
44
45
|
end
|
45
|
-
# inject different policy.Condition it { Create.(params: {}, current_user: Object, "policy.default.eval" => Trailblazer::Operation::Policy::Pundit::Condition.new(Auth, :user_object?))["process"]
|
46
|
-
it { Create.(params: {}, current_user: Module, :"policy.default.eval" => Trailblazer::Operation::Policy::Pundit::Condition.new(Auth, :user_object?))[:process]
|
46
|
+
# inject different policy.Condition it { Create.(params: {}, current_user: Object, "policy.default.eval" => Trailblazer::Operation::Policy::Pundit::Condition.new(Auth, :user_object?))["process"], true }
|
47
|
+
it { assert_nil Create.(params: {}, current_user: Module, :"policy.default.eval" => Trailblazer::Operation::Policy::Pundit::Condition.new(Auth, :user_object?))[:process] }
|
47
48
|
|
48
49
|
|
49
50
|
#---
|
@@ -52,23 +53,25 @@ class PolicyTest < Minitest::Spec
|
|
52
53
|
step Model( Song, :new ), before: :"policy.default.eval"
|
53
54
|
end
|
54
55
|
|
55
|
-
it { Trailblazer::Developer.railway(Show)
|
56
|
+
it { assert_equal Trailblazer::Developer.railway(Show), %{[>model.build,>policy.default.eval,>process]} }
|
56
57
|
|
57
58
|
# invalid because user AND model.
|
58
59
|
it do
|
59
60
|
result = Show.(params: {}, current_user: Module)
|
60
|
-
result[:process]
|
61
|
-
|
62
|
-
|
61
|
+
assert_nil result[:process]
|
62
|
+
|
63
|
+
assert_equal result[:model].inspect, %{#<struct PolicyTest::Song id=nil>}
|
64
|
+
# result["policy"].inspect, %{#<struct PolicyTest::Song id=nil>}
|
63
65
|
end
|
64
66
|
|
65
67
|
# valid because new policy.
|
66
68
|
it do
|
67
69
|
# puts Show["pipetree"].inspect
|
68
70
|
result = Show.(params: {}, current_user: Module, :"policy.default.eval" => Trailblazer::Operation::Policy::Pundit::Condition.new(Auth, :user_and_model?))
|
69
|
-
|
70
|
-
result[:
|
71
|
-
result[:
|
71
|
+
|
72
|
+
assert_equal result[:process], true
|
73
|
+
assert_equal result[:model].inspect, %{#<struct PolicyTest::Song id=nil>}
|
74
|
+
assert_equal result[:"policy.default"].inspect, %{<Auth: user:Module, model:#<struct PolicyTest::Song id=nil>>}
|
72
75
|
end
|
73
76
|
|
74
77
|
##--
|
@@ -86,20 +89,23 @@ class PolicyTest < Minitest::Spec
|
|
86
89
|
# successful.
|
87
90
|
it do
|
88
91
|
result = Edit.(params: { id: 1 }, current_user: Module)
|
89
|
-
|
90
|
-
result[:
|
91
|
-
result[:
|
92
|
-
result[:"result.policy.default"]
|
92
|
+
|
93
|
+
assert_equal result[:process], true
|
94
|
+
assert_equal result[:model].inspect, %{#<struct PolicyTest::Song id=1>}
|
95
|
+
assert_equal result[:"result.policy.default"].success?, true
|
96
|
+
assert_nil result[:"result.policy.default"][:message]
|
93
97
|
# result[:valid].must_be_nil
|
94
|
-
result[:"policy.default"].inspect
|
98
|
+
assert_equal result[:"policy.default"].inspect, %{<Auth: user:Module, model:#<struct PolicyTest::Song id=1>>}
|
95
99
|
end
|
96
100
|
|
97
101
|
# breach.
|
98
102
|
it do
|
99
103
|
result = Edit.(params: { id: 4 }, current_user: nil)
|
100
|
-
|
101
|
-
result[:
|
102
|
-
result[:
|
103
|
-
|
104
|
+
|
105
|
+
assert_equal result[:model].inspect, %{#<struct PolicyTest::Song id=4>}
|
106
|
+
assert_nil result[:process]
|
107
|
+
|
108
|
+
assert_equal result[:"result.policy.default"].success?, false
|
109
|
+
assert_equal result[:"result.policy.default"][:message], "Breach"
|
104
110
|
end
|
105
111
|
end
|
data/test/test_helper.rb
CHANGED
@@ -17,7 +17,7 @@ end
|
|
17
17
|
module Rehash
|
18
18
|
def rehash(ctx, seq:, rehash_raise: false, **)
|
19
19
|
seq << :rehash
|
20
|
-
raise
|
20
|
+
raise rehash_raise if rehash_raise
|
21
21
|
true
|
22
22
|
end
|
23
23
|
end
|
@@ -27,6 +27,13 @@ Minitest::Spec.include Trailblazer::Activity::Testing::Assertions
|
|
27
27
|
Minitest::Spec.class_eval do
|
28
28
|
def trace(activity, ctx)
|
29
29
|
stack, signal, (ctx, _) = Trailblazer::Developer::Trace.invoke(activity, [ctx, {}])
|
30
|
-
|
30
|
+
|
31
|
+
output = Trailblazer::Developer::Trace::Present.(stack) do |trace_nodes:, **|
|
32
|
+
{node_options: {trace_nodes[0] => {label: "TOP"}}}
|
33
|
+
end.gsub(/:\d+/, "")
|
34
|
+
|
35
|
+
return output, signal, ctx
|
31
36
|
end
|
32
37
|
end
|
38
|
+
|
39
|
+
# Trailblazer::Core.convert_operation_test("test/docs/composable_variable_mapping_test.rb")
|
data/trailblazer-macro.gemspec
CHANGED
@@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.add_development_dependency "minitest"
|
21
21
|
spec.add_development_dependency "rake"
|
22
|
-
spec.add_development_dependency "trailblazer-developer"
|
23
|
-
spec.add_dependency "trailblazer-operation", ">= 0.10.
|
22
|
+
spec.add_development_dependency "trailblazer-developer", ">= 0.1.0", "< 0.2.0"
|
23
|
+
spec.add_dependency "trailblazer-operation", ">= 0.10.1" # TODO: this dependency will be removed. currently needed for tests and for Guard::Result
|
24
24
|
|
25
25
|
spec.add_dependency "trailblazer-activity-dsl-linear", ">= 1.2.0", "< 1.3.0"
|
26
26
|
|