1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-23 07:09:39 +02:00
Maybe/test/models/balance_sheet_test.rb
Zach Gollwitzer d75be2282b
New Design System + Codebase Refresh (#1823)
Since the very first 0.1.0-alpha.1 release, we've been moving quickly to add new features to the Maybe app. In doing so, some parts of the codebase have become outdated, unnecessary, or overly-complex as a natural result of this feature prioritization.

Now that "core" Maybe is complete, we're moving into a second phase of development where we'll be working hard to improve the accuracy of existing features and build additional features on top of "core". This PR is a quick overhaul of the existing codebase aimed to:

- Establish the brand new and simplified dashboard view (pictured above)
- Establish and move towards the conventions introduced in Cursor rules and project design overview #1788
- Consolidate layouts and improve the performance of layout queries
- Organize the core models of the Maybe domain (i.e. Account::Entry, Account::Transaction, etc.) and break out specific traits of each model into dedicated concerns for better readability
- Remove stale / dead code from codebase
- Remove overly complex code paths in favor of simpler ones
2025-02-21 11:57:59 -05:00

83 lines
3.2 KiB
Ruby

require "test_helper"
class BalanceSheetTest < ActiveSupport::TestCase
setup do
@family = families(:empty)
end
test "calculates total assets" do
assert_equal 0, BalanceSheet.new(@family).total_assets
create_account(balance: 1000, accountable: Depository.new)
create_account(balance: 5000, accountable: OtherAsset.new)
create_account(balance: 10000, accountable: CreditCard.new) # ignored
assert_equal 1000 + 5000, BalanceSheet.new(@family).total_assets
end
test "calculates total liabilities" do
assert_equal 0, BalanceSheet.new(@family).total_liabilities
create_account(balance: 1000, accountable: CreditCard.new)
create_account(balance: 5000, accountable: OtherLiability.new)
create_account(balance: 10000, accountable: Depository.new) # ignored
assert_equal 1000 + 5000, BalanceSheet.new(@family).total_liabilities
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
assert_equal 10000, BalanceSheet.new(@family).total_assets
assert_equal 1000, BalanceSheet.new(@family).total_liabilities
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
asset_groups = BalanceSheet.new(@family).account_groups("asset")
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
liability_groups = BalanceSheet.new(@family).account_groups("liability")
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