From 76052671b047e814a60e509affb0dc7fbca19e17 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Tue, 8 Apr 2025 22:18:33 -0400 Subject: [PATCH] Fix tests --- app/models/rule.rb | 2 +- test/controllers/rules_controller_test.rb | 78 +++++++++++++---------- test/models/rule/action_test.rb | 2 +- test/models/rule/condition_test.rb | 2 +- test/models/rule_test.rb | 1 + 5 files changed, 49 insertions(+), 36 deletions(-) diff --git a/app/models/rule.rb b/app/models/rule.rb index 1901c3fd..2e028f08 100644 --- a/app/models/rule.rb +++ b/app/models/rule.rb @@ -73,7 +73,7 @@ class Rule < ApplicationRecord end def no_duplicate_actions - action_types = actions.map(&:action_type) + action_types = actions.reject(&:marked_for_destruction?).map(&:action_type) errors.add(:base, "Rule cannot have duplicate actions #{action_types.inspect}") if action_types.uniq.count != action_types.count end diff --git a/test/controllers/rules_controller_test.rb b/test/controllers/rules_controller_test.rb index d58639aa..d45191a5 100644 --- a/test/controllers/rules_controller_test.rb +++ b/test/controllers/rules_controller_test.rb @@ -22,35 +22,35 @@ class RulesControllerTest < ActionDispatch::IntegrationTest active: true, effective_date: 30.days.ago.to_date, resource_type: "transaction", - conditions_attributes: [ - { + conditions_attributes: { + "0" => { condition_type: "transaction_name", operator: "like", value: "starbucks" }, - { + "1" => { condition_type: "compound", operator: "and", - sub_conditions_attributes: [ - { + sub_conditions_attributes: { + "0" => { condition_type: "transaction_amount", operator: ">", value: 20 }, - { + "1" => { condition_type: "transaction_amount", operator: "<", value: 40 } - ] + } } - ], - actions_attributes: [ - { + }, + actions_attributes: { + "0" => { action_type: "set_transaction_category", value: categories(:food_and_drink).id } - ] + } } } @@ -76,22 +76,33 @@ class RulesControllerTest < ActionDispatch::IntegrationTest test "can update rule" do rule = rules(:one) - assert_no_difference [ "Rule.count", "Rule::Condition.count", "Rule::Action.count" ] do + assert_difference -> { Rule.count } => 0, + -> { Rule::Condition.count } => 1, + -> { Rule::Action.count } => 1 do patch rule_url(rule), params: { rule: { active: false, - conditions_attributes: [ - { + conditions_attributes: { + "0" => { id: rule.conditions.first.id, value: "new_value" + }, + "1" => { + condition_type: "transaction_amount", + operator: ">", + value: 100 } - ], - actions_attributes: [ - { + }, + actions_attributes: { + "0" => { id: rule.actions.first.id, value: "new_value" + }, + "1" => { + action_type: "set_transaction_tags", + value: tags(:one).id } - ] + } } } end @@ -99,8 +110,10 @@ class RulesControllerTest < ActionDispatch::IntegrationTest rule.reload assert_not rule.active - assert_equal "new_value", rule.conditions.first.value - assert_equal "new_value", rule.actions.first.value + assert_equal "new_value", rule.conditions.order("created_at ASC").first.value + assert_equal "new_value", rule.actions.order("created_at ASC").first.value + assert_equal tags(:one).id, rule.actions.order("created_at ASC").last.value + assert_equal "100", rule.conditions.order("created_at ASC").last.value assert_redirected_to rules_url end @@ -113,22 +126,21 @@ class RulesControllerTest < ActionDispatch::IntegrationTest patch rule_url(rule), params: { rule: { - conditions_attributes: [ - { id: rule.conditions.first.id, _destroy: true }, - { + conditions_attributes: { + "0" => { id: rule.conditions.first.id, _destroy: true }, + "1" => { condition_type: "transaction_name", operator: "like", value: "new_condition" - }, - { - condition_type: "transaction_amount", - operator: ">", - value: 100 } - ], - actions_attributes: [ - { id: rule.actions.first.id, _destroy: true } - ] + }, + actions_attributes: { + "0" => { id: rule.actions.first.id, _destroy: true }, + "1" => { + action_type: "set_transaction_tags", + value: tags(:one).id + } + } } } @@ -136,7 +148,7 @@ class RulesControllerTest < ActionDispatch::IntegrationTest rule.reload - assert_equal 2, rule.conditions.count + assert_equal 1, rule.conditions.count assert_equal 1, rule.actions.count end diff --git a/test/models/rule/action_test.rb b/test/models/rule/action_test.rb index 40a34dd3..47edca0c 100644 --- a/test/models/rule/action_test.rb +++ b/test/models/rule/action_test.rb @@ -5,7 +5,7 @@ class Rule::ActionTest < ActiveSupport::TestCase setup do @family = families(:empty) - @transaction_rule = @family.rules.create!(resource_type: "transaction") + @transaction_rule = rules(:one) @account = @family.accounts.create!(name: "Rule test", balance: 1000, currency: "USD", accountable: Depository.new) @grocery_category = @family.categories.create!(name: "Grocery") diff --git a/test/models/rule/condition_test.rb b/test/models/rule/condition_test.rb index f2591bc2..dda7ed6a 100644 --- a/test/models/rule/condition_test.rb +++ b/test/models/rule/condition_test.rb @@ -5,7 +5,7 @@ class Rule::ConditionTest < ActiveSupport::TestCase setup do @family = families(:empty) - @transaction_rule = @family.rules.create!(resource_type: "transaction") + @transaction_rule = rules(:one) @account = @family.accounts.create!(name: "Rule test", balance: 1000, currency: "USD", accountable: Depository.new) @grocery_category = @family.categories.create!(name: "Grocery") diff --git a/test/models/rule_test.rb b/test/models/rule_test.rb index 66930b65..f70f06dc 100644 --- a/test/models/rule_test.rb +++ b/test/models/rule_test.rb @@ -61,6 +61,7 @@ class RuleTest < ActiveSupport::TestCase rule = Rule.new( family: @family, resource_type: "transaction", + actions: [ Rule::Action.new(action_type: "set_transaction_category", value: @groceries_category.id) ], conditions: [ Rule::Condition.new(condition_type: "compound", operator: "and", sub_conditions: [ Rule::Condition.new(condition_type: "compound", operator: "and", sub_conditions: [