From f67bb30de8b7bdcc72b8304bac7637b5b1c26c27 Mon Sep 17 00:00:00 2001 From: Kat <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 18 Dec 2024 10:50:59 +0000 Subject: [PATCH] Update some tests that would fail in 2025 collection --- app/models/sales_log.rb | 5 ++-- spec/factories/sales_log.rb | 2 ++ .../collection_resources_helper_spec.rb | 25 ++++++++++++------- spec/models/sales_log_spec.rb | 11 +++++++- .../sales/setup_validations_spec.rb | 18 ++++++------- 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 361aab6f6..ae0bd1c17 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -61,13 +61,14 @@ class SalesLog < Log [by_postcode, param_without_spaces, param_without_spaces]) } scope :age1_answered, -> { where.not(age1: nil).or(where(age1_known: [1, 2])) } + scope :ecstat1_answered, -> { where.not(ecstat1: nil).or(where("saledate >= ?", Time.zone.local(2025, 4, 1))) } scope :duplicate_logs, lambda { |log| visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) .where.not(id: log.id) .where.not(saledate: nil) .where.not(sex1: nil) - .where.not(ecstat1: nil) .where.not(postcode_full: nil) + .ecstat1_answered .age1_answered } scope :after_date, ->(date) { where("saledate >= ?", date) } @@ -77,9 +78,9 @@ class SalesLog < Log .group(*DUPLICATE_LOG_ATTRIBUTES) .where.not(saledate: nil) .where.not(sex1: nil) - .where.not(ecstat1: nil) .where.not(postcode_full: nil) .age1_answered + .ecstat1_answered .having("COUNT(*) > 1") if assigned_to_id diff --git a/spec/factories/sales_log.rb b/spec/factories/sales_log.rb index 687b042a4..d59a03907 100644 --- a/spec/factories/sales_log.rb +++ b/spec/factories/sales_log.rb @@ -33,6 +33,7 @@ FactoryBot.define do noint { 2 } privacynotice { 1 } purchid { rand(999_999_999).to_s } + staircase { 1 } end trait :discounted_ownership_setup_complete do saledate_today @@ -66,6 +67,7 @@ FactoryBot.define do postcode_full { "A1 1AA" } noint { 2 } uprn_known { 0 } + staircase { 1 } end trait :completed do purchid { rand(999_999_999).to_s } diff --git a/spec/helpers/collection_resources_helper_spec.rb b/spec/helpers/collection_resources_helper_spec.rb index 05c164fc1..a146e43cc 100644 --- a/spec/helpers/collection_resources_helper_spec.rb +++ b/spec/helpers/collection_resources_helper_spec.rb @@ -4,10 +4,18 @@ RSpec.describe CollectionResourcesHelper do let(:current_user) { create(:user, :data_coordinator) } let(:user) { create(:user, :data_coordinator) } let(:storage_service) { instance_double(Storage::S3Service, get_file_metadata: nil) } + let(:current_date) { Time.zone.local(2024, 8, 8) } before do allow(Storage::S3Service).to receive(:new).and_return(storage_service) allow(storage_service).to receive(:configuration).and_return(OpenStruct.new(bucket_name: "core-test-collection-resources")) + Timecop.travel(current_date) + Singleton.__init__(FormHandler) + end + + after do + Timecop.return + Singleton.__init__(FormHandler) end describe "when displaying file metadata" do @@ -34,9 +42,10 @@ RSpec.describe CollectionResourcesHelper do describe "#editable_collection_resource_years" do context "when in crossover period" do + let(:current_date) { Time.zone.local(2024, 4, 8) } + before do allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true) - allow(Time.zone).to receive(:today).and_return(Time.zone.local(2024, 4, 8)) end it "returns previous and current years" do @@ -50,9 +59,7 @@ RSpec.describe CollectionResourcesHelper do end context "and after 1st January" do - before do - allow(Time.zone).to receive(:today).and_return(Time.zone.local(2025, 2, 1)) - end + let(:current_date) { Time.zone.local(2025, 2, 1) } it "returns current and next years" do expect(editable_collection_resource_years).to match_array([2024, 2025]) @@ -60,9 +67,7 @@ RSpec.describe CollectionResourcesHelper do end context "and before 1st January" do - before do - allow(Time.zone).to receive(:today).and_return(Time.zone.local(2024, 12, 1)) - end + let(:current_date) { Time.zone.local(2024, 12, 1) } it "returns current year" do expect(editable_collection_resource_years).to eq([2024]) @@ -73,9 +78,10 @@ RSpec.describe CollectionResourcesHelper do describe "#displayed_collection_resource_years" do context "when in crossover period" do + let(:current_date) { Time.zone.local(2024, 4, 8) } + before do allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true) - allow(Time.zone).to receive(:today).and_return(Time.zone.local(2024, 4, 8)) end it "returns previous and current years" do @@ -188,9 +194,10 @@ RSpec.describe CollectionResourcesHelper do end context "when next year is editable" do + let(:current_date) { Time.zone.local(2025, 1, 1) } + before do allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(false) - allow(Time.zone).to receive(:today).and_return(Time.zone.local(2025, 1, 1)) end it "returns true" do diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 9fe5a02a9..d4d6f9aa7 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -297,9 +297,18 @@ RSpec.describe SalesLog, type: :model do end end - context "when there is a log with a different ecstat1" do + context "when there is a 2024 log with a different ecstat1" do let!(:different_ecstat1) { create(:sales_log, :duplicate, ecstat1: 0, owning_organisation: organisation) } + before do + Timecop.freeze(Time.zone.local(2024, 5, 2)) + Singleton.__init__(FormHandler) + end + + after do + Timecop.return + end + it "does not return a log with a different ecstat1 as a duplicate" do expect(described_class.duplicate_logs(log)).not_to include(different_ecstat1) end diff --git a/spec/models/validations/sales/setup_validations_spec.rb b/spec/models/validations/sales/setup_validations_spec.rb index 659cde23f..45579ca42 100644 --- a/spec/models/validations/sales/setup_validations_spec.rb +++ b/spec/models/validations/sales/setup_validations_spec.rb @@ -4,6 +4,8 @@ RSpec.describe Validations::Sales::SetupValidations do subject(:setup_validator) { validator_class.new } let(:validator_class) { Class.new { include Validations::Sales::SetupValidations } } + let(:current_year) { FormHandler.instance.current_sales_form.start_date.year } + let(:previous_year) { FormHandler.instance.previous_sales_form.start_date.year } describe "#validate_saledate_collection_year" do context "with sales_in_crossover_period == false" do @@ -68,7 +70,7 @@ RSpec.describe Validations::Sales::SetupValidations do end context "when saledate is in an open previous collection year" do - let(:record) { build(:sales_log, saledate: Time.zone.local(2024, 1, 1)) } + let(:record) { build(:sales_log, saledate: Time.zone.local(current_year, 1, 1)) } before do allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true) @@ -91,12 +93,12 @@ RSpec.describe Validations::Sales::SetupValidations do it "adds error" do setup_validator.validate_saledate_collection_year(record) - expect(record.errors[:saledate]).to include("Enter a date within the 2023 to 2024 or 2024 to 2025 collection years, which is between 1st April 2023 and 31st March 2025.") + expect(record.errors[:saledate]).to include("Enter a date within the #{previous_year} to #{previous_year + 1} or #{current_year} to #{current_year + 1} collection years, which is between 1st April #{previous_year} and 31st March #{current_year + 1}.") end end context "when saledate is after an open collection year" do - let(:record) { build(:sales_log, saledate: Time.zone.local(2025, 4, 1)) } + let(:record) { build(:sales_log, saledate: Time.zone.local(current_year + 1, 4, 1)) } before do allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true) @@ -105,21 +107,17 @@ RSpec.describe Validations::Sales::SetupValidations do it "adds error" do setup_validator.validate_saledate_collection_year(record) - expect(record.errors[:saledate]).to include("Enter a date within the 2023 to 2024 or 2024 to 2025 collection years, which is between 1st April 2023 and 31st March 2025.") + expect(record.errors[:saledate]).to include("Enter a date within the #{previous_year} to #{previous_year + 1} or #{current_year} to #{current_year + 1} collection years, which is between 1st April #{previous_year} and 31st March #{current_year + 1}.") end end context "when current time is after the new logs end date but before edit end date for the previous period" do let(:record) { build(:sales_log, saledate: nil) } - before do - allow(Time).to receive(:now).and_return(Time.zone.local(2025, 1, 8)) - end - it "cannot create new logs for the archived collection year" do record.saledate = Time.zone.local(2023, 1, 1) setup_validator.validate_saledate_collection_year(record) - expect(record.errors["saledate"]).to include(match "Enter a date within the 2023 to 2024 or 2024 to 2025 collection years, which is between 1st April 2023 and 31st March 2025.") + expect(record.errors["saledate"]).to include(match "Enter a date within the #{previous_year} to #{previous_year + 1} or #{current_year} to #{current_year + 1} collection years, which is between 1st April #{previous_year} and 31st March #{current_year + 1}.") end it "can edit already created logs for the previous collection year" do @@ -127,7 +125,7 @@ RSpec.describe Validations::Sales::SetupValidations do record.save!(validate: false) record.saledate = Time.zone.local(2024, 1, 1) setup_validator.validate_saledate_collection_year(record) - expect(record.errors["saledate"]).not_to include(match "Enter a date within the 2024 to 2025 collection year, which is between 1st April 2024 and 31st March 2025.") + expect(record.errors["saledate"]).not_to include(match "Enter a date within the #{current_year} to #{current_year + 1} collection year, which is between 1st April #{current_year} and 31st March #{current_year + 1}.") end end