Skip to content

Commit

Permalink
more tests for result/ctx.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Nov 27, 2023
1 parent ac2fa2b commit 039bd9a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
8 changes: 6 additions & 2 deletions test/class_dependencies_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ class Home < Trailblazer::Operation
end

# "model.class" gets injected automatically just before {Index}.
Home.({params: {}}).inspect.must_equal %{<Result:true #<Trailblazer::Context::Container wrapped_options={\"model.class\"=>Module} mutable_options=#<Trailblazer::Context::Container wrapped_options={:params=>{}} mutable_options={\"a\"=>Module}>> >}
result = Home.({params: {}})
assert_result result, {:"model.class"=>Module, :params=>{}, :a=>Module}
# .inspect.must_equal %{<Result:true #<Trailblazer::Context::Container wrapped_options={\"model.class\"=>Module} mutable_options=#<Trailblazer::Context::Container wrapped_options={:params=>{}} mutable_options={\"a\"=>Module}>> >}

# "model.class" gets injected by user and overrides class dependencies.
Home.({params: {}, "model.class" => Symbol}).inspect.must_equal %{<Result:true #<Trailblazer::Context::Container wrapped_options={\"model.class\"=>Module} mutable_options=#<Trailblazer::Context::Container wrapped_options={:params=>{}, \"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 %{<Result:true #<Trailblazer::Context::Container wrapped_options={\"model.class\"=>Module} mutable_options=#<Trailblazer::Context::Container wrapped_options={:params=>{}, \"model.class\"=>Symbol} mutable_options={\"a\"=>Symbol}>> >}


class Dashboard < Trailblazer::Operation
Expand Down
19 changes: 6 additions & 13 deletions test/operation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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, %(<Result:true #<Trailblazer::Context::Container wrapped_options={:params=>{}, :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, %(<Result:true #<Trailblazer::Context::Container wrapped_options={:params=>{}} 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, %(<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

Expand Down Expand Up @@ -151,17 +155,6 @@ def self.flow_options_for_public_call(*)
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
Expand Down
7 changes: 7 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 039bd9a

Please sign in to comment.