From c13546020dbd1c614b1f188bedfbe3cc70fc2db7 Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Tue, 1 Apr 2025 16:14:04 +0100 Subject: [PATCH] Add tests for date overrides in collection year logic --- spec/helpers/collection_time_helper_spec.rb | 56 ++++++++++++++++++--- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/spec/helpers/collection_time_helper_spec.rb b/spec/helpers/collection_time_helper_spec.rb index c67557636..cd1230c85 100644 --- a/spec/helpers/collection_time_helper_spec.rb +++ b/spec/helpers/collection_time_helper_spec.rb @@ -146,14 +146,6 @@ RSpec.describe CollectionTimeHelper do 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) } @@ -173,5 +165,53 @@ RSpec.describe CollectionTimeHelper do 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 before the collection year" do + it "ignores the override and returns a different date within the collection year" do + result = generate_different_date_within_collection_year(date, start_date_override: Time.zone.local(2023, 12, 31)) + 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 end_date_override is after the collection year" do + it "ignores the override and returns a different date within the collection year" do + result = generate_different_date_within_collection_year(date, end_date_override: Time.zone.local(2025, 12, 1)) + 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 both start_date_override and end_date_override are provided and valid" 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, 8, 1), end_date_override: Time.zone.local(2024, 9, 1)) + expect(result).not_to eq(date) + expect(result).to be_between(Time.zone.local(2024, 8, 1), Time.zone.local(2024, 9, 1)) + end + end + + context "when both start_date_override and end_date_override are provided but start_date_override is before the collection year" do + it "ignores the start_date_override and returns a different date within the collection year" do + result = generate_different_date_within_collection_year(date, start_date_override: Time.zone.local(2023, 12, 31), end_date_override: Time.zone.local(2024, 5, 1)) + expect(result).not_to eq(date) + expect(result).to be_between(Time.zone.local(2024, 4, 1), Time.zone.local(2024, 5, 1)) + end + end + + context "when both start_date_override and end_date_override are provided but end_date_override is after the collection year" do + it "ignores the end_date_override and returns a different date within the collection year" do + result = generate_different_date_within_collection_year(date, start_date_override: Time.zone.local(2025, 3, 1), end_date_override: Time.zone.local(2025, 12, 1)) + expect(result).not_to eq(date) + expect(result).to be_between(Time.zone.local(2025, 3, 1), Time.zone.local(2025, 3, 31)) + end + end + + context "when both start_date_override and end_date_override are outside the collection year" do + it "ignores both overrides and returns a different date within the collection year" do + result = generate_different_date_within_collection_year(date, start_date_override: Time.zone.local(2023, 12, 31), end_date_override: Time.zone.local(2025, 12, 1)) + 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