The personal finance app for everyone (by everyone)
Ruby
Read usage counters with raw: true for Tiingo, EODHD, Alpha Vantage, Mansa (#5330)
Rajkamal50f56f9df612
diff --git a/app/models/provider/alpha_vantage.rb b/app/models/provider/alpha_vantage.rbindex df5f2df5fd..f881e459bd 100644--- a/app/models/provider/alpha_vantage.rb+++ b/app/models/provider/alpha_vantage.rb@@ -75,7 +75,7 @@ class Provider::AlphaVantage < Providerdef usagewith_provider_response doday_key = "alpha_vantage:daily:#{Date.current}"- used = Rails.cache.read(day_key).to_i+ used = Rails.cache.read(day_key, raw: true).to_iUsageData.new(used: used,diff --git a/app/models/provider/eodhd.rb b/app/models/provider/eodhd.rbindex 75e65079ce..d0aba4231b 100644--- a/app/models/provider/eodhd.rb+++ b/app/models/provider/eodhd.rb@@ -124,7 +124,7 @@ class Provider::Eodhd < Providerdaily_limit = parsed.dig("dailyRateLimit").to_idaily_key = daily_cache_key- used = Rails.cache.read(daily_key).to_i+ used = Rails.cache.read(daily_key, raw: true).to_iUsageData.new(used: used,diff --git a/app/models/provider/mansa.rb b/app/models/provider/mansa.rbindex 402a28375d..5fa44b0fb4 100644--- a/app/models/provider/mansa.rb+++ b/app/models/provider/mansa.rb@@ -92,7 +92,7 @@ class Provider::Mansa < Providerdef usagewith_provider_response do- used = Rails.cache.read(daily_cache_key).to_i+ used = Rails.cache.read(daily_cache_key, raw: true).to_iUsageData.new(used: used,diff --git a/app/models/provider/tiingo.rb b/app/models/provider/tiingo.rbindex 2b011b6d70..cb40cd1c34 100644--- a/app/models/provider/tiingo.rb+++ b/app/models/provider/tiingo.rb@@ -53,7 +53,7 @@ class Provider::Tiingo < Providerdef usagewith_provider_response docount_key = "tiingo:symbol_count:#{Date.current.strftime('%Y-%m')}"- symbols_used = Rails.cache.read(count_key).to_i+ symbols_used = Rails.cache.read(count_key, raw: true).to_iUsageData.new(used: symbols_used,diff --git a/test/models/provider/alpha_vantage_test.rb b/test/models/provider/alpha_vantage_test.rbnew file mode 100644index 0000000000..e0a55a7357--- /dev/null+++ b/test/models/provider/alpha_vantage_test.rb@@ -0,0 +1,22 @@+require "test_helper"++class Provider::AlphaVantageTest < ActiveSupport::TestCase+ # The test environment caches in a null store, so the usage readers need a real+ # RedisCacheStore to notice a counter they cannot read back. The per-run namespace+ # keeps runs sharing one Redis from reading each other's counts.+ test "usage reports the request counter on a real Redis cache" do+ store = ActiveSupport::Cache::RedisCacheStore.new(+ url: ENV.fetch("REDIS_URL", "redis://localhost:6379"),+ namespace: "test:alpha_vantage:#{SecureRandom.hex(8)}"+ )+ Rails.stubs(:cache).returns(store)++ provider = Provider::AlphaVantage.new("test_api_key")+ provider.define_singleton_method(:sleep) { |_duration| }+ 2.times { provider.send(:throttle_request) }++ assert_equal 2, provider.usage.data.used+ ensure+ store&.delete("alpha_vantage:daily:#{Date.current}")+ end+enddiff --git a/test/models/provider/eodhd_test.rb b/test/models/provider/eodhd_test.rbindex 245930c82d..4f61ec88c9 100644--- a/test/models/provider/eodhd_test.rb+++ b/test/models/provider/eodhd_test.rb@@ -464,4 +464,27 @@ class Provider::EodhdTest < ActiveSupport::TestCaseassert_equal 1, Rails.cache.read("eodhd:daily:2026-08-21")assert_equal 5, Rails.cache.read("eodhd:daily:2026-08-22")end++ # The test environment caches in a null store, so the usage readers need a real+ # RedisCacheStore to notice a counter they cannot read back. The per-run namespace+ # keeps runs sharing one Redis from reading each other's counts.+ test "usage reports the request counter on a real Redis cache" do+ store = ActiveSupport::Cache::RedisCacheStore.new(+ url: ENV.fetch("REDIS_URL", "redis://localhost:6379"),+ namespace: "test:eodhd:#{SecureRandom.hex(8)}"+ )+ Rails.stubs(:cache).returns(store)++ stub_request(:get, %r{/api/user}).to_return(+ status: 200,+ body: { apiRequests: 0, dailyRateLimit: 100_000, subscriptionType: "free" }.to_json+ )++ provider = Provider::Eodhd.new("test_api_key")+ 2.times { provider.send(:enforce_daily_limit!) }++ assert_equal 2, provider.usage.data.used+ ensure+ store&.delete(provider&.send(:daily_cache_key))+ endenddiff --git a/test/models/provider/mansa_test.rb b/test/models/provider/mansa_test.rbindex 369cdc8daa..c7dbe3f150 100644--- a/test/models/provider/mansa_test.rb+++ b/test/models/provider/mansa_test.rb@@ -342,6 +342,24 @@ class Provider::MansaTest < ActiveSupport::TestCaseassert_no_match(/=>/, error.message) # would appear in a raw Hash#to_s dumpend+ # The test environment caches in a null store, so the usage readers need a real+ # RedisCacheStore to notice a counter they cannot read back. The per-run namespace+ # keeps runs sharing one Redis from reading each other's counts.+ test "usage reports the request counter on a real Redis cache" do+ store = ActiveSupport::Cache::RedisCacheStore.new(+ url: ENV.fetch("REDIS_URL", "redis://localhost:6379"),+ namespace: "test:mansa:#{SecureRandom.hex(8)}"+ )+ Rails.stubs(:cache).returns(store)++ provider = Provider::Mansa.new("test_api_key")+ 2.times { provider.send(:enforce_daily_limit!) }++ assert_equal 2, provider.usage.data.used+ ensure+ store&.delete(provider&.send(:daily_cache_key))+ end+privatedef stub_search_response(*listings)mock_response = mockdiff --git a/test/models/provider/tiingo_test.rb b/test/models/provider/tiingo_test.rbindex 506a19e28b..719ccb1b99 100644--- a/test/models/provider/tiingo_test.rb+++ b/test/models/provider/tiingo_test.rb@@ -262,4 +262,26 @@ class Provider::TiingoTest < ActiveSupport::TestCaseassert_instance_of Provider::Tiingo::Error, result.errorassert_match "Could not determine currency", result.error.messageend++ # The test environment caches in a null store, so the usage readers need a real+ # RedisCacheStore to notice a counter they cannot read back. The per-run namespace+ # keeps runs sharing one Redis from reading each other's counts.+ test "usage reports the request counter on a real Redis cache" do+ store = ActiveSupport::Cache::RedisCacheStore.new(+ url: ENV.fetch("REDIS_URL", "redis://localhost:6379"),+ namespace: "test:tiingo:#{SecureRandom.hex(8)}"+ )+ Rails.stubs(:cache).returns(store)++ provider = Provider::Tiingo.new("test_api_key")+ # Two distinct symbols: a symbol is counted once a month.+ provider.send(:track_symbol, "AAPL")+ provider.send(:track_symbol, "MSFT")++ assert_equal 2, provider.usage.data.used+ ensure+ month = Date.current.strftime("%Y-%m")+ store&.delete("tiingo:symbol_count:#{month}")+ %w[AAPL MSFT].each { |symbol| store&.delete("tiingo:symbol:#{month}:#{symbol}") }+ endend