@ -106,4 +106,112 @@ 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 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
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