|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class BudgetTest < ActiveSupport::TestCase |
| 4 | + setup do |
| 5 | + @family = families(:empty) |
| 6 | + end |
| 7 | + |
| 8 | + test "budget_date_valid? allows going back 2 years even without entries" do |
| 9 | + two_years_ago = 2.years.ago.beginning_of_month |
| 10 | + assert Budget.budget_date_valid?(two_years_ago, family: @family) |
| 11 | + end |
| 12 | + |
| 13 | + test "budget_date_valid? allows going back to earliest entry date if more than 2 years ago" do |
| 14 | + # Create an entry 3 years ago |
| 15 | + old_account = Account.create!( |
| 16 | + family: @family, |
| 17 | + accountable: Depository.new, |
| 18 | + name: "Old Account", |
| 19 | + status: "active", |
| 20 | + currency: "USD", |
| 21 | + balance: 1000 |
| 22 | + ) |
| 23 | + |
| 24 | + old_entry = Entry.create!( |
| 25 | + account: old_account, |
| 26 | + entryable: Transaction.new(category: categories(:income)), |
| 27 | + date: 3.years.ago, |
| 28 | + name: "Old Transaction", |
| 29 | + amount: 100, |
| 30 | + currency: "USD" |
| 31 | + ) |
| 32 | + |
| 33 | + # Should allow going back to the old entry date |
| 34 | + assert Budget.budget_date_valid?(3.years.ago.beginning_of_month, family: @family) |
| 35 | + end |
| 36 | + |
| 37 | + test "budget_date_valid? does not allow dates before earliest entry or 2 years ago" do |
| 38 | + # Create an entry 1 year ago |
| 39 | + account = Account.create!( |
| 40 | + family: @family, |
| 41 | + accountable: Depository.new, |
| 42 | + name: "Test Account", |
| 43 | + status: "active", |
| 44 | + currency: "USD", |
| 45 | + balance: 500 |
| 46 | + ) |
| 47 | + |
| 48 | + Entry.create!( |
| 49 | + account: account, |
| 50 | + entryable: Transaction.new(category: categories(:income)), |
| 51 | + date: 1.year.ago, |
| 52 | + name: "Recent Transaction", |
| 53 | + amount: 100, |
| 54 | + currency: "USD" |
| 55 | + ) |
| 56 | + |
| 57 | + # Should not allow going back more than 2 years |
| 58 | + refute Budget.budget_date_valid?(3.years.ago.beginning_of_month, family: @family) |
| 59 | + end |
| 60 | + |
| 61 | + test "budget_date_valid? does not allow future dates beyond current month" do |
| 62 | + refute Budget.budget_date_valid?(2.months.from_now, family: @family) |
| 63 | + end |
| 64 | + |
| 65 | + test "previous_budget_param returns nil when date is too old" do |
| 66 | + # Create a budget at the oldest allowed date |
| 67 | + two_years_ago = 2.years.ago.beginning_of_month |
| 68 | + budget = Budget.create!( |
| 69 | + family: @family, |
| 70 | + start_date: two_years_ago, |
| 71 | + end_date: two_years_ago.end_of_month, |
| 72 | + currency: "USD" |
| 73 | + ) |
| 74 | + |
| 75 | + assert_nil budget.previous_budget_param |
| 76 | + end |
| 77 | + |
| 78 | + test "previous_budget_param returns param when date is valid" do |
| 79 | + budget = Budget.create!( |
| 80 | + family: @family, |
| 81 | + start_date: Date.current.beginning_of_month, |
| 82 | + end_date: Date.current.end_of_month, |
| 83 | + currency: "USD" |
| 84 | + ) |
| 85 | + |
| 86 | + assert_not_nil budget.previous_budget_param |
| 87 | + end |
| 88 | +end |
0 commit comments