2025-03-07 17:35:55 -05:00
|
|
|
require "test_helper"
|
|
|
|
|
|
|
|
class Account::Holding::PortfolioCacheTest < ActiveSupport::TestCase
|
2025-03-17 11:54:53 -04:00
|
|
|
include Account::EntriesTestHelper, ProviderTestHelper
|
2025-03-07 17:35:55 -05:00
|
|
|
|
|
|
|
setup do
|
2025-03-17 11:54:53 -04:00
|
|
|
@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
|
2025-03-07 17:35:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "gets price from DB if available" do
|
2025-03-17 11:54:53 -04:00
|
|
|
db_price = 210
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
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
|
2025-03-07 17:35:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "if no price in DB, try fetching from provider" do
|
2025-03-17 11:54:53 -04:00
|
|
|
Security::Price.delete_all
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
provider_price = Security::Price.new(
|
|
|
|
security: @security,
|
|
|
|
date: Date.current,
|
|
|
|
price: 220,
|
|
|
|
currency: "USD"
|
|
|
|
)
|
|
|
|
|
|
|
|
expect_provider_prices([ provider_price ], start_date: @account.start_date)
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
cache = Account::Holding::PortfolioCache.new(@account)
|
|
|
|
assert_equal provider_price.price, cache.get_price(@security.id, Date.current).price
|
2025-03-07 17:35:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "if no price from db or provider, try getting the price from trades" do
|
2025-03-17 11:54:53 -04:00
|
|
|
Security::Price.destroy_all
|
|
|
|
expect_provider_prices([], start_date: @account.start_date)
|
2025-03-07 17:35:55 -05:00
|
|
|
|
|
|
|
cache = Account::Holding::PortfolioCache.new(@account)
|
2025-03-17 11:54:53 -04:00
|
|
|
assert_equal @trade.price, cache.get_price(@security.id, @trade.entry.date).price
|
2025-03-07 17:35:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "if no price from db, provider, or trades, search holdings" do
|
2025-03-17 11:54:53 -04:00
|
|
|
Security::Price.delete_all
|
|
|
|
Account::Entry.delete_all
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
holding = Account::Holding.create!(
|
|
|
|
security: @security,
|
|
|
|
account: @account,
|
|
|
|
date: Date.current,
|
|
|
|
qty: 1,
|
|
|
|
price: 250,
|
|
|
|
amount: 250 * 1,
|
|
|
|
currency: "USD"
|
|
|
|
)
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
expect_provider_prices([], start_date: @account.start_date)
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
cache = Account::Holding::PortfolioCache.new(@account, use_holdings: true)
|
|
|
|
assert_equal holding.price, cache.get_price(@security.id, holding.date).price
|
2025-03-07 17:35:55 -05:00
|
|
|
end
|
2025-03-17 11:54:53 -04:00
|
|
|
|
|
|
|
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(
|
|
|
|
Security::Provideable::PricesData.new(
|
|
|
|
prices: prices
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2025-03-07 17:35:55 -05:00
|
|
|
end
|