The personal finance app for everyone (by everyone)
Ruby
Map Yahoo's Xetra exchange code to XETR (#4963)
arnaurodondev2fc289c4c2ae
diff --git a/app/models/provider/yahoo_finance.rb b/app/models/provider/yahoo_finance.rbindex 869bad02b9..d51c88daa0 100644--- a/app/models/provider/yahoo_finance.rb+++ b/app/models/provider/yahoo_finance.rb@@ -1222,6 +1222,8 @@ class Provider::YahooFinance < Provider"XTSX" # TSX Venture Exchangewhen "LSE", "LON""XLON" # London Stock Exchange+ when "GER"+ "XETR" # Xetrawhen "FRA""XFRA" # Frankfurt Stock Exchangewhen "PAR"diff --git a/app/models/security.rb b/app/models/security.rbindex e5753d5006..e6cb19239b 100644--- a/app/models/security.rb+++ b/app/models/security.rb@@ -12,7 +12,8 @@ class Security < ApplicationRecord# Legacy non-ISO values previously persisted as exchange_operating_mic# (e.g. raw EODHD exchange codes) mapped to their ISO MIC.MIC_ALIASES = {- "WAR" => "XWAR"+ "WAR" => "XWAR",+ "GER" => "XETR" # Yahoo search code for Xetra, stored before it was mapped}.freezeKINDS = %w[standard cash].freeze@@ -298,7 +299,12 @@ class Security < ApplicationRecorddef upcase_symbolsself.ticker = ticker.upcase- self.exchange_operating_mic = self.class.canonical_exchange_operating_mic(exchange_operating_mic) if exchange_operating_mic.present?+ # Only canonicalize a MIC being written. A stored legacy alias is upgraded by+ # find_by_ticker_and_exchange, which checks for a canonical twin first;+ # rewriting it on an unrelated save would collide with that twin.+ if exchange_operating_mic.present? && will_save_change_to_exchange_operating_mic?+ self.exchange_operating_mic = self.class.canonical_exchange_operating_mic(exchange_operating_mic)+ endenddef should_generate_logo?diff --git a/test/models/provider/yahoo_finance_test.rb b/test/models/provider/yahoo_finance_test.rbindex 73ca5a6637..9c6219ed57 100644--- a/test/models/provider/yahoo_finance_test.rb+++ b/test/models/provider/yahoo_finance_test.rb@@ -532,6 +532,27 @@ class Provider::YahooFinanceTest < ActiveSupport::TestCaseassert_equal "CO", security.country_codeend+ test "search_securities maps Yahoo's Xetra exchange code to XETR" do+ mock_response = mock+ mock_response.stubs(:body).returns({+ quotes: [+ { symbol: "SXR8.DE", shortname: "iShares Core S&P 500", exchange: "GER", exchDisp: "XETRA" },+ { symbol: "SXR8.AS", shortname: "iShares Core S&P 500", exchange: "AMS", exchDisp: "Amsterdam" }+ ]+ }.to_json)++ @provider.stubs(:client).returns(mock_client = mock)+ mock_client.stubs(:get).returns(mock_response)++ response = @provider.search_securities("SXR8")++ assert response.success?+ xetra = response.data.index_by(&:symbol).fetch("SXR8.DE")+ assert_equal "XETR", xetra.exchange_operating_mic+ assert_equal "DE", xetra.country_code+ assert_equal "EUR", @provider.send(:default_currency_for_exchange, "GER")+ end+test "search_securities preserves catalog countries and display-name fallback" domock_response = mockmock_response.stubs(:body).returns({diff --git a/test/models/security/resolver_test.rb b/test/models/security/resolver_test.rbindex 075cd77bdb..dd95eead81 100644--- a/test/models/security/resolver_test.rb+++ b/test/models/security/resolver_test.rb@@ -457,4 +457,31 @@ class Security::ResolverTest < ActiveSupport::TestCaseassert resolved.persisted?assert_equal "XWAR", resolved.exchange_operating_micend++ test "upgrades a legacy GER row when Yahoo now reports XETR instead of creating a duplicate" do+ legacy = Security.create!(ticker: "SXR8.DE", exchange_operating_mic: "XETR", country_code: "DE")+ legacy.update_columns(exchange_operating_mic: "GER")++ Security.expects(:search_provider).never++ assert_no_difference "Security.count" do+ resolved = Security::Resolver.new("SXR8.DE", exchange_operating_mic: "XETR", country_code: "DE").resolve++ assert_equal legacy.id, resolved.id+ assert_equal "XETR", resolved.reload.exchange_operating_mic+ end+ end++ test "prefers canonical XETR when both GER and XETR rows exist" do+ canonical = Security.create!(ticker: "SXR8.DE", exchange_operating_mic: "XETR", country_code: "DE")+ legacy = Security.new(ticker: "SXR8.DE", exchange_operating_mic: "GER", country_code: "DE")+ legacy.save!(validate: false)++ Security.expects(:search_provider).never++ resolved = Security::Resolver.new("SXR8.DE", exchange_operating_mic: "GER", country_code: "DE").resolve++ assert_equal canonical.id, resolved.id+ assert_equal "GER", legacy.reload.exchange_operating_mic+ endenddiff --git a/test/models/security_test.rb b/test/models/security_test.rbindex 740dc6de57..cca9d64bec 100644--- a/test/models/security_test.rb+++ b/test/models/security_test.rb@@ -176,6 +176,27 @@ class SecurityTest < ActiveSupport::TestCaseassert_equal "XWAR", found.reload.exchange_operating_micend+ test "find_by_ticker_and_exchange upgrades legacy GER to XETR and avoids duplicates" do+ legacy = Security.create!(ticker: "SXR8.DE", exchange_operating_mic: "XETR")+ legacy.update_columns(exchange_operating_mic: "GER")++ found = Security.find_by_ticker_and_exchange(ticker: "SXR8.DE", exchange_operating_mic: "XETR")++ assert_equal legacy.id, found.id+ assert_equal "XETR", found.reload.exchange_operating_mic+ end++ test "saving a legacy GER row keeps its MIC when an XETR row already exists" do+ Security.create!(ticker: "SXR8.DE", exchange_operating_mic: "XETR")+ legacy = Security.new(ticker: "SXR8.DE", exchange_operating_mic: "GER")+ legacy.save!(validate: false)++ # e.g. Security::HealthChecker bumping its counters on the legacy row+ legacy.reload.update!(failed_fetch_count: 1)++ assert_equal "GER", legacy.reload.exchange_operating_mic+ end+test "cash_for lazily creates a per-account synthetic cash security" doaccount = accounts(:investment)