1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-05 13:35:21 +02:00

Net worth calculation (#508)

* Add classification generated column to account

* Add basic net worth calculation

* Add net worth tests

* Fix lint errors
This commit is contained in:
Zach Gollwitzer 2024-03-04 08:31:22 -05:00 committed by GitHub
parent 19f15e9391
commit facd74f733
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 156 additions and 40 deletions

View file

@ -3,7 +3,7 @@ require "test_helper"
class AccountsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@account = accounts(:generic)
@account = accounts(:checking)
end
test "new" do

View file

@ -3,7 +3,7 @@ require "test_helper"
class ValuationsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@account = accounts(:generic)
@account = accounts(:checking)
end
test "new" do

View file

@ -1,29 +1,27 @@
# No transactions, no valuations, just a generic account
generic:
family: dylan_family
name: No history, generic account
balance: 1200
# Account with only valuations
collectable:
family: dylan_family
name: Collectable Account
balance: 550
accountable_type: Account::OtherAsset
# Account with only transactions
checking:
family: dylan_family
name: Checking Account
balance: 5000
accountable_type: Account::Depository
# Account with both transactions and valuations
savings_with_valuation_overrides:
family: dylan_family
name: Savings account with valuation overrides
balance: 20000
accountable_type: Account::Depository
# Liability account
credit_card:
family: dylan_family
name: Credit Card
balance: 1000
accountable_type: Account::Credit

View file

@ -2,27 +2,80 @@ require "test_helper"
class FamilyTest < ActiveSupport::TestCase
def setup
@dylan_family = families(:dylan_family)
@family = families(:dylan_family)
@family.accounts.each do |account|
account.accountable = account.classification == "asset" ? account_other_assets(:one) : account_other_liabilities(:one)
account.sync
end
end
test "should have many users" do
assert @dylan_family.users.size > 0
assert @dylan_family.users.include?(users(:family_admin))
assert @family.users.size > 0
assert @family.users.include?(users(:family_admin))
end
test "should have many accounts" do
assert @dylan_family.accounts.size > 0
assert @family.accounts.size > 0
end
test "should destroy dependent users" do
assert_difference("User.count", -@dylan_family.users.count) do
@dylan_family.destroy
assert_difference("User.count", -@family.users.count) do
@family.destroy
end
end
test "should destroy dependent accounts" do
assert_difference("Account.count", -@dylan_family.accounts.count) do
@dylan_family.destroy
assert_difference("Account.count", -@family.accounts.count) do
@family.destroy
end
end
test "should calculate total assets" do
assert_equal BigDecimal("25550"), @family.assets
end
test "should calculate total liabilities" do
assert_equal BigDecimal("1000"), @family.liabilities
end
test "should calculate net worth" do
assert_equal BigDecimal("24550"), @family.net_worth
end
test "calculates asset series" do
# Sum of expected balances for all asset accounts in balance_calculator_test.rb
expected_balances = [
25650, 26135, 26135, 26135, 26135, 25385, 25385, 25385, 26460, 26460,
26460, 26460, 24460, 24460, 24460, 24440, 24440, 24440, 25210, 25210,
25210, 25210, 25210, 25210, 25210, 25400, 25250, 26050, 26050, 26050,
25550
].map(&:to_d)
assert_equal expected_balances, @family.asset_series.data.map { |b| b[:value].amount }
end
test "calculates liability series" do
# Sum of expected balances for all liability accounts in balance_calculator_test.rb
expected_balances = [
1040, 940, 940, 940, 940, 940, 940, 940, 940, 940,
940, 940, 940, 940, 940, 960, 960, 960, 990, 990,
990, 990, 990, 990, 990, 1000, 1000, 1000, 1000, 1000,
1000
].map(&:to_d)
assert_equal expected_balances, @family.liability_series.data.map { |b| b[:value].amount }
end
test "calculates net worth" do
# Net difference between asset and liability series above
expected_balances = [
24610, 25195, 25195, 25195, 25195, 24445, 24445, 24445, 25520, 25520,
25520, 25520, 23520, 23520, 23520, 23480, 23480, 23480, 24220, 24220,
24220, 24220, 24220, 24220, 24220, 24400, 24250, 25050, 25050, 25050,
24550
].map(&:to_d)
assert_equal expected_balances, @family.net_worth_series.data.map { |b| b[:value].amount }
end
end