mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Dashboard View and Calculations (#521)
* Handle Turbo updates with tabs Fixes #491 * Add Filterable concern for controllers * Add trendline chart * Extract common UI to partials * Series refactor * Put placeholders for calculations in * Add classification generated column to account * Add basic net worth calculation * Add net worth tests * Get net worth graph working * Fix lint errors * Implement asset grouping query * Make trends and series more intuitive * Fully functional dashboard * Remove logging
This commit is contained in:
parent
680a91d807
commit
6f0e410684
37 changed files with 594 additions and 74 deletions
|
@ -3,7 +3,6 @@ require "test_helper"
|
|||
class Account::BalanceCalculatorTest < ActiveSupport::TestCase
|
||||
test "syncs account with only valuations" do
|
||||
account = accounts(:collectable)
|
||||
account.accountable = account_other_assets(:one)
|
||||
|
||||
daily_balances = Account::BalanceCalculator.new(account).daily_balances
|
||||
|
||||
|
@ -19,7 +18,6 @@ class Account::BalanceCalculatorTest < ActiveSupport::TestCase
|
|||
|
||||
test "syncs account with only transactions" do
|
||||
account = accounts(:checking)
|
||||
account.accountable = account_depositories(:checking)
|
||||
|
||||
daily_balances = Account::BalanceCalculator.new(account).daily_balances
|
||||
|
||||
|
@ -35,7 +33,6 @@ class Account::BalanceCalculatorTest < ActiveSupport::TestCase
|
|||
|
||||
test "syncs account with both valuations and transactions" do
|
||||
account = accounts(:savings_with_valuation_overrides)
|
||||
account.accountable = account_depositories(:savings)
|
||||
daily_balances = Account::BalanceCalculator.new(account).daily_balances
|
||||
|
||||
expected_balances = [
|
||||
|
@ -50,7 +47,6 @@ class Account::BalanceCalculatorTest < ActiveSupport::TestCase
|
|||
|
||||
test "syncs liability account" do
|
||||
account = accounts(:credit_card)
|
||||
account.accountable = account_credits(:one)
|
||||
daily_balances = Account::BalanceCalculator.new(account).daily_balances
|
||||
|
||||
expected_balances = [
|
||||
|
|
|
@ -3,14 +3,12 @@ require "test_helper"
|
|||
class Account::SyncableTest < ActiveSupport::TestCase
|
||||
test "account has no balances until synced" do
|
||||
account = accounts(:savings_with_valuation_overrides)
|
||||
account.accountable = account_depositories(:savings)
|
||||
|
||||
assert_equal 0, account.balances.count
|
||||
end
|
||||
|
||||
test "account has balances after syncing" do
|
||||
account = accounts(:savings_with_valuation_overrides)
|
||||
account.accountable = account_depositories(:savings)
|
||||
account.sync
|
||||
|
||||
assert_equal 31, account.balances.count
|
||||
|
@ -18,7 +16,6 @@ class Account::SyncableTest < ActiveSupport::TestCase
|
|||
|
||||
test "stale balances are purged after syncing" do
|
||||
account = accounts(:savings_with_valuation_overrides)
|
||||
account.accountable = account_depositories(:savings)
|
||||
|
||||
# Create old, stale balances that should be purged (since they are before account start date)
|
||||
account.balances.create!(date: 1.year.ago, balance: 1000)
|
||||
|
|
|
@ -2,9 +2,7 @@ require "test_helper"
|
|||
|
||||
class AccountTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
depository = account_depositories(:checking)
|
||||
@account = accounts(:checking)
|
||||
@account.accountable = depository
|
||||
end
|
||||
|
||||
test "new account should be valid" do
|
||||
|
|
|
@ -5,7 +5,6 @@ class FamilyTest < ActiveSupport::TestCase
|
|||
@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
|
||||
|
@ -78,4 +77,63 @@ class FamilyTest < ActiveSupport::TestCase
|
|||
|
||||
assert_equal expected_balances, @family.net_worth_series.data.map { |b| b[:value].amount }
|
||||
end
|
||||
|
||||
test "calculates balances by type" do
|
||||
verify_balances_by_type(
|
||||
period: Period.all,
|
||||
expected_asset_total: BigDecimal("25550"),
|
||||
expected_liability_total: BigDecimal("1000"),
|
||||
expected_asset_groups: {
|
||||
"Account::OtherAsset" => { end_balance: BigDecimal("550"), start_balance: BigDecimal("400"), allocation: 2.15 },
|
||||
"Account::Depository" => { end_balance: BigDecimal("25000"), start_balance: BigDecimal("25250"), allocation: 97.85 }
|
||||
},
|
||||
expected_liability_groups: {
|
||||
"Account::Credit" => { end_balance: BigDecimal("1000"), start_balance: BigDecimal("1040"), allocation: 100 }
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
test "calculates balances by type with a date range filter" do
|
||||
verify_balances_by_type(
|
||||
period: Period.new(name: "custom", date_range: 7.days.ago.to_date..2.days.ago.to_date),
|
||||
expected_asset_total: BigDecimal("26050"),
|
||||
expected_liability_total: BigDecimal("1000"),
|
||||
expected_asset_groups: {
|
||||
"Account::OtherAsset" => { end_balance: BigDecimal("550"), start_balance: BigDecimal("700"), allocation: 2.11 },
|
||||
"Account::Depository" => { end_balance: BigDecimal("25500"), start_balance: BigDecimal("24510"), allocation: 97.89 }
|
||||
},
|
||||
expected_liability_groups: {
|
||||
"Account::Credit" => { end_balance: BigDecimal("1000"), start_balance: BigDecimal("990"), allocation: 100 }
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def verify_balances_by_type(period:, expected_asset_total:, expected_liability_total:, expected_asset_groups:, expected_liability_groups:)
|
||||
result = @family.accounts.by_group(period)
|
||||
|
||||
asset_total = result[:asset][:total]
|
||||
liability_total = result[:liability][:total]
|
||||
|
||||
assert_equal expected_asset_total, asset_total
|
||||
assert_equal expected_liability_total, liability_total
|
||||
|
||||
asset_groups = result[:asset][:groups]
|
||||
liability_groups = result[:liability][:groups]
|
||||
|
||||
assert_equal expected_asset_groups.keys, asset_groups.keys
|
||||
expected_asset_groups.each do |type, expected_values|
|
||||
assert_equal expected_values[:end_balance], asset_groups[type][:end_balance]
|
||||
assert_equal expected_values[:start_balance], asset_groups[type][:start_balance]
|
||||
assert_equal expected_values[:allocation], asset_groups[type][:allocation]
|
||||
end
|
||||
|
||||
assert_equal expected_liability_groups.keys, liability_groups.keys
|
||||
expected_liability_groups.each do |type, expected_values|
|
||||
assert_equal expected_values[:end_balance], liability_groups[type][:end_balance]
|
||||
assert_equal expected_values[:start_balance], liability_groups[type][:start_balance]
|
||||
assert_equal expected_values[:allocation], liability_groups[type][:allocation]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
37
test/models/trend_test.rb
Normal file
37
test/models/trend_test.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
require "test_helper"
|
||||
|
||||
class TrendTest < ActiveSupport::TestCase
|
||||
test "up" do
|
||||
trend = Trend.new(current: 100, previous: 50)
|
||||
assert_equal "up", trend.direction
|
||||
end
|
||||
|
||||
test "down" do
|
||||
trend = Trend.new(current: 50, previous: 100)
|
||||
assert_equal "down", trend.direction
|
||||
end
|
||||
|
||||
test "flat" do
|
||||
trend = Trend.new(current: 100, previous: 100)
|
||||
assert_equal "flat", trend.direction
|
||||
end
|
||||
|
||||
test "infinitely up" do
|
||||
trend1 = Trend.new(current: 100, previous: nil)
|
||||
trend2 = Trend.new(current: 100, previous: 0)
|
||||
assert_equal "up", trend1.direction
|
||||
assert_equal "up", trend2.direction
|
||||
end
|
||||
|
||||
test "infinitely down" do
|
||||
trend1 = Trend.new(current: nil, previous: 100)
|
||||
trend2 = Trend.new(current: 0, previous: 100)
|
||||
assert_equal "down", trend1.direction
|
||||
assert_equal "down", trend2.direction
|
||||
end
|
||||
|
||||
test "empty" do
|
||||
trend = Trend.new
|
||||
assert_equal "flat", trend.direction
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue