1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-02 20:15:22 +02:00

Data provider simplification, tests, and documentation (#1997)

* Ignore env.test from source control

* Simplification of providers interface

* Synth tests

* Update money to use new find rates method

* Remove unused issues code

* Additional issue feature removals

* Update price data fetching and tests

* Update documentation for providers

* Security test fixes

* Fix self host test

* Update synth usage data access

* Remove AI pr schema changes
This commit is contained in:
Zach Gollwitzer 2025-03-17 11:54:53 -04:00 committed by GitHub
parent dd75cadebc
commit f65b93a352
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
95 changed files with 2014 additions and 1638 deletions

View file

@ -2,7 +2,7 @@ require "test_helper"
require "ostruct"
class Account::ConvertibleTest < ActiveSupport::TestCase
include Account::EntriesTestHelper
include Account::EntriesTestHelper, ProviderTestHelper
setup do
@family = families(:empty)
@ -16,33 +16,28 @@ class Account::ConvertibleTest < ActiveSupport::TestCase
end
test "syncs required exchange rates for an account" do
create_valuation(account: @account, date: 5.days.ago.to_date, amount: 9500, currency: "EUR")
create_valuation(account: @account, date: 1.day.ago.to_date, amount: 9500, currency: "EUR")
# Since we had a valuation 5 days ago, this account starts 6 days ago and needs daily exchange rates looking forward
assert_equal 6.days.ago.to_date, @account.start_date
# Since we had a valuation 1 day ago, this account starts 2 days ago and needs daily exchange rates looking forward
assert_equal 2.days.ago.to_date, @account.start_date
ExchangeRate.delete_all
provider_response = provider_success_response(
ExchangeRate::Provideable::FetchRatesData.new(
rates: [
ExchangeRate.new(from_currency: "EUR", to_currency: "USD", date: 2.days.ago.to_date, rate: 1.1),
ExchangeRate.new(from_currency: "EUR", to_currency: "USD", date: 1.day.ago.to_date, rate: 1.2),
ExchangeRate.new(from_currency: "EUR", to_currency: "USD", date: Date.current, rate: 1.3)
]
)
)
@provider.expects(:fetch_exchange_rates)
.with(
from: "EUR",
to: "USD",
start_date: 6.days.ago.to_date,
end_date: Date.current
).returns(
OpenStruct.new(
success?: true,
rates: [
OpenStruct.new(date: 6.days.ago.to_date, rate: 1.1),
OpenStruct.new(date: 5.days.ago.to_date, rate: 1.2),
OpenStruct.new(date: 4.days.ago.to_date, rate: 1.3),
OpenStruct.new(date: 3.days.ago.to_date, rate: 1.4),
OpenStruct.new(date: 2.days.ago.to_date, rate: 1.5),
OpenStruct.new(date: 1.day.ago.to_date, rate: 1.6),
OpenStruct.new(date: Date.current, rate: 1.7)
]
)
)
.with(from: "EUR", to: "USD", start_date: 2.days.ago.to_date, end_date: Date.current)
.returns(provider_response)
assert_difference "ExchangeRate.count", 7 do
assert_difference "ExchangeRate.count", 3 do
@account.sync_required_exchange_rates
end
end

View file

@ -1,63 +1,93 @@
require "test_helper"
class Account::Holding::PortfolioCacheTest < ActiveSupport::TestCase
include Account::EntriesTestHelper
include Account::EntriesTestHelper, ProviderTestHelper
setup do
# Prices, highest to lowest priority
@db_price = 210
@provider_price = 220
@trade_price = 200
@holding_price = 250
@provider = mock
Security.stubs(:provider).returns(@provider)
@account = families(:empty).accounts.create!(name: "Test Brokerage", balance: 10000, currency: "USD", accountable: Investment.new)
@test_security = Security.create!(name: "Test Security", ticker: "TEST")
@account = families(:empty).accounts.create!(
name: "Test Brokerage",
balance: 10000,
currency: "USD",
accountable: Investment.new
)
@trade = create_trade(@test_security, account: @account, qty: 1, date: Date.current, price: @trade_price)
@holding = Account::Holding.create!(security: @test_security, account: @account, date: Date.current, qty: 1, price: @holding_price, amount: @holding_price, currency: "USD")
Security::Price.create!(security: @test_security, date: Date.current, price: @db_price)
@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
cache = Account::Holding::PortfolioCache.new(@account)
db_price = 210
assert_equal @db_price, cache.get_price(@test_security.id, Date.current).price
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.destroy_all
Security::Price.expects(:find_prices)
.with(security: @test_security, start_date: @account.start_date, end_date: Date.current)
.returns([
Security::Price.new(security: @test_security, date: Date.current, price: @provider_price, currency: "USD")
])
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, cache.get_price(@test_security.id, Date.current).price
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 # No DB prices
Security::Price.expects(:find_prices)
.with(security: @test_security, start_date: @account.start_date, end_date: Date.current)
.returns([]) # No provider prices
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(@test_security.id, Date.current).price
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.destroy_all # No DB prices
Security::Price.expects(:find_prices)
.with(security: @test_security, start_date: @account.start_date, end_date: Date.current)
.returns([]) # No provider prices
Security::Price.delete_all
Account::Entry.delete_all
@account.entries.destroy_all # No prices from trades
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(@test_security.id, Date.current).price
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(
Security::Provideable::PricesData.new(
prices: prices
)
)
)
end
end

