2024-02-10 16:18:56 -06:00
|
|
|
require "test_helper"
|
2024-05-02 10:18:06 -04:00
|
|
|
require "ostruct"
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2024-02-10 16:18:56 -06:00
|
|
|
class ExchangeRateTest < ActiveSupport::TestCase
|
2024-07-08 09:04:59 -04:00
|
|
|
setup do
|
|
|
|
@provider = mock
|
|
|
|
|
|
|
|
ExchangeRate.stubs(:exchange_rates_provider).returns(@provider)
|
2024-03-27 09:16:00 -06:00
|
|
|
end
|
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
test "exchange rate provider nil if no api key configured" do
|
|
|
|
ExchangeRate.unstub(:exchange_rates_provider)
|
2024-03-27 09:16:00 -06:00
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
with_env_overrides SYNTH_API_KEY: nil do
|
2024-08-01 19:43:23 -04:00
|
|
|
assert_not ExchangeRate.exchange_rates_provider
|
2024-05-27 18:10:28 +02:00
|
|
|
end
|
2024-03-27 09:16:00 -06:00
|
|
|
end
|
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
test "finds single rate in DB" do
|
|
|
|
@provider.expects(:fetch_exchange_rate).never
|
|
|
|
|
|
|
|
rate = exchange_rates(:one)
|
|
|
|
|
2024-08-01 19:43:23 -04:00
|
|
|
assert_equal rate, ExchangeRate.find_rate(from: rate.from_currency, to: rate.to_currency, date: rate.date)
|
2024-03-27 09:16:00 -06:00
|
|
|
end
|
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
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))
|
|
|
|
|
|
|
|
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)
|
2024-03-27 09:16:00 -06:00
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
assert_equal expected_rate, fetched_rate.rate
|
|
|
|
assert_equal expected_rate, refetched_rate.rate
|
|
|
|
end
|
2024-03-27 09:16:00 -06:00
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
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))
|
|
|
|
|
2024-08-01 19:43:23 -04:00
|
|
|
assert_not ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current)
|
2024-07-08 09:04:59 -04:00
|
|
|
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
|
2024-08-01 19:43:23 -04:00
|
|
|
assert_not ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current)
|
2024-05-27 18:10:28 +02:00
|
|
|
end
|
2024-03-27 09:16:00 -06:00
|
|
|
end
|
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
test "finds multiple rates in DB" do
|
|
|
|
@provider.expects(:fetch_exchange_rate).never
|
|
|
|
|
|
|
|
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_rate).with(from: "EUR", to: "USD", date: 1.day.ago.to_date).returns(OpenStruct.new(success?: true, rate: 1.1)).once
|
|
|
|
@provider.expects(:fetch_exchange_rate).with(from: "EUR", to: "USD", date: Date.current).returns(OpenStruct.new(success?: true, 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)
|
|
|
|
|
|
|
|
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_rate).with(from: "EUR", to: "GBP", date: 2.days.ago.to_date).returns(OpenStruct.new(success?: true, rate: 1.1)).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(:exchange_rates_provider)
|
2024-03-27 09:16:00 -06:00
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
with_env_overrides SYNTH_API_KEY: nil do
|
|
|
|
assert_equal [], ExchangeRate.find_rates(from: "USD", to: "JPY", start_date: 10.days.ago.to_date)
|
2024-03-27 09:16:00 -06:00
|
|
|
end
|
|
|
|
end
|
2024-02-10 16:18:56 -06:00
|
|
|
end
|