1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-23 23:29:39 +02:00
Maybe/test/models/exchange_rate_test.rb
Jakub Kottnauer 483d67846c
Fix foreign account sync crash (#794)
* Fix foreign account sync crash

* Refactor synth provider and show UI error if not configured

* Generate error message on missing exchange rates while converting balances

* Ignore sync messaged in i18n-tasks unused

* Generate missing exchange rate error during entry normalization

* Update alert classes
2024-05-27 12:10:28 -04:00

50 lines
1.7 KiB
Ruby

require "test_helper"
require "ostruct"
class ExchangeRateTest < ActiveSupport::TestCase
test "find rate in db" do
assert_equal exchange_rates(:day_29_ago_eur_to_usd),
ExchangeRate.find_rate_or_fetch(from: "EUR", to: "USD", date: 29.days.ago.to_date)
end
test "fetch rate from provider when it's not found in db" do
with_env_overrides SYNTH_API_KEY: "true" do
ExchangeRate
.expects(:fetch_rate_from_provider)
.returns(ExchangeRate.new(base_currency: "USD", converted_currency: "MXN", rate: 1.0, date: Date.current))
ExchangeRate.find_rate_or_fetch from: "USD", to: "MXN", date: Date.current
end
end
test "provided rates are saved to the db" do
with_env_overrides SYNTH_API_KEY: "true" do
VCR.use_cassette "synth_exchange_rate" do
assert_difference "ExchangeRate.count", 1 do
ExchangeRate.find_rate_or_fetch from: "USD", to: "MXN", date: Date.current
end
end
end
end
test "retrying, then raising on provider error" do
with_env_overrides SYNTH_API_KEY: "true" do
Faraday.expects(:get).returns(OpenStruct.new(success?: false)).times(3)
error = assert_raises Provider::Base::ProviderError do
ExchangeRate.find_rate_or_fetch from: "USD", to: "MXN", date: Date.current
end
assert_match "Failed to fetch exchange rate from Provider::Synth", error.message
end
end
test "retrying, then raising on network error" do
with_env_overrides SYNTH_API_KEY: "true" do
Faraday.expects(:get).raises(Faraday::TimeoutError).times(3)
assert_raises Faraday::TimeoutError do
ExchangeRate.find_rate_or_fetch from: "USD", to: "MXN", date: Date.current
end
end
end
end