View file

@ -2,116 +2,99 @@ require "test_helper"
require "ostruct"
class ExchangeRateTest < ActiveSupport::TestCase
include ProviderTestHelper
setup do
@provider = mock
ExchangeRate.stubs(:provider).returns(@provider)
end
test "exchange rate provider nil if no api key configured" do
ExchangeRate.unstub(:provider)
test "finds rate in DB" do
existing_rate = exchange_rates(:one)
Setting.stubs(:synth_api_key).returns(nil)
with_env_overrides SYNTH_API_KEY: nil do
assert_not ExchangeRate.provider
end
end
test "finds single rate in DB" do
@provider.expects(:fetch_exchange_rate).never
rate = exchange_rates(:one)
assert_equal rate, ExchangeRate.find_rate(from: rate.from_currency, to: rate.to_currency, date: rate.date)
assert_equal existing_rate, ExchangeRate.find_or_fetch_rate(
from: existing_rate.from_currency,
to: existing_rate.to_currency,
date: existing_rate.date
)
end
test "finds single rate from provider and caches to DB" do
expected_rate = 1.21
@provider.expects(:fetch_exchange_rate).once.returns(OpenStruct.new(success?: true, rate: expected_rate))
test "fetches rate from provider without cache" do
ExchangeRate.delete_all
fetched_rate = ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current, cache: true)
refetched_rate = ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current, cache: true)
provider_response = provider_success_response(
ExchangeRate::Provideable::FetchRateData.new(
rate: ExchangeRate.new(
from_currency: "USD",
to_currency: "EUR",
date: Date.current,
rate: 1.2
)
)
)
assert_equal expected_rate, fetched_rate.rate
assert_equal expected_rate, refetched_rate.rate
end
@provider.expects(:fetch_exchange_rate).returns(provider_response)
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_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(:provider)
Setting.stubs(:synth_api_key).returns(nil)
with_env_overrides SYNTH_API_KEY: nil do
assert_not ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current)
assert_no_difference "ExchangeRate.count" do
assert_equal 1.2, ExchangeRate.find_or_fetch_rate(from: "USD", to: "EUR", date: Date.current, cache: false).rate
end
end
test "finds multiple rates in DB" do
@provider.expects(:fetch_exchange_rate).never
test "fetches rate from provider with cache" do
ExchangeRate.delete_all
rate1 = exchange_rates(:one) # EUR -> GBP, today
rate2 = exchange_rates(:two) # EUR -> GBP, yesterday
fetched_rates = ExchangeRate.find_rates(from: rate1.from_currency, to: rate1.to_currency, start_date: 1.day.ago.to_date).sort_by(&:date)
assert_equal rate1, fetched_rates[1]
assert_equal rate2, fetched_rates[0]
end
test "finds multiple rates from provider and caches to DB" do
@provider.expects(:fetch_exchange_rates).with(from: "EUR", to: "USD", start_date: 1.day.ago.to_date, end_date: Date.current)
.returns(
OpenStruct.new(
rates: [
OpenStruct.new(date: 1.day.ago.to_date, rate: 1.1),
OpenStruct.new(date: Date.current, rate: 1.2)
],
success?: true
provider_response = provider_success_response(
ExchangeRate::Provideable::FetchRateData.new(
rate: ExchangeRate.new(
from_currency: "USD",
to_currency: "EUR",
date: Date.current,
rate: 1.2
)
).once
)
)
fetched_rates = ExchangeRate.find_rates(from: "EUR", to: "USD", start_date: 1.day.ago.to_date, cache: true)
refetched_rates = ExchangeRate.find_rates(from: "EUR", to: "USD", start_date: 1.day.ago.to_date)
@provider.expects(:fetch_exchange_rate).returns(provider_response)
assert_equal [ 1.1, 1.2 ], fetched_rates.sort_by(&:date).map(&:rate)
assert_equal [ 1.1, 1.2 ], refetched_rates.sort_by(&:date).map(&:rate)
end
test "finds missing db rates from provider and appends to results" do
@provider.expects(:fetch_exchange_rates).with(from: "EUR", to: "GBP", start_date: 2.days.ago.to_date, end_date: 2.days.ago.to_date)
.returns(
OpenStruct.new(
rates: [
OpenStruct.new(date: 2.day.ago.to_date, rate: 1.1)
],
success?: true
)
).once
rate1 = exchange_rates(:one) # EUR -> GBP, today
rate2 = exchange_rates(:two) # EUR -> GBP, yesterday
fetched_rates = ExchangeRate.find_rates(from: "EUR", to: "GBP", start_date: 2.days.ago.to_date, cache: true)
refetched_rates = ExchangeRate.find_rates(from: "EUR", to: "GBP", start_date: 2.days.ago.to_date)
assert_equal [ 1.1, rate2.rate, rate1.rate ], fetched_rates.sort_by(&:date).map(&:rate)
assert_equal [ 1.1, rate2.rate, rate1.rate ], refetched_rates.sort_by(&:date).map(&:rate)
end
test "returns empty array if no rates found in DB or provider" do
ExchangeRate.unstub(:provider)
Setting.stubs(:synth_api_key).returns(nil)
with_env_overrides SYNTH_API_KEY: nil do
assert_equal [], ExchangeRate.find_rates(from: "USD", to: "JPY", start_date: 10.days.ago.to_date)
assert_difference "ExchangeRate.count", 1 do
assert_equal 1.2, ExchangeRate.find_or_fetch_rate(from: "USD", to: "EUR", date: Date.current, cache: true).rate
end
end
test "returns nil on provider error" do
provider_response = provider_error_response(Provider::ProviderError.new("Test error"))
@provider.expects(:fetch_exchange_rate).returns(provider_response)
assert_nil ExchangeRate.find_or_fetch_rate(from: "USD", to: "EUR", date: Date.current, cache: true)
end
test "upserts rates for currency pair and date range" do
ExchangeRate.delete_all
ExchangeRate.create!(date: 1.day.ago.to_date, from_currency: "USD", to_currency: "EUR", rate: 0.9)
provider_response = provider_success_response(
ExchangeRate::Provideable::FetchRatesData.new(
rates: [
ExchangeRate.new(from_currency: "USD", to_currency: "EUR", date: Date.current, rate: 1.3),
ExchangeRate.new(from_currency: "USD", to_currency: "EUR", date: 1.day.ago.to_date, rate: 1.4),
ExchangeRate.new(from_currency: "USD", to_currency: "EUR", date: 2.days.ago.to_date, rate: 1.5)
]
)
)
@provider.expects(:fetch_exchange_rates)
.with(from: "USD", to: "EUR", start_date: 2.days.ago.to_date, end_date: Date.current)
.returns(provider_response)
ExchangeRate.sync_provider_rates(from: "USD", to: "EUR", start_date: 2.days.ago.to_date)
assert_equal 1.3, ExchangeRate.find_by(from_currency: "USD", to_currency: "EUR", date: Date.current).rate
assert_equal 1.4, ExchangeRate.find_by(from_currency: "USD", to_currency: "EUR", date: 1.day.ago.to_date).rate
assert_equal 1.5, ExchangeRate.find_by(from_currency: "USD", to_currency: "EUR", date: 2.days.ago.to_date).rate
end
end

View file

@ -2,55 +2,42 @@ require "test_helper"
require "ostruct"
class Provider::SynthTest < ActiveSupport::TestCase
include ExchangeRateProviderInterfaceTest, SecurityPriceProviderInterfaceTest
include ExchangeRateProviderInterfaceTest, SecurityProviderInterfaceTest
setup do
@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",
mic_code: "XNAS",
start_date: Date.iso8601("2024-01-01"),
end_date: Date.iso8601("2024-08-01")
)
assert 213, response.size
test "health check" do
VCR.use_cassette("synth/health") do
assert @synth.healthy?
end
end
test "fetches paginated exchange_rate historical data" do
VCR.use_cassette("synth/exchange_rate_historical") do
response = @synth.fetch_exchange_rates(
from: "USD", to: "GBP", start_date: Date.parse("01.01.2024"), end_date: Date.parse("31.07.2024")
)
assert 213, response.rates.size # 213 days between 01.01.2024 and 31.07.2024
assert_equal [ :date, :rate ], response.rates.first.keys
test "usage info" do
VCR.use_cassette("synth/usage") do
usage = @synth.usage.data
assert usage.used.present?
assert usage.limit.present?
assert usage.utilization.present?
assert usage.plan.present?
end
end
test "retries then provides failed response" do
@client = mock
Faraday.stubs(:new).returns(@client)
test "enriches transaction" do
VCR.use_cassette("synth/transaction_enrich") do
response = @synth.enrich_transaction(
"UBER EATS",
amount: 25.50,
date: Date.iso8601("2025-03-16"),
city: "San Francisco",
state: "CA",
country: "US"
)
@client.expects(:get).returns(OpenStruct.new(success?: false)).times(3)
response = @synth.fetch_exchange_rate from: "USD", to: "MXN", date: Date.iso8601("2024-08-01")
assert_match "Failed to fetch data from Provider::Synth", response.error.message
end
test "retrying, then raising on network error" do
@client = mock
Faraday.stubs(:new).returns(@client)
@client.expects(:get).raises(Faraday::TimeoutError).times(3)
assert_raises Faraday::TimeoutError do
@synth.fetch_exchange_rate from: "USD", to: "MXN", date: Date.iso8601("2024-08-01")
data = response.data
assert data.name.present?
assert data.category.present?
end
end
end

