1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 05:09:38 +02:00

Add test cases for multi-currency investment syncing

This commit is contained in:
Zach Gollwitzer 2024-08-13 16:32:07 -04:00
parent 0691041d37
commit 993be6615a
5 changed files with 101 additions and 23 deletions

View file

@ -200,6 +200,11 @@ class Account::Entry < ApplicationRecord
end end
def trade_valid? def trade_valid?
if account_trade.currency != currency
# i18n-tasks-use t('activerecord.errors.models.account/entry.attributes.base.currency_mismatch')
errors.add(:base, :currency_mismatch)
end
if account_trade.sell? if account_trade.sell?
current_qty = account.holding_qty(account_trade.security) current_qty = account.holding_qty(account_trade.security)

View file

@ -6,5 +6,6 @@ en:
account/entry: account/entry:
attributes: attributes:
base: base:
currency_mismatch: Entry currency must match trade currency
invalid_sell_quantity: cannot sell %{sell_qty} shares of %{ticker} because invalid_sell_quantity: cannot sell %{sell_qty} shares of %{ticker} because
you only own %{current_qty} shares you only own %{current_qty} shares

View file

@ -110,9 +110,22 @@ class Account::EntryTest < ActiveSupport::TestCase
amount: 100, amount: 100,
currency: "USD", currency: "USD",
name: "Sell 10 shares of AMZN", name: "Sell 10 shares of AMZN",
entryable: Account::Trade.new(qty: -10, price: 200, security: security) entryable: Account::Trade.new(qty: -10, price: 200, currency: "USD", security: security)
end end
assert_match /cannot sell 10.0 shares of AAPL because you only own 0.0 shares/, error.message assert_match /cannot sell 10.0 shares of AAPL because you only own 0.0 shares/, error.message
end end
# Trade has a denormalized currency field that must match its parent Entry currency
test "trade must have same currency as entry" do
account = accounts(:investment)
assert_raises ActiveRecord::RecordInvalid do
account.entries.create! \
date: Date.current,
amount: 100,
currency: "USD",
name: "Test",
entryable: Account::Trade.new(qty: 10, price: 10, currency: "EUR", security: securities(:aapl))
end
end
end end

View file

