test/docs/nested_test.rb in trailblazer-macro-2.1.0.rc1 vs test/docs/nested_test.rb in trailblazer-macro-2.1.0.rc11
- old
+ new
@@ -1,113 +1,109 @@
require "test_helper"
class NestedInput < Minitest::Spec
- #:input-multiply
- class Multiplier < Trailblazer::Operation
- step ->(options, x:, y:, **) { options["product"] = x*y }
- end
- #:input-multiply end
+ let(:edit) do
+ edit = Class.new(Trailblazer::Operation) do
+ step :c
- #:input-pi
- class MultiplyByPi < Trailblazer::Operation
- step ->(options, **) { options["pi_constant"] = 3.14159 }
- step Nested( Multiplier, input: ->(options, **) do
- { "y" => options["pi_constant"],
- "x" => options["x"]
- }
- end )
+ include T.def_steps(:c)
+ end
end
- #:input-pi end
- it { MultiplyByPi.("x" => 9).inspect("product").must_equal %{<Result:true [28.27431] >} }
-
- it do
- #:input-result
- result = MultiplyByPi.("x" => 9)
- result["product"] #=> [28.27431]
- #:input-result end
+ let(:update) do
+ edit = Class.new(Trailblazer::Operation) do
+ step :d
+ include T.def_steps(:d)
+ end
end
-end
-class NestedInputCallable < Minitest::Spec
- Multiplier = NestedInput::Multiplier
+ it "Nested(Edit), without any options" do
+ edit = self.edit
- #:input-callable
- class MyInput
- def self.call(options, **)
- {
- "y" => options["pi_constant"],
- "x" => options["x"]
- }
+ create = Class.new(Trailblazer::Operation) do
+ step :a
+ step Nested( edit )
+ step :b
+
+ include T.def_steps(:a, :b)
end
- end
- #:input-callable end
- #:input-callable-op
- class MultiplyByPi < Trailblazer::Operation
- step ->(options, **) { options["pi_constant"] = 3.14159 }
- step Nested( Multiplier, input: MyInput )
+ # this will print a DEPRECATION warning.
+ # success
+ create.(seq: []).inspect(:seq).must_equal %{<Result:true [[:a, :c, :b]] >}
+ # failure in Nested
+ create.(seq: [], c: false).inspect(:seq).must_equal %{<Result:false [[:a, :c]] >}
end
- #:input-callable-op end
- it { MultiplyByPi.("x" => 9).inspect("product").must_equal %{<Result:true [28.27431] >} }
-end
+ it "Nested(Edit), with Output rewiring" do
+ edit = self.edit
-class NestedWithCallableAndInputTest < Minitest::Spec
- Memo = Struct.new(:title, :text, :created_by)
+ create = Class.new(Trailblazer::Operation) do
+ step :a
+ step Nested( edit ), Output(:failure) => Track(:success)
+ step :b
- class Memo::Upsert < Trailblazer::Operation
- step Nested( :operation_class, input: :input_for_create )
-
- def operation_class( ctx, ** )
- ctx[:id] ? Update : Create
+ include T.def_steps(:a, :b)
end
- # only let :title pass through.
- def input_for_create( ctx, ** )
- { title: ctx[:title] }
- end
+ # success
+ create.(seq: []).inspect(:seq).must_equal %{<Result:true [[:a, :c, :b]] >}
+ # failure in Nested
+ create.(seq: [], c: false).inspect(:seq).must_equal %{<Result:true [[:a, :c, :b]] >}
+ end
- class Create < Trailblazer::Operation
- step :create_memo
+ it "Nested(:method)" do
+ create = Class.new(Trailblazer::Operation) do
+ step :a
+ step Nested(:compute_edit)
+ step :b
- def create_memo( ctx, ** )
- ctx[:model] = Memo.new(ctx[:title], ctx[:text], :create)
+ def compute_edit(ctx, what:, **)
+ what
end
+
+ include T.def_steps(:a, :b)
end
- class Update < Trailblazer::Operation
- step :find_by_title
+ # `edit` and `update` can be called from Nested()
- def find_by_title( ctx, ** )
- ctx[:model] = Memo.new(ctx[:title], ctx[:text], :update)
- end
- end
- end
+ # edit/success
+ create.(seq: [], what: edit).inspect(:seq).must_equal %{<Result:true [[:a, :c, :b]] >}
- it "runs Create without :id" do
- Memo::Upsert.( title: "Yay!" ).inspect(:model).
- must_equal %{<Result:true [#<struct NestedWithCallableAndInputTest::Memo title=\"Yay!\", text=nil, created_by=:create>] >}
- end
+ # update/success
+ create.(seq: [], what: update).inspect(:seq).must_equal %{<Result:true [[:a, :d, :b]] >}
- it "runs Update without :id" do
- Memo::Upsert.( id: 1, title: "Yay!" ).inspect(:model).
- must_equal %{<Result:true [#<struct NestedWithCallableAndInputTest::Memo title=\"Yay!\", text=nil, created_by=:update>] >}
+
+# wiring of fail:
+ # edit/failure
+ create.(seq: [], what: edit, c: false).inspect(:seq).must_equal %{<Result:false [[:a, :c]] >}
+ # update/failure
+ create.(seq: [], what: update, d: false).inspect(:seq).must_equal %{<Result:false [[:a, :d]] >}
end
-end
-# builder: Nested + deviate to left if nil / skip_track if true
+ let(:compute_edit) {
+ ->(ctx, what:, **) { what }
+ }
-#---
-# automatic :name
-class NestedNameTest < Minitest::Spec
- class Create < Trailblazer::Operation
- class Present < Trailblazer::Operation
- # ...
+ it "Nested(:method), :pass_fast => :fail_fast doesn't work with standard wiring" do
+ skip "we need to allow adding :outputs"
+
+ compute_edit = self.compute_edit
+
+ pass_fast = Class.new(Trailblazer::Operation) do
+ step :p, pass_fast: true
+ include T.def_steps(:p)
end
- step Nested( Present )
- # ...
- end
+ create = Class.new(Trailblazer::Operation) do
+ step :a
+ step Nested(compute_edit, auto_wire: [pass_fast]), Output(:pass_fast) => Track(:fail_fast)
+ step :b
+ include T.def_steps(:a, :b)
+ end
- it { Operation::Inspect.(Create).must_equal %{[>Nested(NestedNameTest::Create::Present)]} }
+
+ create.(seq: [], what: pass_fast).inspect(:seq).must_equal %{<Result:false [[:a, :c]] >}
+ end
end
+
+# TODO: test with :input/:output, tracing