diff --git a/app/controllers/test_data_controller.rb b/app/controllers/test_data_controller.rb index 8617ff3b5..93361bfcf 100644 --- a/app/controllers/test_data_controller.rb +++ b/app/controllers/test_data_controller.rb @@ -1,4 +1,6 @@ class TestDataController < ApplicationController + include CollectionTimeHelper + rescue_from ActiveRecord::RecordNotFound, with: :render_not_found def create_test_lettings_log @@ -20,7 +22,7 @@ class TestDataController < ApplicationController return render_not_found unless FeatureToggle.create_test_logs_enabled? file = Tempfile.new("#{year}_test_lettings_log.csv") - log = FactoryBot.create(:lettings_log, :completed, assigned_to: current_user, ppostcode_full: "SW1A 1AA", startdate: Time.zone.local(year.to_i, rand(4..12), rand(1..28))) + log = FactoryBot.create(:lettings_log, :completed, assigned_to: current_user, ppostcode_full: "SW1A 1AA", startdate: generate_different_date_within_collection_year(Time.zone.local(year.to_i, 4, 1), end_date_override: Time.zone.now + 14.days)) log_to_csv = BulkUpload::LettingsLogToCsv.new(log:, line_ending: "\n", overrides: { organisation_id: "ORG#{log.owning_organisation_id}", managing_organisation_id: "ORG#{log.owning_organisation_id}" }) file.write(log_to_csv.default_field_numbers_row) file.write(log_to_csv.to_csv_row) @@ -51,12 +53,12 @@ class TestDataController < ApplicationController redirect_to sales_log_path(log) end - [2024, 2025].each do |year| + %w[2024 2025].each do |year| define_method("create_#{year}_test_sales_bulk_upload") do return render_not_found unless FeatureToggle.create_test_logs_enabled? file = Tempfile.new("#{year}_test_sales_log.csv") - log = FactoryBot.create(:sales_log, :completed, assigned_to: current_user, value: 180_000, deposit: 150_000, county: "Somerset", saledate: Time.zone.local(year.to_i, rand(4..12), rand(1..28))) + log = FactoryBot.create(:sales_log, :completed, assigned_to: current_user, value: 180_000, deposit: 150_000, county: "Somerset", saledate: generate_different_date_within_collection_year(Time.zone.local(year.to_i, 4, 1), end_date_override: Time.zone.now + 14.days)) log_to_csv = BulkUpload::SalesLogToCsv.new(log:, line_ending: "\n", overrides: { organisation_id: "ORG#{log.owning_organisation_id}", managing_organisation_id: "ORG#{log.owning_organisation_id}" }) file.write(log_to_csv.default_field_numbers_row) file.write(log_to_csv.to_csv_row) diff --git a/app/helpers/collection_time_helper.rb b/app/helpers/collection_time_helper.rb index f5ffd66bf..05fe44449 100644 --- a/app/helpers/collection_time_helper.rb +++ b/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 available_dates = (start_date..end_date).to_a - [date.to_date] available_dates.empty? ? nil : available_dates.sample diff --git a/spec/helpers/collection_time_helper_spec.rb b/spec/helpers/collection_time_helper_spec.rb index a23fc905d..cd1230c85 100644 --- a/spec/helpers/collection_time_helper_spec.rb +++ b/spec/helpers/collection_time_helper_spec.rb @@ -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