From ce2a2cfdb50ca6cd043854a27ef47335ef53e63a Mon Sep 17 00:00:00 2001 From: kinoppyd Date: Thu, 16 May 2024 13:25:47 +0900 Subject: [PATCH] Fix a bug Condition's includes action returns false positive when empty array --- app/models/trigger/condition.rb | 4 +--- test/models/trigger/condition_test.rb | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/models/trigger/condition.rb b/app/models/trigger/condition.rb index 81df1a72..f34defb7 100644 --- a/app/models/trigger/condition.rb +++ b/app/models/trigger/condition.rb @@ -108,9 +108,7 @@ def count_check(num, operator) end def includes_check(includes) - resolve_model_attribute.each do |obj| - return false unless includes.include?(obj) - end + resolve_model_attribute.any? { |obj| includes.include?(obj) } end end end diff --git a/test/models/trigger/condition_test.rb b/test/models/trigger/condition_test.rb index 4cf22ede..9d192d6b 100644 --- a/test/models/trigger/condition_test.rb +++ b/test/models/trigger/condition_test.rb @@ -377,9 +377,23 @@ class ConditionTest < ActiveSupport::TestCase assert_not condition.satisfy? end - test '#satisfy? returns true when include action chain methods' do - Friend.create!(from: profiles(:profile_one).id, to: profiles(:profile_two).id) + test '#satisfy? returns false when include action(with empty array)' do + condition = Trigger::Condition.new( + { + model: 'Friend', + target: 'Profile', + props: { + from: 'target' + }, + action: { 'includes' => [profiles(:profile_three).id], 'attribute' => 'to' } + }, + profiles(:profile_one) + ) + + assert_not condition.satisfy? + end + test '#satisfy? returns false when include action chain methods' do condition = Trigger::Condition.new( { model: 'Friend', @@ -392,7 +406,7 @@ class ConditionTest < ActiveSupport::TestCase profiles(:profile_one) ) - assert condition.satisfy? + assert_not condition.satisfy? end test '#satisfy? returns true when include action chain methods with eager loading' do