2025-04-02 12:47:07 -04:00
|
|
|
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)
|
|
|
|
|
2025-04-02 17:48:34 -04:00
|
|
|
@shopping_category = @family.categories.create!(name: "Shopping")
|
2025-04-02 12:47:07 -04:00
|
|
|
|
|
|
|
# Some sample transactions to work with
|
2025-04-02 17:48:34 -04:00
|
|
|
create_transaction(date: Date.current, account: @account, amount: 100, name: "Rule test transaction1", merchant: merchants(:amazon))
|
2025-04-02 12:47:07 -04:00
|
|
|
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")
|
2025-04-02 17:48:34 -04:00
|
|
|
create_transaction(date: 1.year.ago.to_date, account: @account, amount: 10, name: "Rule test transaction4", merchant: merchants(:amazon))
|
2025-04-02 12:47:07 -04:00
|
|
|
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",
|
2025-04-02 17:48:34 -04:00
|
|
|
value: @shopping_category.id
|
2025-04-02 12:47:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
action.apply(@rule_scope)
|
|
|
|
|
|
|
|
@rule_scope.reload.each do |transaction|
|
2025-04-02 17:48:34 -04:00
|
|
|
assert_equal @shopping_category.id, transaction.category_id
|
2025-04-02 12:47:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|