Files
sure/test/models/security/price_test.rb
soky srm 7908f7d8a4 Expand financial providers (#1407)
* Initial implementation

* Tiingo fixes

* Adds 2 providers, remove 2

* Add  extra checks

* FIX a big hotwire race condition

// Fix hotwire_combobox race condition: when typing quickly, a slow response for
// an early query (e.g. "A") can overwrite the correct results for the final query
// (e.g. "AAPL"). We abort the previous in-flight request whenever a new one fires,
// so stale Turbo Stream responses never reach the DOM.

* pipelock

* Update price_test.rb

* Reviews

* i8n

* fixes

* fixes

* Update tiingo.rb

* fixes

* Improvements

* Big revamp

* optimisations

* Update 20260408151837_add_offline_reason_to_securities.rb

* Add missing tests, fixes

* small rank tests

* FIX tests

* Update show.html.erb

* Update resolver.rb

* Update usd_converter.rb

* Update holdings_controller.rb

* Update holdings_controller.rb

* Update holdings_controller.rb

* Update holdings_controller.rb

* Update holdings_controller.rb

* Update _yahoo_finance_settings.html.erb
2026-04-09 18:33:59 +02:00

64 lines
2.0 KiB
Ruby

require "test_helper"
require "ostruct"
class Security::PriceTest < ActiveSupport::TestCase
include ProviderTestHelper
setup do
@provider = mock
@security = securities(:aapl)
@security.stubs(:price_data_provider).returns(@provider)
end
test "finds single security price in DB" do
@provider.expects(:fetch_security_price).never
price = security_prices(:one)
assert_equal price, @security.find_or_fetch_price(date: price.date)
end
test "caches prices from provider to DB" do
price_date = 10.days.ago.to_date
expected_price = Security::Price.new(
security: @security,
date: price_date,
price: 314.34,
currency: "USD"
)
expect_provider_price(security: @security, price: expected_price, date: price_date)
assert_difference "Security::Price.count", 1 do
fetched_price = @security.find_or_fetch_price(date: price_date, cache: true)
assert_equal expected_price.price, fetched_price.price
end
end
test "returns nil if no price found in DB or from provider" do
security = securities(:aapl)
Security::Price.delete_all # Clear any existing prices
with_provider_response = provider_error_response(StandardError.new("Test error"))
@provider.expects(:fetch_security_price)
.with(symbol: security.ticker, exchange_operating_mic: security.exchange_operating_mic, date: Date.current)
.returns(with_provider_response)
assert_not @security.find_or_fetch_price(date: Date.current)
end
private
def expect_provider_price(security:, price:, date:)
@provider.expects(:fetch_security_price)
.with(symbol: security.ticker, exchange_operating_mic: security.exchange_operating_mic, date: date)
.returns(provider_success_response(price))
end
def expect_provider_prices(security:, prices:, start_date:, end_date:)
@provider.expects(:fetch_security_prices)
.with(security, start_date: start_date, end_date: end_date)
.returns(provider_success_response(prices))
end
end