Add throttling and cross-rate for twelve data (#1396)

* Add throttling and cross-rate for twelve data

* FIX yahoo precision also

* FIXES

* Update importer.rb

* Fixes

* Revert job

* Fixes
This commit is contained in:
soky srm
2026-04-07 20:46:05 +02:00
committed by GitHub
parent ec1562782b
commit be42988adf
10 changed files with 379 additions and 61 deletions

View File

@@ -41,6 +41,7 @@ class Account::MarketDataImporterTest < ActiveSupport::TestCase
expected_start_date = (existing_date + 1.day) - EXCHANGE_RATE_BUFFER
end_date = Date.current.in_time_zone("America/New_York").to_date
# Only the forward pair (CAD→USD) should be fetched; inverse (USD→CAD) is computed automatically
@provider.expects(:fetch_exchange_rates)
.with(from: "CAD",
to: "USD",
@@ -50,20 +51,15 @@ class Account::MarketDataImporterTest < ActiveSupport::TestCase
OpenStruct.new(from: "CAD", to: "USD", date: existing_date, rate: 1.5)
]))
@provider.expects(:fetch_exchange_rates)
.with(from: "USD",
to: "CAD",
start_date: expected_start_date,
end_date: end_date)
.returns(provider_success_response([
OpenStruct.new(from: "USD", to: "CAD", date: existing_date, rate: 0.67)
]))
before = ExchangeRate.count
Account::MarketDataImporter.new(account).import_all
after = ExchangeRate.count
assert_operator after, :>, before + 1, "Should insert at least two new exchange-rate rows"
assert_operator after, :>, before + 1, "Should insert at least two new exchange-rate rows (forward + computed inverse)"
# Verify inverse rates were computed from the forward rates
assert ExchangeRate.where(from_currency: "USD", to_currency: "CAD").where("date > ?", existing_date).exists?,
"Inverse rates should be computed automatically"
end
test "syncs security prices for securities traded by the account" do
@@ -237,7 +233,7 @@ class Account::MarketDataImporterTest < ActiveSupport::TestCase
expected_start_date = (existing_date + 1.day) - EXCHANGE_RATE_BUFFER
end_date = Date.current.in_time_zone("America/New_York").to_date
# Simulate provider returning an error response
# Only the forward pair (CAD→USD) should be fetched; inverse is computed automatically
@provider.expects(:fetch_exchange_rates)
.with(from: "CAD",
to: "USD",
@@ -247,15 +243,6 @@ class Account::MarketDataImporterTest < ActiveSupport::TestCase
Provider::TwelveData::Error.new("Rate limit exceeded", details: { code: 429, message: "Rate limit exceeded" })
))
@provider.expects(:fetch_exchange_rates)
.with(from: "USD",
to: "CAD",
start_date: expected_start_date,
end_date: end_date)
.returns(provider_error_response(
Provider::TwelveData::Error.new("Rate limit exceeded", details: { code: 429, message: "Rate limit exceeded" })
))
before = ExchangeRate.count
# Should not raise an error, just log and continue