View file

@ -0,0 +1,61 @@
require "test_helper"
require "ostruct"
class TestProvider < Provider
def fetch_data
provider_response(retries: 3) do
client.get("/test")
end
end
private
def client
@client ||= Faraday.new
end
def retryable_errors
[ Faraday::TimeoutError ]
end
end
class ProviderTest < ActiveSupport::TestCase
setup do
@provider = TestProvider.new
end
test "retries then provides failed response" do
client = mock
Faraday.stubs(:new).returns(client)
client.expects(:get)
.with("/test")
.raises(Faraday::TimeoutError)
.times(3)
response = @provider.fetch_data
assert_not response.success?
assert_match "timeout", response.error.message
end
test "fail, retry, succeed" do
client = mock
Faraday.stubs(:new).returns(client)
sequence = sequence("retry_sequence")
client.expects(:get)
.with("/test")
.raises(Faraday::TimeoutError)
.in_sequence(sequence)
client.expects(:get)
.with("/test")
.returns(Provider::ProviderResponse.new(success?: true, data: "success", error: nil))
.in_sequence(sequence)
response = @provider.fetch_data
assert response.success?
end
end

View file

@ -0,0 +1,27 @@
require "test_helper"
class ProvidersTest < ActiveSupport::TestCase
test "synth configured with ENV" do
Setting.stubs(:synth_api_key).returns(nil)
with_env_overrides SYNTH_API_KEY: "123" do
assert_instance_of Provider::Synth, Providers.synth
end
end
test "synth configured with Setting" do
Setting.stubs(:synth_api_key).returns("123")
with_env_overrides SYNTH_API_KEY: nil do
assert_instance_of Provider::Synth, Providers.synth
end
end
test "synth not configured" do
Setting.stubs(:synth_api_key).returns(nil)
with_env_overrides SYNTH_API_KEY: nil do
assert_nil Providers.synth
end
end
end

View file

@ -2,120 +2,82 @@ require "test_helper"
require "ostruct"
class Security::PriceTest < ActiveSupport::TestCase
include ProviderTestHelper
setup do
@provider = mock
Security.stubs(:provider).returns(@provider)
Security::Price.stubs(:provider).returns(@provider)
end
test "security price provider nil if no api key provided" do
Security::Price.unstub(:provider)
Setting.stubs(:synth_api_key).returns(nil)
with_env_overrides SYNTH_API_KEY: nil do
assert_not Security::Price.provider
end
@security = securities(:aapl)
end
test "finds single security price in DB" do
@provider.expects(:fetch_security_prices).never
security = securities(:aapl)
@provider.expects(:fetch_security_price).never
price = security_prices(:one)
assert_equal price, Security::Price.find_price(security: security, date: price.date)
assert_equal price, @security.find_or_fetch_price(date: price.date)
end
test "caches prices to DB" do
expected_price = 314.34
security = securities(:aapl)
tomorrow = Date.current + 1.day
test "caches prices from provider to DB" do
price_date = 10.days.ago.to_date
@provider.expects(:fetch_security_prices)
.with(ticker: security.ticker, mic_code: security.exchange_operating_mic, start_date: tomorrow, end_date: tomorrow)
.once
.returns(
OpenStruct.new(
success?: true,
prices: [ { date: tomorrow, price: expected_price, currency: "USD" } ]
)
)
expected_price = Security::Price.new(
security: @security,
date: price_date,
price: 314.34,
currency: "USD"
)
fetched_rate = Security::Price.find_price(security: security, date: tomorrow, cache: true)
refetched_rate = Security::Price.find_price(security: security, date: tomorrow, cache: true)
expect_provider_price(security: @security, price: expected_price, date: price_date)
assert_equal expected_price, fetched_rate.price
assert_equal expected_price, refetched_rate.price
assert_difference "Security::Price.count", 1 do
fetched_price = @security.find_or_fetch_price(date: price_date, cache: true)
assert_equal expected_price.price, fetched_price.price
end
end
test "returns nil if no price found in DB or from provider" do
security = securities(:aapl)
Security::Price.delete_all # Clear any existing prices
@provider.expects(:fetch_security_prices)
.with(ticker: security.ticker, mic_code: security.exchange_operating_mic, start_date: Date.current, end_date: Date.current)
.once
.returns(OpenStruct.new(success?: false))
provider_response = provider_error_response(Provider::ProviderError.new("Test error"))
assert_not Security::Price.find_price(security: security, date: Date.current)
@provider.expects(:fetch_security_price)
.with(security, date: Date.current)
.returns(provider_response)
assert_not @security.find_or_fetch_price(date: Date.current)
end
test "returns nil if price not found in DB and provider disabled" do
Security::Price.unstub(:provider)
test "upserts historical prices from provider" do
Security::Price.delete_all
Setting.stubs(:synth_api_key).returns(nil)
# Will be overwritten by upsert
Security::Price.create!(security: @security, date: 1.day.ago.to_date, price: 190, currency: "USD")
security = Security.new(ticker: "NVDA")
expect_provider_prices(security: @security, start_date: 2.days.ago.to_date, end_date: Date.current, prices: [
Security::Price.new(security: @security, date: Date.current, price: 215, currency: "USD"),
Security::Price.new(security: @security, date: 1.day.ago.to_date, price: 214, currency: "USD"),
Security::Price.new(security: @security, date: 2.days.ago.to_date, price: 213, currency: "USD")
])
with_env_overrides SYNTH_API_KEY: nil do
assert_not Security::Price.find_price(security: security, date: Date.current)
@security.sync_provider_prices(start_date: 2.days.ago.to_date)
assert_equal 215, @security.prices.find_by(date: Date.current).price
assert_equal 214, @security.prices.find_by(date: 1.day.ago.to_date).price
assert_equal 213, @security.prices.find_by(date: 2.days.ago.to_date).price
end
private
def expect_provider_price(security:, price:, date:)
@provider.expects(:fetch_security_price)
.with(security, date: date)
.returns(provider_success_response(Security::Provideable::PriceData.new(price: price)))
end
end
test "fetches multiple dates at once" do
@provider.expects(:fetch_security_prices).never
security = securities(:aapl)
price1 = security_prices(:one) # AAPL today
price2 = security_prices(:two) # AAPL yesterday
fetched_prices = Security::Price.find_prices(security: security, start_date: 1.day.ago.to_date, end_date: Date.current).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
security = securities(:aapl)
@provider.expects(:fetch_security_prices)
.with(ticker: security.ticker,
mic_code: security.exchange_operating_mic,
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, currency: "USD" } ]))
.once
price1 = security_prices(:one) # AAPL today
price2 = security_prices(:two) # AAPL yesterday
fetched_prices = Security::Price.find_prices(security: security, start_date: 2.days.ago.to_date, end_date: Date.current, cache: true)
refetched_prices = Security::Price.find_prices(security: security, 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)
assert Security::Price.exists?(security: security, date: 2.days.ago.to_date, price: missing_price)
end
test "returns empty array if no prices found in DB or from provider" do
Security::Price.unstub(:provider)
Setting.stubs(:synth_api_key).returns(nil)
with_env_overrides SYNTH_API_KEY: nil do
assert_equal [], Security::Price.find_prices(security: Security.new(ticker: "NVDA"), start_date: 10.days.ago.to_date, end_date: Date.current)
def expect_provider_prices(security:, prices:, start_date:, end_date:)
@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
end
end

View file

@ -6,6 +6,8 @@ class TradeImportTest < ActiveSupport::TestCase
setup do
@subject = @import = imports(:trade)
@provider = mock
Security.stubs(:provider).returns(@provider)
end
test "imports trades and accounts" do
@ -14,7 +16,7 @@ class TradeImportTest < ActiveSupport::TestCase
# We should only hit the provider for GOOGL since AAPL already exists
Security.expects(:search_provider).with(
query: "GOOGL",
"GOOGL",
exchange_operating_mic: "XNAS"
).returns([
Security.new(