test/operation_test.rb in trailblazer-operation-0.10.1 vs test/operation_test.rb in trailblazer-operation-0.11.0

- old
+ new

@@ -1,7 +1,57 @@ require "test_helper" +# Tests around {Operation.call}. +class OperationTest < Minitest::Spec + class Noop < Trailblazer::Operation + def self.capture_circuit_options((ctx, flow_options), **circuit_options) + ctx[:capture_circuit_options] = circuit_options.keys.inspect + + return Trailblazer::Activity::Right, [ctx, flow_options] + end + + 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: {}}, {}], + # real circuit_options + variable_for_circuit_options: true + ) # call_with_public_interface + #@ {:variable_for_circuit_options} is not supposed to be in {ctx}. + assert_equal CU.inspect(ctx), %({:params=>{}, :capture_circuit_options=>"[:variable_for_circuit_options, :exec_context, :activity, :runner]"}) + end + + 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_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_result result, {params: {}, capture_circuit_options: "[:variable_for_circuit_options, :wrap_runtime, :activity, :exec_context, :runner]"} + # assert_equal result.inspect, %(<Result:true #<Trailblazer::Context::Container wrapped_options={:params=>{}} mutable_options={:capture_circuit_options=>\"[:variable_for_circuit_options, :wrap_runtime, :activity, :exec_context, :runner]\"}> >) + end +end + +# TODO: remove this test as many cases are covered via autogenerated activity tests. + class DeclarativeApiTest < Minitest::Spec it "doesn't invoke {call} twice when using public interface" do class MyOp < Trailblazer::Operation @@GLOBAL = [] def self.global; @@GLOBAL; end @@ -31,11 +81,11 @@ class Create < Trailblazer::Operation step :decide! pass :wasnt_ok! pass :was_ok! fail :return_true! - fail :return_false! + left :return_false! step :bla, input: ->(ctx, *) { {id: ctx.inspect} }, output: ->(scope, ctx) { ctx["hello"] = scope["1"]; ctx } def bla(ctx, id:1, **) puts id @@ -115,21 +165,10 @@ it "allows to inherit" do Upsert.("params" => {decide: true}).inspect("a", "b", "c", "d", "e").must_equal %{<Result:true [false, true, nil, 1, nil] >} Unset. ("params" => {decide: true}).inspect("a", "b", "c", "d", "e").must_equal %{<Result:true [false, true, nil, 1, 2] >} 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 %{<Result:true #<Trailblazer::Context::Container wrapped_options={:params=>{}, \"current_user\"=>Module} mutable_options={}> >} - end - #--- #- ctx container it do options = { "params" => {decide: true} } @@ -139,12 +178,13 @@ # Circuit interface call signal, (ctx, _) = Update.([Update.options_for_public_call(options), {}], **{}) signal.inspect.must_equal %{#<Trailblazer::Activity::Railway::End::Success semantic=:success>} - ctx.inspect.must_equal %{#<Trailblazer::Context::Container wrapped_options={\"params\"=>{:decide=>true}} mutable_options={\"a\"=>false, \"b\"=>true}>} + assert_equal ctx.inspect, %(#<Trailblazer::Context::Container wrapped_options=#{{"params" => {:decide=>true}}} mutable_options=#{{"a" => false, "b" => true}}>) # Call by passing aliases as an argument. + # This uses {#call}'s second positional argument. if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0") result = Update.( options, { context_options: {