From 9f2e7f5ce165ede9fffdf58a129838d2129b72f2 Mon Sep 17 00:00:00 2001 From: Nick Sutterer Date: Tue, 25 Jun 2024 13:33:08 +0200 Subject: [PATCH] add tests for Find(). --- test/docs/model/find_test.rb | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/test/docs/model/find_test.rb b/test/docs/model/find_test.rb index 12136e6..8529ba9 100644 --- a/test/docs/model/find_test.rb +++ b/test/docs/model/find_test.rb @@ -108,9 +108,13 @@ 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 @@ -118,6 +122,7 @@ class Update < Trailblazer::Activity::Railway #~meths end end end + #:query end it do current_user = Module @@ -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 @@ -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 #=> #