Skip to content

Commit

Permalink
add tests for Find().
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Jun 25, 2024
1 parent f1902bd commit 9f2e7f5
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions test/docs/model/find_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,21 @@ def self.where(id:, user:)
end
end

#:query
module Song::Activity
class Update < Trailblazer::Activity::Railway
step Model::Find(Song, query: ->(ctx, id:, current_user:, **) { where(id: id, user: current_user) })
step Model::Find(
Song,
query: ->(ctx, id:, current_user:, **) { where(id: id, user: current_user) }
)
step :validate
step :save
#~meths
include T.def_steps(:validate, :save)
#~meths end
end
end
#:query end

it do
current_user = Module
Expand Down Expand Up @@ -631,7 +636,13 @@ class Create < Trailblazer::Activity::Railway
# step Model::Find(Song, find_by: :id, not_found_terminus: true)
#
class ModelFind404TerminusTest < Minitest::Spec
Song = Class.new(DocsModelFindTest::Song)
Song = Struct.new(:id) do
def self.find_by(id:)
return if id.nil?
return if id == 2
new(id)
end
end

#:not-found
class Song
Expand All @@ -648,11 +659,18 @@ class Update < Trailblazer::Activity::Railway
end
#:not-found end

it do
it "terminates on {not_found} for missing ID in {params}" do
assert_invoke Song::Activity::Update, params: {id: 1},
seq: "[:validate, :save]", expected_ctx_variables: {model: Song.find_by(id: 1)}
assert_invoke Song::Activity::Update, params: {id: nil}, terminus: :not_found

# no {params} at all.
assert_invoke Song::Activity::Update, terminus: :not_found

# no model matching ID.
# NOTE: we assign {model: nil} - do we want that?
assert_invoke Song::Activity::Update, params: {id: 2}, terminus: :not_found, expected_ctx_variables: {model: nil}

#:not-found-invoke
signal, (ctx, _) = Trailblazer::Activity::TaskWrap.invoke(Song::Activity::Update, [{params: {id: nil}},{}])
puts signal #=> #<Trailblazer::Activity::End semantic=:not_found>
Expand Down

0 comments on commit 9f2e7f5

Please sign in to comment.