diff --git a/lib/trailblazer/macro/nested.rb b/lib/trailblazer/macro/nested.rb index d5de136..ecc60bf 100644 --- a/lib/trailblazer/macro/nested.rb +++ b/lib/trailblazer/macro/nested.rb @@ -104,7 +104,7 @@ def self.call_dynamic_nested_activity((ctx, flow_options), runner:, **circuit_op def self.compute_legacy_return_signal(return_signal) actual_semantic = return_signal.to_h[:semantic] - applied_signal = SUCCESS_SEMANTICS.include?(actual_semantic) ? Activity::Right : Activity::Left # TODO: we could also provide PassFast/FailFast. + _applied_signal = SUCCESS_SEMANTICS.include?(actual_semantic) ? Activity::Right : Activity::Left # TODO: we could also provide PassFast/FailFast. end # This is used in Nested and Each where some tasks don't have a corresponding, hard-wired diff --git a/test/docs/autogenerated/operation_model_test.rb b/test/docs/autogenerated/operation_model_test.rb index 5076af1..8b742f4 100644 --- a/test/docs/autogenerated/operation_model_test.rb +++ b/test/docs/autogenerated/operation_model_test.rb @@ -1,263 +1,265 @@ require "test_helper" module Autogenerated -class DocsModelTest < Minitest::Spec - Song = Struct.new(:id, :title) do - def self.find_by(args) - key, value = args.flatten - return nil if value.nil? - return new(value) if key == :id - new(2, value) if key == :title + class DocsModelTest < Minitest::Spec + Song = Struct.new(:id, :title) do + def self.find_by(args) + key, value = args.flatten + return if value.nil? + return new(value) if key == :id + + new(2, value) if key == :title + end + + undef :[] + def self.[](id) + id.nil? ? nil : new(id + 99) + end end - def self.[](id) - id.nil? ? nil : new(id+99) + #:op + module Song::Operation + class Create < Trailblazer::Operation + step Model(Song, :new) + step :validate + step :save + #~meths + include T.def_steps(:validate, :save) + #~meths end + end end - end + #:op end - #:op - module Song::Operation - class Create < Trailblazer::Operation - step Model(Song, :new) - step :validate - step :save - #~meths - include T.def_steps(:validate, :save) - #~meths end + #:update + module Song::Operation + class Update < Trailblazer::Operation + step Model(Song, :find_by) + step :validate + step :save + #~meths + include T.def_steps(:validate, :save) + #~meths end + end end - end - #:op end - - #:update - module Song::Operation - class Update < Trailblazer::Operation - step Model(Song, :find_by) - step :validate - step :save - #~meths - include T.def_steps(:validate, :save) - #~meths end + #:update end + + it "defaults {:params} to empty hash when not passed" do + assert_invoke Song::Operation::Create, seq: "[:validate, :save]", + expected_ctx_variables: {model: Song.new} + + assert_invoke Song::Operation::Update, seq: "[]", + terminus: :failure end - end - #:update end - it "defaults {:params} to empty hash when not passed" do - assert_invoke Song::Operation::Create, seq: "[:validate, :save]", - expected_ctx_variables: {model: Song.new} + #~ctx_to_result + it do + #:create + result = Song::Operation::Create.(params: {}, seq: []) + puts result[:model] #=> # + #:create end - assert_invoke Song::Operation::Update, seq: "[]", - terminus: :failure - end + assert_invoke Song::Operation::Create, params: {}, + seq: "[:validate, :save]", expected_ctx_variables: {model: Song.new} + end - #~ctx_to_result - it do - #:create - result = Song::Operation::Create.(params: {}, seq: []) - puts result[:model] #=> # - #:create end + it do + #:update-ok + result = Song::Operation::Update.(params: {id: 1}, seq: []) + result[:model] #=> # + result.success? # => true + #:update-ok end - assert_invoke Song::Operation::Create, params: {}, - seq: "[:validate, :save]", expected_ctx_variables: {model: Song.new} - end + assert_equal result[:model].inspect, %{#} + assert_equal(:success, result.event.to_h[:semantic]) + end - it do - #:update-ok - result = Song::Operation::Update.(params: {id: 1}, seq: []) - result[:model] #=> # - result.success? # => true - #:update-ok end + it do + #:update-fail + result = Song::Operation::Update.(params: {}) + result[:model] #=> nil + result.success? # => false + #:update-fail end - assert_equal result[:model].inspect, %{#} - assert_equal result.event.to_h[:semantic], :success + assert_equal(%{nil}, result[:model].inspect) + assert_equal(:failure, result.event.to_h[:semantic]) + end + #~ctx_to_result end end - it do - #:update-fail - result = Song::Operation::Update.(params: {}) - result[:model] #=> nil - result.success? # => false - #:update-fail end + class DocsModelFindByTitleTest < Minitest::Spec + Song = Class.new(DocsModelTest::Song) + #:update-with-find-by-key + module Song::Operation + class Update < Trailblazer::Operation + step Model(Song, :find_by, :title) # third positional argument. + step :validate + step :save + #~meths + include T.def_steps(:validate, :save) + #~meths end + end + end + #:update-with-find-by-key end - assert_equal result[:model].inspect, %{nil} - assert_equal result.event.to_h[:semantic], :failure - end - #~ctx_to_result end -end + #~ctx_to_result + it do + #:update-with-find-by-key-ok + result = Song::Operation::Update.(params: {title: "Test"}, seq: []) + result[:model] #=> # + #:update-with-find-by-key-ok end -class DocsModelFindByTitleTest < Minitest::Spec - Song = Class.new(DocsModelTest::Song) - - #:update-with-find-by-key - module Song::Operation - class Update < Trailblazer::Operation - step Model(Song, :find_by, :title) # third positional argument. - step :validate - step :save - #~meths - include T.def_steps(:validate, :save) - #~meths end + assert_equal result[:model].inspect, %{#} end - end - #:update-with-find-by-key end - #~ctx_to_result - it do - #:update-with-find-by-key-ok - result = Song::Operation::Update.(params: {title: "Test"}, seq: []) - result[:model] #=> # - #:update-with-find-by-key-ok end + it do + #:key-title-fail + result = Song::Operation::Update.(params: {title: nil}, seq: []) - assert_equal result[:model].inspect, %{#} + assert_equal(%{nil}, result[:model].inspect) + #:key-title-fail end + end + #~ctx_to_result end end - it do - #:key-title-fail - result = Song::Operation::Update.(params: {title: nil}, seq: []) - assert_equal result[:model].inspect, %{nil} - #:key-title-fail end - end - #~ctx_to_result end -end + class DocsModelAccessorTest < Minitest::Spec + Song = Class.new(DocsModelTest::Song) -class DocsModelAccessorTest < Minitest::Spec - Song = Class.new(DocsModelTest::Song) - - #:show - module Song::Operation - class Update < Trailblazer::Operation - step Model(Song, :[]) - step :validate - step :save - #~meths - include T.def_steps(:validate, :save) - #~meths end + #:show + module Song::Operation + class Update < Trailblazer::Operation + step Model(Song, :[]) + step :validate + step :save + #~meths + include T.def_steps(:validate, :save) + #~meths end + end end - end - #:show end + #:show end - #~ctx_to_result - it do - #:show-ok - result = Song::Operation::Update.(params: {id: 1}, seq: []) - result[:model] #=> # - #:show-ok end - - assert_equal result[:model].inspect, %{#} - end - #~ctx_to_result end -end + #~ctx_to_result + it do + #:show-ok + result = Song::Operation::Update.(params: {id: 1}, seq: []) + result[:model] #=> # + #:show-ok end -class DocsModelDependencyInjectionTest < Minitest::Spec - Song = Class.new(DocsModelTest::Song) - - module Song::Operation - class Create < Trailblazer::Operation - step Model(Song, :new) - step :validate - step :save - #~meths - include T.def_steps(:validate, :save) - #~meths end + assert_equal result[:model].inspect, %{#} end + #~ctx_to_result end end - it "allows injecting {:model.class} and friends" do - class Hit < Song + class DocsModelDependencyInjectionTest < Minitest::Spec + Song = Class.new(DocsModelTest::Song) + + module Song::Operation + class Create < Trailblazer::Operation + step Model(Song, :new) + step :validate + step :save + #~meths + include T.def_steps(:validate, :save) + #~meths end + end end - #:di-model-class - result = Song::Operation::Create.(params: {}, :"model.class" => Hit, seq: []) - #:di-model-class end + it "allows injecting {:model.class} and friends" do + class Hit < Song + end - assert_equal result[:model].inspect, %{#} + #:di-model-class + result = Song::Operation::Create.(params: {}, :"model.class" => Hit, seq: []) + #:di-model-class end - # inject all variables - #:di-all - result = Song::Operation::Create.( - params: {title: "Olympia"}, # some random variable. - "model.class": Hit, - "model.action": :find_by, - "model.find_by_key": :title, seq: [] - ) - #:di-all end + assert_equal result[:model].inspect, %{#} - assert_equal result[:model].inspect, %{#} -end + # inject all variables + #:di-all + result = Song::Operation::Create.( + params: {title: "Olympia"}, # some random variable. + "model.class": Hit, + "model.action": :find_by, + "model.find_by_key": :title, seq: [] + ) + #:di-all end - # use empty Model() and inject {model.class} and {model.action} -class DocsModelEmptyDITest < Minitest::Spec - Song = Class.new(DocsModelTest::Song) - Hit = Class.new(Song) - - #:op-model-empty - module Song::Operation - class Create < Trailblazer::Operation - step Model() - step :validate - step :save - #~meths - include T.def_steps(:validate, :save) - #~meths end + assert_equal result[:model].inspect, %{#} end - #:op-model-empty end - end - it do - result = Song::Operation::Create.(params: {}, :"model.class" => Hit, seq: []) - assert_equal result[:model].inspect, %{#} - end -end + # use empty Model() and inject {model.class} and {model.action} + class DocsModelEmptyDITest < Minitest::Spec + Song = Class.new(DocsModelTest::Song) + Hit = Class.new(Song) + + #:op-model-empty + module Song::Operation + class Create < Trailblazer::Operation + step Model() + step :validate + step :save + #~meths + include T.def_steps(:validate, :save) + #~meths end + end + #:op-model-empty end + end -class DocsModelIOTest < Minitest::Spec - Song = Class.new(DocsModelTest::Song) - Hit = Class.new(Song) + it do + result = Song::Operation::Create.(params: {}, :"model.class" => Hit, seq: []) - it "allows to use composable I/O with macros" do - #:in - module Song::Operation - class Create < Trailblazer::Operation - step Model(Song, :find_by), - In() => ->(ctx, my_id:, **) { ctx.merge(params: {id: my_id}) } # Model() needs {params[:id]}. - # ... + assert_equal result[:model].inspect, %{#} end end - #:in end - - result = Song::Operation::Create.(params: {}, my_id: 1, :"model.class" => Hit) - assert_equal result[:model].inspect, %{#} -=begin -#:in-call -result = Create.(my_id: 1) -#:in-call end -=end + + class DocsModelIOTest < Minitest::Spec + Song = Class.new(DocsModelTest::Song) + Hit = Class.new(Song) + + it "allows to use composable I/O with macros" do + #:in + module Song::Operation + class Create < Trailblazer::Operation + step Model(Song, :find_by), + In() => ->(ctx, my_id:, **) { ctx.merge(params: {id: my_id}) } # Model() needs {params[:id]}. + # ... + end + end + #:in end + + result = Song::Operation::Create.(params: {}, my_id: 1, :"model.class" => Hit) + + assert_equal result[:model].inspect, %{#} + # #:in-call + # result = Create.(my_id: 1) + #:in-call end + end end end -end -class Model404TerminusTest < Minitest::Spec - Song = Class.new(DocsModelTest::Song) - #:update-with-not-found-end - module Song::Operation - class Update < Trailblazer::Operation - step Model(Song, :find_by, not_found_terminus: true) - step :validate - step :save - #~meths - include T.def_steps(:validate, :save) - #~meths end + class Model404TerminusTest < Minitest::Spec + Song = Class.new(DocsModelTest::Song) + #:update-with-not-found-end + module Song::Operation + class Update < Trailblazer::Operation + step Model(Song, :find_by, not_found_terminus: true) + step :validate + step :save + #~meths + include T.def_steps(:validate, :save) + #~meths end + end end - end - #:update-with-not-found-end end + #:update-with-not-found-end end - it do - assert_invoke Song::Operation::Update, params: {id: 1}, - seq: "[:validate, :save]", expected_ctx_variables: {model: Song.find_by(id: 1)} - assert_invoke Song::Operation::Update, params: {id: nil}, terminus: :not_found + it do + assert_invoke Song::Operation::Update, params: {id: 1}, + seq: "[:validate, :save]", expected_ctx_variables: {model: Song.find_by(id: 1)} + assert_invoke Song::Operation::Update, params: {id: nil}, terminus: :not_found - #:not_found - result = Song::Operation::Update.(params: {id: nil}) - result.success? # => false - #:not_found end + #:not_found + result = Song::Operation::Update.(params: {id: nil}) + result.success? # => false + #:not_found end + end end end -end \ No newline at end of file diff --git a/test/docs/each_test.rb b/test/docs/each_test.rb index c0ba1a7..9434d72 100644 --- a/test/docs/each_test.rb +++ b/test/docs/each_test.rb @@ -1,6 +1,5 @@ require "test_helper" - # step Macro::Each(:report_templates, key: :report_template) { # step Subprocess(ReportTemplate::Update), input: :input_report_template # fail :set_report_template_errors @@ -10,8 +9,7 @@ # end class EachTest < Minitest::Spec - class Composer < Struct.new(:full_name, :email) - end + Composer = Struct.new(:full_name, :email) class Mailer def self.send(**options) @@ -23,17 +21,13 @@ class << self end end -#@ operation has {#composers_for_each} + #@ operation has {#composers_for_each} module B - class Song < Struct.new(:id, :title, :band, :composers) + Song = Struct.new(:id, :title, :band, :composers) do def self.find_by(id:) - if id == 2 - return Song.new(id, nil, nil, [Composer.new("Fat Mike", "mike@fat.wreck"), Composer.new("El Hefe")]) - end + return Song.new(id, nil, nil, [Composer.new("Fat Mike", "mike@fat.wreck"), Composer.new("El Hefe")]) if id == 2 - if id == 3 - return Song.new(id, nil, nil, [Composer.new("Fat Mike", "mike@fat.wreck"), Composer.new("El Hefe", "scammer@spam")]) - end + return Song.new(id, nil, nil, [Composer.new("Fat Mike", "mike@fat.wreck"), Composer.new("El Hefe", "scammer@spam")]) if id == 3 Song.new(id, nil, nil, [Composer.new("Fat Mike"), Composer.new("El Hefe")]) end @@ -51,9 +45,10 @@ class Cover < Trailblazer::Activity::Railway step :rearrange # "decider interface" - def composers_for_each(ctx, model:, **) + def composers_for_each(_ctx, model:, **) model.composers end + #:iterated-value def notify_composers(ctx, index:, item:, **) ctx[:value] = [index, item.full_name] @@ -70,14 +65,14 @@ def model(ctx, params:, **) end end #:each end - end # B + end it "allows a dataset compute in the hosting activity" do - #@ {:dataset} is not part of the {ctx}. + #@ {:dataset} is not part of the {ctx}. assert_invoke B::Song::Activity::Cover, params: {id: 1}, expected_ctx_variables: { model: B::Song.find_by(id: 1), - collected_from_each: [[0, "Fat Mike"], [1, "El Hefe"],] + collected_from_each: [[0, "Fat Mike"], [1, "El Hefe"]] }, seq: "[:rearrange]" @@ -112,7 +107,7 @@ def composers_for_each(ctx, model:, **) end end -#@ operation has dedicated step {#find_composers} + #@ operation has dedicated step {#find_composers} module C class Song < B::Song; end @@ -121,7 +116,7 @@ class Cover < Trailblazer::Activity::Railway step :model step :find_composers step Each(collect: true) { - step :notify_composers + step :notify_composers }, In() => {:composers => :dataset} step :rearrange @@ -135,7 +130,7 @@ def find_composers(ctx, model:, **) #~meths end end end - end # C + end it "dataset can come from the hosting activity" do #@ {:dataset} is not part of the outgoing {ctx}. @@ -152,11 +147,11 @@ def find_composers(ctx, model:, **) expected_ctx_variables: { model: C::Song.find_by(id: 1), composers: [Composer.new("Fat Mike"), Composer.new("El Hefe")], - collected_from_each: [[0, "Fat Mike"], [1, "El Hefe"],] + collected_from_each: [[0, "Fat Mike"], [1, "El Hefe"]] }, seq: "[:rearrange]" end -#@ {:item_key} + #@ {:item_key} module E class Song < B::Song; end @@ -174,7 +169,6 @@ class Cover < Trailblazer::Activity::Railway #:item_key end step :rearrange - # circuit-step interface! "decider interface" def composers_for_each(ctx, model:, **) model.composers @@ -191,13 +185,14 @@ def notify_composers(ctx, index:, composer:, **) it "{item_key: :composer}" do E::Mailer.send_options = [] + assert_invoke E::Song::Activity::Cover, params: {id: 1}, expected_ctx_variables: { - model: B::Song.find_by(id: 1), + model: B::Song.find_by(id: 1) # collected_from_each: ["Fat Mike", "El Hefe"] }, seq: "[:rearrange]" - assert_equal E::Mailer.send_options, [{:to=>nil, :message=>"0) You, Fat Mike, have been warned about your song being copied."}, {:to=>nil, :message=>"1) You, El Hefe, have been warned about your song being copied."}] + assert_equal([{:to => nil, :message => "0) You, Fat Mike, have been warned about your song being copied."}, {:to => nil, :message => "1) You, El Hefe, have been warned about your song being copied."}], E::Mailer.send_options) end #@ failure in Each @@ -207,6 +202,7 @@ class Song < B::Song; end class Notify def self.send_email(email) return if email.nil? + true end end @@ -220,17 +216,15 @@ class Cover < Trailblazer::Activity::Railway step :rearrange def notify_composers(ctx, item:, **) - if Notify.send_email(item.email) - ctx[:value] = item.email # let's collect all emails that could be sent. - return true - else - return false - end + return false unless Notify.send_email(item.email) + + ctx[:value] = item.email # let's collect all emails that could be sent. + return true end #~meths # circuit-step interface! "decider interface" - def composers_for_each(ctx, model:, **) + def composers_for_each(_ctx, model:, **) model.composers end include CoverMethods @@ -243,7 +237,7 @@ def composers_for_each(ctx, model:, **) assert_invoke F::Song::Activity::Cover, params: {id: 2}, expected_ctx_variables: { model: B::Song.find_by(id: 2), - collected_from_each: ["mike@fat.wreck", nil], + collected_from_each: ["mike@fat.wreck", nil] }, seq: "[]", terminus: :failure @@ -262,7 +256,7 @@ module Song::Activity class Notify < Trailblazer::Activity::Railway step :send_email - def send_email(ctx, index:, item:, **) + def send_email(_ctx, index:, item:, **) Mailer.send(to: item.email, message: "#{index}) You, #{item.full_name}, have been warned about your song being copied.") end end @@ -276,7 +270,7 @@ class Cover < Trailblazer::Activity::Railway step Each(Notify, dataset_from: :composers_for_each) step :rearrange #~meths - def composers_for_each(ctx, model:, **) + def composers_for_each(_ctx, model:, **) model.composers end include CoverMethods @@ -288,13 +282,14 @@ def composers_for_each(ctx, model:, **) it "Each(Activity::Railway)" do D::Mailer.send_options = [] + assert_invoke D::Song::Activity::Cover, params: {id: 1}, seq: "[:rearrange]", expected_ctx_variables: { - model: D::Song.find_by(id: 1), + model: D::Song.find_by(id: 1) # collected_from_each: [[0, "Fat Mike"], [1, "El Hefe"],] } - assert_equal D::Mailer.send_options, [{:to=>nil, :message=>"0) You, Fat Mike, have been warned about your song being copied."}, {:to=>nil, :message=>"1) You, El Hefe, have been warned about your song being copied."}] + assert_equal([{:to => nil, :message => "0) You, Fat Mike, have been warned about your song being copied."}, {:to => nil, :message => "1) You, El Hefe, have been warned about your song being copied."}], D::Mailer.send_options) end #@ Each with operation with three outcomes. Notify terminates on {End.spam_email}, @@ -312,6 +307,7 @@ class Notify < Trailblazer::Activity::Railway def send_email(ctx, index:, item:, **) return false if item.email == "scammer@spam" + ctx[:value] = [index, item.full_name] end end @@ -323,10 +319,10 @@ class Cover < Trailblazer::Activity::Railway step :model step Each(Notify, dataset_from: :composers_for_each, collect: true), - Output(:spam_email) => Track(:spam_alert) + Output(:spam_email) => Track(:spam_alert) step :rearrange #~meths - def composers_for_each(ctx, model:, **) + def composers_for_each(_ctx, model:, **) model.composers end include CoverMethods @@ -343,7 +339,7 @@ def composers_for_each(ctx, model:, **) seq: "[]", expected_ctx_variables: { model: G::Song.find_by(id: 3), - collected_from_each: [[0, "Fat Mike"], nil,] + collected_from_each: [[0, "Fat Mike"], nil] } end end @@ -353,7 +349,7 @@ class EachCtxDiscardedTest < Minitest::Spec Composer = EachTest::Composer Song = Class.new(EachTest::B::Song) -#@ iterated steps write to ctx, gets discarded. + #@ iterated steps write to ctx, gets discarded. module Song::Activity class Cover < Trailblazer::Activity::Railway step :model @@ -385,7 +381,7 @@ def write_to_ctx(ctx, index:, seq:, **) it "discards {ctx[:variable]}" do assert_invoke Song::Activity::Cover, params: {id: 1}, expected_ctx_variables: { - model: Song.find_by(id: 1), + model: Song.find_by(id: 1) # collected_from_each: [[0, "Fat Mike"], [1, "El Hefe"],] }, seq: "[:write_to_ctx, :write_to_ctx, :rearrange]" @@ -400,18 +396,16 @@ class EachCtxAddsCollectedFromEachTest < Minitest::Spec module Song::Activity class Cover < Trailblazer::Activity::Railway step :model - step Each(dataset_from: :composers_for_each, + step Each( + dataset_from: :composers_for_each, # all filters called before/after each iteration! - Inject(:collected_from_each) => ->(ctx, **) { [] }, # this is called only once. - Out() => ->(ctx, collected_from_each:, **) { {collected_from_each: collected_from_each += [ctx[:value]] } } - - - + Inject(:collected_from_each) => ->(_ctx, **) { [] }, # this is called only once. + Out() => ->(ctx, collected_from_each:, **) { {collected_from_each: collected_from_each + [ctx[:value]]} } ) { - step :notify_composers - step :write_to_ctx - } + step :notify_composers + step :write_to_ctx + } step :rearrange def write_to_ctx(ctx, index:, seq:, item:, **) @@ -431,7 +425,7 @@ def write_to_ctx(ctx, index:, seq:, item:, **) assert_invoke Song::Activity::Cover, params: {id: 1}, expected_ctx_variables: { model: Song.find_by(id: 1), - collected_from_each: [[0, "Fat Mike"], [1, "El Hefe"],] + collected_from_each: [[0, "Fat Mike"], [1, "El Hefe"]] }, seq: "[:write_to_ctx, :write_to_ctx, :rearrange]" end @@ -446,24 +440,20 @@ class EachCtxInOutTest < Minitest::Spec module Song::Activity class Cover < Trailblazer::Activity::Railway step :model - step Each(dataset_from: :composers_for_each, + step Each( + dataset_from: :composers_for_each, # Inject(always: true) => { - Inject(:composer_index) => ->(ctx, index:, **) { index }, + Inject(:composer_index) => ->(_ctx, index:, **) { index }, # all filters called before/after each iteration! - Out() => ->(ctx, index:, variable:, **) { {:"composer-#{index}-value" => variable} } - - - - - + Out() => ->(_ctx, index:, variable:, **) { {:"composer-#{index}-value" => variable} } ) { - step :notify_composers - step :write_to_ctx - } + step :notify_composers + step :write_to_ctx + } step :rearrange def write_to_ctx(ctx, composer_index:, model:, **) - ctx[:variable] = "#{composer_index} + #{model.class.name.split('::').last}" + ctx[:variable] = "#{composer_index} + #{model.class.name.split("::").last}" end #~meths @@ -478,17 +468,15 @@ def write_to_ctx(ctx, composer_index:, model:, **) expected_ctx_variables: { model: Song.find_by(id: 1), :"composer-0-value" => "0 + Song", - :"composer-1-value" => "1 + Song", + :"composer-1-value" => "1 + Song" }, seq: "[:rearrange]" end end class EachOuterCtxTest < Minitest::Spec - end - #@ {:errors} is first initialized with a default injection, #@ then passed across iterations. # TODO: similar test above with {:collected_from_each}. @@ -499,14 +487,15 @@ class EachSharedIterationVariableTest < Minitest::Spec module Song::Activity class Cover < Trailblazer::Activity::Railway step :model - step Each(dataset_from: :composers_for_each, + step Each( + dataset_from: :composers_for_each, Inject(:messages) => ->(*) { {} }, # all filters called before/after each iteration! Out() => [:messages] ) { - step :write_to_ctx - } + step :write_to_ctx + } step :rearrange def write_to_ctx(ctx, item:, messages:, index:, **) @@ -524,10 +513,10 @@ def write_to_ctx(ctx, item:, messages:, index:, **) assert_invoke Song::Activity::Cover, params: {id: 1}, expected_ctx_variables: { model: Song.find_by(id: 1), - messages: {0=>"Fat Mike", 1=>"El Hefe"}}, + messages: {0 => "Fat Mike", 1 => "El Hefe"} + }, seq: "[:rearrange]" end - end #@ Each without any option @@ -549,15 +538,16 @@ class Cover < Trailblazer::Activity::Railway # "decider interface" #:dataset_from - def composers_for_each(ctx, model:, **) + def composers_for_each(_ctx, model:, **) model.composers end #:dataset_from end #:iterated - def notify_composers(ctx, index:, item:, **) + def notify_composers(_ctx, index:, item:, **) Mailer.send(to: item.email, message: "#{index}) You, #{item.full_name}, have been warned about your song being copied.") end + #:iterated end #~meths def model(ctx, params:, **) @@ -575,11 +565,11 @@ def model(ctx, params:, **) #@ {:dataset} is not part of the {ctx}. assert_invoke Song::Activity::Cover, params: {id: 1}, expected_ctx_variables: { - model: Song.find_by(id: 1), + model: Song.find_by(id: 1) }, seq: "[:rearrange]" - assert_equal Mailer.send_options, [{:to=>nil, :message=>"0) You, Fat Mike, have been warned about your song being copied."}, {:to=>nil, :message=>"1) You, El Hefe, have been warned about your song being copied."}] + assert_equal([{:to => nil, :message => "0) You, Fat Mike, have been warned about your song being copied."}, {:to => nil, :message => "1) You, El Hefe, have been warned about your song being copied."}], Mailer.send_options) end end @@ -602,24 +592,26 @@ class EachStrategyComplianceTest < Minitest::Spec #@ Original class isn't changed. assert_invoke Song::Activity::Cover, params: {id: 1}, seq: [], expected_ctx_variables: { - model: Song.find_by(id: 1), - }, + model: Song.find_by(id: 1) + }, seq: "[:rearrange]" #@ Patched class runs # Trailblazer::Developer.wtf?(cover_patched, [params: {id: 1}, seq: []]) assert_invoke cover_patched, params: {id: 1}, seq: [], expected_ctx_variables: { - model: Song.find_by(id: 1), - }, + model: Song.find_by(id: 1) + }, seq: "[:notify_composers, :log_email, :notify_composers, :log_email, :rearrange]" end - it "find_path" do - assert_equal Trailblazer::Developer::Introspect.find_path(Song::Activity::Cover, - ["Each/composers_for_each", "Each.iterate.block", "invoke_block_activity", :notify_composers])[0].task.inspect, - %{#} + assert_equal( + %{#}, Trailblazer::Developer::Introspect.find_path( + Song::Activity::Cover, + ["Each/composers_for_each", "Each.iterate.block", "invoke_block_activity", :notify_composers] + )[0].task.inspect + ) =begin #:find_path @@ -645,48 +637,55 @@ class EachStrategyComplianceTest < Minitest::Spec step Each(sub_activity, dataset_from: :composers_for_each) step :rearrange - def composers_for_each(ctx, model:, **) + def composers_for_each(_ctx, model:, **) model.composers end # include CoverMethods end - node, _activity = Trailblazer::Developer::Introspect.find_path(activity, - [%{Each/#{id}}, "Each.iterate.#{id}", "invoke_block_activity"]) + _, _activity = Trailblazer::Developer::Introspect.find_path( + activity, + [%{Each/#{id}}, "Each.iterate.#{id}", "invoke_block_activity"] + ) - assert_equal _activity.class.inspect, "Hash" # container_activity + assert_equal("Hash", _activity.class.inspect) # container_activity #@ inside {invoke_block_activity} - node, _activity = Trailblazer::Developer::Introspect.find_path(activity, - [%{Each/#{id}}, "Each.iterate.#{id}", "invoke_block_activity", :notify_composers]) + node, _activity = Trailblazer::Developer::Introspect.find_path( + activity, + [%{Each/#{id}}, "Each.iterate.#{id}", "invoke_block_activity", :notify_composers] + ) - assert_equal node.task.inspect, - %{#} - assert_equal _activity.class.inspect, "Trailblazer::Activity" + assert_equal(%{#}, node.task.inspect) + assert_equal("Trailblazer::Activity", _activity.class.inspect) end - it "tracing" do EachPureTest::Mailer.send_options = [] #:wtf - Trailblazer::Developer.wtf?(Song::Activity::Cover, [{ - params: {id: 1}, - #~meths - seq: [] - #~meths end - }]) + Trailblazer::Developer.wtf?( + Song::Activity::Cover, [ + { + params: {id: 1}, + #~meths + seq: [] + #~meths end + } + ] + ) #:wtf end end end - #@ dataset: [] class EachEmptyDatasetTest < Minitest::Spec it do activity = Class.new(Trailblazer::Activity::Railway) do - step Each() { - step :raise - } + step( + Each() do + step :raise + end + ) end assert_invoke activity, dataset: [] @@ -699,17 +698,17 @@ class Validate < Trailblazer::Activity::Railway it "assigns IDs via {Macro.id_for}" do activity = Class.new(Trailblazer::Activity::Railway) do - step Each() {} + step(Each() {}) step Each(Validate) step Each() {}, id: "Each-1" step Each(dataset_from: :composers_for_each) {} end - assert_equal Trailblazer::Developer::Introspect.find_path(activity, ["Each/EachIDTest::Validate"])[0].id, "Each/EachIDTest::Validate" - assert_equal Trailblazer::Developer::Introspect.find_path(activity, ["Each-1"])[0].id, "Each-1" - assert_equal Trailblazer::Developer::Introspect.find_path(activity, ["Each/composers_for_each"])[0].id, "Each/composers_for_each" + assert_equal("Each/EachIDTest::Validate", Trailblazer::Developer::Introspect.find_path(activity, ["Each/EachIDTest::Validate"])[0].id) + assert_equal("Each-1", Trailblazer::Developer::Introspect.find_path(activity, ["Each-1"])[0].id) + assert_equal("Each/composers_for_each", Trailblazer::Developer::Introspect.find_path(activity, ["Each/composers_for_each"])[0].id) - assert_match /Each\/\w+/, Trailblazer::Activity::Introspect::Nodes(activity).values[1].id # FIXME: this test sucks. + assert_match(%r{Each/\w+}, Trailblazer::Activity::Introspect::Nodes(activity).values[1].id) # FIXME: this test sucks. end end @@ -721,7 +720,7 @@ def compute_item(ctx, item:, index:, **) end def self.block - -> (*){ + ->(*) { step :compute_item } end @@ -736,15 +735,16 @@ def self.block step :b end - ctx = {seq: [], dataset: [3,2,1]} + ctx = {seq: [], dataset: [3, 2, 1]} - stack, signal, (ctx, _) = Trailblazer::Developer::Trace.invoke(activity, [ctx, {}]) + stack, = Trailblazer::Developer::Trace.invoke(activity, [ctx, {}]) output = Trailblazer::Developer::Trace::Present.(stack) do |trace_nodes:, **| {node_options: {trace_nodes[0] => {label: ""}}} end - assert_equal output, %{ + assert_equal( + %{ |-- Start.default |-- a |-- Each/1 @@ -764,34 +764,38 @@ def self.block | | `-- End.success | `-- End.success |-- b -`-- End.success} +`-- End.success}, output + ) - #@ compile time - #@ make sure we can find tasks/compile-time artifacts in Each by using their {compile_id}. - assert_equal Trailblazer::Developer::Introspect.find_path(activity, - ["Each/1", "Each.iterate.block", "invoke_block_activity", :compute_item])[0].task.inspect, - %{#} + #@ compile time + #@ make sure we can find tasks/compile-time artifacts in Each by using their {compile_id}. + assert_equal( + %{#}, Trailblazer::Developer::Introspect.find_path( + activity, + ["Each/1", "Each.iterate.block", "invoke_block_activity", :compute_item] + )[0].task.inspect + ) # puts Trailblazer::Developer::Render::TaskWrap.(activity, ["Each/1", "Each.iterate.block", "invoke_block_activity", :compute_item]) - # TODO: grab runtime ctx for iteration 134 + # TODO: grab runtime ctx for iteration 134 end it "Each::Circuit" do activity = Trailblazer::Macro.Each(collect: true, &DocsEachUnitTest.block)[:task] - my_exec_context = Class.new do + my_exec_context = Class.new { include ComputeItem - end.new + }.new ctx = { - dataset: [1,2,3] + dataset: [1, 2, 3] } # signal, (_ctx, _) = Trailblazer::Activity::TaskWrap.invoke(activity, [ctx]) - signal, (_ctx, _) = Trailblazer::Developer.wtf?(activity, [ctx], exec_context: my_exec_context) - assert_equal _ctx[:collected_from_each], ["1-0", "2-1", "3-2"] - end + _, (_ctx,_) = Trailblazer::Developer.wtf?(activity, [ctx], exec_context: my_exec_context) + assert_equal(%w[1-0 2-1 3-2], _ctx[:collected_from_each]) + end it "accepts iterated {block}" do activity = Class.new(Trailblazer::Activity::Railway) do @@ -802,9 +806,9 @@ def self.block } end - Trailblazer::Developer.wtf?(activity, [{dataset: ["one", "two", "three"]}, {}]) + Trailblazer::Developer.wtf?(activity, [{dataset: %w[one two three]}, {}]) - assert_invoke activity, dataset: ["one", "two", "three"], expected_ctx_variables: {collected_from_each: ["one-0", "two-1", "three-2"]} + assert_invoke activity, dataset: %w[one two three], expected_ctx_variables: {collected_from_each: %w[one-0 two-1 three-2]} end it "can see the entire ctx" do @@ -820,19 +824,22 @@ def compute_item_with_current_user(ctx, item:, index:, current_user:, **) Trailblazer::Developer.wtf?( activity, - [{ - dataset: ["one", "two", "three"], - current_user: Object, + [ + { + dataset: %w[one two three], + current_user: Object }, - {}] + {} + ] ) - assert_invoke activity, dataset: ["one", "two", "three"], current_user: Object, expected_ctx_variables: {collected_from_each: ["one-0-Object", "two-1-Object", "three-2-Object"]} + + assert_invoke activity, dataset: %w[one two three], current_user: Object, expected_ctx_variables: {collected_from_each: ["one-0-Object", "two-1-Object", "three-2-Object"]} end it "allows taskWrap in Each" do activity = Class.new(Trailblazer::Activity::Railway) do step Each(collect: true) { # expects {:dataset} # NOTE: use {} not {do ... end} - step :compute_item, In() => {:current_user => :user}, In() => [:item, :index] + step :compute_item, In() => {:current_user => :user}, In() => %i[item index] } def compute_item(ctx, item:, index:, user:, **) @@ -840,7 +847,7 @@ def compute_item(ctx, item:, index:, user:, **) end end - assert_invoke activity, dataset: ["one", "two", "three"], current_user: "Yogi", expected_ctx_variables: {collected_from_each: ["one-0-Yogi", "two-1-Yogi", "three-2-Yogi"]} + assert_invoke activity, dataset: %w[one two three], current_user: "Yogi", expected_ctx_variables: {collected_from_each: ["one-0-Yogi", "two-1-Yogi", "three-2-Yogi"]} end it "accepts operation" do @@ -857,11 +864,11 @@ def compute_item(ctx, item:, index:, **) end # Trailblazer::Developer.wtf?(activity, [{dataset: ["one", "two", "three"]}, {}]) - assert_invoke activity, dataset: ["one", "two", "three"], expected_ctx_variables: {collected_from_each: ["one-0", "two-1", "three-2"]} + assert_invoke activity, dataset: %w[one two three], expected_ctx_variables: {collected_from_each: %w[one-0 two-1 three-2]} end it "doesn't override an existing ctx[:index]" do - activity = Class.new(Trailblazer::Activity::Railway) do + activity = Class.new(Trailblazer::Activity::Railway) do include T.def_steps(:a, :b) include ComputeItem @@ -870,11 +877,10 @@ def compute_item(ctx, item:, index:, **) def b(ctx, seq:, index:, **) ctx[:seq] = seq + [index] end - end - assert_invoke activity, dataset: [1,2,3], index: 9, - expected_ctx_variables: {collected_from_each: ["1-0", "2-1", "3-2"]}, + assert_invoke activity, dataset: [1, 2, 3], index: 9, + expected_ctx_variables: {collected_from_each: %w[1-0 2-1 3-2]}, seq: "[9]" end @@ -890,31 +896,32 @@ def check(ctx, item:, **) ctx[:value] = item.to_s #@ always collect the value, even in failure case. return false if item >= 3 + true end end #@ all works - assert_invoke activity, dataset: [1,2], - expected_ctx_variables: {collected_from_each: ["1", "2"]}, + assert_invoke activity, dataset: [1, 2], + expected_ctx_variables: {collected_from_each: %w[1 2]}, seq: "[:a]" #@ fail at 3 but still collect 3rd iteration! - Trailblazer::Developer.wtf?(activity, [{dataset: [1,2,3]}, {}]) - assert_invoke activity, dataset: [1,2,3], - expected_ctx_variables: {collected_from_each: ["1", "2", "3"]}, + Trailblazer::Developer.wtf?(activity, [{dataset: [1, 2, 3]}, {}]) + + assert_invoke activity, dataset: [1, 2, 3], + expected_ctx_variables: {collected_from_each: %w[1 2 3]}, seq: "[]", terminus: :failure #@ fail at 3, skip 4 - assert_invoke activity, dataset: [1,2,3,4], - expected_ctx_variables: {collected_from_each: ["1", "2", "3"]}, + assert_invoke activity, dataset: [1, 2, 3, 4], + expected_ctx_variables: {collected_from_each: %w[1 2 3]}, seq: "[]", terminus: :failure end end - class EachInEachTest < Minitest::Spec it "what" do activity = Class.new(Trailblazer::Activity::Railway) do @@ -934,12 +941,12 @@ def capture_inner(ctx, inner:, **) ctx[:seq] << inner end - def inner_dataset(ctx, outer:, **) + def inner_dataset(_ctx, outer:, **) outer.collect { |i| i * 10 } end end - assert_invoke activity, dataset: [[1,2],[3,4],[5,6]], + assert_invoke activity, dataset: [[1, 2], [3, 4], [5, 6]], seq: %{[[1, 2], 10, 20, [3, 4], 30, 40, [5, 6], 50, 60]} end end diff --git a/test/docs/guard_test.rb b/test/docs/guard_test.rb index 643a751..f242534 100644 --- a/test/docs/guard_test.rb +++ b/test/docs/guard_test.rb @@ -5,7 +5,7 @@ class DocsGuardProcTest < Minitest::Spec #:proc class Create < Trailblazer::Operation - step Policy::Guard(->(options, pass:, **) { pass }) + step Policy::Guard(->(_options, pass:, **) { pass }) #:pipeonly step :process @@ -16,20 +16,20 @@ def process(options, **) end #:proc end - it { Create.(pass: false)[:x].must_be_nil } - it { Create.(pass: true)[:x].must_equal true } + it { assert_nil Create.(pass: false)[:x] } + it { assert Create.(pass: true)[:x] } #- result object, guard - it { Create.(pass: true)[:"result.policy.default"].success?.must_equal true } - it { Create.(pass: false)[:"result.policy.default"].success?.must_equal false } + it { assert Create.(pass: true)[:"result.policy.default"].success? } + it { refute Create.(pass: false)[:"result.policy.default"].success? } #--- #- Guard inheritance class New < Create - step Policy::Guard( ->(options, current_user:, **) { current_user } ), override: true + step Policy::Guard( ->(_options, current_user:, **) { current_user } ), override: true end - it { Trailblazer::Developer.railway(New).must_equal %{[>policy.default.eval,>process]} } + it { assert_equal Trailblazer::Developer.railway(New), %{[>policy.default.eval,>process]} } end #--- @@ -58,8 +58,8 @@ def process(options, **) end #:callable-op end - it { Create.(pass: false)[:x].must_be_nil } - it { Create.(pass: true)[:x].must_equal true } + it { assert_nil Create.(pass: false)[:x] } + it { assert Create.(pass: true)[:x] } end #--- @@ -83,8 +83,8 @@ def process(options, **) end #:method end - it { Create.(pass: false).inspect(:x).must_equal %{} } - it { Create.(pass: true).inspect(:x).must_equal %{} } + it { assert_equal "", Create.(pass: false).inspect(:x) } + it { assert_equal "", Create.(pass: true).inspect(:x) } end #--- @@ -92,13 +92,13 @@ def process(options, **) class DocsGuardNamedTest < Minitest::Spec #:name class Create < Trailblazer::Operation - step Policy::Guard( ->(options, current_user:, **) { current_user }, name: :user ) + step Policy::Guard( ->(_options, current_user:, **) { current_user }, name: :user ) # ... end #:name end - it { Create.(:current_user => nil )[:"result.policy.user"].success?.must_equal false } - it { Create.(:current_user => Module)[:"result.policy.user"].success?.must_equal true } + it { refute Create.(:current_user => nil )[:"result.policy.user"].success? } + it { assert Create.(:current_user => Module)[:"result.policy.user"].success? } it { #:name-result @@ -113,31 +113,33 @@ class Create < Trailblazer::Operation class DocsGuardInjectionTest < Minitest::Spec #:di-op class Create < Trailblazer::Operation - step Policy::Guard( ->(options, current_user:, **) { current_user == Module } ) + step Policy::Guard( ->(_options, current_user:, **) { current_user == Module } ) end #:di-op end - it { Create.(:current_user => Module).inspect("").must_equal %{} } + it { assert_equal "", Create.(:current_user => Module).inspect("") } + it { result = - #:di-call - Create.( - :current_user => Module, - :"policy.default.eval" => Trailblazer::Operation::Policy::Guard.build(->(options, **) { false }) - ) - #:di-call end - result.inspect("").must_equal %{} } + #:di-call + Create.( + :current_user => Module, + :"policy.default.eval" => Trailblazer::Operation::Policy::Guard.build(->(_options, **) { false }) + ) + #:di-call end + assert_equal "", result.inspect("") + } end #--- # missing current_user throws exception class DocsGuardMissingKeywordTest < Minitest::Spec class Create < Trailblazer::Operation - step Policy::Guard( ->(options, current_user:, **) { current_user == Module } ) + step Policy::Guard( ->(_options, current_user:, **) { current_user == Module } ) end it { assert_raises(ArgumentError) { Create.() } } - it { Create.(:current_user => Module).success?.must_equal true } + it { assert Create.(:current_user => Module).success? } end #--- @@ -151,7 +153,7 @@ class Create < Trailblazer::Operation end #:before end - it { Trailblazer::Developer.railway(Create).must_equal %{[>policy.default.eval,>model!]} } + it { assert_equal "[>policy.default.eval,>model!]", Trailblazer::Developer.railway(Create) } it do #:before-pipe Trailblazer::Developer.railway(Create, style: :rows) #=> diff --git a/test/docs/macro_test.rb b/test/docs/macro_test.rb index 8660bd9..212e842 100644 --- a/test/docs/macro_test.rb +++ b/test/docs/macro_test.rb @@ -28,7 +28,7 @@ class Create < Trailblazer::Operation end =end - it { Trailblazer::Developer.railway(Create).must_equal %{[>my_policy.manager]} } + it { assert_equal Trailblazer::Developer.railway(Create), %{[>my_policy.manager]} } end diff --git a/test/docs/model_test.rb b/test/docs/model_test.rb index 625590c..bf2c479 100644 --- a/test/docs/model_test.rb +++ b/test/docs/model_test.rb @@ -4,13 +4,15 @@ class DocsModelTest < Minitest::Spec Song = Struct.new(:id, :title) do def self.find_by(args) key, value = args.flatten - return nil if value.nil? + return if value.nil? return new(value) if key == :id + new(2, value) if key == :title end + undef :[] def self.[](id) - id.nil? ? nil : new(id+99) + id.nil? ? nil : new(id + 99) end end @@ -67,7 +69,7 @@ class Update < Trailblazer::Activity::Railway #:update-ok end assert_equal ctx[:model].inspect, %{#} - assert_equal signal.to_h[:semantic], :success + assert_equal(:success, signal.to_h[:semantic]) end it do @@ -77,8 +79,8 @@ class Update < Trailblazer::Activity::Railway puts signal #=> # #:update-fail end - assert_equal ctx[:model].inspect, %{nil} - assert_equal signal.to_h[:semantic], :failure + assert_equal(%{nil}, ctx[:model].inspect) + assert_equal(:failure, signal.to_h[:semantic]) end #~ctx_to_result end end @@ -112,7 +114,8 @@ class Update < Trailblazer::Activity::Railway it do #:key-title-fail signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {title: nil}, seq: []) - assert_equal ctx[:model].inspect, %{nil} + + assert_equal(%{nil}, ctx[:model].inspect) #:key-title-fail end end #~ctx_to_result end @@ -165,14 +168,15 @@ class Hit < Song end #:di-model-class - signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, :"model.class" => Hit, seq: []) + _signal, (ctx,) = Trailblazer::Activity.(Song::Activity::Create, params: {}, :"model.class" => Hit, seq: []) #:di-model-class end assert_equal ctx[:model].inspect, %{#} - # inject all variables + # inject all variables #:di-all - signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, + signal, (ctx, _) = Trailblazer::Activity.( + Song::Activity::Create, params: {title: "Olympia"}, # some random variable. "model.class": Hit, "model.action": :find_by, @@ -181,54 +185,54 @@ class Hit < Song #:di-all end assert_equal ctx[:model].inspect, %{#} -end + end # use empty Model() and inject {model.class} and {model.action} -class DocsModelEmptyDITest < Minitest::Spec - Song = Class.new(DocsModelTest::Song) - Hit = Class.new(Song) + class DocsModelEmptyDITest < Minitest::Spec + Song = Class.new(DocsModelTest::Song) + Hit = Class.new(Song) - #:op-model-empty - module Song::Activity - class Create < Trailblazer::Activity::Railway - step Model() - step :validate - step :save - #~meths - include T.def_steps(:validate, :save) - #~meths end + #:op-model-empty + module Song::Activity + class Create < Trailblazer::Activity::Railway + step Model() + step :validate + step :save + #~meths + include T.def_steps(:validate, :save) + #~meths end + end + #:op-model-empty end end - #:op-model-empty end - end - it do - signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, :"model.class" => Hit, seq: []) - assert_equal ctx[:model].inspect, %{#} - end -end + it do + signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, :"model.class" => Hit, seq: []) -class DocsModelIOTest < Minitest::Spec - Song = Class.new(DocsModelTest::Song) - Hit = Class.new(Song) + assert_equal ctx[:model].inspect, %{#} + end + end - it "allows to use composable I/O with macros" do - #:in - module Song::Activity - class Create < Trailblazer::Operation - step Model(Song, :find_by), - In() => ->(ctx, my_id:, **) { ctx.merge(params: {id: my_id}) } # Model() needs {params[:id]}. - # ... + class DocsModelIOTest < Minitest::Spec + Song = Class.new(DocsModelTest::Song) + Hit = Class.new(Song) + + it "allows to use composable I/O with macros" do + #:in + module Song::Activity + class Create < Trailblazer::Operation + step Model(Song, :find_by), + In() => ->(ctx, my_id:, **) { ctx.merge(params: {id: my_id}) } # Model() needs {params[:id]}. + # ... + end end - end - #:in end - - signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, my_id: 1, :"model.class" => Hit) - assert_equal ctx[:model].inspect, %{#} -=begin -#:in-call -result = Create.(my_id: 1) -#:in-call end -=end + #:in end + + signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, my_id: 1, :"model.class" => Hit) + + assert_equal ctx[:model].inspect, %{#} + # #:in-call + # result = Create.(my_id: 1) + #:in-call end end end end diff --git a/test/docs/pundit_test.rb b/test/docs/pundit_test.rb index b12fbe7..4c48206 100644 --- a/test/docs/pundit_test.rb +++ b/test/docs/pundit_test.rb @@ -29,9 +29,9 @@ class Create < Trailblazer::Operation end #:pundit end - it { Trailblazer::Developer.railway(Create).must_equal %{[>model.build,>policy.default.eval]} } - it { Create.(params: {}, current_user: Module).inspect(:model).must_equal %{] >} } - it { Create.(params: {} ).inspect(:model).must_equal %{] >} } + it { assert_equal Trailblazer::Developer.railway(Create), %{[>model.build,>policy.default.eval]} } + it { assert_equal Create.(params: {}, current_user: Module).inspect(:model), %{] >} } + it { assert_equal Create.(params: {} ).inspect(:model), %{] >} } it do #:pundit-result @@ -39,8 +39,8 @@ class Create < Trailblazer::Operation result[:"result.policy.default"].success? #=> true result[:"result.policy.default"][:policy] #=> # #:pundit-result end - result[:"result.policy.default"].success?.must_equal true - result[:"result.policy.default"][:policy].is_a?(MyPolicy).must_equal true + assert result[:"result.policy.default"].success? + assert result[:"result.policy.default"][:policy].is_a?(MyPolicy) end #--- @@ -49,9 +49,11 @@ class New < Create step Policy::Pundit( MyPolicy, :new? ), override: true end - it { Trailblazer::Developer.railway(New).must_equal %{[>model.build,>policy.default.eval]} } - it { New.(params: {}, current_user: Class ).inspect(:model).must_equal %{] >} } - it { New.(params: {}, current_user: nil ).inspect(:model).must_equal %{] >} } + it { assert_equal "[>model.build,>policy.default.eval]", Trailblazer::Developer.railway(New) } + + it { assert_equal "] >", New.(params: {}, current_user: Class).inspect(:model) } + + it { assert_equal "] >", New.(params: {}, current_user: nil).inspect(:model) } #--- #- override with :name @@ -64,10 +66,10 @@ class Update < Edit step Policy::Pundit( MyPolicy, :new?, name: "first" ), override: true end - it { Trailblazer::Developer.railway(Edit).must_equal %{[>policy.first.eval,>policy.second.eval]} } - it { Edit.(params: {}, current_user: Class).inspect(:model).must_equal %{} } - it { Trailblazer::Developer.railway(Update).must_equal %{[>policy.first.eval,>policy.second.eval]} } - it { Update.(params: {}, current_user: Class).inspect(:model).must_equal %{} } + it { assert_equal "[>policy.first.eval,>policy.second.eval]", Trailblazer::Developer.railway(Edit) } + it { assert_equal "", Edit.(params: {}, current_user: Class).inspect(:model) } + it { assert_equal "[>policy.first.eval,>policy.second.eval]", Trailblazer::Developer.railway(Update) } + it { assert_equal "", Update.(params: {}, current_user: Class).inspect(:model) } #--- # dependency injection @@ -85,7 +87,8 @@ def create? :"policy.default.eval" => Trailblazer::Operation::Policy::Pundit.build(AnotherPolicy, :create?) ) #:di-call end - result.inspect("").must_equal %{} } + assert_equal "", result.inspect("") + } end #- @@ -106,7 +109,7 @@ class Create < Trailblazer::Operation result = Create.(params: {}, current_user: Module) result[:"result.policy.after_model"].success? #=> true #:name-call end - result[:"result.policy.after_model"].success?.must_equal true } + assert result[:"result.policy.after_model"].success? } end #--- @@ -123,7 +126,7 @@ class Create < Trailblazer::Operation # #:class-level end # it { Create.(); Create["result.policy"].must_be_nil } -# it { Create.(params: {}, current_user: Module)["x"].must_equal true } +# it { assert Create.(params: {}, current_user: Module)["x"] } # it { Create.(params: {} )["x"].must_be_nil } # end diff --git a/test/docs/rescue_test.rb b/test/docs/rescue_test.rb index 69d832c..16bfda4 100644 --- a/test/docs/rescue_test.rb +++ b/test/docs/rescue_test.rb @@ -23,18 +23,18 @@ class NestedInsanity < Trailblazer::Operation fail ->(options, **) { options["outer-err"] = true }, id: "nested/failure" end - it { Trailblazer::Developer.railway(NestedInsanity).must_match /\[>Rescue\/.{1,3},>nested/ } # FIXME: better introspect tests for all id-generating macros. - it { NestedInsanity.().inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err").must_equal %{} } - it { NestedInsanity.( "raise-y" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err").must_equal %{} } - it { NestedInsanity.( "raise-a" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err").must_equal %{} } + it { assert_match /\[>Rescue\/.{1,3},>nested/ , Trailblazer::Developer.railway(NestedInsanity) } + it { assert_equal "", NestedInsanity.().inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err") } + it { assert_equal "", NestedInsanity.( "raise-y" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err") } + it { assert_equal "", NestedInsanity.( "raise-a" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err") } #- # inheritance class UbernestedInsanity < NestedInsanity end - it { UbernestedInsanity.().inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err").must_equal %{} } - it { UbernestedInsanity.( "raise-a" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err").must_equal %{} } + it { assert_equal "", UbernestedInsanity.().inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err") } + it { assert_equal "", UbernestedInsanity.( "raise-a" => true).inspect("a", "y", "z", "b", "c", "e", "inner-err", "outer-err") } end class RescueTest < Minitest::Spec @@ -130,8 +130,8 @@ class Memo::Create < Trailblazer::Operation include Rehash end - it { Memo::Create.( { seq: [], } ).inspect(:seq, :exception_class).must_equal %{} } - it { Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq, :exception_class).must_equal %{} } + it { assert_equal Memo::Create.( { seq: [], } ).inspect(:seq, :exception_class), %{} } + it { assert_equal Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq, :exception_class), %{} } end =begin @@ -160,9 +160,15 @@ def my_handler(exception, (ctx), *) end #:rescue-method end - it { Memo::Create.( { seq: [], } ).inspect(:seq, :exception_class).must_equal %{} } - it { Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq, :exception_class).must_equal %{} } - end + it do + result = Memo::Create.( { seq: [] } ) + assert_equal "", result.inspect(:seq, :exception_class) + end + + it do + result = Memo::Create.( { seq: [], rehash_raise: RuntimeError } ) + assert_equal "", result.inspect(:seq, :exception_class) + end end =begin Rescue(), fast_track: true {} @@ -185,8 +191,10 @@ class Memo::Create < Trailblazer::Operation include T.def_steps(:find_model, :update, :notify, :log_error, :rehash) end - it { Memo::Create.( { seq: [], } ).inspect(:seq).must_equal %{} } - it { Memo::Create.( { seq: [], update: false } ).inspect(:seq).must_equal %{} } + it { assert_equal "", Memo::Create.({ seq: [], }).inspect(:seq) } + + it { assert_equal "", Memo::Create.({ seq: [], update: false }).inspect(:seq) } + end class RescueIDTest < Minitest::Spec @@ -212,13 +220,13 @@ def self.call(*) # assert_equal Trailblazer::Developer::Introspect.find_path(activity, ["Each-1"])[0].id, "Each-1" # assert_equal Trailblazer::Developer::Introspect.find_path(activity, ["Each/composers_for_each"])[0].id, "Each/composers_for_each" - assert_match /Rescue\/\d+/, Trailblazer::Activity::Introspect::Nodes(activity).values[1].id - assert_match /Rescue\/\d+/, Trailblazer::Activity::Introspect::Nodes(activity).values[2].id - assert_match /Rescue\/\d+/, Trailblazer::Activity::Introspect::Nodes(activity).values[3].id + assert_match(/Rescue\/\d+/, Trailblazer::Activity::Introspect::Nodes(activity).values[1].id) + assert_match(/Rescue\/\d+/, Trailblazer::Activity::Introspect::Nodes(activity).values[2].id) + assert_match(/Rescue\/\d+/, Trailblazer::Activity::Introspect::Nodes(activity).values[3].id) assert_match "Rescue-1", Trailblazer::Activity::Introspect::Nodes(activity).values[4].id assert_match "Rescue-2", Trailblazer::Activity::Introspect::Nodes(activity).values[5].id - assert_match /Rescue\/\d+/, Trailblazer::Activity::Introspect::Nodes(activity).values[6].id - assert_match /Rescue\/\d+/, Trailblazer::Activity::Introspect::Nodes(activity).values[7].id + assert_match(/Rescue\/\d+/, Trailblazer::Activity::Introspect::Nodes(activity).values[6].id) + assert_match(/Rescue\/\d+/, Trailblazer::Activity::Introspect::Nodes(activity).values[7].id) end end diff --git a/test/docs/wrap_test.rb b/test/docs/wrap_test.rb index cf2d5ec..429410f 100644 --- a/test/docs/wrap_test.rb +++ b/test/docs/wrap_test.rb @@ -204,8 +204,8 @@ def self.call((ctx), *, &block) #~methods end end - it { Memo::Create.( { seq: [] } ).inspect(:seq).must_equal %{} } - it { Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq).must_equal %{} } + it { assert_equal "", Memo::Create.( { seq: [] } ).inspect(:seq) } + it { assert_equal "", Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq) } end =begin @@ -241,8 +241,8 @@ class Memo::Create < Trailblazer::Operation end #:fail-fast end - it { Memo::Create.( { seq: [] } ).inspect(:seq).must_equal %{} } - it { Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq).must_equal %{} } + it { assert_equal Memo::Create.( { seq: [] } ).inspect(:seq), %{} } + it { assert_equal Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq), %{} } end =begin @@ -284,14 +284,14 @@ class Memo::Create < Trailblazer::Operation it do result = Memo::Create.( { seq: [] } ) - result.inspect(:seq).must_equal %{} - result.event.inspect.must_equal %{#} + assert_equal "", result.inspect(:seq) + assert_equal "#", result.event.inspect end it do result = Memo::Create.( { seq: [], rehash_raise: RuntimeError } ) - result.inspect(:seq).must_equal %{} - result.event.inspect.must_equal %{#} + assert_equal "", result.inspect(:seq) + assert_equal "#", result.event.inspect end end @@ -325,8 +325,8 @@ def self.call((ctx), *, &block) #~methods end end - it { Memo::Create.( { seq: [] } ).inspect(:seq).must_equal %{} } - it { Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq).must_equal %{} } + it { assert_equal "", Memo::Create.( { seq: [] } ).inspect(:seq) } + it { assert_equal "", Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq) } end =begin @@ -362,8 +362,8 @@ def self.call((ctx), *, &block) it "translates true returned form a wrap to a signal with a `success` semantic" do result = Memo::Create.( { seq: [], rehash_raise: RuntimeError } ) - result.inspect(:seq).must_equal %{} - result.event.inspect.must_equal %{#} + assert_equal "", result.inspect(:seq) + assert_equal "#", result.event.inspect end end @@ -400,8 +400,8 @@ def self.call((ctx), *, &block) it "translates false returned form a wrap to a signal with a `failure` semantic" do result = Memo::Create.( { seq: [], rehash_raise: RuntimeError } ) - result.inspect(:seq).must_equal %{} - result.event.inspect.must_equal %{#} + assert_equal result.inspect(:seq), %{} + assert_equal result.event.inspect, %{#} end end @@ -438,8 +438,8 @@ def self.call((ctx), *, &block) it "translates nil returned form a wrap to a signal with a `failure` semantic" do result = Memo::Create.( { seq: [], rehash_raise: RuntimeError } ) - result.inspect(:seq).must_equal %{} - result.event.inspect.must_equal %{#} + assert_equal "", result.inspect(:seq) + assert_match(//, result.event.inspect) end end @@ -453,7 +453,7 @@ class WrapWithTransactionTest < Minitest::Spec module Sequel def self.transaction - end_event, (ctx, flow_options) = yield + _, (_, _) = yield end end @@ -483,8 +483,8 @@ class Memo::Create < Trailblazer::Operation end #:transaction end - it { Memo::Create.( { seq: [] } ).inspect(:seq).must_equal %{} } - it { Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq).must_equal %{} } + it { assert_equal "", Memo::Create.( { seq: [] } ).inspect(:seq) } + it { assert_equal "", Memo::Create.( { seq: [], rehash_raise: RuntimeError } ).inspect(:seq) } end =begin @@ -523,8 +523,8 @@ class Memo::Create < Trailblazer::Operation end #:transaction end - it { Memo::Create.( { seq: [] } ).inspect(:seq).must_equal %{} } - it { Memo::Create.( { seq: [], update: false } ).inspect(:seq).must_equal %{} } + it { assert_equal "", Memo::Create.( { seq: [] } ).inspect(:seq) } + it { assert_equal "", Memo::Create.( { seq: [], update: false } ).inspect(:seq) } end diff --git a/test/operation/integration_test.rb b/test/operation/integration_test.rb index 72eaacd..f6ed2ae 100644 --- a/test/operation/integration_test.rb +++ b/test/operation/integration_test.rb @@ -35,7 +35,7 @@ class SongSpecialCreate < Trailblazer::Operation end it "create Artist and Song" do - result = SongSpecialCreate.trace( + result = SongSpecialCreate.wtf?( params: { artist: { name: "My Artist" @@ -46,10 +46,10 @@ class SongSpecialCreate < Trailblazer::Operation } ) - puts result.wtf? + puts result # this should return song - result[:model].name.must_match "My Song" - result[:model].artist_name.must_match "My Artist" + assert_match "My Song", result[:model].name + assert_match "My Artist", result[:model].artist_name end end diff --git a/test/operation/pundit_test.rb b/test/operation/pundit_test.rb index aa5081c..09f4b0e 100644 --- a/test/operation/pundit_test.rb +++ b/test/operation/pundit_test.rb @@ -27,24 +27,23 @@ def process(options, **) # successful. it do result = Create.(params: {}, current_user: Module) - result[:process].must_equal true + assert result[:process] #- result object, policy - result[:"result.policy.default"].success?.must_equal true - result[:"result.policy.default"][:message].must_be_nil - # result[:valid].must_be_nil - result[:"policy.default"].inspect.must_equal %{} + assert result[:"result.policy.default"].success? + assert_nil result[:"result.policy.default"][:message] + assert_equal "", result[:"policy.default"].inspect end # breach. it do result = Create.(params: {}, current_user: nil) - result[:process].must_be_nil - #- result object, policy - result[:"result.policy.default"].success?.must_equal false - result[:"result.policy.default"][:message].must_equal "Breach" + assert_nil result[:process] + refute result[:"result.policy.default"].success? + assert_equal "Breach", result[:"result.policy.default"][:message] end # inject different policy.Condition it { Create.(params: {}, current_user: Object, "policy.default.eval" => Trailblazer::Operation::Policy::Pundit::Condition.new(Auth, :user_object?))["process"].must_equal true } - it { Create.(params: {}, current_user: Module, :"policy.default.eval" => Trailblazer::Operation::Policy::Pundit::Condition.new(Auth, :user_object?))[:process].must_be_nil } + it { assert_nil Create.(params: {}, current_user: Module, :"policy.default.eval" => Trailblazer::Operation::Policy::Pundit::Condition.new(Auth, :user_object?))[:process] } + it { assert_nil Create.(params: {}, current_user: Module, :"policy.default.eval" => Trailblazer::Operation::Policy::Pundit::Condition.new(Auth, :user_object?))[:process] } #--- # inheritance, adding Model @@ -52,23 +51,22 @@ class Show < Create step Model( Song, :new ), before: :"policy.default.eval" end - it { Trailblazer::Developer.railway(Show).must_equal %{[>model.build,>policy.default.eval,>process]} } + it { assert_equal "[>model.build,>policy.default.eval,>process]", Trailblazer::Developer.railway(Show) } # invalid because user AND model. it do result = Show.(params: {}, current_user: Module) - result[:process].must_be_nil - result[:model].inspect.must_equal %{#} - # result["policy"].inspect.must_equal %{#} + assert_nil result[:process] + assert_match(/#/, result[:model].inspect) end # valid because new policy. it do # puts Show["pipetree"].inspect result = Show.(params: {}, current_user: Module, :"policy.default.eval" => Trailblazer::Operation::Policy::Pundit::Condition.new(Auth, :user_and_model?)) - result[:process].must_equal true - result[:model].inspect.must_equal %{#} - result[:"policy.default"].inspect.must_equal %{>} + assert result[:process] + assert_match(/#/, result[:"model"].inspect) + assert_match(/>/, result[:"policy.default"].inspect) end ##-- @@ -86,20 +84,19 @@ def process(options, **) # successful. it do result = Edit.(params: { id: 1 }, current_user: Module) - result[:process].must_equal true - result[:model].inspect.must_equal %{#} - result[:"result.policy.default"].success?.must_equal true - result[:"result.policy.default"][:message].must_be_nil - # result[:valid].must_be_nil - result[:"policy.default"].inspect.must_equal %{>} + assert result[:process] + assert_equal result[:model].inspect, %{#} + assert result[:"result.policy.default"].success? + assert_nil result[:"result.policy.default"][:message] + assert_match(/>/, result[:"policy.default"].inspect) end # breach. it do result = Edit.(params: { id: 4 }, current_user: nil) - result[:model].inspect.must_equal %{#} - result[:process].must_be_nil - result[:"result.policy.default"].success?.must_equal false - result[:"result.policy.default"][:message].must_equal "Breach" + assert_match(//, result[:model].inspect) + assert_nil result[:process] + refute result[:"result.policy.default"].success? + assert_equal "Breach", result[:"result.policy.default"][:message] end end