From 039bd9ade0d7b67ce7341d87321e66f95e4c215d Mon Sep 17 00:00:00 2001 From: Nick Sutterer Date: Mon, 27 Nov 2023 15:42:55 +0100 Subject: [PATCH] more tests for result/ctx. --- test/class_dependencies_test.rb | 8 ++++++-- test/operation_test.rb | 19 ++++++------------- test/test_helper.rb | 7 +++++++ 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/test/class_dependencies_test.rb b/test/class_dependencies_test.rb index 4ff70ea..081d9ac 100644 --- a/test/class_dependencies_test.rb +++ b/test/class_dependencies_test.rb @@ -28,10 +28,14 @@ class Home < Trailblazer::Operation end # "model.class" gets injected automatically just before {Index}. - Home.({params: {}}).inspect.must_equal %{Module} mutable_options=#{}} mutable_options={\"a\"=>Module}>> >} + result = Home.({params: {}}) + assert_result result, {:"model.class"=>Module, :params=>{}, :a=>Module} + # .inspect.must_equal %{Module} mutable_options=#{}} mutable_options={\"a\"=>Module}>> >} # "model.class" gets injected by user and overrides class dependencies. - Home.({params: {}, "model.class" => Symbol}).inspect.must_equal %{Module} mutable_options=#{}, \"model.class\"=>Symbol} mutable_options={\"a\"=>Symbol}>> >} + result = Home.({params: {}, "model.class" => Symbol}) + assert_result result, {:"model.class"=>Symbol, :params=>{}, :a=>Symbol } + # .inspect.must_equal %{Module} mutable_options=#{}, \"model.class\"=>Symbol} mutable_options={\"a\"=>Symbol}>> >} class Dashboard < Trailblazer::Operation diff --git a/test/operation_test.rb b/test/operation_test.rb index a5a1b72..2c558fe 100644 --- a/test/operation_test.rb +++ b/test/operation_test.rb @@ -12,6 +12,9 @@ def self.capture_circuit_options((ctx, flow_options), **circuit_options) step task: method(:capture_circuit_options) end + # Mixing keywords and string keys in {Operation.call}. + # Test that {.(params: {}, "current_user" => user)} is processed properly + it "doesn't mistake circuit options as ctx variables when using circuit-interface" do signal, (ctx, _) = Noop.call([{params: {}}, {}], variable_for_circuit_options: true) # call_with_public_interface #@ {:variable_for_circuit_options} is not supposed to be in {ctx}. @@ -21,14 +24,15 @@ def self.capture_circuit_options((ctx, flow_options), **circuit_options) it "doesn't mistake circuit options as ctx variables when using the call interface" do result = Noop.call(params: {}, model: true, "current_user" => Object) # call with public interface. #@ {:variable_for_circuit_options} is not supposed to be in {ctx}. - assert_equal result.inspect, %({}, :model=>true, "current_user"=>Object} mutable_options={:capture_circuit_options=>"[:wrap_runtime, :activity, :exec_context, :runner]"}> >) + assert_result result, {params: {}, model: true, current_user: Object, capture_circuit_options: "[:wrap_runtime, :activity, :exec_context, :runner]"} end #@ {#call_with_public_interface} it "doesn't mistake circuit options as ctx variables when using circuit-interface" do result = Noop.call_with_public_interface({params: {}}, {}, variable_for_circuit_options: true) # call_with_public_interface has two positional args, and kwargs for {circuit_options}. - assert_equal result.inspect, %({}} mutable_options={:capture_circuit_options=>\"[:variable_for_circuit_options, :wrap_runtime, :activity, :exec_context, :runner]\"}> >) + assert_result result, {params: {}, capture_circuit_options: "[:variable_for_circuit_options, :wrap_runtime, :activity, :exec_context, :runner]"} + # assert_equal result.inspect, %({}} mutable_options={:capture_circuit_options=>\"[:variable_for_circuit_options, :wrap_runtime, :activity, :exec_context, :runner]\"}> >) end end @@ -151,17 +155,6 @@ def self.flow_options_for_public_call(*) Unset. ("params" => {decide: true}).inspect("a", "b", "c", "d", "e").must_equal %{} end -# Mixing keywords and string keys in {Operation.call}. -# Test that {.(params: {}, "current_user" => user)} is processed properly - class Collect < Trailblazer::Operation - # step ->(ctx, **) { ctx[:keys] } - end - - it "contains all keys from {call}" do - result = Collect.(params: {}, "current_user" => Module) - _(result.inspect).must_equal %{{}, \"current_user\"=>Module} mutable_options={}> >} - end - #--- #- ctx container it do diff --git a/test/test_helper.rb b/test/test_helper.rb index b42817f..327a514 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -15,6 +15,13 @@ def assert_equal(asserted, expected, *args) super(expected, asserted, *args) end + + def assert_result(result, variables, outcome: true) + assert_equal result.success?, outcome + + # assert_equal result.send(:data).sort_by { |key, _| key.to_s }.to_h.inspect, variables.sort_by { |key, _| key.to_s }.to_h.inspect + assert_equal result.send(:data).to_h, variables + end end # TODO: replace all this with {Activity::Testing.def_steps}