2025-05-15 10:19:56 -04:00
|
|
|
require "test_helper"
|
|
|
|
require "ostruct"
|
|
|
|
|
2025-05-17 16:37:16 -04:00
|
|
|
class MarketDataImporterTest < ActiveSupport::TestCase
|
2025-05-16 14:17:56 -04:00
|
|
|
include ProviderTestHelper
|
|
|
|
|
2025-05-17 16:37:16 -04:00
|
|
|
SNAPSHOT_START_DATE = MarketDataImporter::SNAPSHOT_DAYS.days.ago.to_date
|
2025-05-16 14:17:56 -04:00
|
|
|
PROVIDER_BUFFER = 5.days
|
|
|
|
|
|
|
|
setup do
|
|
|
|
Security::Price.delete_all
|
|
|
|
ExchangeRate.delete_all
|
|
|
|
Trade.delete_all
|
|
|
|
Holding.delete_all
|
|
|
|
Security.delete_all
|
|
|
|
|
|
|
|
@provider = mock("provider")
|
|
|
|
Provider::Registry.any_instance
|
|
|
|
.stubs(:get_provider)
|
|
|
|
.with(:synth)
|
|
|
|
.returns(@provider)
|
|
|
|
end
|
2025-05-15 10:19:56 -04:00
|
|
|
|
2025-05-16 14:17:56 -04:00
|
|
|
test "syncs required exchange rates" do
|
|
|
|
family = Family.create!(name: "Smith", currency: "USD")
|
|
|
|
family.accounts.create!(name: "Chequing",
|
|
|
|
currency: "CAD",
|
|
|
|
balance: 100,
|
|
|
|
accountable: Depository.new)
|
|
|
|
|
|
|
|
# Seed stale rate so only the next missing day is fetched
|
|
|
|
ExchangeRate.create!(from_currency: "CAD",
|
|
|
|
to_currency: "USD",
|
|
|
|
date: SNAPSHOT_START_DATE,
|
|
|
|
rate: 2.0)
|
|
|
|
|
|
|
|
expected_start_date = (SNAPSHOT_START_DATE + 1.day) - PROVIDER_BUFFER
|
|
|
|
end_date = Date.current.in_time_zone("America/New_York").to_date
|
|
|
|
|
|
|
|
@provider.expects(:fetch_exchange_rates)
|
|
|
|
.with(from: "CAD",
|
|
|
|
to: "USD",
|
|
|
|
start_date: expected_start_date,
|
|
|
|
end_date: end_date)
|
|
|
|
.returns(provider_success_response([
|
|
|
|
OpenStruct.new(from: "CAD", to: "USD", date: SNAPSHOT_START_DATE, rate: 1.5)
|
|
|
|
]))
|
|
|
|
|
|
|
|
before = ExchangeRate.count
|
2025-05-17 16:37:16 -04:00
|
|
|
MarketDataImporter.new(mode: :snapshot).import_exchange_rates
|
2025-05-16 14:17:56 -04:00
|
|
|
after = ExchangeRate.count
|
|
|
|
|
|
|
|
assert_operator after, :>, before, "Should insert at least one new exchange-rate row"
|
2025-05-15 10:19:56 -04:00
|
|
|
end
|
|
|
|
|
2025-05-16 14:17:56 -04:00
|
|
|
test "syncs security prices" do
|
|
|
|
security = Security.create!(ticker: "AAPL", exchange_operating_mic: "XNAS")
|
2025-05-15 10:19:56 -04:00
|
|
|
|
2025-05-16 14:17:56 -04:00
|
|
|
expected_start_date = SNAPSHOT_START_DATE - PROVIDER_BUFFER
|
|
|
|
end_date = Date.current.in_time_zone("America/New_York").to_date
|
2025-05-15 10:19:56 -04:00
|
|
|
|
2025-05-16 14:17:56 -04:00
|
|
|
@provider.expects(:fetch_security_prices)
|
|
|
|
.with(symbol: security.ticker,
|
|
|
|
exchange_operating_mic: security.exchange_operating_mic,
|
|
|
|
start_date: expected_start_date,
|
|
|
|
end_date: end_date)
|
|
|
|
.returns(provider_success_response([
|
|
|
|
OpenStruct.new(security: security,
|
|
|
|
date: SNAPSHOT_START_DATE,
|
|
|
|
price: 100,
|
|
|
|
currency: "USD")
|
|
|
|
]))
|
2025-05-15 10:19:56 -04:00
|
|
|
|
2025-05-16 14:17:56 -04:00
|
|
|
@provider.stubs(:fetch_security_info)
|
|
|
|
.with(symbol: "AAPL", exchange_operating_mic: "XNAS")
|
|
|
|
.returns(provider_success_response(OpenStruct.new(name: "Apple", logo_url: "logo")))
|
2025-05-15 10:19:56 -04:00
|
|
|
|
2025-05-16 14:17:56 -04:00
|
|
|
# Ignore exchange rate calls for this test
|
|
|
|
@provider.stubs(:fetch_exchange_rates).returns(provider_success_response([]))
|
2025-05-15 10:19:56 -04:00
|
|
|
|
2025-05-17 16:37:16 -04:00
|
|
|
MarketDataImporter.new(mode: :snapshot).import_security_prices
|
2025-05-15 10:19:56 -04:00
|
|
|
|
2025-05-16 14:17:56 -04:00
|
|
|
assert_equal 1, Security::Price.where(security: security, date: SNAPSHOT_START_DATE).count
|
2025-05-15 10:19:56 -04:00
|
|
|
end
|
|
|
|
end
|