The personal finance app for everyone (by everyone)
Ruby
Reject Yahoo exchange rate quotes that come without a currency (#4437)
Artem Danilov020f6fa3d39c
diff --git a/app/models/provider/yahoo_finance.rb b/app/models/provider/yahoo_finance.rbindex 9093ce0707..869bad02b9 100644--- a/app/models/provider/yahoo_finance.rb+++ b/app/models/provider/yahoo_finance.rb@@ -805,7 +805,7 @@ class Provider::YahooFinance < Providerdef fetch_currency_pair_data(from, to, start_date, end_date)symbol = "#{from}#{to}=X"- fetch_chart_data(symbol, start_date, end_date) do |timestamp, close_rate|+ fetch_chart_data(symbol, start_date, end_date, currency: to) do |timestamp, close_rate|Rate.new(date: Time.at(timestamp).utc.to_date,from: from,@@ -817,7 +817,7 @@ class Provider::YahooFinance < Providerdef fetch_inverse_currency_pair_data(from, to, start_date, end_date)symbol = "#{to}#{from}=X"- rates = fetch_chart_data(symbol, start_date, end_date) do |timestamp, close_rate|+ rates = fetch_chart_data(symbol, start_date, end_date, currency: from) do |timestamp, close_rate|Rate.new(date: Time.at(timestamp).utc.to_date,from: from,@@ -874,7 +874,9 @@ class Provider::YahooFinance < Providerdataend- def fetch_chart_data(symbol, start_date, end_date, &block)+ # `currency` is what the quote must be in. Yahoo leaves it blank on some+ # scaled crosses: RUBGEL=X is GEL per 100 RUB, a 100x error once inverted.+ def fetch_chart_data(symbol, start_date, end_date, currency:, &block)period1 = start_date.to_time.utc.to_iperiod2 = end_date.end_of_day.to_time.utc.to_i@@ -895,6 +897,19 @@ class Provider::YahooFinance < Providerchart_data = data.dig("chart", "result", 0)return nil unless chart_data+ quote_currency = chart_data.dig("meta", "currency")+ unless quote_currency&.casecmp?(currency)+ DebugLogEntry.capture(+ category: "exchange_rates",+ level: "warn",+ message: "Yahoo Finance quote for #{symbol} is in #{quote_currency.presence || "no currency"}, not #{currency}; ignored",+ source: self.class.name,+ provider_key: "yahoo_finance",+ metadata: { symbol: symbol, expected_currency: currency, quote_currency: quote_currency }+ )+ return nil+ end+timestamps = chart_data.dig("timestamp") || []quotes = chart_data.dig("indicators", "quote", 0) || {}closes = quotes["close"] || []diff --git a/test/models/provider/yahoo_finance_test.rb b/test/models/provider/yahoo_finance_test.rbindex 93106a0d7f..73ca5a6637 100644--- a/test/models/provider/yahoo_finance_test.rb+++ b/test/models/provider/yahoo_finance_test.rb@@ -383,6 +383,47 @@ class Provider::YahooFinanceTest < ActiveSupport::TestCase# Exchange Rate Tests# ================================+ test "fetch_exchange_rates rejects a quote Yahoo gives no currency for" do+ @provider.stubs(:throttle_request)+ date = Date.new(2026, 10, 22)+ @provider.stubs(:fetch_authenticated_chart).with("GELRUB=X", anything)+ .returns("chart" => { "error" => { "code" => "Not Found" } })+ # Live on that date: GEL per 100 RUB, with no currency.+ @provider.stubs(:fetch_authenticated_chart).with("RUBGEL=X", anything).returns(fx_chart(date, nil, 3.1481))+ DebugLogEntry.expects(:capture).with do |attributes|+ attributes[:category] == "exchange_rates" &&+ attributes[:metadata] == { symbol: "RUBGEL=X", expected_currency: "GEL", quote_currency: nil }+ end++ response = @provider.fetch_exchange_rates(from: "GEL", to: "RUB", start_date: date, end_date: date)++ assert_not response.success?+ end++ test "fetch_exchange_rates takes a quote in the pair's currency as is and inverts the reverse quote" do+ @provider.stubs(:throttle_request)+ date = Date.new(2026, 10, 22)+ @provider.stubs(:fetch_authenticated_chart).with("EURUSD=X", anything).returns(fx_chart(date, "USD", 1.12))+ @provider.stubs(:fetch_authenticated_chart).with("USDEUR=X", anything)+ .returns("chart" => { "error" => { "code" => "Not Found" } })++ eur_usd = @provider.fetch_exchange_rates(from: "EUR", to: "USD", start_date: date, end_date: date)+ usd_eur = @provider.fetch_exchange_rates(from: "USD", to: "EUR", start_date: date, end_date: date)++ assert_in_delta 1.12, eur_usd.data.first.rate.to_f+ assert_in_delta 1 / 1.12, usd_eur.data.first.rate.to_f, 1e-9+ end++ test "fetch_exchange_rates matches the quote currency regardless of case" do+ @provider.stubs(:throttle_request)+ date = Date.new(2026, 10, 22)+ @provider.stubs(:fetch_authenticated_chart).with("eurusd=X", anything).returns(fx_chart(date, "USD", 1.12))++ response = @provider.fetch_exchange_rates(from: "eur", to: "usd", start_date: date, end_date: date)++ assert_in_delta 1.12, response.data.first.rate.to_f+ end+test "fetch_exchange_rate returns 1.0 for same currency" dodate = Date.parse("2024-01-15")response = @provider.fetch_exchange_rate(from: "USD", to: "USD", date: date)@@ -849,7 +890,7 @@ class Provider::YahooFinanceTest < ActiveSupport::TestCaseattributes[:metadata][:exception_class] == "Faraday::ConnectionFailed"end- result = @provider.send(:fetch_chart_data, "EURUSD=X", Date.current - 5.days, Date.current) { |ts, close| close }+ result = @provider.send(:fetch_chart_data, "EURUSD=X", Date.current - 5.days, Date.current, currency: "USD") { |ts, close| close }assert_nil resultend@@ -1139,6 +1180,14 @@ class Provider::YahooFinanceTest < ActiveSupport::TestCaseprovider.stubs(:health_auth_client).returns(auth_client)end+ def fx_chart(date, currency, close)+ { "chart" => { "result" => [ {+ "meta" => { "currency" => currency },+ "timestamp" => [ date.to_time.utc.to_i ],+ "indicators" => { "quote" => [ { "close" => [ close ] } ] }+ } ] } }+ end+def stub_health_chart_responses(*responses, provider: @provider)chart_client = mockchart_client.expects(:get).times(responses.length).returns(*responses)