Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(demo): stop the goal-seeding matrix crashing rake demo_data:default (#3314)

buzzromain2bca87b85675
diff --git a/app/models/demo/generator.rb b/app/models/demo/generator.rb
index 93323cc54b..5777336486 100644
--- a/app/models/demo/generator.rb
+++ b/app/models/demo/generator.rb
@@ -1329,23 +1329,26 @@ class Demo::Generator
target: 20_000,
target_date: 4.months.from_now.to_date,
accounts: [ secondary ],
+ allocations: { secondary => 3_000 },
pledges: [
{ account: secondary, amount: 250, kind: "transfer", status: "open", expires_at: 5.days.from_now }
]
},
- # active · reached — primary balance comfortably above target
+ # active · reached — earmark comfortably above target
{
name: "Wedding fund",
target: 2_400,
target_date: 12.months.from_now.to_date,
- accounts: [ primary ]
+ accounts: [ primary ],
+ allocations: { primary => 2_500 }
},
# active · no_target_date — secondary so progress doesn't auto-cap at 100%
{
name: "Emergency fund",
target: 30_000,
target_date: nil,
- accounts: [ secondary ]
+ accounts: [ secondary ],
+ allocations: { secondary => 3_000 }
},
# active · behind big — combined pools still well short of the target
{
@@ -1353,12 +1356,14 @@ class Demo::Generator
target: 500_000,
target_date: 24.months.from_now.to_date,
accounts: eligible.first(2),
+ allocations: { primary => 1_000, secondary => 2_000 },
pledges: [
{ account: primary, amount: 2_000, kind: "transfer", status: "open", expires_at: 4.days.from_now }
]
},
- # active · on_track — primary balance close to target, long horizon makes
- # the required monthly rate small enough for the demo's pace to cover
+ # active · on_track — the one goal left holding all of `primary`, so
+ # its balance stays close enough to the target for a 60-month pace to
+ # read as on track
{
name: "Long-term portfolio",
target: 200_000,
@@ -1371,7 +1376,8 @@ class Demo::Generator
name: "Tax prep buffer",
target: 1_200,
target_date: 2.months.ago.to_date,
- accounts: [ secondary ]
+ accounts: [ secondary ],
+ allocations: { secondary => 1_000 }
},
# AASM paused
{
@@ -1379,7 +1385,8 @@ class Demo::Generator
target: 15_000,
target_date: 18.months.from_now.to_date,
state: "paused",
- accounts: [ primary ]
+ accounts: [ primary ],
+ allocations: { primary => 1_000 }
},
# AASM archived
{
@@ -1387,7 +1394,8 @@ class Demo::Generator
target: 1_500,
target_date: 12.months.ago.to_date,
state: "archived",
- accounts: [ primary ]
+ accounts: [ primary ],
+ allocations: { primary => 500 }
},
# AASM completed
{
@@ -1395,7 +1403,8 @@ class Demo::Generator
target: 8_000,
target_date: 6.months.ago.to_date,
state: "completed",
- accounts: [ primary ]
+ accounts: [ primary ],
+ allocations: { primary => 8_000 }
}
]
@@ -1409,7 +1418,9 @@ class Demo::Generator
color: Goal::COLORS.sample,
state: goal_spec[:state] || "active"
)
- goal_spec[:accounts].uniq.each { |a| goal.goal_accounts.build(account: a) }
+ goal_spec[:accounts].uniq.each do |a|
+ goal.goal_accounts.build(account: a, allocated_amount: goal_spec.dig(:allocations, a))
+ end
goal.save!
wedding_goal = goal if goal_spec[:name] == "Wedding fund"
diff --git a/test/models/demo/generator_test.rb b/test/models/demo/generator_test.rb
index c096d3a10a..3cf99b4222 100644
--- a/test/models/demo/generator_test.rb
+++ b/test/models/demo/generator_test.rb
@@ -39,6 +39,22 @@ class Demo::GeneratorTest < ActiveSupport::TestCase
assert_equal 1, ApiKey.where(display_key: ApiKey::DEMO_MONITORING_KEY).count
end
+ # Regression for a `rake demo_data:default` crash: the goal-seeding matrix
+ # linked several active goals to the same account as a 100%-whole-account
+ # claim each, which violates GoalAccount#whole_account_link_must_be_exclusive
+ # the moment the second one tries to save.
+ test "generate_goals! seeds the full matrix without raising" do
+ @family.update!(currency: "USD")
+ @family.accounts.create!(accountable: Depository.new, name: "Primary Checking",
+ currency: @family.currency, balance: 150_000)
+ @family.accounts.create!(accountable: Depository.new, name: "Secondary Savings",
+ currency: @family.currency, balance: 10_000)
+
+ assert_difference "@family.goals.count", 9 do
+ Demo::Generator.new.send(:generate_goals!, @family)
+ end
+ end
+
private
def create_user!(family, email)
family.users.create!(