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
|
2025-03-17 11:54:53 -04:00
|
|
|
include ProviderTestHelper
|
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
setup do
|
|
|
|
@provider = mock
|
|
|
|
|
2025-02-28 11:35:10 -05:00
|
|
|
ExchangeRate.stubs(:provider).returns(@provider)
|
2024-03-27 09:16:00 -06:00
|
|
|
end
|
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
test "finds rate in DB" do
|
|
|
|
existing_rate = exchange_rates(:one)
|
2024-03-27 09:16:00 -06:00
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
@provider.expects(:fetch_exchange_rate).never
|
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
assert_equal existing_rate, ExchangeRate.find_or_fetch_rate(
|
|
|
|
from: existing_rate.from_currency,
|
|
|
|
to: existing_rate.to_currency,
|
|
|
|
date: existing_rate.date
|
|
|
|
)
|
2024-03-27 09:16:00 -06:00
|
|
|
end
|
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
test "fetches rate from provider without cache" do
|
|
|
|
ExchangeRate.delete_all
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
provider_response = provider_success_response(
|
|
|
|
ExchangeRate::Provideable::FetchRateData.new(
|
|
|
|
rate: ExchangeRate.new(
|
|
|
|
from_currency: "USD",
|
|
|
|
to_currency: "EUR",
|
|
|
|
date: Date.current,
|
|
|
|
rate: 1.2
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2025-02-28 11:35:10 -05:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
@provider.expects(:fetch_exchange_rate).returns(provider_response)
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
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
|
2024-05-27 18:10:28 +02:00
|
|
|
end
|
2024-03-27 09:16:00 -06:00
|
|
|
end
|
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
test "fetches rate from provider with cache" do
|
|
|
|
ExchangeRate.delete_all
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
provider_response = provider_success_response(
|
|
|
|
ExchangeRate::Provideable::FetchRateData.new(
|
|
|
|
rate: ExchangeRate.new(
|
|
|
|
from_currency: "USD",
|
|
|
|
to_currency: "EUR",
|
|
|
|
date: Date.current,
|
|
|
|
rate: 1.2
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
@provider.expects(:fetch_exchange_rate).returns(provider_response)
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
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
|
2024-07-08 09:04:59 -04:00
|
|
|
end
|
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
test "returns nil on provider error" do
|
|
|
|
provider_response = provider_error_response(Provider::ProviderError.new("Test error"))
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
@provider.expects(:fetch_exchange_rate).returns(provider_response)
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
assert_nil ExchangeRate.find_or_fetch_rate(from: "USD", to: "EUR", date: Date.current, cache: true)
|
2024-07-08 09:04:59 -04:00
|
|
|
end
|
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
test "upserts rates for currency pair and date range" do
|
|
|
|
ExchangeRate.delete_all
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
ExchangeRate.create!(date: 1.day.ago.to_date, from_currency: "USD", to_currency: "EUR", rate: 0.9)
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
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)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
@provider.expects(:fetch_exchange_rates)
|
|
|
|
.with(from: "USD", to: "EUR", start_date: 2.days.ago.to_date, end_date: Date.current)
|
|
|
|
.returns(provider_response)
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
ExchangeRate.sync_provider_rates(from: "USD", to: "EUR", start_date: 2.days.ago.to_date)
|
2025-02-28 11:35:10 -05:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
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
|
2024-03-27 09:16:00 -06:00
|
|
|
end
|
2024-02-10 16:18:56 -06:00
|
|
|
end
|