mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 23:59:40 +02:00
Match Plaid holding values on current day (#2212)
* Match Plaid holding values on current day * Fix chart timezone issue * Add timezone tests for syncs * Hide sidebars on trades test
This commit is contained in:
parent
470b753833
commit
2000f05453
16 changed files with 242 additions and 21 deletions
|
@ -13,6 +13,23 @@ class Balance::ForwardCalculatorTest < ActiveSupport::TestCase
|
|||
)
|
||||
end
|
||||
|
||||
test "balance generation respects user timezone and last generated date is current user date" do
|
||||
# Simulate user in EST timezone
|
||||
Time.zone = "America/New_York"
|
||||
|
||||
# Set current time to 1am UTC on Jan 5, 2025
|
||||
# This would be 8pm EST on Jan 4, 2025 (user's time, and the last date we should generate balances for)
|
||||
travel_to Time.utc(2025, 01, 05, 1, 0, 0)
|
||||
|
||||
# Create a valuation for Jan 3, 2025
|
||||
create_valuation(account: @account, date: "2025-01-03", amount: 17000)
|
||||
|
||||
expected = [ [ "2025-01-02", 0 ], [ "2025-01-03", 17000 ], [ "2025-01-04", 17000 ] ]
|
||||
calculated = Balance::ForwardCalculator.new(@account).calculate
|
||||
|
||||
assert_equal expected, calculated.map { |b| [ b.date.to_s, b.balance ] }
|
||||
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
|
||||
|
@ -71,4 +88,42 @@ class Balance::ForwardCalculatorTest < ActiveSupport::TestCase
|
|||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
|
||||
test "holdings and trades sync" do
|
||||
aapl = securities(:aapl)
|
||||
|
||||
# Account starts at a value of $5000
|
||||
create_valuation(account: @account, date: 2.days.ago.to_date, amount: 5000)
|
||||
|
||||
# Share purchase reduces cash balance by $1000, but keeps overall balance same
|
||||
create_trade(aapl, account: @account, qty: 10, date: 1.day.ago.to_date, price: 100)
|
||||
|
||||
Holding.create!(date: 1.day.ago.to_date, account: @account, security: aapl, qty: 10, price: 100, amount: 1000, currency: "USD")
|
||||
Holding.create!(date: Date.current, account: @account, security: aapl, qty: 10, price: 100, amount: 1000, currency: "USD")
|
||||
|
||||
# Given constant prices, overall balance (account value) should be constant
|
||||
# (the single trade doesn't affect balance; it just alters cash vs. holdings composition)
|
||||
expected = [ 0, 5000, 5000, 5000 ]
|
||||
calculated = Balance::ForwardCalculator.new(@account).calculate.sort_by(&:date).map(&:balance)
|
||||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
|
||||
# Balance calculator is entirely reliant on HoldingCalculator and respects whatever holding records it creates.
|
||||
test "holdings are additive to total balance" do
|
||||
aapl = securities(:aapl)
|
||||
|
||||
# Account starts at a value of $5000
|
||||
create_valuation(account: @account, date: 2.days.ago.to_date, amount: 5000)
|
||||
|
||||
# Even though there are no trades in the history, the calculator will still add the holdings to the total balance
|
||||
Holding.create!(date: 1.day.ago.to_date, account: @account, security: aapl, qty: 10, price: 100, amount: 1000, currency: "USD")
|
||||
Holding.create!(date: Date.current, account: @account, security: aapl, qty: 10, price: 100, amount: 1000, currency: "USD")
|
||||
|
||||
# Start at zero, then valuation of $5000, then tack on $1000 of holdings for remaining 2 days
|
||||
expected = [ 0, 5000, 6000, 6000 ]
|
||||
calculated = Balance::ForwardCalculator.new(@account).calculate.sort_by(&:date).map(&:balance)
|
||||
|
||||
assert_equal expected, calculated
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue