mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-18 20:59:39 +02:00
* PlaidConnectable concern * Remove bad abstraction * Put sync implementations in own concerns * Sync strategies * Move sync orchestration to Sync class * Clean up sync class, add state machine * Basic market data sync cron * Fix price sync * Improve sync window column names, add timestamps * 30 day syncs by default * Clean up market data methods * Report high duplicate sync counts to Sentry * Add sync states throughout app * account tab session * Persistent account tab selections * Remove manual sleep * Add migration to clear stale syncs on self hosted apps * Tweak sync states * Sync completion event broadcasts * Fix timezones in tests * Cleanup * More cleanup * Plaid item UI broadcasts for sync * Fix account ID namespace conflict * Sync broadcasters * Smoother account sync refreshes * Remove test sync delay
64 lines
1.9 KiB
Ruby
64 lines
1.9 KiB
Ruby
require "test_helper"
|
|
require "ostruct"
|
|
|
|
class Security::PriceTest < ActiveSupport::TestCase
|
|
include ProviderTestHelper
|
|
|
|
setup do
|
|
@provider = mock
|
|
Security.stubs(:provider).returns(@provider)
|
|
|
|
@security = securities(:aapl)
|
|
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(security, 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(security, 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
|