1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 15:49:39 +02:00

Add security prices provider (Synth integration) (#1039)

* User tickers as primary lookup symbol instead of isin

* Add security price provider

* Fetch security prices in bulk to improve sync performance

* Fetch prices in bulk, better mocking for tests
This commit is contained in:
Zach Gollwitzer 2024-08-01 19:43:23 -04:00 committed by GitHub
parent c70c8b6d86
commit 453a54e5e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 584 additions and 118 deletions

View file

@ -93,8 +93,10 @@ class Account::Balance::SyncerTest < ActiveSupport::TestCase
syncer = Account::Balance::Syncer.new(@account)
assert_raises Money::ConversionError do
syncer.run
with_env_overrides SYNTH_API_KEY: nil do
assert_raises Money::ConversionError do
syncer.run
end
end
end
@ -104,7 +106,10 @@ class Account::Balance::SyncerTest < ActiveSupport::TestCase
@account.update! currency: "EUR"
syncer = Account::Balance::Syncer.new(@account)
syncer.run
with_env_overrides SYNTH_API_KEY: nil do
syncer.run
end
assert_equal 1, syncer.warnings.count
end

View file

@ -113,6 +113,6 @@ class Account::EntryTest < ActiveSupport::TestCase
entryable: Account::Trade.new(qty: -10, price: 200, security: security)
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

View file

@ -33,11 +33,29 @@ class Account::Holding::SyncerTest < ActiveSupport::TestCase
create_trade(security1, account: @account, qty: -10, date: Date.current) # sell 10 shares of AMZN
expected = [
{ symbol: "AMZN", qty: 10, price: 214, amount: 10 * 214, date: 2.days.ago.to_date },
{ symbol: "AMZN", qty: 12, price: 215, amount: 12 * 215, date: 1.day.ago.to_date },
{ symbol: "AMZN", qty: 2, price: 216, amount: 2 * 216, date: Date.current },
{ symbol: "NVDA", qty: 20, price: 122, amount: 20 * 122, date: 1.day.ago.to_date },
{ symbol: "NVDA", qty: 20, price: 124, amount: 20 * 124, date: Date.current }
{ ticker: "AMZN", qty: 10, price: 214, amount: 10 * 214, date: 2.days.ago.to_date },
{ ticker: "AMZN", qty: 12, price: 215, amount: 12 * 215, date: 1.day.ago.to_date },
{ ticker: "AMZN", qty: 2, price: 216, amount: 2 * 216, date: Date.current },
{ ticker: "NVDA", qty: 20, price: 122, amount: 20 * 122, date: 1.day.ago.to_date },
{ ticker: "NVDA", qty: 20, price: 124, amount: 20 * 124, date: Date.current }
]
run_sync_for(@account)
assert_holdings(expected)
end
test "generates holdings with prices" do
provider = mock
Security::Price.stubs(:security_prices_provider).returns(provider)
provider.expects(:fetch_security_prices).never
amzn = create_security("AMZN", prices: [ { date: Date.current, price: 215 } ])
create_trade(amzn, account: @account, qty: 10, date: Date.current, price: 215)
expected = [
{ ticker: "AMZN", qty: 10, price: 215, amount: 10 * 215, date: Date.current }
]
run_sync_for(@account)
@ -46,21 +64,26 @@ class Account::Holding::SyncerTest < ActiveSupport::TestCase
end
test "generates all holdings even when missing security prices" do
aapl = create_security("AMZN", prices: [
{ date: 1.day.ago.to_date, price: 215 }
])
amzn = create_security("AMZN", prices: [])
create_trade(aapl, account: @account, qty: 10, date: 2.days.ago.to_date, price: 210)
create_trade(amzn, account: @account, qty: 10, date: 2.days.ago.to_date, price: 210)
# 2 days ago — no daily price found, but since this is day of entry, we fall back to entry price
# 1 day ago — finds daily price, uses it
# Today — no daily price, no entry, so price and amount are `nil`
expected = [
{ symbol: "AMZN", qty: 10, price: 210, amount: 10 * 210, date: 2.days.ago.to_date },
{ symbol: "AMZN", qty: 10, price: 215, amount: 10 * 215, date: 1.day.ago.to_date },
{ symbol: "AMZN", qty: 10, price: nil, amount: nil, date: Date.current }
{ ticker: "AMZN", qty: 10, price: 210, amount: 10 * 210, date: 2.days.ago.to_date },
{ ticker: "AMZN", qty: 10, price: 215, amount: 10 * 215, date: 1.day.ago.to_date },
{ ticker: "AMZN", qty: 10, price: nil, amount: nil, date: Date.current }
]
Security::Price.expects(:find_prices)
.with(start_date: 2.days.ago.to_date, end_date: Date.current, ticker: "AMZN")
.once
.returns([
Security::Price.new(ticker: "AMZN", date: 1.day.ago.to_date, price: 215)
])
run_sync_for(@account)
assert_holdings(expected)
@ -71,17 +94,17 @@ class Account::Holding::SyncerTest < ActiveSupport::TestCase
def assert_holdings(expected_holdings)
holdings = @account.holdings.includes(:security).to_a
expected_holdings.each do |expected_holding|
actual_holding = holdings.find { |holding| holding.security.symbol == expected_holding[:symbol] && 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]
expected_price = expected_holding[:price]
expected_qty = expected_holding[:qty]
expected_amount = expected_holding[:amount]
symbol = expected_holding[:symbol]
ticker = expected_holding[:ticker]
assert actual_holding, "expected #{symbol} holding on date: #{date}"
assert_equal expected_holding[:qty], actual_holding.qty, "expected #{expected_qty} qty for holding #{symbol} on date: #{date}"
assert_equal expected_holding[:amount], actual_holding.amount, "expected #{expected_amount} amount for holding #{symbol} on date: #{date}"
assert_equal expected_holding[:price], actual_holding.price, "expected #{expected_price} price for holding #{symbol} 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_holding[:amount], actual_holding.amount, "expected #{expected_amount} amount for holding #{ticker} on date: #{date}"
assert_equal expected_holding[:price], actual_holding.price, "expected #{expected_price} price for holding #{ticker} on date: #{date}"
end
end

View file

@ -58,7 +58,7 @@ class Account::HoldingTest < ActiveSupport::TestCase
end
def create_holding(security, date, qty)
price = Security::Price.find_by(date: date, isin: security.isin).price
price = Security::Price.find_by(date: date, ticker: security.ticker).price
@account.holdings.create! \
date: date,

View file

@ -76,7 +76,9 @@ class AccountTest < ActiveSupport::TestCase
end
test "generates empty series if no balances and no exchange rate" do
assert_equal 0, @account.series(currency: "NZD").values.count
with_env_overrides SYNTH_API_KEY: nil do
assert_equal 0, @account.series(currency: "NZD").values.count
end
end
test "calculates shares owned of holding for date" do

View file

@ -12,7 +12,7 @@ class ExchangeRateTest < ActiveSupport::TestCase
ExchangeRate.unstub(:exchange_rates_provider)
with_env_overrides SYNTH_API_KEY: nil do
assert_nil ExchangeRate.exchange_rates_provider
assert_not ExchangeRate.exchange_rates_provider
end
end
@ -21,7 +21,7 @@ class ExchangeRateTest < ActiveSupport::TestCase
rate = exchange_rates(:one)
assert_equal exchange_rates(:one), ExchangeRate.find_rate(from: rate.from_currency, to: rate.to_currency, date: rate.date)
assert_equal rate, ExchangeRate.find_rate(from: rate.from_currency, to: rate.to_currency, date: rate.date)
end
test "finds single rate from provider and caches to DB" do
@ -38,14 +38,14 @@ class ExchangeRateTest < ActiveSupport::TestCase
test "nil if rate is not found in DB and provider throws an error" do
@provider.expects(:fetch_exchange_rate).with(from: "USD", to: "EUR", date: Date.current).once.returns(OpenStruct.new(success?: false))
assert_nil ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current)
assert_not ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current)
end
test "nil if rate is not found in DB and provider is disabled" do
ExchangeRate.unstub(:exchange_rates_provider)
with_env_overrides SYNTH_API_KEY: nil do
assert_nil ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current)
assert_not ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current)
end
end

