mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Account:: namespace simplifications and cleanup (#2110)
* Flatten Holding model * Flatten balance model * Entries domain renames * Fix valuations reference * Fix trades stream * Fix brakeman warnings * Fix tests * Replace existing entryable type references in DB
This commit is contained in:
parent
f181ba941f
commit
e657c40d19
172 changed files with 1297 additions and 1258 deletions
|
@ -1,74 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class Account::Balance::ForwardCalculatorTest < ActiveSupport::TestCase
|
||||
include Account::EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@account = families(:empty).accounts.create!(
|
||||
name: "Test",
|
||||
balance: 20000,
|
||||
cash_balance: 20000,
|
||||
currency: "USD",
|
||||
accountable: Investment.new
|
||||
)
|
||||
end
|
||||
|
||||
# When syncing forwards, we don't care about the account balance. We generate everything based on entries, starting from 0.
|
||||
test "no entries sync" do
|
||||
assert_equal 0, @account.balances.count
|
||||
|
||||
expected = [ 0, 0 ]
|
||||
calculated = Account::Balance::ForwardCalculator.new(@account).calculate
|
||||
|
||||
assert_equal expected, calculated.map(&:balance)
|
||||
end
|
||||
|
||||
test "valuations sync" do
|
||||
create_valuation(account: @account, date: 4.days.ago.to_date, amount: 17000)
|
||||
create_valuation(account: @account, date: 2.days.ago.to_date, amount: 19000)
|
||||
|
||||
expected = [ 0, 17000, 17000, 19000, 19000, 19000 ]
|
||||
calculated = Account::Balance::ForwardCalculator.new(@account).calculate.sort_by(&:date).map(&:balance)
|
||||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
|
||||
test "transactions sync" do
|
||||
create_transaction(account: @account, date: 4.days.ago.to_date, amount: -500) # income
|
||||
create_transaction(account: @account, date: 2.days.ago.to_date, amount: 100) # expense
|
||||
|
||||
expected = [ 0, 500, 500, 400, 400, 400 ]
|
||||
calculated = Account::Balance::ForwardCalculator.new(@account).calculate.sort_by(&:date).map(&:balance)
|
||||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
|
||||
test "multi-entry sync" do
|
||||
create_transaction(account: @account, date: 8.days.ago.to_date, amount: -5000)
|
||||
create_valuation(account: @account, date: 6.days.ago.to_date, amount: 17000)
|
||||
create_transaction(account: @account, date: 6.days.ago.to_date, amount: -500)
|
||||
create_transaction(account: @account, date: 4.days.ago.to_date, amount: -500)
|
||||
create_valuation(account: @account, date: 3.days.ago.to_date, amount: 17000)
|
||||
create_transaction(account: @account, date: 1.day.ago.to_date, amount: 100)
|
||||
|
||||
expected = [ 0, 5000, 5000, 17000, 17000, 17500, 17000, 17000, 16900, 16900 ]
|
||||
calculated = Account::Balance::ForwardCalculator.new(@account).calculate.sort_by(&:date).map(&:balance)
|
||||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
|
||||
test "multi-currency sync" do
|
||||
ExchangeRate.create! date: 1.day.ago.to_date, from_currency: "EUR", to_currency: "USD", rate: 1.2
|
||||
|
||||
create_transaction(account: @account, date: 3.days.ago.to_date, amount: -100, currency: "USD")
|
||||
create_transaction(account: @account, date: 2.days.ago.to_date, amount: -300, currency: "USD")
|
||||
|
||||
# Transaction in different currency than the account's main currency
|
||||
create_transaction(account: @account, date: 1.day.ago.to_date, amount: -500, currency: "EUR") # €500 * 1.2 = $600
|
||||
|
||||
expected = [ 0, 100, 400, 1000, 1000 ]
|
||||
calculated = Account::Balance::ForwardCalculator.new(@account).calculate.sort_by(&:date).map(&:balance)
|
||||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
end
|
|
@ -1,59 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class Account::Balance::ReverseCalculatorTest < ActiveSupport::TestCase
|
||||
include Account::EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@account = families(:empty).accounts.create!(
|
||||
name: "Test",
|
||||
balance: 20000,
|
||||
cash_balance: 20000,
|
||||
currency: "USD",
|
||||
accountable: Investment.new
|
||||
)
|
||||
end
|
||||
|
||||
# When syncing backwards, we start with the account balance and generate everything from there.
|
||||
test "no entries sync" do
|
||||
assert_equal 0, @account.balances.count
|
||||
|
||||
expected = [ @account.balance, @account.balance ]
|
||||
calculated = Account::Balance::ReverseCalculator.new(@account).calculate
|
||||
|
||||
assert_equal expected, calculated.map(&:balance)
|
||||
end
|
||||
|
||||
test "valuations sync" do
|
||||
create_valuation(account: @account, date: 4.days.ago.to_date, amount: 17000)
|
||||
create_valuation(account: @account, date: 2.days.ago.to_date, amount: 19000)
|
||||
|
||||
expected = [ 17000, 17000, 19000, 19000, 20000, 20000 ]
|
||||
calculated = Account::Balance::ReverseCalculator.new(@account).calculate.sort_by(&:date).map(&:balance)
|
||||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
|
||||
test "transactions sync" do
|
||||
create_transaction(account: @account, date: 4.days.ago.to_date, amount: -500) # income
|
||||
create_transaction(account: @account, date: 2.days.ago.to_date, amount: 100) # expense
|
||||
|
||||
expected = [ 19600, 20100, 20100, 20000, 20000, 20000 ]
|
||||
calculated = Account::Balance::ReverseCalculator.new(@account).calculate.sort_by(&:date).map(&:balance)
|
||||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
|
||||
test "multi-entry sync" do
|
||||
create_transaction(account: @account, date: 8.days.ago.to_date, amount: -5000)
|
||||
create_valuation(account: @account, date: 6.days.ago.to_date, amount: 17000)
|
||||
create_transaction(account: @account, date: 6.days.ago.to_date, amount: -500)
|
||||
create_transaction(account: @account, date: 4.days.ago.to_date, amount: -500)
|
||||
create_valuation(account: @account, date: 3.days.ago.to_date, amount: 17000)
|
||||
create_transaction(account: @account, date: 1.day.ago.to_date, amount: 100)
|
||||
|
||||
expected = [ 12000, 17000, 17000, 17000, 16500, 17000, 17000, 20100, 20000, 20000 ]
|
||||
calculated = Account::Balance::ReverseCalculator.new(@account).calculate.sort_by(&:date).map(&:balance)
|
||||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
end
|
|
@ -1,51 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class Account::Balance::SyncerTest < ActiveSupport::TestCase
|
||||
include Account::EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@account = families(:empty).accounts.create!(
|
||||
name: "Test",
|
||||
balance: 20000,
|
||||
cash_balance: 20000,
|
||||
currency: "USD",
|
||||
accountable: Investment.new
|
||||
)
|
||||
end
|
||||
|
||||
test "syncs balances" do
|
||||
Account::Holding::Syncer.any_instance.expects(:sync_holdings).returns([]).once
|
||||
|
||||
@account.expects(:start_date).returns(2.days.ago.to_date)
|
||||
|
||||
Account::Balance::ForwardCalculator.any_instance.expects(:calculate).returns(
|
||||
[
|
||||
Account::Balance.new(date: 1.day.ago.to_date, balance: 1000, cash_balance: 1000, currency: "USD"),
|
||||
Account::Balance.new(date: Date.current, balance: 1000, cash_balance: 1000, currency: "USD")
|
||||
]
|
||||
)
|
||||
|
||||
assert_difference "@account.balances.count", 2 do
|
||||
Account::Balance::Syncer.new(@account, strategy: :forward).sync_balances
|
||||
end
|
||||
end
|
||||
|
||||
test "purges stale balances and holdings" do
|
||||
# Balance before start date is stale
|
||||
@account.expects(:start_date).returns(2.days.ago.to_date).twice
|
||||
stale_balance = Account::Balance.new(date: 3.days.ago.to_date, balance: 10000, cash_balance: 10000, currency: "USD")
|
||||
|
||||
Account::Balance::ForwardCalculator.any_instance.expects(:calculate).returns(
|
||||
[
|
||||
stale_balance,
|
||||
Account::Balance.new(date: 2.days.ago.to_date, balance: 10000, cash_balance: 10000, currency: "USD"),
|
||||
Account::Balance.new(date: 1.day.ago.to_date, balance: 1000, cash_balance: 1000, currency: "USD"),
|
||||
Account::Balance.new(date: Date.current, balance: 1000, cash_balance: 1000, currency: "USD")
|
||||
]
|
||||
)
|
||||
|
||||
assert_difference "@account.balances.count", 3 do
|
||||
Account::Balance::Syncer.new(@account, strategy: :forward).sync_balances
|
||||
end
|
||||
end
|
||||
end
|
|
@ -2,7 +2,7 @@ require "test_helper"
|
|||
require "ostruct"
|
||||
|
||||
class Account::ConvertibleTest < ActiveSupport::TestCase
|
||||
include Account::EntriesTestHelper, ProviderTestHelper
|
||||
include EntriesTestHelper, ProviderTestHelper
|
||||
|
||||
setup do
|
||||
@family = families(:empty)
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
require "test_helper"
|
||||
|
||||
class Account::EntryTest < ActiveSupport::TestCase
|
||||
include Account::EntriesTestHelper
|
||||
class EntryTest < ActiveSupport::TestCase
|
||||
include EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@entry = account_entries :transaction
|
||||
@entry = entries :transaction
|
||||
end
|
||||
|
||||
test "entry cannot be older than 10 years ago" do
|
||||
|
@ -14,10 +14,10 @@ class Account::EntryTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
test "valuations cannot have more than one entry per day" do
|
||||
existing_valuation = account_entries :valuation
|
||||
existing_valuation = entries :valuation
|
||||
|
||||
new_valuation = Account::Entry.new \
|
||||
entryable: Account::Valuation.new,
|
||||
new_valuation = Entry.new \
|
||||
entryable: Valuation.new,
|
||||
account: existing_valuation.account,
|
||||
date: existing_valuation.date, # invalid
|
||||
currency: existing_valuation.currency,
|
||||
|
@ -76,7 +76,7 @@ class Account::EntryTest < ActiveSupport::TestCase
|
|||
accounts(:credit_card).update!(is_active: false)
|
||||
|
||||
# Test the scope
|
||||
active_entries = Account::Entry.active
|
||||
active_entries = Entry.active
|
||||
|
||||
# Should include entry from active account
|
||||
assert_includes active_entries, active_transaction
|
||||
|
|
|
@ -1,146 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class Account::Holding::ForwardCalculatorTest < ActiveSupport::TestCase
|
||||
include Account::EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@account = families(:empty).accounts.create!(
|
||||
name: "Test",
|
||||
balance: 20000,
|
||||
cash_balance: 20000,
|
||||
currency: "USD",
|
||||
accountable: Investment.new
|
||||
)
|
||||
end
|
||||
|
||||
test "no holdings" do
|
||||
calculated = Account::Holding::ForwardCalculator.new(@account).calculate
|
||||
assert_equal [], calculated
|
||||
end
|
||||
|
||||
test "forward portfolio calculation" do
|
||||
load_prices
|
||||
|
||||
# Build up to 10 shares of VOO (current value $5000)
|
||||
create_trade(@voo, qty: 20, date: 3.days.ago.to_date, price: 470, account: @account)
|
||||
create_trade(@voo, qty: -15, date: 2.days.ago.to_date, price: 480, account: @account)
|
||||
create_trade(@voo, qty: 5, date: 1.day.ago.to_date, price: 490, account: @account)
|
||||
|
||||
# Amazon won't exist in current holdings because qty is zero, but should show up in historical portfolio
|
||||
create_trade(@amzn, qty: 1, date: 2.days.ago.to_date, price: 200, account: @account)
|
||||
create_trade(@amzn, qty: -1, date: 1.day.ago.to_date, price: 200, account: @account)
|
||||
|
||||
# Build up to 100 shares of WMT (current value $10000)
|
||||
create_trade(@wmt, qty: 100, date: 1.day.ago.to_date, price: 100, account: @account)
|
||||
|
||||
expected = [
|
||||
# 4 days ago
|
||||
Account::Holding.new(security: @voo, date: 4.days.ago.to_date, qty: 0, price: 460, amount: 0),
|
||||
Account::Holding.new(security: @wmt, date: 4.days.ago.to_date, qty: 0, price: 100, amount: 0),
|
||||
Account::Holding.new(security: @amzn, date: 4.days.ago.to_date, qty: 0, price: 200, amount: 0),
|
||||
|
||||
# 3 days ago
|
||||
Account::Holding.new(security: @voo, date: 3.days.ago.to_date, qty: 20, price: 470, amount: 9400),
|
||||
Account::Holding.new(security: @wmt, date: 3.days.ago.to_date, qty: 0, price: 100, amount: 0),
|
||||
Account::Holding.new(security: @amzn, date: 3.days.ago.to_date, qty: 0, price: 200, amount: 0),
|
||||
|
||||
# 2 days ago
|
||||
Account::Holding.new(security: @voo, date: 2.days.ago.to_date, qty: 5, price: 480, amount: 2400),
|
||||
Account::Holding.new(security: @wmt, date: 2.days.ago.to_date, qty: 0, price: 100, amount: 0),
|
||||
Account::Holding.new(security: @amzn, date: 2.days.ago.to_date, qty: 1, price: 200, amount: 200),
|
||||
|
||||
# 1 day ago
|
||||
Account::Holding.new(security: @voo, date: 1.day.ago.to_date, qty: 10, price: 490, amount: 4900),
|
||||
Account::Holding.new(security: @wmt, date: 1.day.ago.to_date, qty: 100, price: 100, amount: 10000),
|
||||
Account::Holding.new(security: @amzn, date: 1.day.ago.to_date, qty: 0, price: 200, amount: 0),
|
||||
|
||||
# Today
|
||||
Account::Holding.new(security: @voo, date: Date.current, qty: 10, price: 500, amount: 5000),
|
||||
Account::Holding.new(security: @wmt, date: Date.current, qty: 100, price: 100, amount: 10000),
|
||||
Account::Holding.new(security: @amzn, date: Date.current, qty: 0, price: 200, amount: 0)
|
||||
]
|
||||
|
||||
calculated = Account::Holding::ForwardCalculator.new(@account).calculate
|
||||
|
||||
assert_equal expected.length, calculated.length
|
||||
assert_holdings(expected, calculated)
|
||||
end
|
||||
|
||||
# Carries the previous record forward if no holding exists for a date
|
||||
# to ensure that net worth historical rollups have a value for every date
|
||||
test "uses locf to fill missing holdings" do
|
||||
load_prices
|
||||
|
||||
create_trade(@wmt, qty: 100, date: 1.day.ago.to_date, price: 100, account: @account)
|
||||
|
||||
expected = [
|
||||
Account::Holding.new(security: @wmt, date: 2.days.ago.to_date, qty: 0, price: 100, amount: 0),
|
||||
Account::Holding.new(security: @wmt, date: 1.day.ago.to_date, qty: 100, price: 100, amount: 10000),
|
||||
Account::Holding.new(security: @wmt, date: Date.current, qty: 100, price: 100, amount: 10000)
|
||||
]
|
||||
|
||||
# Price missing today, so we should carry forward the holding from 1 day ago
|
||||
Security.stubs(:find).returns(@wmt)
|
||||
Security::Price.stubs(:find_price).with(security: @wmt, date: 2.days.ago.to_date).returns(Security::Price.new(price: 100))
|
||||
Security::Price.stubs(:find_price).with(security: @wmt, date: 1.day.ago.to_date).returns(Security::Price.new(price: 100))
|
||||
Security::Price.stubs(:find_price).with(security: @wmt, date: Date.current).returns(nil)
|
||||
|
||||
calculated = Account::Holding::ForwardCalculator.new(@account).calculate
|
||||
|
||||
assert_equal expected.length, calculated.length
|
||||
assert_holdings(expected, calculated)
|
||||
end
|
||||
|
||||
test "offline tickers sync holdings based on most recent trade price" do
|
||||
offline_security = Security.create!(ticker: "OFFLINE", name: "Offline Ticker")
|
||||
|
||||
create_trade(offline_security, qty: 1, date: 3.days.ago.to_date, price: 90, account: @account)
|
||||
create_trade(offline_security, qty: 1, date: 1.day.ago.to_date, price: 100, account: @account)
|
||||
|
||||
expected = [
|
||||
Account::Holding.new(security: offline_security, date: 3.days.ago.to_date, qty: 1, price: 90, amount: 90),
|
||||
Account::Holding.new(security: offline_security, date: 2.days.ago.to_date, qty: 1, price: 90, amount: 90),
|
||||
Account::Holding.new(security: offline_security, date: 1.day.ago.to_date, qty: 2, price: 100, amount: 200),
|
||||
Account::Holding.new(security: offline_security, date: Date.current, qty: 2, price: 100, amount: 200)
|
||||
]
|
||||
|
||||
calculated = Account::Holding::ForwardCalculator.new(@account).calculate
|
||||
|
||||
assert_equal expected.length, calculated.length
|
||||
assert_holdings(expected, calculated)
|
||||
end
|
||||
|
||||
private
|
||||
def assert_holdings(expected, calculated)
|
||||
expected.each do |expected_entry|
|
||||
calculated_entry = calculated.find { |c| c.security == expected_entry.security && c.date == expected_entry.date }
|
||||
|
||||
assert_equal expected_entry.qty, calculated_entry.qty, "Qty mismatch for #{expected_entry.security.ticker} on #{expected_entry.date}"
|
||||
assert_equal expected_entry.price, calculated_entry.price, "Price mismatch for #{expected_entry.security.ticker} on #{expected_entry.date}"
|
||||
assert_equal expected_entry.amount, calculated_entry.amount, "Amount mismatch for #{expected_entry.security.ticker} on #{expected_entry.date}"
|
||||
end
|
||||
end
|
||||
|
||||
def load_prices
|
||||
@voo = Security.create!(ticker: "VOO", name: "Vanguard S&P 500 ETF")
|
||||
Security::Price.create!(security: @voo, date: 4.days.ago.to_date, price: 460)
|
||||
Security::Price.create!(security: @voo, date: 3.days.ago.to_date, price: 470)
|
||||
Security::Price.create!(security: @voo, date: 2.days.ago.to_date, price: 480)
|
||||
Security::Price.create!(security: @voo, date: 1.day.ago.to_date, price: 490)
|
||||
Security::Price.create!(security: @voo, date: Date.current, price: 500)
|
||||
|
||||
@wmt = Security.create!(ticker: "WMT", name: "Walmart Inc.")
|
||||
Security::Price.create!(security: @wmt, date: 4.days.ago.to_date, price: 100)
|
||||
Security::Price.create!(security: @wmt, date: 3.days.ago.to_date, price: 100)
|
||||
Security::Price.create!(security: @wmt, date: 2.days.ago.to_date, price: 100)
|
||||
Security::Price.create!(security: @wmt, date: 1.day.ago.to_date, price: 100)
|
||||
Security::Price.create!(security: @wmt, date: Date.current, price: 100)
|
||||
|
||||
@amzn = Security.create!(ticker: "AMZN", name: "Amazon.com Inc.")
|
||||
Security::Price.create!(security: @amzn, date: 4.days.ago.to_date, price: 200)
|
||||
Security::Price.create!(security: @amzn, date: 3.days.ago.to_date, price: 200)
|
||||
Security::Price.create!(security: @amzn, date: 2.days.ago.to_date, price: 200)
|
||||
Security::Price.create!(security: @amzn, date: 1.day.ago.to_date, price: 200)
|
||||
Security::Price.create!(security: @amzn, date: Date.current, price: 200)
|
||||
end
|
||||
end
|
|
@ -1,87 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class Account::Holding::PortfolioCacheTest < ActiveSupport::TestCase
|
||||
include Account::EntriesTestHelper, ProviderTestHelper
|
||||
|
||||
setup do
|
||||
@provider = mock
|
||||
Security.stubs(:provider).returns(@provider)
|
||||
|
||||
@account = families(:empty).accounts.create!(
|
||||
name: "Test Brokerage",
|
||||
balance: 10000,
|
||||
currency: "USD",
|
||||
accountable: Investment.new
|
||||
)
|
||||
|
||||
@security = Security.create!(name: "Test Security", ticker: "TEST", exchange_operating_mic: "TEST")
|
||||
|
||||
@trade = create_trade(@security, account: @account, qty: 1, date: 2.days.ago.to_date, price: 210.23).account_trade
|
||||
end
|
||||
|
||||
test "gets price from DB if available" do
|
||||
db_price = 210
|
||||
|
||||
Security::Price.create!(
|
||||
security: @security,
|
||||
date: Date.current,
|
||||
price: db_price
|
||||
)
|
||||
|
||||
expect_provider_prices([], start_date: @account.start_date)
|
||||
|
||||
cache = Account::Holding::PortfolioCache.new(@account)
|
||||
assert_equal db_price, cache.get_price(@security.id, Date.current).price
|
||||
end
|
||||
|
||||
test "if no price in DB, try fetching from provider" do
|
||||
Security::Price.delete_all
|
||||
|
||||
provider_price = Security::Price.new(
|
||||
security: @security,
|
||||
date: Date.current,
|
||||
price: 220,
|
||||
currency: "USD"
|
||||
)
|
||||
|
||||
expect_provider_prices([ provider_price ], start_date: @account.start_date)
|
||||
|
||||
cache = Account::Holding::PortfolioCache.new(@account)
|
||||
assert_equal provider_price.price, cache.get_price(@security.id, Date.current).price
|
||||
end
|
||||
|
||||
test "if no price from db or provider, try getting the price from trades" do
|
||||
Security::Price.destroy_all
|
||||
expect_provider_prices([], start_date: @account.start_date)
|
||||
|
||||
cache = Account::Holding::PortfolioCache.new(@account)
|
||||
assert_equal @trade.price, cache.get_price(@security.id, @trade.entry.date).price
|
||||
end
|
||||
|
||||
test "if no price from db, provider, or trades, search holdings" do
|
||||
Security::Price.delete_all
|
||||
Account::Entry.delete_all
|
||||
|
||||
holding = Account::Holding.create!(
|
||||
security: @security,
|
||||
account: @account,
|
||||
date: Date.current,
|
||||
qty: 1,
|
||||
price: 250,
|
||||
amount: 250 * 1,
|
||||
currency: "USD"
|
||||
)
|
||||
|
||||
expect_provider_prices([], start_date: @account.start_date)
|
||||
|
||||
cache = Account::Holding::PortfolioCache.new(@account, use_holdings: true)
|
||||
assert_equal holding.price, cache.get_price(@security.id, holding.date).price
|
||||
end
|
||||
|
||||
private
|
||||
def expect_provider_prices(prices, start_date:, end_date: Date.current)
|
||||
@provider.expects(:fetch_security_prices)
|
||||
.with(@security, start_date: start_date, end_date: end_date)
|
||||
.returns(provider_success_response(prices))
|
||||
end
|
||||
end
|
|
@ -1,155 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class Account::Holding::ReverseCalculatorTest < ActiveSupport::TestCase
|
||||
include Account::EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@account = families(:empty).accounts.create!(
|
||||
name: "Test",
|
||||
balance: 20000,
|
||||
cash_balance: 20000,
|
||||
currency: "USD",
|
||||
accountable: Investment.new
|
||||
)
|
||||
end
|
||||
|
||||
test "no holdings" do
|
||||
calculated = Account::Holding::ReverseCalculator.new(@account).calculate
|
||||
assert_equal [], calculated
|
||||
end
|
||||
|
||||
# Should be able to handle this case, although we should not be reverse-syncing an account without provided current day holdings
|
||||
test "reverse portfolio with trades but without current day holdings" do
|
||||
voo = Security.create!(ticker: "VOO", name: "Vanguard S&P 500 ETF")
|
||||
Security::Price.create!(security: voo, date: Date.current, price: 470)
|
||||
Security::Price.create!(security: voo, date: 1.day.ago.to_date, price: 470)
|
||||
|
||||
create_trade(voo, qty: -10, date: Date.current, price: 470, account: @account)
|
||||
|
||||
calculated = Account::Holding::ReverseCalculator.new(@account).calculate
|
||||
assert_equal 2, calculated.length
|
||||
end
|
||||
|
||||
test "reverse portfolio calculation" do
|
||||
load_today_portfolio
|
||||
|
||||
# Build up to 10 shares of VOO (current value $5000)
|
||||
create_trade(@voo, qty: 20, date: 3.days.ago.to_date, price: 470, account: @account)
|
||||
create_trade(@voo, qty: -15, date: 2.days.ago.to_date, price: 480, account: @account)
|
||||
create_trade(@voo, qty: 5, date: 1.day.ago.to_date, price: 490, account: @account)
|
||||
|
||||
# Amazon won't exist in current holdings because qty is zero, but should show up in historical portfolio
|
||||
create_trade(@amzn, qty: 1, date: 2.days.ago.to_date, price: 200, account: @account)
|
||||
create_trade(@amzn, qty: -1, date: 1.day.ago.to_date, price: 200, account: @account)
|
||||
|
||||
# Build up to 100 shares of WMT (current value $10000)
|
||||
create_trade(@wmt, qty: 100, date: 1.day.ago.to_date, price: 100, account: @account)
|
||||
|
||||
expected = [
|
||||
# 4 days ago
|
||||
Account::Holding.new(security: @voo, date: 4.days.ago.to_date, qty: 0, price: 460, amount: 0),
|
||||
Account::Holding.new(security: @wmt, date: 4.days.ago.to_date, qty: 0, price: 100, amount: 0),
|
||||
Account::Holding.new(security: @amzn, date: 4.days.ago.to_date, qty: 0, price: 200, amount: 0),
|
||||
|
||||
# 3 days ago
|
||||
Account::Holding.new(security: @voo, date: 3.days.ago.to_date, qty: 20, price: 470, amount: 9400),
|
||||
Account::Holding.new(security: @wmt, date: 3.days.ago.to_date, qty: 0, price: 100, amount: 0),
|
||||
Account::Holding.new(security: @amzn, date: 3.days.ago.to_date, qty: 0, price: 200, amount: 0),
|
||||
|
||||
# 2 days ago
|
||||
Account::Holding.new(security: @voo, date: 2.days.ago.to_date, qty: 5, price: 480, amount: 2400),
|
||||
Account::Holding.new(security: @wmt, date: 2.days.ago.to_date, qty: 0, price: 100, amount: 0),
|
||||
Account::Holding.new(security: @amzn, date: 2.days.ago.to_date, qty: 1, price: 200, amount: 200),
|
||||
|
||||
# 1 day ago
|
||||
Account::Holding.new(security: @voo, date: 1.day.ago.to_date, qty: 10, price: 490, amount: 4900),
|
||||
Account::Holding.new(security: @wmt, date: 1.day.ago.to_date, qty: 100, price: 100, amount: 10000),
|
||||
Account::Holding.new(security: @amzn, date: 1.day.ago.to_date, qty: 0, price: 200, amount: 0),
|
||||
|
||||
# Today
|
||||
Account::Holding.new(security: @voo, date: Date.current, qty: 10, price: 500, amount: 5000),
|
||||
Account::Holding.new(security: @wmt, date: Date.current, qty: 100, price: 100, amount: 10000),
|
||||
Account::Holding.new(security: @amzn, date: Date.current, qty: 0, price: 200, amount: 0)
|
||||
]
|
||||
|
||||
calculated = Account::Holding::ReverseCalculator.new(@account).calculate
|
||||
|
||||
assert_equal expected.length, calculated.length
|
||||
|
||||
expected.each do |expected_entry|
|
||||
calculated_entry = calculated.find { |c| c.security == expected_entry.security && c.date == expected_entry.date }
|
||||
|
||||
assert_equal expected_entry.qty, calculated_entry.qty, "Qty mismatch for #{expected_entry.security.ticker} on #{expected_entry.date}"
|
||||
assert_equal expected_entry.price, calculated_entry.price, "Price mismatch for #{expected_entry.security.ticker} on #{expected_entry.date}"
|
||||
assert_equal expected_entry.amount, calculated_entry.amount, "Amount mismatch for #{expected_entry.security.ticker} on #{expected_entry.date}"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def assert_holdings(expected, calculated)
|
||||
expected.each do |expected_entry|
|
||||
calculated_entry = calculated.find { |c| c.security == expected_entry.security && c.date == expected_entry.date }
|
||||
|
||||
assert_equal expected_entry.qty, calculated_entry.qty, "Qty mismatch for #{expected_entry.security.ticker} on #{expected_entry.date}"
|
||||
assert_equal expected_entry.price, calculated_entry.price, "Price mismatch for #{expected_entry.security.ticker} on #{expected_entry.date}"
|
||||
assert_equal expected_entry.amount, calculated_entry.amount, "Amount mismatch for #{expected_entry.security.ticker} on #{expected_entry.date}"
|
||||
end
|
||||
end
|
||||
|
||||
def load_prices
|
||||
@voo = Security.create!(ticker: "VOO", name: "Vanguard S&P 500 ETF")
|
||||
Security::Price.create!(security: @voo, date: 4.days.ago.to_date, price: 460)
|
||||
Security::Price.create!(security: @voo, date: 3.days.ago.to_date, price: 470)
|
||||
Security::Price.create!(security: @voo, date: 2.days.ago.to_date, price: 480)
|
||||
Security::Price.create!(security: @voo, date: 1.day.ago.to_date, price: 490)
|
||||
Security::Price.create!(security: @voo, date: Date.current, price: 500)
|
||||
|
||||
@wmt = Security.create!(ticker: "WMT", name: "Walmart Inc.")
|
||||
Security::Price.create!(security: @wmt, date: 4.days.ago.to_date, price: 100)
|
||||
Security::Price.create!(security: @wmt, date: 3.days.ago.to_date, price: 100)
|
||||
Security::Price.create!(security: @wmt, date: 2.days.ago.to_date, price: 100)
|
||||
Security::Price.create!(security: @wmt, date: 1.day.ago.to_date, price: 100)
|
||||
Security::Price.create!(security: @wmt, date: Date.current, price: 100)
|
||||
|
||||
@amzn = Security.create!(ticker: "AMZN", name: "Amazon.com Inc.")
|
||||
Security::Price.create!(security: @amzn, date: 4.days.ago.to_date, price: 200)
|
||||
Security::Price.create!(security: @amzn, date: 3.days.ago.to_date, price: 200)
|
||||
Security::Price.create!(security: @amzn, date: 2.days.ago.to_date, price: 200)
|
||||
Security::Price.create!(security: @amzn, date: 1.day.ago.to_date, price: 200)
|
||||
Security::Price.create!(security: @amzn, date: Date.current, price: 200)
|
||||
end
|
||||
|
||||
# Portfolio holdings:
|
||||
# +--------+-----+--------+---------+
|
||||
# | Ticker | Qty | Price | Amount |
|
||||
# +--------+-----+--------+---------+
|
||||
# | VOO | 10 | $500 | $5,000 |
|
||||
# | WMT | 100 | $100 | $10,000 |
|
||||
# +--------+-----+--------+---------+
|
||||
# Brokerage Cash: $5,000
|
||||
# Holdings Value: $15,000
|
||||
# Total Balance: $20,000
|
||||
def load_today_portfolio
|
||||
@account.update!(cash_balance: 5000)
|
||||
|
||||
load_prices
|
||||
|
||||
@account.holdings.create!(
|
||||
date: Date.current,
|
||||
price: 500,
|
||||
qty: 10,
|
||||
amount: 5000,
|
||||
currency: "USD",
|
||||
security: @voo
|
||||
)
|
||||
|
||||
@account.holdings.create!(
|
||||
date: Date.current,
|
||||
price: 100,
|
||||
qty: 100,
|
||||
amount: 10000,
|
||||
currency: "USD",
|
||||
security: @wmt
|
||||
)
|
||||
end
|
||||
end
|
|
@ -1,29 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class Account::Holding::SyncerTest < ActiveSupport::TestCase
|
||||
include Account::EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@family = families(:empty)
|
||||
@account = @family.accounts.create!(name: "Test", balance: 20000, cash_balance: 20000, currency: "USD", accountable: Investment.new)
|
||||
@aapl = securities(:aapl)
|
||||
end
|
||||
|
||||
test "syncs holdings" do
|
||||
create_trade(@aapl, account: @account, qty: 1, price: 200, date: Date.current)
|
||||
|
||||
# Should have yesterday's and today's holdings
|
||||
assert_difference "@account.holdings.count", 2 do
|
||||
Account::Holding::Syncer.new(@account, strategy: :forward).sync_holdings
|
||||
end
|
||||
end
|
||||
|
||||
test "purges stale holdings for unlinked accounts" do
|
||||
# Since the account has no entries, there should be no holdings
|
||||
Account::Holding.create!(account: @account, security: @aapl, qty: 1, price: 100, amount: 100, currency: "USD", date: Date.current)
|
||||
|
||||
assert_difference "Account::Holding.count", -1 do
|
||||
Account::Holding::Syncer.new(@account, strategy: :forward).sync_holdings
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,79 +0,0 @@
|
|||
require "test_helper"
|
||||
require "ostruct"
|
||||
|
||||
class Account::HoldingTest < ActiveSupport::TestCase
|
||||
include Account::EntriesTestHelper, SecuritiesTestHelper
|
||||
|
||||
setup do
|
||||
@account = families(:empty).accounts.create!(name: "Test Brokerage", balance: 20000, cash_balance: 0, currency: "USD", accountable: Investment.new)
|
||||
|
||||
# Current day holding instances
|
||||
@amzn, @nvda = load_holdings
|
||||
end
|
||||
|
||||
test "calculates portfolio weight" do
|
||||
expected_amzn_weight = 3240.0 / @account.balance * 100
|
||||
expected_nvda_weight = 3720.0 / @account.balance * 100
|
||||
|
||||
assert_in_delta expected_amzn_weight, @amzn.weight, 0.001
|
||||
assert_in_delta expected_nvda_weight, @nvda.weight, 0.001
|
||||
end
|
||||
|
||||
test "calculates simple average cost basis" do
|
||||
create_trade(@amzn.security, account: @account, qty: 10, price: 212.00, date: 1.day.ago.to_date)
|
||||
create_trade(@amzn.security, account: @account, qty: 15, price: 216.00, date: Date.current)
|
||||
|
||||
create_trade(@nvda.security, account: @account, qty: 5, price: 128.00, date: 1.day.ago.to_date)
|
||||
create_trade(@nvda.security, account: @account, qty: 30, price: 124.00, date: Date.current)
|
||||
|
||||
assert_equal Money.new((212.0 + 216.0) / 2), @amzn.avg_cost
|
||||
assert_equal Money.new((128.0 + 124.0) / 2), @nvda.avg_cost
|
||||
end
|
||||
|
||||
test "calculates total return trend" do
|
||||
@amzn.stubs(:avg_cost).returns(Money.new(214.00))
|
||||
@nvda.stubs(:avg_cost).returns(Money.new(126.00))
|
||||
|
||||
# Gained $30, or 0.93%
|
||||
assert_equal Money.new(30), @amzn.trend.value
|
||||
assert_in_delta 0.9, @amzn.trend.percent, 0.001
|
||||
|
||||
# Lost $60, or -1.59%
|
||||
assert_equal Money.new(-60), @nvda.trend.value
|
||||
assert_in_delta -1.6, @nvda.trend.percent, 0.001
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_holdings
|
||||
security1 = create_security("AMZN", prices: [
|
||||
{ date: 1.day.ago.to_date, price: 212.00 },
|
||||
{ date: Date.current, price: 216.00 }
|
||||
])
|
||||
|
||||
security2 = create_security("NVDA", prices: [
|
||||
{ date: 1.day.ago.to_date, price: 128.00 },
|
||||
{ date: Date.current, price: 124.00 }
|
||||
])
|
||||
|
||||
create_holding(security1, 1.day.ago.to_date, 10)
|
||||
amzn = create_holding(security1, Date.current, 15)
|
||||
|
||||
create_holding(security2, 1.day.ago.to_date, 5)
|
||||
nvda = create_holding(security2, Date.current, 30)
|
||||
|
||||
[ amzn, nvda ]
|
||||
end
|
||||
|
||||
def create_holding(security, date, qty)
|
||||
price = Security::Price.find_by(date: date, security: security).price
|
||||
|
||||
@account.holdings.create! \
|
||||
date: date,
|
||||
security: security,
|
||||
qty: qty,
|
||||
price: price,
|
||||
amount: qty * price,
|
||||
currency: "USD"
|
||||
end
|
||||
end
|
|
@ -1,5 +1,5 @@
|
|||
require "test_helper"
|
||||
|
||||
class Account::TransactionTest < ActiveSupport::TestCase
|
||||
include Account::EntriesTestHelper
|
||||
class TransactionTest < ActiveSupport::TestCase
|
||||
include EntriesTestHelper
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue