2024-07-16 09:26:49 -04:00
|
|
|
require "test_helper"
|
2024-08-01 19:43:23 -04:00
|
|
|
require "ostruct"
|
2024-07-16 09:26:49 -04:00
|
|
|
|
|
|
|
class Security::PriceTest < ActiveSupport::TestCase
|
2025-03-17 11:54:53 -04:00
|
|
|
include ProviderTestHelper
|
|
|
|
|
2024-08-01 19:43:23 -04:00
|
|
|
setup do
|
|
|
|
@provider = mock
|
2025-03-17 11:54:53 -04:00
|
|
|
Security.stubs(:provider).returns(@provider)
|
2024-08-01 19:43:23 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
@security = securities(:aapl)
|
2024-08-01 19:43:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "finds single security price in DB" do
|
2025-03-17 11:54:53 -04:00
|
|
|
@provider.expects(:fetch_security_price).never
|
2024-08-01 19:43:23 -04:00
|
|
|
price = security_prices(:one)
|
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
assert_equal price, @security.find_or_fetch_price(date: price.date)
|
2024-08-01 19:43:23 -04:00
|
|
|
end
|
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
test "caches prices from provider to DB" do
|
|
|
|
price_date = 10.days.ago.to_date
|
2025-02-28 11:35:10 -05:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
expected_price = Security::Price.new(
|
|
|
|
security: @security,
|
|
|
|
date: price_date,
|
|
|
|
price: 314.34,
|
|
|
|
currency: "USD"
|
|
|
|
)
|
2025-02-28 11:35:10 -05:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
expect_provider_price(security: @security, price: expected_price, date: price_date)
|
2024-08-01 19:43:23 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
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
|
2024-08-01 19:43:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
test "returns nil if no price found in DB or from provider" do
|
2024-10-29 15:37:59 -04:00
|
|
|
security = securities(:aapl)
|
2025-03-17 11:54:53 -04:00
|
|
|
Security::Price.delete_all # Clear any existing prices
|
2024-08-01 19:43:23 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
provider_response = provider_error_response(Provider::ProviderError.new("Test error"))
|
2024-08-01 19:43:23 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
@provider.expects(:fetch_security_price)
|
|
|
|
.with(security, date: Date.current)
|
|
|
|
.returns(provider_response)
|
2024-08-01 19:43:23 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
assert_not @security.find_or_fetch_price(date: Date.current)
|
|
|
|
end
|
2024-10-29 15:37:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
test "upserts historical prices from provider" do
|
|
|
|
Security::Price.delete_all
|
2024-08-01 19:43:23 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
# Will be overwritten by upsert
|
|
|
|
Security::Price.create!(security: @security, date: 1.day.ago.to_date, price: 190, currency: "USD")
|
2024-08-01 19:43:23 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
expect_provider_prices(security: @security, start_date: 2.days.ago.to_date, end_date: Date.current, prices: [
|
|
|
|
Security::Price.new(security: @security, date: Date.current, price: 215, currency: "USD"),
|
|
|
|
Security::Price.new(security: @security, date: 1.day.ago.to_date, price: 214, currency: "USD"),
|
|
|
|
Security::Price.new(security: @security, date: 2.days.ago.to_date, price: 213, currency: "USD")
|
|
|
|
])
|
2024-08-01 19:43:23 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
@security.sync_provider_prices(start_date: 2.days.ago.to_date)
|
2024-10-29 15:37:59 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
assert_equal 215, @security.prices.find_by(date: Date.current).price
|
|
|
|
assert_equal 214, @security.prices.find_by(date: 1.day.ago.to_date).price
|
|
|
|
assert_equal 213, @security.prices.find_by(date: 2.days.ago.to_date).price
|
2024-08-01 19:43:23 -04:00
|
|
|
end
|
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
private
|
|
|
|
def expect_provider_price(security:, price:, date:)
|
|
|
|
@provider.expects(:fetch_security_price)
|
|
|
|
.with(security, date: date)
|
|
|
|
.returns(provider_success_response(Security::Provideable::PriceData.new(price: price)))
|
|
|
|
end
|
2024-08-01 19:43:23 -04:00
|
|
|
|
2025-03-17 11:54:53 -04:00
|
|
|
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(Security::Provideable::PricesData.new(prices: prices)))
|
2024-08-01 19:43:23 -04:00
|
|
|
end
|
2024-07-16 09:26:49 -04:00
|
|
|
end
|