-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |