1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 15:49:39 +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,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