Skip to content

armgit / sure

Public Imported from GitHub
0

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.rb
index df5f2df5fd..f881e459bd 100644
--- a/app/models/provider/alpha_vantage.rb
+++ b/app/models/provider/alpha_vantage.rb
@@ -75,7 +75,7 @@ class Provider::AlphaVantage < Provider
def usage
with_provider_response do
day_key = "alpha_vantage:daily:#{Date.current}"
- used = Rails.cache.read(day_key).to_i
+ used = Rails.cache.read(day_key, raw: true).to_i
UsageData.new(
used: used,
diff --git a/app/models/provider/eodhd.rb b/app/models/provider/eodhd.rb
index 75e65079ce..d0aba4231b 100644
--- a/app/models/provider/eodhd.rb
+++ b/app/models/provider/eodhd.rb
@@ -124,7 +124,7 @@ class Provider::Eodhd < Provider
daily_limit = parsed.dig("dailyRateLimit").to_i
daily_key = daily_cache_key
- used = Rails.cache.read(daily_key).to_i
+ used = Rails.cache.read(daily_key, raw: true).to_i
UsageData.new(
used: used,
diff --git a/app/models/provider/mansa.rb b/app/models/provider/mansa.rb
index 402a28375d..5fa44b0fb4 100644
--- a/app/models/provider/mansa.rb
+++ b/app/models/provider/mansa.rb
@@ -92,7 +92,7 @@ class Provider::Mansa < Provider
def usage
with_provider_response do
- used = Rails.cache.read(daily_cache_key).to_i
+ used = Rails.cache.read(daily_cache_key, raw: true).to_i
UsageData.new(
used: used,
diff --git a/app/models/provider/tiingo.rb b/app/models/provider/tiingo.rb
index 2b011b6d70..cb40cd1c34 100644
--- a/app/models/provider/tiingo.rb
+++ b/app/models/provider/tiingo.rb
@@ -53,7 +53,7 @@ class Provider::Tiingo < Provider
def usage
with_provider_response do
count_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_i
UsageData.new(
used: symbols_used,
diff --git a/test/models/provider/alpha_vantage_test.rb b/test/models/provider/alpha_vantage_test.rb
new file mode 100644
index 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
+end
diff --git a/test/models/provider/eodhd_test.rb b/test/models/provider/eodhd_test.rb
index 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::TestCase
assert_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))
+ end
end
diff --git a/test/models/provider/mansa_test.rb b/test/models/provider/mansa_test.rb
index 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::TestCase
assert_no_match(/=>/, error.message) # would appear in a raw Hash#to_s dump
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: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
+
private
def stub_search_response(*listings)
mock_response = mock
diff --git a/test/models/provider/tiingo_test.rb b/test/models/provider/tiingo_test.rb
index 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::TestCase
assert_instance_of Provider::Tiingo::Error, result.error
assert_match "Could not determine currency", result.error.message
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: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}") }
+ end
end