2025-02-21 11:57:59 -05:00
|
|
|
require "test_helper"
|
|
|
|
|
|
|
|
class BalanceSheetTest < ActiveSupport::TestCase
|
|
|
|
setup do
|
|
|
|
@family = families(:empty)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "calculates total assets" do
|
2025-06-10 18:20:06 -04:00
|
|
|
assert_equal 0, BalanceSheet.new(@family).assets.total
|
2025-02-21 11:57:59 -05:00
|
|
|
|
|
|
|
create_account(balance: 1000, accountable: Depository.new)
|
|
|
|
create_account(balance: 5000, accountable: OtherAsset.new)
|
|
|
|
create_account(balance: 10000, accountable: CreditCard.new) # ignored
|
|
|
|
|
2025-06-10 18:20:06 -04:00
|
|
|
assert_equal 1000 + 5000, BalanceSheet.new(@family).assets.total
|
2025-02-21 11:57:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "calculates total liabilities" do
|
2025-06-10 18:20:06 -04:00
|
|
|
assert_equal 0, BalanceSheet.new(@family).liabilities.total
|
2025-02-21 11:57:59 -05:00
|
|
|
|
|
|
|
create_account(balance: 1000, accountable: CreditCard.new)
|
|
|
|
create_account(balance: 5000, accountable: OtherLiability.new)
|
|
|
|
create_account(balance: 10000, accountable: Depository.new) # ignored
|
|
|
|
|
2025-06-10 18:20:06 -04:00
|
|
|
assert_equal 1000 + 5000, BalanceSheet.new(@family).liabilities.total
|
2025-02-21 11:57:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "calculates net worth" do
|
|
|
|
assert_equal 0, BalanceSheet.new(@family).net_worth
|
|
|
|
|
|
|
|
create_account(balance: 1000, accountable: CreditCard.new)
|
|
|
|
create_account(balance: 50000, accountable: Depository.new)
|
|
|
|
|
|
|
|
assert_equal 50000 - 1000, BalanceSheet.new(@family).net_worth
|
|
|
|
end
|
|
|
|
|
|
|
|
test "disabled accounts do not affect totals" do
|
|
|
|
create_account(balance: 1000, accountable: CreditCard.new)
|
|
|
|
create_account(balance: 10000, accountable: Depository.new)
|
|
|
|
|
|
|
|
other_liability = create_account(balance: 5000, accountable: OtherLiability.new)
|
|
|
|
other_liability.update!(is_active: false)
|
|
|
|
|
|
|
|
assert_equal 10000 - 1000, BalanceSheet.new(@family).net_worth
|
2025-06-10 18:20:06 -04:00
|
|
|
assert_equal 10000, BalanceSheet.new(@family).assets.total
|
|
|
|
assert_equal 1000, BalanceSheet.new(@family).liabilities.total
|
2025-02-21 11:57:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "calculates asset group totals" do
|
|
|
|
create_account(balance: 1000, accountable: Depository.new)
|
|
|
|
create_account(balance: 2000, accountable: Depository.new)
|
|
|
|
create_account(balance: 3000, accountable: Investment.new)
|
|
|
|
create_account(balance: 5000, accountable: OtherAsset.new)
|
|
|
|
create_account(balance: 10000, accountable: CreditCard.new) # ignored
|
|
|
|
|
2025-06-10 18:20:06 -04:00
|
|
|
asset_groups = BalanceSheet.new(@family).assets.account_groups
|
2025-02-21 11:57:59 -05:00
|
|
|
|
|
|
|
assert_equal 3, asset_groups.size
|
|
|
|
assert_equal 1000 + 2000, asset_groups.find { |ag| ag.name == "Cash" }.total
|
|
|
|
assert_equal 3000, asset_groups.find { |ag| ag.name == "Investments" }.total
|
|
|
|
assert_equal 5000, asset_groups.find { |ag| ag.name == "Other Assets" }.total
|
|
|
|
end
|
|
|
|
|
|
|
|
test "calculates liability group totals" do
|
|
|
|
create_account(balance: 1000, accountable: CreditCard.new)
|
|
|
|
create_account(balance: 2000, accountable: CreditCard.new)
|
|
|
|
create_account(balance: 3000, accountable: OtherLiability.new)
|
|
|
|
create_account(balance: 5000, accountable: OtherLiability.new)
|
|
|
|
create_account(balance: 10000, accountable: Depository.new) # ignored
|
|
|
|
|
2025-06-10 18:20:06 -04:00
|
|
|
liability_groups = BalanceSheet.new(@family).liabilities.account_groups
|
2025-02-21 11:57:59 -05:00
|
|
|
|
|
|
|
assert_equal 2, liability_groups.size
|
|
|
|
assert_equal 1000 + 2000, liability_groups.find { |ag| ag.name == "Credit Cards" }.total
|
|
|
|
assert_equal 3000 + 5000, liability_groups.find { |ag| ag.name == "Other Liabilities" }.total
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def create_account(attributes = {})
|
|
|
|
account = @family.accounts.create! name: "Test", currency: "USD", **attributes
|
|
|
|
account
|
|
|
|
end
|
|
|
|
end
|