2024-03-27 09:16:00 -06:00
|
|
|
require "test_helper"
|
2024-07-08 09:04:59 -04:00
|
|
|
require "ostruct"
|
2024-03-27 09:16:00 -06:00
|
|
|
|
|
|
|
class Provider::SynthTest < ActiveSupport::TestCase
|
2024-08-01 19:43:23 -04:00
|
|
|
include ExchangeRateProviderInterfaceTest, SecurityPriceProviderInterfaceTest
|
2024-03-27 09:16:00 -06:00
|
|
|
|
|
|
|
setup do
|
2024-08-01 19:43:23 -04:00
|
|
|
@subject = @synth = Provider::Synth.new(ENV["SYNTH_API_KEY"])
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fetches paginated securities prices" do
|
|
|
|
VCR.use_cassette("synth/security_prices") do
|
2024-10-29 15:37:59 -04:00
|
|
|
response = @synth.fetch_security_prices(
|
|
|
|
ticker: "AAPL",
|
|
|
|
mic_code: "XNAS",
|
|
|
|
start_date: Date.iso8601("2024-01-01"),
|
|
|
|
end_date: Date.iso8601("2024-08-01")
|
|
|
|
)
|
2024-08-01 19:43:23 -04:00
|
|
|
|
|
|
|
assert 213, response.size
|
|
|
|
end
|
2024-07-08 09:04:59 -04:00
|
|
|
end
|
|
|
|
|
2024-08-09 16:57:33 +02:00
|
|
|
test "fetches paginated exchange_rate historical data" do
|
|
|
|
VCR.use_cassette("synth/exchange_rate_historical") do
|
|
|
|
response = @synth.fetch_exchange_rates(
|
|
|
|
from: "USD", to: "GBP", start_date: Date.parse("01.01.2024"), end_date: Date.parse("31.07.2024")
|
|
|
|
)
|
|
|
|
|
|
|
|
assert 213, response.rates.size # 213 days between 01.01.2024 and 31.07.2024
|
|
|
|
assert_equal [ :date, :rate ], response.rates.first.keys
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
test "retries then provides failed response" do
|
2024-08-05 12:21:12 -04:00
|
|
|
@client = mock
|
|
|
|
Faraday.stubs(:new).returns(@client)
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2024-08-05 12:21:12 -04:00
|
|
|
@client.expects(:get).returns(OpenStruct.new(success?: false)).times(3)
|
|
|
|
|
|
|
|
response = @synth.fetch_exchange_rate from: "USD", to: "MXN", date: Date.iso8601("2024-08-01")
|
2024-07-08 09:04:59 -04:00
|
|
|
|
2024-08-01 19:43:23 -04:00
|
|
|
assert_match "Failed to fetch data from Provider::Synth", response.error.message
|
2024-07-08 09:04:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "retrying, then raising on network error" do
|
2024-08-05 12:21:12 -04:00
|
|
|
@client = mock
|
|
|
|
Faraday.stubs(:new).returns(@client)
|
|
|
|
|
|
|
|
@client.expects(:get).raises(Faraday::TimeoutError).times(3)
|
2024-07-08 09:04:59 -04:00
|
|
|
|
|
|
|
assert_raises Faraday::TimeoutError do
|
2024-08-05 12:21:12 -04:00
|
|
|
@synth.fetch_exchange_rate from: "USD", to: "MXN", date: Date.iso8601("2024-08-01")
|
2024-07-08 09:04:59 -04:00
|
|
|
end
|
2024-03-27 09:16:00 -06:00
|
|
|
end
|
|
|
|
end
|