Skip to content

Commit

Permalink
add step_test.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Nov 13, 2023
1 parent 39567f3 commit 62878cc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
17 changes: 14 additions & 3 deletions test/docs/mechanics_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def validate(ctx, params:, **)
end

class WriteToCtx_DocsMechanicsTest < Minitest::Spec
Memo = Class.new do
class Memo
def initialize(*); end
end
it "what" do
Expand Down Expand Up @@ -124,7 +124,17 @@ def notify(ctx, model:, **)
end
#:ctx-write-read end

result = Memo::Operation::Create.call(params: {memo: nil})
#:result-read
result = Memo::Operation::Create.call(params: {memo: {content: "remember that"}})

result[:model] #=> #<Memo id: 1, ...>
#:result-read end

#:result-success
puts result.success? #=> true
#:result-success end

assert_equal result[:model].class, Memo
assert_equal result.success?, true

user = Object
Expand Down Expand Up @@ -198,6 +208,7 @@ class NetworkError < Trailblazer::Activity::Signal
#:signal-steps
step :validate
step :save
left :handle_errors
step :notify,
Output(NetworkError, :network_error) => End(:network_error)
#:signal-steps end
Expand Down Expand Up @@ -231,7 +242,7 @@ def notify(ctx, model:, params:, **)

result = Memo::Operation::Create.call(params: {memo: nil, network_broken: true})
assert_equal result.success?, false
assert_equal result.event.inspect, %(asdf)
assert_equal result.event.inspect, %(#<Trailblazer::Activity::End semantic=:network_error>)
end
end

Expand Down
68 changes: 68 additions & 0 deletions test/docs/step_dsl_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require "test_helper"

module A
class DocsStepTest < Minitest::Spec
Memo = Module.new

#:railway
module Memo::Operation
class Create < Trailblazer::Operation
step :validate
step :save
#~step
left :handle_errors
#~left
step :notify
#~meths
# fail :log_error
# step :save
def validate(ctx, params:, **)
ctx[:input] = Form.validate(params) # true/false
end

# def create(ctx, input:, create:, **)
# create
# end

# def log_error(ctx, logger:, params:, **)
# logger.error("wrong params: #{params.inspect}")
# end
#~meths end
end
end
#~step end
#~left end
#:railway end

# it "what" do
# ctx = {params: {text: "Hydrate!"}, create: true}
# signal, (ctx, _flow_options) = D::Create.([ctx, {}])
# end
end
end


module B
class DocsStepTest < Minitest::Spec
Memo = Module.new

#:fail
module Memo::Operation
class Create < Trailblazer::Operation
step :validate
step :save
fail :handle_errors # just like {#left}
#~meths
step :notify
include T.def_steps(:validate, :save, :handle_errors, :notify)
#~meths end
end
end
#:fail end

it "what" do
assert_invoke Memo::Operation::Create, seq: "[:validate, :save, :notify]"
assert_invoke Memo::Operation::Create, validate: false, terminus: :failure, seq: "[:validate, :handle_errors]"
end
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Minitest::Spec.class_eval do
Activity = Trailblazer::Activity
T = Activity::Testing
include Trailblazer::Activity::Testing::Assertions
end

# TODO: replace all this with {Activity::Testing.def_steps}
Expand Down

0 comments on commit 62878cc

Please sign in to comment.