2018-10-03 15:48:30 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
|
require_relative "helper"
|
|
|
|
|
require "day"
|
2018-10-03 15:48:30 +02:00
|
|
|
|
|
|
|
|
|
describe Day do
|
2024-11-19 20:06:47 +01:00
|
|
|
|
describe ".latest" do
|
2024-11-20 14:13:17 +01:00
|
|
|
|
it "returns latest available rates on given date" do
|
|
|
|
|
date = Date.parse("2010-01-04")
|
|
|
|
|
data = Day.latest(date)
|
|
|
|
|
_(data.to_a.sample.date).must_equal(date)
|
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
|
date = Date.parse("2010-01-01")
|
2018-10-03 15:48:30 +02:00
|
|
|
|
data = Day.latest(date)
|
2024-11-20 14:13:17 +01:00
|
|
|
|
_(data.to_a.sample.date).must_equal(Date.parse("2009-12-31"))
|
2018-10-03 15:48:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
2024-11-20 14:13:17 +01:00
|
|
|
|
it "returns nothing if date predates dataset" do
|
|
|
|
|
_(Day.latest(Date.parse("1901-01-01"))).must_be_empty
|
2018-10-03 15:48:30 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-11-19 20:06:47 +01:00
|
|
|
|
describe ".between" do
|
2024-11-20 14:13:17 +01:00
|
|
|
|
it "returns rates between given working dates" do
|
|
|
|
|
start_date = Date.parse("2010-01-04")
|
|
|
|
|
end_date = Date.parse("2010-01-29")
|
2018-10-03 15:48:30 +02:00
|
|
|
|
dates = Day.between((start_date..end_date)).map(:date).sort
|
2024-11-20 14:13:17 +01:00
|
|
|
|
_(dates.first).must_equal(start_date)
|
|
|
|
|
_(dates.last).must_equal(end_date)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "starts on preceding business day if start date is a holiday" do
|
|
|
|
|
start_date = Date.parse("2024-11-03")
|
|
|
|
|
end_date = Date.parse("2024-11-04")
|
|
|
|
|
dates = Day.between((start_date..end_date)).map(:date)
|
|
|
|
|
_(dates).must_include(Date.parse("2024-11-01"))
|
2018-10-03 15:48:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
2024-11-20 14:13:17 +01:00
|
|
|
|
it "returns nothing if end date predates dataset" do
|
|
|
|
|
interval = (Date.parse("1901-01-01")..Date.parse("1901-01-31"))
|
2020-05-02 18:08:47 +02:00
|
|
|
|
_(Day.between(interval)).must_be_empty
|
2018-10-03 15:48:30 +02:00
|
|
|
|
end
|
2024-11-20 14:13:17 +01:00
|
|
|
|
|
|
|
|
|
it "allows start date to predate dataset" do
|
|
|
|
|
start_date = Date.parse("1901-01-01")
|
|
|
|
|
end_date = Date.parse("2024-01-01")
|
|
|
|
|
dates = Day.between((start_date..end_date)).map(:date)
|
|
|
|
|
_(dates).wont_be_empty
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns nothing if queried for the future" do
|
|
|
|
|
start_date = Date.today + 1
|
|
|
|
|
end_date = start_date + 1
|
|
|
|
|
dates = Day.between((start_date..end_date)).map(:date)
|
|
|
|
|
_(dates).must_be_empty
|
|
|
|
|
end
|
2018-10-03 15:48:30 +02:00
|
|
|
|
end
|
|
|
|
|
end
|