Browse Source

Add validation for date overrides and implement tests for date generation within collection year

pull/3031/head
Manny Dinssa 1 month ago
parent
commit
e56facbb6d
  1. 2
      app/helpers/collection_time_helper.rb
  2. 68
      spec/helpers/collection_time_helper_spec.rb

2
app/helpers/collection_time_helper.rb

@ -58,7 +58,7 @@ module CollectionTimeHelper
start_date = [start_date_override, collection_start_date(date).to_date].compact.max.to_date
end_date = [end_date_override, collection_end_date(date).to_date].compact.min.to_date
return nil if start_date > end_date
return nil if start_date > end_date || start_date == end_date
available_dates = (start_date..end_date).to_a - [date.to_date]
available_dates.empty? ? nil : available_dates.sample

68
spec/helpers/collection_time_helper_spec.rb

@ -106,4 +106,72 @@ RSpec.describe CollectionTimeHelper do
end
end
end
describe "#generate_different_date_within_collection_year" do
let(:date) { Time.zone.local(2024, 6, 15) }
context "when no overrides are provided" do
it "returns a different date within the collection year" do
result = generate_different_date_within_collection_year(date)
expect(result).not_to eq(date)
expect(result).to be_between(Time.zone.local(2024, 4, 1), Time.zone.local(2025, 3, 31))
end
end
context "when start_date_override is the last day" do
it "returns nil" do
result = generate_different_date_within_collection_year(date, start_date_override: Time.zone.local(2025, 3, 31))
expect(result).to be_nil
end
end
context "when end_date_override is the first day" do
it "returns nil" do
result = generate_different_date_within_collection_year(date, end_date_override: Time.zone.local(2024, 4, 1))
expect(result).to be_nil
end
end
context "when start_date_override and end_date_override are the same day" do
it "returns nil" do
result = generate_different_date_within_collection_year(date, start_date_override: Time.zone.local(2024, 6, 15), end_date_override: Time.zone.local(2024, 6, 15))
expect(result).to be_nil
end
end
context "when start_date_override is greater than end_date_override" do
it "returns nil" do
result = generate_different_date_within_collection_year(date, start_date_override: Time.zone.local(2024, 7, 1), end_date_override: Time.zone.local(2024, 6, 1))
expect(result).to be_nil
end
end
context "when valid overrides are provided" do
it "returns a different date within the overridden range" do
result = generate_different_date_within_collection_year(date, start_date_override: Time.zone.local(2024, 5, 1), end_date_override: Time.zone.local(2024, 7, 1))
expect(result).not_to eq(date)
expect(result).to be_between(Time.zone.local(2024, 5, 1), Time.zone.local(2024, 7, 1))
end
end
context "when the date is at the start of the collection year" do
let(:date) { Time.zone.local(2024, 4, 1) }
it "returns a different date within the collection year" do
result = generate_different_date_within_collection_year(date)
expect(result).not_to eq(date)
expect(result).to be_between(Time.zone.local(2024, 4, 1), Time.zone.local(2025, 3, 31))
end
end
context "when the date is at the end of the collection year" do
let(:date) { Time.zone.local(2025, 3, 31) }
it "returns a different date within the collection year" do
result = generate_different_date_within_collection_year(date)
expect(result).not_to eq(date)
expect(result).to be_between(Time.zone.local(2024, 4, 1), Time.zone.local(2025, 3, 31))
end
end
end
end

Loading…
Cancel
Save