Skip to content

Commit

Permalink
add test files.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Nov 20, 2023
1 parent f6adaae commit 2979940
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
81 changes: 81 additions & 0 deletions test/ruby/activity_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require "test_helper"

module X
class DocsActivityTest < Minitest::Spec
it "basic activity" do
Memo = Struct.new(:options) do
def save
true
end
end

#:memo-create
module Memo::Activity
class Create < Trailblazer::Activity::Railway
step :validate
#~body
step :save
left :handle_errors
step :notify
#~meths
include T.def_steps(:validate, :save, :handle_errors, :notify)

#:save
def save(ctx, params:, **)
memo = Memo.new(params[:memo])
memo.save

ctx[:model] = memo # you can write to the {ctx}.
end
#:save end

def notify(ctx, **)
true
end

#~body end
def validate(ctx, params:, **) # home-made validation
params.key?(:memo) &&
params[:memo].key?(:text) &&
params[:memo][:text].size > 9
# return value matters!
end
#~meths end
end
end
#:memo-create end

#:memo-call
signal, (ctx, _) = Trailblazer::Activity.(Memo::Activity::Create,
params: {memo: {text: "Do not forget!"}}
)

puts signal #=> #<Trailblazer::Activity::End semantic=:success>
puts signal.to_h[:semantic] #=> :success
#:memo-call end

#:memo-call-model
signal, (ctx, _) = Trailblazer::Activity.(Memo::Activity::Create,
params: {memo: {text: "Do not forget!"}}
)

#~ctx_to_result
puts ctx[:model] #=> #<Memo id: 1 text: "Do not forget!">
#:memo-call-model end
#~ctx_to_result end

assert_equal signal.inspect, %(#<Trailblazer::Activity::End semantic=:success>)
assert_equal ctx.inspect, %({:params=>{:memo=>{:text=>\"Do not forget!\"}}, :model=>#<struct X::DocsActivityTest::Memo options={:text=>\"Do not forget!\"}>})

model = ctx[:model]

assert_invoke Memo::Activity::Create, seq: "[]", params: {memo: {text: "Do not forget!"}}, expected_ctx_variables: {model: model}
assert_invoke Memo::Activity::Create, seq: "[:handle_errors]", params: {}, terminus: :failure

Trailblazer::Activity::Railway #!hint variable = Trailblazer::Operation
end

class Update < Trailblazer::Activity::FastTrack
end
end
end
82 changes: 82 additions & 0 deletions test/ruby/operation/activity_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require "test_helper"
# THIS FILE IS AUTOGENERATED FROM trailblazer-activity/test/ruby/activity_test.rb

module X
class DocsActivityTest < Minitest::Spec
it "basic activity" do
Memo = Struct.new(:options) do
def save
true
end
end

#:memo-create
module Memo::Operation
class Create < Trailblazer::Operation
step :validate
#~body
step :save
left :handle_errors
step :notify
#~meths
include T.def_steps(:validate, :save, :handle_errors, :notify)

#:save
def save(ctx, params:, **)
memo = Memo.new(params[:memo])
memo.save

ctx[:model] = memo # you can write to the {ctx}.
end
#:save end

def notify(ctx, **)
true
end

#~body end
def validate(ctx, params:, **) # home-made validation
params.key?(:memo) &&
params[:memo].key?(:text) &&
params[:memo][:text].size > 9
# return value matters!
end
#~meths end
end
end
#:memo-create end

#:memo-call
result = Memo::Operation::Create.(
params: {memo: {text: "Do not forget!"}}
)

result.success? # => true
puts signal.to_h[:semantic] #=> :success
#:memo-call end

#:memo-call-model
result = Memo::Operation::Create.(
params: {memo: {text: "Do not forget!"}}
)

#~ctx_to_result
puts result[:model] #=> #<Memo id: 1 text: "Do not forget!">
#:memo-call-model end
#~ctx_to_result end

assert_equal result.event.inspect, %(#<Trailblazer::Operation::End semantic=:success>)
assert_equal result.inspect, %({:params=>{:memo=>{:text=>\"Do not forget!\"}}, :model=>#<struct X::DocsActivityTest::Memo options={:text=>\"Do not forget!\"}>})

model = ctx[:model]

assert_invoke Memo::Operation::Create, seq: "[]", params: {memo: {text: "Do not forget!"}}, expected_ctx_variables: {model: model}
assert_invoke Memo::Operation::Create, seq: "[:handle_errors]", params: {}, terminus: :failure

variable = Trailblazer::Operation
end

class Update < Trailblazer::Operation
end
end
end

0 comments on commit 2979940

Please sign in to comment.