@ -33,16 +33,16 @@ class Account::Holding::SyncerTest < ActiveSupport::TestCase
create_trade(security1, account: @account, qty: -10, date: Date.current) # sell 10 shares of AMZN create_trade(security1, account: @account, qty: -10, date: Date.current) # sell 10 shares of AMZN
expected = [ expected = [
{ ticker: "AMZN", qty: 10, price: 214, amount: 10 * 214, date: 2.days.ago.to_date }, { ticker: "AMZN", qty: 10, price: 214, amount: 10 * 214, date: 2.days.ago.to_date, currency: "USD" },
{ ticker: "AMZN", qty: 12, price: 215, amount: 12 * 215, date: 1.day.ago.to_date }, { ticker: "AMZN", qty: 12, price: 215, amount: 12 * 215, date: 1.day.ago.to_date, currency: "USD" },
{ ticker: "AMZN", qty: 2, price: 216, amount: 2 * 216, date: Date.current }, { ticker: "AMZN", qty: 2, price: 216, amount: 2 * 216, date: Date.current, currency: "USD" },
{ ticker: "NVDA", qty: 20, price: 122, amount: 20 * 122, date: 1.day.ago.to_date }, { ticker: "NVDA", qty: 20, price: 122, amount: 20 * 122, date: 1.day.ago.to_date, currency: "USD" },
{ ticker: "NVDA", qty: 20, price: 124, amount: 20 * 124, date: Date.current } { ticker: "NVDA", qty: 20, price: 124, amount: 20 * 124, date: Date.current, currency: "USD" }
] ]
run_sync_for(@account) run_sync_for(@account)
assert_holdings(expected) assert_holdings(expected, @account)
end end
test "generates holdings with prices" do test "generates holdings with prices" do
@ -55,12 +55,12 @@ class Account::Holding::SyncerTest < ActiveSupport::TestCase
create_trade(amzn, account: @account, qty: 10, date: Date.current, price: 215) create_trade(amzn, account: @account, qty: 10, date: Date.current, price: 215)
expected = [ expected = [
{ ticker: "AMZN", qty: 10, price: 215, amount: 10 * 215, date: Date.current } { ticker: "AMZN", qty: 10, price: 215, amount: 10 * 215, date: Date.current, currency: "USD" }
] ]
run_sync_for(@account) run_sync_for(@account)
assert_holdings(expected) assert_holdings(expected, @account)
end end
test "generates all holdings even when missing security prices" do test "generates all holdings even when missing security prices" do
@ -72,9 +72,9 @@ class Account::Holding::SyncerTest < ActiveSupport::TestCase
# 1 day ago — finds daily price, uses it # 1 day ago — finds daily price, uses it
# Today — no daily price, no entry, so price and amount are `nil` # Today — no daily price, no entry, so price and amount are `nil`
expected = [ expected = [
{ ticker: "AMZN", qty: 10, price: 210, amount: 10 * 210, date: 2.days.ago.to_date }, { ticker: "AMZN", qty: 10, price: 210, amount: 10 * 210, date: 2.days.ago.to_date, currency: "USD" },
{ ticker: "AMZN", qty: 10, price: 215, amount: 10 * 215, date: 1.day.ago.to_date }, { ticker: "AMZN", qty: 10, price: 215, amount: 10 * 215, date: 1.day.ago.to_date, currency: "USD" },
{ ticker: "AMZN", qty: 10, price: nil, amount: nil, date: Date.current } { ticker: "AMZN", qty: 10, price: nil, amount: nil, date: Date.current, currency: "USD" }
] ]
Security::Price.expects(:find_prices) Security::Price.expects(:find_prices)
@ -86,25 +86,84 @@ class Account::Holding::SyncerTest < ActiveSupport::TestCase
run_sync_for(@account) run_sync_for(@account)
assert_holdings(expected) assert_holdings(expected, @account)
end
# TODO
test "syncs multi currency trade" do
price_currency = "USD" # Stock price fetched from provider is USD
trade_currency = "EUR" # Trade performed in EUR
amzn = create_security("AMZN", prices: [
{ date: 1.day.ago.to_date, price: 200, currency: price_currency },
{ date: Date.current, price: 210, currency: price_currency }
])
create_trade(amzn, account: @account, qty: 10, date: 1.day.ago.to_date, price: 180, currency: trade_currency)
# We expect holding to be generated in the account's currency (which is what shows to the user)
expected = [
{ ticker: "AMZN", qty: 10, price: 200, amount: 10 * 200, date: 1.day.ago.to_date, currency: "USD" },
{ ticker: "AMZN", qty: 10, price: 210, amount: 10 * 210, date: Date.current, currency: "USD" }
]
run_sync_for(@account)
assert_holdings(expected, @account)
end
# TODO
test "syncs foreign currency investment account" do
# Account is EUR, but family is USD. Must show holdings on account page in EUR, but aggregate holdings in USD for family views
@account.update! currency: "EUR"
assert_not_equal @account.currency, @account.family.currency
price_currency = "USD" # Stock price fetched from provider is USD
trade_currency = "EUR" # Trade performed in EUR
amzn = create_security("AMZN", prices: [
{ date: 1.day.ago.to_date, price: 200, currency: price_currency },
{ date: Date.current, price: 210, currency: price_currency }
])
create_trade(amzn, account: @account, qty: 10, date: 1.day.ago.to_date, price: 200, currency: trade_currency)
ExchangeRate.create! date: 1.day.ago.to_date, from_currency: "USD", to_currency: "EUR", rate: 0.9
ExchangeRate.create! date: Date.current, from_currency: "USD", to_currency: "EUR", rate: 0.9
expected = [
# Holdings in the account's currency for the account view
{ ticker: "AMZN", qty: 10, price: 200 * 0.9, amount: 10 * 200 * 0.9, date: 1.day.ago.to_date, currency: "EUR" },
{ ticker: "AMZN", qty: 10, price: 200 * 0.9, amount: 10 * 200 * 0.9, date: Date.current, currency: "EUR" },
# Holdings in the family's currency for aggregated calculations
{ ticker: "AMZN", qty: 10, price: 200, amount: 10 * 200, date: 1.day.ago.to_date, currency: "USD" },
{ ticker: "AMZN", qty: 10, price: 200, amount: 10 * 200, date: Date.current, currency: "USD" }
]
run_sync_for(@account)
assert_holdings(expected, @account)
end end
private private
def assert_holdings(expected_holdings) def assert_holdings(expected_holdings, account)
holdings = @account.holdings.includes(:security).to_a holdings = account.holdings.includes(:security).to_a
expected_holdings.each do |expected_holding| expected_holdings.each do |expected_holding|
actual_holding = holdings.find { |holding| holding.security.ticker == expected_holding[:ticker] && holding.date == expected_holding[:date] } actual_holding = holdings.find { |holding| holding.security.ticker == expected_holding[:ticker] && holding.date == expected_holding[:date] }
date = expected_holding[:date] date = expected_holding[:date]
expected_price = expected_holding[:price] expected_price = expected_holding[:price].to_d
expected_qty = expected_holding[:qty] expected_qty = expected_holding[:qty]
expected_amount = expected_holding[:amount] expected_amount = expected_holding[:amount].to_d
expected_currency = expected_holding[:currency]
ticker = expected_holding[:ticker] ticker = expected_holding[:ticker]
assert actual_holding, "expected #{ticker} holding on date: #{date}" assert actual_holding, "expected #{ticker} holding on date: #{date}"
assert_equal expected_holding[:qty], actual_holding.qty, "expected #{expected_qty} qty for holding #{ticker} on date: #{date}" assert_equal expected_qty, actual_holding.qty, "expected #{expected_qty} qty for holding #{ticker} on date: #{date}"
assert_equal expected_holding[:amount].to_d, actual_holding.amount.to_d, "expected #{expected_amount} amount for holding #{ticker} on date: #{date}" assert_equal expected_amount, actual_holding.amount.to_d, "expected #{expected_amount} amount for holding #{ticker} on date: #{date}"
assert_equal expected_holding[:price].to_d, actual_holding.price.to_d, "expected #{expected_price} price for holding #{ticker} on date: #{date}" assert_equal expected_price, actual_holding.price.to_d, "expected #{expected_price} price for holding #{ticker} on date: #{date}"
assert_equal expected_currency, actual_holding.currency, "expected #{expected_currency} price for holding #{ticker} on date: #{date}"
end end
end end

View file

@ -28,20 +28,20 @@ module Account::EntriesTestHelper
Account::Entry.create! entry_defaults.merge(attributes) Account::Entry.create! entry_defaults.merge(attributes)
end end
def create_trade(security, account:, qty:, date:, price: nil) def create_trade(security, account:, qty:, date:, currency: "USD", price: nil)
trade_price = price || Security::Price.find_by!(ticker: security.ticker, date: date).price trade_price = price || Security::Price.find_by!(ticker: security.ticker, date: date).price
trade = Account::Trade.new \ trade = Account::Trade.new \
qty: qty, qty: qty,
security: security, security: security,
price: trade_price, price: trade_price,
currency: "USD" currency: currency
account.entries.create! \ account.entries.create! \
name: "Trade", name: "Trade",
date: date, date: date,
amount: qty * trade_price, amount: qty * trade_price,
currency: "USD", currency: currency,
entryable: trade entryable: trade
end end
end end