View file

@ -2,10 +2,18 @@ require "test_helper"
require "ostruct"
class Provider::SynthTest < ActiveSupport::TestCase
include ExchangeRateProviderInterfaceTest
include ExchangeRateProviderInterfaceTest, SecurityPriceProviderInterfaceTest
setup do
@subject = @synth = Provider::Synth.new("fookey")
@subject = @synth = Provider::Synth.new(ENV["SYNTH_API_KEY"])
end
test "fetches paginated securities prices" do
VCR.use_cassette("synth/security_prices") do
response = @synth.fetch_security_prices ticker: "AAPL", start_date: Date.iso8601("2024-01-01"), end_date: Date.iso8601("2024-08-01")
assert 213, response.size
end
end
test "retries then provides failed response" do
@ -13,7 +21,7 @@ class Provider::SynthTest < ActiveSupport::TestCase
response = @synth.fetch_exchange_rate from: "USD", to: "MXN", date: Date.current
assert_match "Failed to fetch exchange rate from Provider::Synth", response.error.message
assert_match "Failed to fetch data from Provider::Synth", response.error.message
end
test "retrying, then raising on network error" do

View file

@ -1,7 +1,98 @@
require "test_helper"
require "ostruct"
class Security::PriceTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
setup do
@provider = mock
Security::Price.stubs(:security_prices_provider).returns(@provider)
end
test "security price provider nil if no api key provided" do
Security::Price.unstub(:security_prices_provider)
with_env_overrides SYNTH_API_KEY: nil do
assert_not Security::Price.security_prices_provider
end
end
test "finds single security price in DB" do
@provider.expects(:fetch_security_prices).never
price = security_prices(:one)
assert_equal price, Security::Price.find_price(ticker: price.ticker, date: price.date)
end
test "caches prices to DB" do
expected_price = 314.34
@provider.expects(:fetch_security_prices)
.once
.returns(
OpenStruct.new(
success?: true,
prices: [ { date: Date.current, price: expected_price } ]
)
)
fetched_rate = Security::Price.find_price(ticker: "NVDA", date: Date.current, cache: true)
refetched_rate = Security::Price.find_price(ticker: "NVDA", date: Date.current, cache: true)
assert_equal expected_price, fetched_rate.price
assert_equal expected_price, refetched_rate.price
end
test "returns nil if no price found in DB or from provider" do
@provider.expects(:fetch_security_prices)
.with(ticker: "NVDA", start_date: Date.current, end_date: Date.current)
.once
.returns(OpenStruct.new(success?: false))
assert_not Security::Price.find_price(ticker: "NVDA", date: Date.current)
end
test "returns nil if price not found in DB and provider disabled" do
Security::Price.unstub(:security_prices_provider)
with_env_overrides SYNTH_API_KEY: nil do
assert_not Security::Price.find_price(ticker: "NVDA", date: Date.current)
end
end
test "fetches multiple dates at once" do
@provider.expects(:fetch_security_prices).never
price1 = security_prices(:one) # AAPL today
price2 = security_prices(:two) # AAPL yesterday
fetched_prices = Security::Price.find_prices(start_date: 1.day.ago.to_date, end_date: Date.current, ticker: "AAPL").sort_by(&:date)
assert_equal price1, fetched_prices[1]
assert_equal price2, fetched_prices[0]
end
test "caches multiple prices to DB" do
missing_price = 213.21
@provider.expects(:fetch_security_prices)
.with(ticker: "AAPL", start_date: 2.days.ago.to_date, end_date: 2.days.ago.to_date)
.returns(OpenStruct.new(success?: true, prices: [ { date: 2.days.ago.to_date, price: missing_price } ]))
.once
price1 = security_prices(:one) # AAPL today
price2 = security_prices(:two) # AAPL yesterday
fetched_prices = Security::Price.find_prices(ticker: "AAPL", start_date: 2.days.ago.to_date, end_date: Date.current, cache: true)
refetched_prices = Security::Price.find_prices(ticker: "AAPL", start_date: 2.days.ago.to_date, end_date: Date.current, cache: true)
assert_equal [ missing_price, price2.price, price1.price ], fetched_prices.sort_by(&:date).map(&:price)
assert_equal [ missing_price, price2.price, price1.price ], refetched_prices.sort_by(&:date).map(&:price)
end
test "returns empty array if no prices found in DB or from provider" do
Security::Price.unstub(:security_prices_provider)
with_env_overrides SYNTH_API_KEY: nil do
assert_equal [], Security::Price.find_prices(ticker: "NVDA", start_date: 10.days.ago.to_date, end_date: Date.current)
end
end
end