mirror of
https://github.com/we-promise/sure.git
synced 2026-04-18 11:34:13 +00:00
Implement Run all rules (#582)
This commit is contained in:
@@ -179,4 +179,17 @@ class RulesControllerTest < ActionDispatch::IntegrationTest
|
||||
|
||||
assert_redirected_to rules_url
|
||||
end
|
||||
|
||||
test "should get confirm_all" do
|
||||
get confirm_all_rules_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "apply_all enqueues job and redirects" do
|
||||
assert_enqueued_with(job: ApplyAllRulesJob) do
|
||||
post apply_all_rules_url
|
||||
end
|
||||
|
||||
assert_redirected_to rules_url
|
||||
end
|
||||
end
|
||||
|
||||
41
test/jobs/apply_all_rules_job_test.rb
Normal file
41
test/jobs/apply_all_rules_job_test.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
require "test_helper"
|
||||
|
||||
class ApplyAllRulesJobTest < ActiveJob::TestCase
|
||||
include EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@family = families(:empty)
|
||||
@account = @family.accounts.create!(name: "Test Account", balance: 1000, currency: "USD", accountable: Depository.new)
|
||||
@groceries_category = @family.categories.create!(name: "Groceries")
|
||||
end
|
||||
|
||||
test "applies all rules for a family" do
|
||||
# Create a rule
|
||||
rule = Rule.create!(
|
||||
family: @family,
|
||||
resource_type: "transaction",
|
||||
effective_date: 1.day.ago.to_date,
|
||||
conditions: [ Rule::Condition.new(condition_type: "transaction_name", operator: "like", value: "Whole Foods") ],
|
||||
actions: [ Rule::Action.new(action_type: "set_transaction_category", value: @groceries_category.id) ]
|
||||
)
|
||||
|
||||
# Mock RuleJob to verify it gets called for each rule
|
||||
RuleJob.expects(:perform_now).with(rule, ignore_attribute_locks: true, execution_type: "manual").once
|
||||
|
||||
ApplyAllRulesJob.perform_now(@family)
|
||||
end
|
||||
|
||||
test "applies all rules with custom execution type" do
|
||||
rule = Rule.create!(
|
||||
family: @family,
|
||||
resource_type: "transaction",
|
||||
effective_date: 1.day.ago.to_date,
|
||||
conditions: [ Rule::Condition.new(condition_type: "transaction_name", operator: "like", value: "Test") ],
|
||||
actions: [ Rule::Action.new(action_type: "set_transaction_category", value: @groceries_category.id) ]
|
||||
)
|
||||
|
||||
RuleJob.expects(:perform_now).with(rule, ignore_attribute_locks: true, execution_type: "scheduled").once
|
||||
|
||||
ApplyAllRulesJob.perform_now(@family, execution_type: "scheduled")
|
||||
end
|
||||
end
|
||||
@@ -201,4 +201,39 @@ class RuleTest < ActiveSupport::TestCase
|
||||
assert_equal business_category, transaction_entry.transaction.category, "Transaction with 'business' in notes should be categorized"
|
||||
assert_nil transaction_entry2.transaction.category, "Transaction without 'business' in notes should not be categorized"
|
||||
end
|
||||
|
||||
test "total_affected_resource_count deduplicates overlapping rules" do
|
||||
# Create transactions
|
||||
transaction_entry1 = create_transaction(date: Date.current, account: @account, name: "Whole Foods", amount: 50)
|
||||
transaction_entry2 = create_transaction(date: Date.current, account: @account, name: "Whole Foods", amount: 100)
|
||||
transaction_entry3 = create_transaction(date: Date.current, account: @account, name: "Target", amount: 75)
|
||||
|
||||
# Rule 1: Match transactions with name "Whole Foods" (matches txn 1 and 2)
|
||||
rule1 = Rule.create!(
|
||||
family: @family,
|
||||
resource_type: "transaction",
|
||||
effective_date: 1.day.ago.to_date,
|
||||
conditions: [ Rule::Condition.new(condition_type: "transaction_name", operator: "like", value: "Whole Foods") ],
|
||||
actions: [ Rule::Action.new(action_type: "set_transaction_category", value: @groceries_category.id) ]
|
||||
)
|
||||
|
||||
# Rule 2: Match transactions with amount > 60 (matches txn 2 and 3)
|
||||
rule2 = Rule.create!(
|
||||
family: @family,
|
||||
resource_type: "transaction",
|
||||
effective_date: 1.day.ago.to_date,
|
||||
conditions: [ Rule::Condition.new(condition_type: "transaction_amount", operator: ">", value: 60) ],
|
||||
actions: [ Rule::Action.new(action_type: "set_transaction_category", value: @groceries_category.id) ]
|
||||
)
|
||||
|
||||
# Rule 1 affects 2 transactions, Rule 2 affects 2 transactions
|
||||
# But transaction_entry2 is matched by both, so total unique should be 3
|
||||
assert_equal 2, rule1.affected_resource_count
|
||||
assert_equal 2, rule2.affected_resource_count
|
||||
assert_equal 3, Rule.total_affected_resource_count([ rule1, rule2 ])
|
||||
end
|
||||
|
||||
test "total_affected_resource_count returns zero for empty rules" do
|
||||
assert_equal 0, Rule.total_affected_resource_count([])
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user