require "test_helper" class Rule::ActionTest < ActiveSupport::TestCase include Account::EntriesTestHelper setup do @family = families(:empty) @transaction_rule = @family.rules.create!(resource_type: "transaction") @account = @family.accounts.create!(name: "Rule test", balance: 1000, currency: "USD", accountable: Depository.new) @shopping_category = @family.categories.create!(name: "Shopping") # Some sample transactions to work with create_transaction(date: Date.current, account: @account, amount: 100, name: "Rule test transaction1", merchant: merchants(:amazon)) create_transaction(date: Date.current, account: @account, amount: -200, name: "Rule test transaction2") create_transaction(date: 1.day.ago.to_date, account: @account, amount: 50, name: "Rule test transaction3") create_transaction(date: 1.year.ago.to_date, account: @account, amount: 10, name: "Rule test transaction4", merchant: merchants(:amazon)) create_transaction(date: 1.year.ago.to_date, account: @account, amount: 1000, name: "Rule test transaction5") @rule_scope = @account.transactions end test "set_transaction_category" do action = Rule::Action.new( rule: @transaction_rule, action_type: "set_transaction_category", value: @shopping_category.id ) action.apply(@rule_scope) @rule_scope.reload.each do |transaction| assert_equal @shopping_category.id, transaction.category_id end end end