Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
13.9 KB
Raw Blame
require "test_helper"

class Rule::RunnerTest < ActiveSupport::TestCase
  include EntriesTestHelper

  setup do
    @family = families(:empty)
    @account = @family.accounts.create!(name: "Runner test", balance: 1000, currency: "USD", accountable: Depository.new)
    @groceries = @family.categories.create!(name: "Groceries")
    @dining = @family.categories.create!(name: "Dining")
    @tag_a = @family.tags.create!(name: "Tag A")
    @tag_b = @family.tags.create!(name: "Tag B")

    @whole_foods = create_transaction(account: @account, name: "Whole Foods Market").transaction
    @other = create_transaction(account: @account, name: "Hardware store").transaction
  end

  test "the rule higher up wins when two rules set the same field" do
    create_rule("Top", "Whole Foods", category: @groceries)
    create_rule("Bottom", "Whole Foods", category: @dining)

    run_active_rules

    assert_equal @groceries, @whole_foods.reload.category
  end

  test "reordering changes which rule wins" do
    top = create_rule("Top", "Whole Foods", category: @groceries)
    bottom = create_rule("Bottom", "Whole Foods", category: @dining)
    Rule::Layout.new(@family).reorder!([ "rule:#{bottom.id}", "rule:#{top.id}" ])

    run_active_rules

    assert_equal @dining, @whole_foods.reload.category
  end

  test "rules in a group run where the group stands in the list" do
    create_rule("Loose", "Whole Foods", category: @groceries)
    grouped = create_rule("Grouped", "Whole Foods", category: @dining)
    group = @family.rule_groups.create!(name: "Dining")
    grouped.update!(rule_group: group)

    run_active_rules
    assert_equal @groceries, @whole_foods.reload.category

    @whole_foods.update!(category: nil)
    loose_key = "rule:#{@family.rules.find_by!(name: "Loose").id}"
    Rule::Layout.new(@family).reorder!([ "group:#{group.id}", loose_key ])

    run_active_rules
    assert_equal @dining, @whole_foods.reload.category
  end

  test "a rule higher up claims the field even when it changes nothing" do
    @whole_foods.update!(category: @groceries)
    create_rule("Top", "Whole Foods", category: @groceries)
    create_rule("Bottom", "Whole Foods", category: @dining)

    run_active_rules

    assert_equal @groceries, @whole_foods.reload.category
  end

  test "rules further down still set fields the rules above leave alone" do
    create_rule("Top", "Whole Foods", category: @groceries)
    create_rule("Bottom", "Whole", name: "Whole Foods")

    run_active_rules

    @whole_foods.reload
    assert_equal @groceries, @whole_foods.category
    assert_equal "Whole Foods", @whole_foods.entry.name
  end

  test "a rule above whose category was deleted does not claim the field" do
    deleted = @family.categories.create!(name: "Deleted")
    create_rule("Top", "Whole Foods", category: deleted)
    create_rule("Bottom", "Whole Foods", category: @dining)
    deleted.destroy!

    run_active_rules

    assert_equal @dining, @whole_foods.reload.category
  end

  test "a rule above with a blank name does not claim the name" do
    top = create_rule("Top", "Whole Foods", name: "Placeholder")
    top.actions.first.update_column(:value, "")
    create_rule("Bottom", "Whole", name: "Whole Foods")

    run_active_rules

    assert_equal "Whole Foods", @whole_foods.reload.entry.name
  end

  test "stop processing keeps rules further down away from matched transactions" do
    create_rule("Top", "Whole Foods", category: @groceries, stop_processing: true)
    create_rule("Bottom", "o", name: "Renamed")

    run_active_rules

    assert_equal "Whole Foods Market", @whole_foods.reload.entry.name
    assert_equal "Renamed", @other.reload.entry.name
  end

  test "tags from several rules add up" do
    create_rule("Top", "Whole Foods", tags: [ @tag_a ])
    create_rule("Bottom", "Whole Foods", tags: [ @tag_b ])

    run_active_rules

    assert_equal [ @tag_a, @tag_b ].map(&:id).sort, @whole_foods.reload.tag_ids.sort
  end

  test "locked fields stay unchanged in the scheduled run" do
    @whole_foods.update!(category: @dining)
    @whole_foods.lock_attr!(:category_id)
    create_rule("Top", "Whole Foods", category: @groceries)

    run_active_rules

    assert_equal @dining, @whole_foods.reload.category
  end

  test "applying a single rule runs the active rules above it first" do
    top = create_rule("Top", "Whole Foods", category: @groceries)
    bottom = create_rule("Bottom", "Whole Foods", category: @dining)

    runs = Rule::Runner.new(@family, rules: [ bottom ], execution_type: "manual").run

    # Same as the full run: the top rule sets the category and wins
    assert_equal @groceries, @whole_foods.reload.category
    assert_equal [ top, bottom ], runs.map(&:rule)
  end

  test "applying a single rule does not run the rules below it" do
    top = create_rule("Top", "Whole Foods", category: @groceries)
    create_rule("Bottom", "Hardware", category: @dining)

    Rule::Runner.new(@family, rules: [ top ], execution_type: "manual").run

    assert_equal @groceries, @whole_foods.reload.category
    assert_nil @other.reload.category
  end

  test "ignoring locks only applies to the applied rule, not the rules above" do
    @whole_foods.entry.lock_attr!(:name)
    create_rule("Top", "Whole Foods", name: "Renamed")
    bottom = create_rule("Bottom", "Whole Foods", category: @dining)

    Rule::Runner.new(@family, rules: [ bottom ], execution_type: "manual", ignore_attribute_locks: true).run

    @whole_foods.reload
    assert_equal "Whole Foods Market", @whole_foods.entry.name
    assert_equal @dining, @whole_foods.category
  end

  test "applying a single rule respects stop processing above it" do
    create_rule("Top", "Whole Foods", category: @groceries, stop_processing: true)
    bottom = create_rule("Bottom", "Whole Foods", name: "Renamed")

    Rule::Runner.new(@family, rules: [ bottom ], execution_type: "manual").run

    assert_equal "Whole Foods Market", @whole_foods.reload.entry.name
  end

  test "inactive rules above do not claim fields" do
    create_rule("Top", "Whole Foods", category: @groceries, active: false)
    bottom = create_rule("Bottom", "Whole Foods", category: @dining)

    Rule::Runner.new(@family, rules: [ bottom ], execution_type: "manual").run

    assert_equal @dining, @whole_foods.reload.category
  end

  test "an AI action above does not keep rules below from setting the field" do
    @family.rules.create!(
      name: "AI",
      resource_type: "transaction",
      active: true,
      conditions: [ Rule::Condition.new(condition_type: "transaction_name", operator: "like", value: "Whole Foods") ],
      actions: [ Rule::Action.new(action_type: "auto_categorize") ]
    )
    bottom = create_rule("Bottom", "Whole Foods", category: @dining)

    Rule::Runner.new(@family, rules: [ bottom ], execution_type: "manual").run

    assert_equal @dining, @whole_foods.reload.category
  end

  test "a rule above that cannot be matched does not stop the applied rule" do
    broken = create_rule("Broken", "Whole Foods", category: @groceries)
    broken.conditions.first.update!(condition_type: "transaction_notes")
    Rule::ConditionFilter::TransactionNotes.any_instance.stubs(:apply).raises(ActiveRecord::StatementInvalid, "bad condition")
    bottom = create_rule("Bottom", "Whole Foods", category: @dining)

    Rule::Runner.new(@family, rules: [ bottom ], execution_type: "manual").run

    assert_equal @dining, @whole_foods.reload.category
  end

  test "the effective date still limits which transactions a rule touches" do
    old = create_transaction(account: @account, name: "Whole Foods old", date: 2.years.ago.to_date).transaction
    create_rule("Top", "Whole Foods", category: @groceries, effective_date: 1.year.ago.to_date)

    run_active_rules

    assert_equal @groceries, @whole_foods.reload.category
    assert_nil old.reload.category
  end

  test "records a rule run per executed rule with the scheduled type" do
    top = create_rule("Top", "Whole Foods", category: @groceries, stop_processing: true)
    bottom = create_rule("Bottom", "o", name: "Renamed")

    runs = run_active_rules

    assert_equal [ top, bottom ], runs.map(&:rule)
    assert_equal %w[scheduled scheduled], runs.map(&:execution_type)
    # The bottom rule no longer sees the transaction the top rule stopped
    assert_equal 1, runs.last.transactions_queued
  end

  test "a failing rule is recorded and the remaining rules still run" do
    top = create_rule("Top", "Whole Foods", category: @groceries)
    create_rule("Bottom", "Hardware", name: "Renamed")
    Rule::ActionExecutor::SetTransactionCategory.any_instance.stubs(:execute).raises(StandardError, "boom")

    runner = Rule::Runner.new(@family, rules: @family.rules, execution_type: "manual")
    runs = runner.run

    assert_equal "failed", runs.first.status
    assert_equal top, runs.first.rule
    assert_equal "Renamed", @other.reload.entry.name
    assert_equal [ "boom" ], runner.errors.map(&:message)
  end

  test "every action of a rule works on the transactions it matched" do
    @family.rules.create!(
      name: "Rename and categorize",
      resource_type: "transaction",
      active: true,
      conditions: [ Rule::Condition.new(condition_type: "transaction_name", operator: "like", value: "Whole Foods") ],
      # The rename runs first and changes the name the condition reads
      actions: [
        Rule::Action.new(action_type: "set_transaction_name", value: "Groceries store"),
        Rule::Action.new(action_type: "set_transaction_category", value: @groceries.id)
      ]
    )
    create_rule("Catch-all", "o", category: @dining)

    run_active_rules

    @whole_foods.reload
    assert_equal "Groceries store", @whole_foods.entry.name
    assert_equal @groceries, @whole_foods.category
    assert_equal @dining, @other.reload.category
  end

  test "applying a single rule sees the changes the rules above make" do
    create_rule("Rename", "Whole Foods", name: "Supermarket")
    middle = create_rule("Middle", "Supermarket", category: @groceries)
    create_rule("Catch-all", "o", category: @dining)

    Rule::Runner.new(@family, rules: [ middle ], execution_type: "manual").run

    @whole_foods.reload
    single_result = [ @whole_foods.entry.name, @whole_foods.category ]
    assert_equal [ "Supermarket", @groceries ], single_result
    # The catch-all below the applied rule did not run
    assert_nil @other.reload.category

    run_active_rules

    @whole_foods.reload
    assert_equal single_result, [ @whole_foods.entry.name, @whole_foods.category ]
    assert_equal @dining, @other.reload.category
  end

  test "a rule with a broken action does not stop the rules below" do
    broken = create_rule("Broken", "Whole Foods", category: @groceries)
    broken.actions.first.update_column(:action_type, "unknown_action")
    create_rule("Bottom", "Whole Foods", category: @dining)

    runner = Rule::Runner.new(@family, rules: @family.rules, execution_type: "manual")
    runs = runner.run

    assert_equal [ "failed", "success" ], runs.map(&:status)
    assert_equal @dining, @whole_foods.reload.category
  end

  test "a broken action above an applied rule does not stop it" do
    broken = create_rule("Broken", "Whole Foods", category: @groceries)
    broken.actions.first.update_column(:action_type, "unknown_action")
    bottom = create_rule("Bottom", "Whole Foods", category: @dining)

    Rule::Runner.new(@family, rules: [ bottom ], execution_type: "manual").run

    assert_equal @dining, @whole_foods.reload.category
  end

  test "a split rule higher up keeps the categories it gives the children" do
    split = { splits: [
      { type: "percentage", name: "Whole Foods groceries", share: "50", category_id: @groceries.id },
      { type: "percentage", name: "Whole Foods other", share: "50" }
    ] }
    @family.rules.create!(
      name: "Split", resource_type: "transaction", active: true,
      conditions: [ Rule::Condition.new(condition_type: "transaction_name", operator: "like", value: "Whole Foods") ],
      actions: [ Rule::Action.new(action_type: "split_transaction", value: split.to_json) ]
    )
    create_rule("Bottom", "Whole Foods", category: @dining)

    run_active_rules

    children = @whole_foods.entry.reload.child_entries.index_by(&:name)
    assert_equal @groceries, children["Whole Foods groceries"].transaction.category
    # The split set no category here, so the rule below still may.
    assert_equal @dining, children["Whole Foods other"].transaction.category
  end

  test "raises LockBusy while another run holds the family lock" do
    create_rule("Top", "Whole Foods", category: @groceries)

    # Transactional tests share one PG session, and a session can re-take its
    # own advisory lock, so holding it needs a separate connection.
    config = ActiveRecord::Base.connection_pool.db_config.configuration_hash
    other = PG.connect({
      dbname: config[:database], host: config[:host], port: config[:port],
      user: config[:username] || config[:user], password: config[:password]
    }.compact)
    other.exec("SELECT pg_advisory_lock(#{Rule::Runner.advisory_lock_key(@family.id)})")

    assert_raises(Rule::Runner::LockBusy) { run_active_rules }
    assert_nil @whole_foods.reload.category
  ensure
    other&.close
  end

  private
    def run_active_rules
      Rule::Runner.new(@family, rules: @family.rules.where(active: true), execution_type: "scheduled").run
    end

    def create_rule(name, match, category: nil, tags: nil, stop_processing: false, active: true, effective_date: nil, **options)
      actions = []
      actions << Rule::Action.new(action_type: "set_transaction_category", value: category.id) if category
      actions << Rule::Action.new(action_type: "set_transaction_tags", value: tags.map(&:id)) if tags
      actions << Rule::Action.new(action_type: "set_transaction_name", value: options[:name]) if options[:name]

      @family.rules.create!(
        name: name,
        resource_type: "transaction",
        active: active,
        stop_processing: stop_processing,
        effective_date: effective_date,
        conditions: [ Rule::Condition.new(condition_type: "transaction_name", operator: "like", value: match) ],
        actions: actions
      )
    end
end