-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathincome_statement_test.rb
More file actions
619 lines (494 loc) · 27.2 KB
/
Copy pathincome_statement_test.rb
File metadata and controls
619 lines (494 loc) · 27.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
require "test_helper"
class IncomeStatementTest < ActiveSupport::TestCase
include EntriesTestHelper
setup do
@family = families(:empty)
@income_category = @family.categories.create! name: "Income"
@food_category = @family.categories.create! name: "Food"
@groceries_category = @family.categories.create! name: "Groceries", parent: @food_category
@checking_account = @family.accounts.create! name: "Checking", currency: @family.currency, balance: 5000, accountable: Depository.new
@credit_card_account = @family.accounts.create! name: "Credit Card", currency: @family.currency, balance: 1000, accountable: CreditCard.new
@loan_account = @family.accounts.create! name: "Mortgage", currency: @family.currency, balance: 50000, accountable: Loan.new
create_transaction(account: @checking_account, amount: -1000, category: @income_category)
create_transaction(account: @checking_account, amount: 200, category: @groceries_category)
create_transaction(account: @credit_card_account, amount: 300, category: @groceries_category)
create_transaction(account: @credit_card_account, amount: 400, category: @groceries_category)
end
test "calculates totals for transactions" do
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
assert_equal Money.new(1000, @family.currency), totals.income_money
assert_equal Money.new(200 + 300 + 400, @family.currency), totals.expense_money
assert_equal 4, totals.transactions_count
end
test "calculates expenses for a period" do
income_statement = IncomeStatement.new(@family)
expense_totals = income_statement.expense_totals(period: Period.last_30_days)
expected_total_expense = 200 + 300 + 400
assert_equal expected_total_expense, expense_totals.total
assert_equal expected_total_expense, expense_totals.category_totals.find { |ct| ct.category.id == @groceries_category.id }.total
assert_equal expected_total_expense, expense_totals.category_totals.find { |ct| ct.category.id == @food_category.id }.total
end
test "calculates income for a period" do
income_statement = IncomeStatement.new(@family)
income_totals = income_statement.income_totals(period: Period.last_30_days)
expected_total_income = 1000
assert_equal expected_total_income, income_totals.total
assert_equal expected_total_income, income_totals.category_totals.find { |ct| ct.category.id == @income_category.id }.total
end
test "calculates median expense" do
income_statement = IncomeStatement.new(@family)
assert_equal 200 + 300 + 400, income_statement.expense_totals(period: Period.last_30_days).total
end
test "calculates median income" do
income_statement = IncomeStatement.new(@family)
assert_equal 1000, income_statement.income_totals(period: Period.last_30_days).total
end
# NEW TESTS: Statistical Methods
test "calculates median expense correctly with known dataset" do
# Clear existing transactions by deleting entries
Entry.joins(:account).where(accounts: { family_id: @family.id }).destroy_all
# Create expenses: 100, 200, 300, 400, 500 (median should be 300)
create_transaction(account: @checking_account, amount: 100, category: @groceries_category)
create_transaction(account: @checking_account, amount: 200, category: @groceries_category)
create_transaction(account: @checking_account, amount: 300, category: @groceries_category)
create_transaction(account: @checking_account, amount: 400, category: @groceries_category)
create_transaction(account: @checking_account, amount: 500, category: @groceries_category)
income_statement = IncomeStatement.new(@family)
# CORRECT BUSINESS LOGIC: Calculates median of time-period totals for budget planning
# All transactions in same month = monthly total of 1500, so median = 1500.0
assert_equal 1500.0, income_statement.median_expense(interval: "month")
end
test "calculates median income correctly with known dataset" do
# Clear existing transactions by deleting entries
Entry.joins(:account).where(accounts: { family_id: @family.id }).destroy_all
# Create income: -200, -300, -400, -500, -600 (median should be -400, displayed as 400)
create_transaction(account: @checking_account, amount: -200, category: @income_category)
create_transaction(account: @checking_account, amount: -300, category: @income_category)
create_transaction(account: @checking_account, amount: -400, category: @income_category)
create_transaction(account: @checking_account, amount: -500, category: @income_category)
create_transaction(account: @checking_account, amount: -600, category: @income_category)
income_statement = IncomeStatement.new(@family)
# CORRECT BUSINESS LOGIC: Calculates median of time-period totals for budget planning
# All transactions in same month = monthly total of -2000, so median = 2000.0
assert_equal 2000.0, income_statement.median_income(interval: "month")
end
test "calculates average expense correctly with known dataset" do
# Clear existing transactions by deleting entries
Entry.joins(:account).where(accounts: { family_id: @family.id }).destroy_all
# Create expenses: 100, 200, 300 (average should be 200)
create_transaction(account: @checking_account, amount: 100, category: @groceries_category)
create_transaction(account: @checking_account, amount: 200, category: @groceries_category)
create_transaction(account: @checking_account, amount: 300, category: @groceries_category)
income_statement = IncomeStatement.new(@family)
# CORRECT BUSINESS LOGIC: Calculates average of time-period totals for budget planning
# All transactions in same month = monthly total of 600, so average = 600.0
assert_equal 600.0, income_statement.avg_expense(interval: "month")
end
test "calculates category-specific median expense" do
# Clear existing transactions by deleting entries
Entry.joins(:account).where(accounts: { family_id: @family.id }).destroy_all
# Create different amounts for groceries vs other food
other_food_category = @family.categories.create! name: "Restaurants", parent: @food_category
# Groceries: 100, 300, 500 (median = 300)
create_transaction(account: @checking_account, amount: 100, category: @groceries_category)
create_transaction(account: @checking_account, amount: 300, category: @groceries_category)
create_transaction(account: @checking_account, amount: 500, category: @groceries_category)
# Restaurants: 50, 150 (median = 100)
create_transaction(account: @checking_account, amount: 50, category: other_food_category)
create_transaction(account: @checking_account, amount: 150, category: other_food_category)
income_statement = IncomeStatement.new(@family)
# CORRECT BUSINESS LOGIC: Calculates median of time-period totals for budget planning
# All groceries in same month = monthly total of 900, so median = 900.0
assert_equal 900.0, income_statement.median_expense(interval: "month", category: @groceries_category)
# For restaurants: monthly total = 200, so median = 200.0
restaurants_median = income_statement.median_expense(interval: "month", category: other_food_category)
assert_equal 200.0, restaurants_median
end
test "calculates category-specific average expense" do
# Clear existing transactions by deleting entries
Entry.joins(:account).where(accounts: { family_id: @family.id }).destroy_all
# Create different amounts for groceries
# Groceries: 100, 200, 300 (average = 200)
create_transaction(account: @checking_account, amount: 100, category: @groceries_category)
create_transaction(account: @checking_account, amount: 200, category: @groceries_category)
create_transaction(account: @checking_account, amount: 300, category: @groceries_category)
income_statement = IncomeStatement.new(@family)
# CORRECT BUSINESS LOGIC: Calculates average of time-period totals for budget planning
# All transactions in same month = monthly total of 600, so average = 600.0
assert_equal 600.0, income_statement.avg_expense(interval: "month", category: @groceries_category)
end
# NEW TESTS: Transfer and Kind Filtering
# NOTE: These tests now pass because kind filtering is working after the refactoring!
test "excludes regular transfers from income statement calculations" do
# Create a regular transfer between accounts
outflow_transaction = create_transaction(account: @checking_account, amount: 500, kind: "funds_movement")
inflow_transaction = create_transaction(account: @credit_card_account, amount: -500, kind: "funds_movement")
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# NOW WORKING: Excludes transfers correctly after refactoring
assert_equal 4, totals.transactions_count # Only original 4 transactions
assert_equal Money.new(1000, @family.currency), totals.income_money
assert_equal Money.new(900, @family.currency), totals.expense_money
end
test "includes loan payments as expenses in income statement" do
# Create a loan payment transaction
loan_payment = create_transaction(account: @checking_account, amount: 1000, category: nil, kind: "loan_payment")
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# CONTINUES TO WORK: Includes loan payments as expenses (loan_payment not in exclusion list)
assert_equal 5, totals.transactions_count
assert_equal Money.new(1000, @family.currency), totals.income_money
assert_equal Money.new(1900, @family.currency), totals.expense_money # 900 + 1000
end
test "includes one-time transactions in income statement totals and reports" do
create_transaction(account: @checking_account, amount: 250, category: @groceries_category, kind: "one_time")
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
assert_equal 5, totals.transactions_count
assert_equal Money.new(1000, @family.currency), totals.income_money
assert_equal Money.new(1150, @family.currency), totals.expense_money
expense_totals = income_statement.expense_totals(period: Period.last_30_days)
assert_equal 200 + 300 + 400 + 250, expense_totals.total
end
test "excludes one-time transactions from budget-scoped period totals" do
create_transaction(account: @checking_account, amount: 250, category: @groceries_category, kind: "one_time")
income_statement = IncomeStatement.new(@family)
expense_totals = income_statement.expense_totals(period: Period.last_30_days, for_budget: true)
assert_equal 200 + 300 + 400, expense_totals.total
end
test "excludes payment transactions from income statement calculations" do
# Create a payment transaction (credit card payment)
payment_transaction = create_transaction(account: @checking_account, amount: 300, category: nil, kind: "cc_payment")
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# NOW WORKING: Excludes payment transactions correctly after refactoring
assert_equal 4, totals.transactions_count # Only original 4 transactions
assert_equal Money.new(1000, @family.currency), totals.income_money
assert_equal Money.new(900, @family.currency), totals.expense_money
end
test "excludes excluded transactions from income statement calculations" do
# Create an excluded transaction
excluded_transaction_entry = create_transaction(account: @checking_account, amount: 250, category: @groceries_category)
excluded_transaction_entry.update!(excluded: true)
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# Should exclude excluded transactions
assert_equal 4, totals.transactions_count # Only original 4 transactions
assert_equal Money.new(1000, @family.currency), totals.income_money
assert_equal Money.new(900, @family.currency), totals.expense_money
end
# NEW TESTS: Interval-Based Calculations
test "different intervals return different statistical results with multi-period data" do
# Clear existing transactions
Entry.joins(:account).where(accounts: { family_id: @family.id }).destroy_all
# Create transactions across multiple weeks to test interval behavior
# Week 1: 100, 200 (total: 300, median: 150)
create_transaction(account: @checking_account, amount: 100, category: @groceries_category, date: 3.weeks.ago)
create_transaction(account: @checking_account, amount: 200, category: @groceries_category, date: 3.weeks.ago + 1.day)
# Week 2: 400, 600 (total: 1000, median: 500)
create_transaction(account: @checking_account, amount: 400, category: @groceries_category, date: 2.weeks.ago)
create_transaction(account: @checking_account, amount: 600, category: @groceries_category, date: 2.weeks.ago + 1.day)
# Week 3: 800 (total: 800, median: 800)
create_transaction(account: @checking_account, amount: 800, category: @groceries_category, date: 1.week.ago)
income_statement = IncomeStatement.new(@family)
month_median = income_statement.median_expense(interval: "month")
week_median = income_statement.median_expense(interval: "week")
# CRITICAL TEST: Different intervals should return different results
# Month interval: median of monthly totals (if all in same month) vs individual transactions
# Week interval: median of weekly totals [300, 1000, 800] = 800 vs individual transactions [100,200,400,600,800] = 400
refute_equal month_median, week_median, "Different intervals should return different statistical results when data spans multiple time periods"
# Both should still be numeric
assert month_median.is_a?(Numeric)
assert week_median.is_a?(Numeric)
assert month_median > 0
assert week_median > 0
end
# NEW TESTS: Edge Cases
test "handles empty dataset gracefully" do
# Create a truly empty family
empty_family = Family.create!(name: "Empty Test Family", currency: "USD")
income_statement = IncomeStatement.new(empty_family)
# Should return 0 for statistical measures
assert_equal 0, income_statement.median_expense(interval: "month")
assert_equal 0, income_statement.median_income(interval: "month")
assert_equal 0, income_statement.avg_expense(interval: "month")
end
test "handles category not found gracefully" do
nonexistent_category = Category.new(id: 99999, name: "Nonexistent")
income_statement = IncomeStatement.new(@family)
assert_equal 0, income_statement.median_expense(interval: "month", category: nonexistent_category)
assert_equal 0, income_statement.avg_expense(interval: "month", category: nonexistent_category)
end
test "handles transactions without categories" do
# Create transaction without category
create_transaction(account: @checking_account, amount: 150, category: nil)
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# Should still include uncategorized transaction in totals
assert_equal 5, totals.transactions_count
assert_equal Money.new(1050, @family.currency), totals.expense_money # 900 + 150
end
test "includes investment_contribution transactions as expenses in income statement" do
# Create a transfer to investment account (marked as investment_contribution)
investment_contribution = create_transaction(
account: @checking_account,
amount: 1000,
category: nil,
kind: "investment_contribution"
)
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# investment_contribution should be included as an expense (visible in cashflow)
assert_equal 5, totals.transactions_count # Original 4 + investment_contribution
assert_equal Money.new(1000, @family.currency), totals.income_money
assert_equal Money.new(1900, @family.currency), totals.expense_money # 900 + 1000 investment
end
test "includes provider-imported investment_contribution inflows as expenses" do
# Simulates a 401k contribution that was auto-deducted from payroll
# Provider imports this as an inflow to the investment account (negative amount)
# but it should still appear as an expense in cashflow
investment_account = @family.accounts.create!(
name: "401k",
currency: @family.currency,
balance: 10000,
accountable: Investment.new
)
# Provider-imported contribution shows as inflow (negative amount) to the investment account
# kind is investment_contribution, which should be treated as expense regardless of sign
provider_contribution = create_transaction(
account: investment_account,
amount: -500, # Negative = inflow to account
category: nil,
kind: "investment_contribution"
)
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# The provider-imported contribution should appear as an expense
assert_equal 5, totals.transactions_count # Original 4 + provider contribution
assert_equal Money.new(1000, @family.currency), totals.income_money
assert_equal Money.new(1400, @family.currency), totals.expense_money # 900 + 500 (abs of -500)
end
# Tax-Advantaged Account Exclusion Tests
test "excludes transactions from tax-advantaged Roth IRA accounts" do
# Create a Roth IRA (tax-exempt) investment account
roth_ira = @family.accounts.create!(
name: "Roth IRA",
currency: @family.currency,
balance: 50000,
accountable: Investment.new(subtype: "roth_ira")
)
# Create a dividend transaction in the Roth IRA
# This should NOT appear in budget totals
create_transaction(account: roth_ira, amount: -200, category: @income_category)
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# The Roth IRA dividend should be excluded
assert_equal 4, totals.transactions_count # Only original 4 transactions
assert_equal Money.new(1000, @family.currency), totals.income_money
assert_equal Money.new(900, @family.currency), totals.expense_money
end
test "excludes transactions from tax-deferred 401k accounts" do
# Create a 401k (tax-deferred) investment account
account_401k = @family.accounts.create!(
name: "Company 401k",
currency: @family.currency,
balance: 100000,
accountable: Investment.new(subtype: "401k")
)
# Create a dividend transaction in the 401k
# This should NOT appear in budget totals
create_transaction(account: account_401k, amount: -500, category: @income_category)
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# The 401k dividend should be excluded
assert_equal 4, totals.transactions_count
assert_equal Money.new(1000, @family.currency), totals.income_money
assert_equal Money.new(900, @family.currency), totals.expense_money
end
test "includes transactions from taxable brokerage accounts" do
# Create a taxable brokerage account
brokerage = @family.accounts.create!(
name: "Brokerage",
currency: @family.currency,
balance: 25000,
accountable: Investment.new(subtype: "brokerage")
)
# Create a dividend transaction in the taxable account
# This SHOULD appear in budget totals
create_transaction(account: brokerage, amount: -300, category: @income_category)
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# The brokerage dividend SHOULD be included
assert_equal 5, totals.transactions_count
assert_equal Money.new(1300, @family.currency), totals.income_money # 1000 + 300
assert_equal Money.new(900, @family.currency), totals.expense_money
end
test "includes transactions from default taxable crypto accounts" do
# Create a crypto account (default taxable)
crypto_account = @family.accounts.create!(
name: "Coinbase",
currency: @family.currency,
balance: 5000,
accountable: Crypto.new
)
# Create a transaction in the crypto account
create_transaction(account: crypto_account, amount: 100, category: @groceries_category)
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# Crypto transaction SHOULD be included (default is taxable)
assert_equal 5, totals.transactions_count
assert_equal Money.new(1000, @family.currency), totals.expense_money # 900 + 100
end
test "excludes transactions from tax-deferred crypto accounts" do
# Create a crypto account in a tax-deferred retirement account
crypto_in_ira = @family.accounts.create!(
name: "Crypto IRA",
currency: @family.currency,
balance: 10000,
accountable: Crypto.new(tax_treatment: "tax_deferred")
)
# Create a transaction in the tax-deferred crypto account
create_transaction(account: crypto_in_ira, amount: 250, category: @groceries_category)
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# The tax-deferred crypto transaction should be excluded
assert_equal 4, totals.transactions_count
assert_equal Money.new(900, @family.currency), totals.expense_money
end
test "family.tax_advantaged_account_ids returns correct accounts" do
# Create various accounts
roth_ira = @family.accounts.create!(
name: "Roth IRA",
currency: @family.currency,
balance: 50000,
accountable: Investment.new(subtype: "roth_ira")
)
traditional_ira = @family.accounts.create!(
name: "Traditional IRA",
currency: @family.currency,
balance: 30000,
accountable: Investment.new(subtype: "ira")
)
brokerage = @family.accounts.create!(
name: "Brokerage",
currency: @family.currency,
balance: 25000,
accountable: Investment.new(subtype: "brokerage")
)
crypto_taxable = @family.accounts.create!(
name: "Crypto Taxable",
currency: @family.currency,
balance: 5000,
accountable: Crypto.new
)
crypto_deferred = @family.accounts.create!(
name: "Crypto IRA",
currency: @family.currency,
balance: 10000,
accountable: Crypto.new(tax_treatment: "tax_deferred")
)
# Clear the memoized value
@family.instance_variable_set(:@tax_advantaged_account_ids, nil)
tax_advantaged_ids = @family.tax_advantaged_account_ids
# Should include Roth IRA, Traditional IRA, and tax-deferred Crypto
assert_includes tax_advantaged_ids, roth_ira.id
assert_includes tax_advantaged_ids, traditional_ira.id
assert_includes tax_advantaged_ids, crypto_deferred.id
# Should NOT include taxable accounts
refute_includes tax_advantaged_ids, brokerage.id
refute_includes tax_advantaged_ids, crypto_taxable.id
# Should NOT include non-investment accounts
refute_includes tax_advantaged_ids, @checking_account.id
refute_includes tax_advantaged_ids, @credit_card_account.id
end
# net_category_totals tests
test "net_category_totals nets expense and refund in the same category" do
Entry.joins(:account).where(accounts: { family_id: @family.id }).destroy_all
# $200 expense and $50 refund both on Food
create_transaction(account: @checking_account, amount: 200, category: @food_category)
create_transaction(account: @checking_account, amount: -50, category: @food_category)
net = IncomeStatement.new(@family).net_category_totals(period: Period.last_30_days)
assert_equal 150, net.total_net_expense
assert_equal 0, net.total_net_income
food_net = net.net_expense_categories.find { |ct| ct.category.id == @food_category.id }
assert_not_nil food_net
assert_equal 150, food_net.total
assert_in_delta 100.0, food_net.weight, 0.1
end
test "net_category_totals places category on income side when refunds exceed expenses" do
Entry.joins(:account).where(accounts: { family_id: @family.id }).destroy_all
# $100 expense but $250 refund on Food => net income of 150
create_transaction(account: @checking_account, amount: 100, category: @food_category)
create_transaction(account: @checking_account, amount: -250, category: @food_category)
net = IncomeStatement.new(@family).net_category_totals(period: Period.last_30_days)
assert_equal 0, net.total_net_expense
assert_equal 150, net.total_net_income
food_net = net.net_income_categories.find { |ct| ct.category.id == @food_category.id }
assert_not_nil food_net
assert_equal 150, food_net.total
# Should not appear on expense side
assert_nil net.net_expense_categories.find { |ct| ct.category.id == @food_category.id }
end
test "empty account_ids returns no results for category stats" do
results = IncomeStatement::CategoryStats.new(@family, account_ids: []).call
assert_empty results
end
test "empty account_ids returns no results for family stats" do
results = IncomeStatement::FamilyStats.new(@family, account_ids: []).call
assert_empty results
end
test "returns zero totals when family has only tax-advantaged accounts" do
# Create a fresh family with ONLY tax-advantaged accounts
family_only_retirement = Family.create!(
name: "Retirement Only Family",
currency: "USD",
locale: "en",
date_format: "%Y-%m-%d"
)
# Create a 401k account (tax-deferred)
retirement_account = family_only_retirement.accounts.create!(
name: "401k",
currency: "USD",
balance: 100000,
accountable: Investment.new(subtype: "401k")
)
# Create a Roth IRA account (tax-exempt)
roth_account = family_only_retirement.accounts.create!(
name: "Roth IRA",
currency: "USD",
balance: 50000,
accountable: Investment.new(subtype: "roth_ira")
)
# Add transactions to these accounts (would normally be contributions/trades)
# Using standard kind to simulate transactions that would normally appear
Entry.create!(
account: retirement_account,
name: "401k Contribution",
date: 5.days.ago,
amount: 500,
currency: "USD",
entryable: Transaction.new(kind: "standard")
)
Entry.create!(
account: roth_account,
name: "Roth IRA Contribution",
date: 3.days.ago,
amount: 200,
currency: "USD",
entryable: Transaction.new(kind: "standard")
)
# Verify the accounts are correctly identified as tax-advantaged
tax_advantaged_ids = family_only_retirement.tax_advantaged_account_ids
assert_equal 2, tax_advantaged_ids.count
assert_includes tax_advantaged_ids, retirement_account.id
assert_includes tax_advantaged_ids, roth_account.id
# Get income statement totals
income_statement = IncomeStatement.new(family_only_retirement)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# All transactions should be excluded, resulting in zero totals
assert_equal 0, totals.transactions_count
assert_equal Money.new(0, "USD"), totals.income_money
assert_equal Money.new(0, "USD"), totals.expense_money
end
end