2018-07-05 21:19:37 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
require_relative "../helper"
|
|
|
|
require "quote/interval"
|
2018-07-05 21:19:37 +02:00
|
|
|
|
|
|
|
module Quote
|
|
|
|
describe Interval do
|
2020-05-02 18:08:47 +02:00
|
|
|
let(:dates) do
|
2024-11-19 20:06:47 +01:00
|
|
|
(Date.parse("2010-01-01")..Date.parse("2010-12-31"))
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:quote) do
|
2020-05-02 18:08:47 +02:00
|
|
|
Interval.new(date: dates)
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
quote.perform
|
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "returns rates" do
|
|
|
|
_(quote.formatted[:rates]).wont_be(:empty?)
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "quotes given date interval" do
|
2024-11-20 14:13:17 +01:00
|
|
|
returned_start = Date.parse(quote.formatted[:start_date])
|
|
|
|
returned_end = Date.parse(quote.formatted[:end_date])
|
|
|
|
|
|
|
|
# The returned start date should be the closest working day (before or on) the requested start
|
|
|
|
_(returned_start).must_be(:<=, dates.first)
|
|
|
|
# But it shouldn't be too far back (maybe 10 business days)
|
|
|
|
_(returned_start).must_be(:>, dates.first - 10)
|
|
|
|
|
|
|
|
# End date should still be within the requested range
|
|
|
|
_(returned_end).must_be(:<=, dates.last)
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "quotes against the Euro" do
|
2018-07-05 21:19:37 +02:00
|
|
|
quote.formatted[:rates].each_value do |rates|
|
2024-11-19 20:06:47 +01:00
|
|
|
_(rates.keys).wont_include("EUR")
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "sorts rates" do
|
2018-07-05 21:19:37 +02:00
|
|
|
quote.formatted[:rates].each_value do |rates|
|
2024-11-19 20:06:47 +01:00
|
|
|
_(rates.keys).must_equal(rates.keys.sort)
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "has a cache key" do
|
|
|
|
_(quote.cache_key).wont_be(:empty?)
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
describe "given a new base" do
|
2018-07-05 21:19:37 +02:00
|
|
|
let(:quote) do
|
2024-11-19 20:06:47 +01:00
|
|
|
Interval.new(date: dates, base: "USD")
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "quotes against that base" do
|
2018-07-05 21:19:37 +02:00
|
|
|
quote.formatted[:rates].each_value do |rates|
|
2024-11-19 20:06:47 +01:00
|
|
|
_(rates.keys).wont_include("USD")
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "sorts rates" do
|
2018-07-05 21:19:37 +02:00
|
|
|
quote.formatted[:rates].each_value do |rates|
|
2024-11-19 20:06:47 +01:00
|
|
|
_(rates.keys).must_equal(rates.keys.sort)
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
describe "given symbols" do
|
2018-07-05 21:19:37 +02:00
|
|
|
let(:quote) do
|
2024-11-19 20:06:47 +01:00
|
|
|
Interval.new(date: dates, symbols: ["USD", "GBP", "JPY"])
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "quotes only for those symbols" do
|
2018-07-05 21:19:37 +02:00
|
|
|
quote.formatted[:rates].each_value do |rates|
|
2024-11-19 20:06:47 +01:00
|
|
|
_(rates.keys).must_include("USD")
|
|
|
|
_(rates.keys).wont_include("CAD")
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "sorts rates" do
|
2018-07-05 21:19:37 +02:00
|
|
|
quote.formatted[:rates].each_value do |rates|
|
2024-11-19 20:06:47 +01:00
|
|
|
_(rates.keys).must_equal(rates.keys.sort)
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
describe "when given an amount" do
|
2018-07-05 21:19:37 +02:00
|
|
|
let(:quote) do
|
2020-05-02 18:08:47 +02:00
|
|
|
Interval.new(date: dates, amount: 100)
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
it "calculates quotes for that amount" do
|
2018-07-05 21:19:37 +02:00
|
|
|
quote.formatted[:rates].each_value do |rates|
|
2024-11-19 20:06:47 +01:00
|
|
|
_(rates["USD"]).must_be(:>, 10)
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|