From 84318b762bb83817cd0251b2968f6e376806265e Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 22 May 2024 13:47:53 +0100 Subject: [PATCH] Update a few more model specs --- .../location_deactivation_period_spec.rb | 38 ++++---- spec/models/location_spec.rb | 87 ++++++------------- .../models/scheme_deactivation_period_spec.rb | 33 ++++--- spec/models/scheme_spec.rb | 42 +++------ 4 files changed, 83 insertions(+), 117 deletions(-) diff --git a/spec/models/location_deactivation_period_spec.rb b/spec/models/location_deactivation_period_spec.rb index 4e0bb3384..e2cfd5758 100644 --- a/spec/models/location_deactivation_period_spec.rb +++ b/spec/models/location_deactivation_period_spec.rb @@ -2,31 +2,34 @@ require "rails_helper" RSpec.describe LocationDeactivationPeriod do let(:validator) { LocationDeactivationPeriodValidator.new } - let(:location) { FactoryBot.create(:location, startdate: now - 2.years) } - let(:record) { FactoryBot.create(:location_deactivation_period, deactivation_date: now, location:) } + let(:previous_collection_start_date) { Time.zone.local(2022, 4, 1) } + let(:current_collection_start_date) { Time.zone.local(2023, 4, 1) } + let(:location) { FactoryBot.create(:location, startdate: previous_collection_start_date - 2.years) } + let(:record) { FactoryBot.create(:location_deactivation_period, deactivation_date: current_collection_start_date, location:) } describe "#validate" do - around do |example| - Timecop.freeze(now) do - example.run - end + before do + allow(FormHandler.instance).to receive(:previous_collection_start_date).and_return(previous_collection_start_date) + allow(FormHandler.instance).to receive(:current_collection_start_date).and_return(current_collection_start_date) end context "when not in a crossover period" do - let(:now) { Time.utc(2023, 3, 1) } + before do + allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(false) + end context "with a deactivation date before the current collection period" do it "adds an error" do - record.deactivation_date = now - 1.year + record.deactivation_date = current_collection_start_date - 1.year location.location_deactivation_periods.clear validator.validate(record) - expect(record.errors[:deactivation_date]).to include "The date must be on or after the 1 April 2022" + expect(record.errors[:deactivation_date]).to include "The date must be on or after the 1 April 2023" end end context "with a deactivation date in the current collection period" do it "does not add an error" do - record.deactivation_date = now - 1.day + record.deactivation_date = current_collection_start_date + 1.day location.location_deactivation_periods.clear validator.validate(record) expect(record.errors).to be_empty @@ -35,11 +38,13 @@ RSpec.describe LocationDeactivationPeriod do end context "when in a crossover period" do - let(:now) { Time.utc(2023, 5, 1) } + before do + allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true) + end context "with a deactivation date before the previous collection period" do it "does not add an error" do - record.deactivation_date = now - 2.years + record.deactivation_date = previous_collection_start_date - 2.years location.location_deactivation_periods.clear validator.validate(record) expect(record.errors[:deactivation_date]).to include "The date must be on or after the 1 April 2022" @@ -48,7 +53,7 @@ RSpec.describe LocationDeactivationPeriod do context "with a deactivation date in the previous collection period" do it "does not add an error" do - record.deactivation_date = now - 1.year + record.deactivation_date = previous_collection_start_date + 1.day location.location_deactivation_periods.clear validator.validate(record) expect(record.errors).to be_empty @@ -57,7 +62,7 @@ RSpec.describe LocationDeactivationPeriod do context "with a deactivation date in the current collection period" do it "does not add an error" do - record.deactivation_date = now - 1.day + record.deactivation_date = current_collection_start_date + 1.day location.location_deactivation_periods.clear validator.validate(record) expect(record.errors).to be_empty @@ -66,11 +71,12 @@ RSpec.describe LocationDeactivationPeriod do context "but the location was created in the current collection period" do let(:location) { FactoryBot.create(:location, startdate:) } - let(:startdate) { now - 2.days } + let(:startdate) { current_collection_start_date + 2.days } + let(:record) { FactoryBot.create(:location_deactivation_period, deactivation_date: current_collection_start_date + 3.days, location:) } context "with a deactivation date in the previous collection period" do it "adds an error" do - record.deactivation_date = now - 1.year + record.deactivation_date = previous_collection_start_date + 1.day location.location_deactivation_periods.clear validator.validate(record) start_date = startdate.to_formatted_s(:govuk_date) diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index 50dc4b4f4..44118bcc6 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -99,11 +99,7 @@ RSpec.describe Location, type: :model do let(:today) { Time.zone.local(2022, 4, 1) } before do - Timecop.freeze(today) - end - - after do - Timecop.unfreeze + allow(Time).to receive(:now).and_return(today) end it "returns a list of local authorities" do @@ -425,11 +421,7 @@ RSpec.describe Location, type: :model do let(:today) { Time.zone.local(2023, 5, 1) } before do - Timecop.freeze(today) - end - - after do - Timecop.unfreeze + allow(Time).to receive(:now).and_return(today) end it "returns a list of local authorities" do @@ -842,15 +834,7 @@ RSpec.describe Location, type: :model do end describe "status" do - let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 4, 1)) } - - before do - Timecop.freeze(2022, 6, 7) - end - - after do - Timecop.unfreeze - end + let(:location) { FactoryBot.build(:location, startdate: Time.zone.today - 2.months) } context "when location is not confirmed" do it "returns incomplete " do @@ -865,7 +849,7 @@ RSpec.describe Location, type: :model do end it "returns deactivating soon if deactivation_date is in the future" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today + 2.months, location:) location.save! expect(location.status).to eq(:deactivating_soon) end @@ -876,25 +860,25 @@ RSpec.describe Location, type: :model do end it "returns deactivated if deactivation_date is in the past" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.yesterday, location:) location.save! expect(location.status).to eq(:deactivated) end it "returns deactivated if deactivation_date is today" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today, location:) location.save! expect(location.status).to eq(:deactivated) end it "returns reactivating soon if the location has a future reactivation date" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 8), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today, reactivation_date: Time.zone.tomorrow, location:) location.save! expect(location.status).to eq(:reactivating_soon) end it "returns activating soon if the location has a future startdate" do - location.startdate = Time.zone.local(2022, 7, 7) + location.startdate = Time.zone.today + 1.month location.save! expect(location.status).to eq(:activating_soon) end @@ -902,7 +886,7 @@ RSpec.describe Location, type: :model do context "when there have been previous deactivations" do before do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 4), reactivation_date: Time.zone.local(2022, 6, 5), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today - 1.month, reactivation_date: Time.zone.today - 2.days, location:) location.save! end @@ -911,39 +895,37 @@ RSpec.describe Location, type: :model do end it "returns deactivating soon if deactivation_date is in the future" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today + 1.month, location:) location.save! expect(location.status).to eq(:deactivating_soon) end it "returns deactivated if deactivation_date is in the past" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.yesterday, location:) location.save! expect(location.status).to eq(:deactivated) end it "returns deactivated if deactivation_date is today" do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today, location:) location.save! expect(location.status).to eq(:deactivated) end it "returns reactivating soon if the location has a future reactivation date" do - Timecop.freeze(2022, 6, 8) - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 9), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.yesterday, reactivation_date: Time.zone.tomorrow, location:) location.save! expect(location.status).to eq(:reactivating_soon) end it "returns reactivating soon if the location had a deactivation during another deactivation" do - Timecop.freeze(2022, 6, 4) - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today - 1.month, reactivation_date: Time.zone.today + 2.days, location:) location.save! expect(location.status).to eq(:reactivating_soon) end it "returns activating soon if the location has a future startdate" do - location.startdate = Time.zone.local(2022, 7, 7) + location.startdate = Time.zone.tomorrow location.save! expect(location.status).to eq(:activating_soon) end @@ -951,24 +933,16 @@ RSpec.describe Location, type: :model do end describe "status_at" do - let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 4, 1)) } - - before do - Timecop.freeze(2022, 6, 7) - end - - after do - Timecop.unfreeze - end + let(:location) { FactoryBot.build(:location, startdate: Time.zone.today - 3.months) } context "when there have been previous deactivations" do before do - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 4), reactivation_date: Time.zone.local(2022, 6, 5), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today - 1.month, reactivation_date: Time.zone.today - 2.days, location:) location.save! end it "returns active if the location has no relevant deactivation records" do - expect(location.status_at(Time.zone.local(2022, 4, 4))).to eq(:active) + expect(location.status_at(Time.zone.today - 2.months)).to eq(:active) end end end @@ -977,28 +951,23 @@ RSpec.describe Location, type: :model do let!(:deactivated_organisation) { FactoryBot.create(:organisation, active: false) } let!(:deactivated_by_organisation_scheme) { FactoryBot.create(:scheme, owning_organisation: deactivated_organisation) } let!(:deactivated_by_organisation_location) { FactoryBot.create(:location, scheme: deactivated_by_organisation_scheme) } - let!(:incomplete_location) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.local(2022, 4, 1)) } - let!(:incomplete_location_with_nil_confirmed) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.local(2022, 4, 1), confirmed: nil) } - let!(:active_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } - let(:deactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } - let(:deactivated_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } - let(:reactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } - let!(:activating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 7, 7)) } + let!(:incomplete_location) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.today - 3.months) } + let!(:incomplete_location_with_nil_confirmed) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.today - 3.months, confirmed: nil) } + let!(:active_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) } + let(:deactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) } + let(:deactivated_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) } + let(:reactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) } + let!(:activating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.today + 1.day) } before do - Timecop.freeze(2022, 6, 7) - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8), location: deactivating_soon_location) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today + 1.month, location: deactivating_soon_location) deactivating_soon_location.save! - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), location: deactivated_location) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.yesterday, location: deactivated_location) deactivated_location.save! - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 8), location: reactivating_soon_location) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today, reactivation_date: Time.zone.tomorrow, location: reactivating_soon_location) reactivating_soon_location.save! end - after do - Timecop.unfreeze - end - context "when filtering by incomplete status" do it "returns only incomplete locations" do expect(described_class.filter_by_status(%w[incomplete]).count).to eq(2) diff --git a/spec/models/scheme_deactivation_period_spec.rb b/spec/models/scheme_deactivation_period_spec.rb index eb46ee62f..9e59399e9 100644 --- a/spec/models/scheme_deactivation_period_spec.rb +++ b/spec/models/scheme_deactivation_period_spec.rb @@ -2,31 +2,34 @@ require "rails_helper" RSpec.describe SchemeDeactivationPeriod do let(:validator) { SchemeDeactivationPeriodValidator.new } - let(:scheme) { FactoryBot.create(:scheme, created_at: now - 2.years) } - let(:record) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: now, scheme:) } + let(:previous_collection_start_date) { Time.zone.local(2022, 4, 1) } + let(:current_collection_start_date) { Time.zone.local(2023, 4, 1) } + let(:scheme) { FactoryBot.create(:scheme, created_at: previous_collection_start_date - 2.years) } + let(:record) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: current_collection_start_date, scheme:) } describe "#validate" do - around do |example| - Timecop.freeze(now) do - example.run - end + before do + allow(FormHandler.instance).to receive(:previous_collection_start_date).and_return(previous_collection_start_date) + allow(FormHandler.instance).to receive(:current_collection_start_date).and_return(current_collection_start_date) end context "when not in a crossover period" do - let(:now) { Time.utc(2023, 3, 1) } + before do + allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(false) + end context "with a deactivation date before the current collection period" do it "adds an error" do - record.deactivation_date = now - 1.year + record.deactivation_date = current_collection_start_date - 1.year scheme.scheme_deactivation_periods.clear validator.validate(record) - expect(record.errors[:deactivation_date]).to include("The date must be on or after the 1 April 2022") + expect(record.errors[:deactivation_date]).to include("The date must be on or after the 1 April 2023") end end context "with a deactivation date in the current collection period" do it "does not add an error" do - record.deactivation_date = now - 1.day + record.deactivation_date = current_collection_start_date + 1.day scheme.scheme_deactivation_periods.clear validator.validate(record) expect(record.errors[:deactivation_date]).to be_empty @@ -35,11 +38,13 @@ RSpec.describe SchemeDeactivationPeriod do end context "when in a crossover period" do - let(:now) { Time.utc(2023, 5, 1) } + before do + allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true) + end context "with a deactivation date before the previous collection period" do it "does not add an error" do - record.deactivation_date = now - 2.years + record.deactivation_date = previous_collection_start_date - 2.years scheme.scheme_deactivation_periods.clear validator.validate(record) expect(record.errors[:deactivation_date]).to include("The date must be on or after the 1 April 2022") @@ -48,7 +53,7 @@ RSpec.describe SchemeDeactivationPeriod do context "with a deactivation date in the previous collection period" do it "does not add an error" do - record.deactivation_date = now - 1.year + record.deactivation_date = previous_collection_start_date + 1.year scheme.scheme_deactivation_periods.clear validator.validate(record) expect(record.errors[:deactivation_date]).to be_empty @@ -57,7 +62,7 @@ RSpec.describe SchemeDeactivationPeriod do context "with a deactivation date in the current collection period" do it "does not add an error" do - record.deactivation_date = now - 1.day + record.deactivation_date = current_collection_start_date + 1.day scheme.scheme_deactivation_periods.clear validator.validate(record) expect(record.errors[:deactivation_date]).to be_empty diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index c84be444f..75fe66833 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -216,11 +216,6 @@ RSpec.describe Scheme, type: :model do before do FactoryBot.create(:location, scheme:) - Timecop.freeze(2022, 6, 7) - end - - after do - Timecop.unfreeze end context "when there have not been any previous deactivations" do @@ -229,7 +224,7 @@ RSpec.describe Scheme, type: :model do end it "returns deactivating soon if deactivation_date is in the future" do - FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today + 2.weeks, scheme:) scheme.reload expect(scheme.status).to eq(:deactivating_soon) end @@ -240,34 +235,32 @@ RSpec.describe Scheme, type: :model do end it "returns deactivated if deactivation_date is in the past" do - FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.yesterday, scheme:) scheme.reload expect(scheme.status).to eq(:deactivated) end it "returns deactivated if deactivation_date is today" do - FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today, scheme:) scheme.reload expect(scheme.status).to eq(:deactivated) end it "returns reactivating soon if the scheme has a future reactivation date" do - FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 8), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today, reactivation_date: Time.zone.tomorrow, scheme:) scheme.save! expect(scheme.status).to eq(:reactivating_soon) end it "returns activating soon if the scheme has a future startdate" do - Timecop.freeze(2022, 6, 4) - scheme.startdate = Time.zone.local(2022, 7, 7) - scheme.save! + scheme.startdate = Time.zone.today + 2.weeks expect(scheme.status).to eq(:activating_soon) end end context "when there have been previous deactivations" do before do - FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 6, 5), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today - 3.days, reactivation_date: Time.zone.today - 2.days, scheme:) end it "returns active if the scheme has no relevant deactivation records" do @@ -275,39 +268,37 @@ RSpec.describe Scheme, type: :model do end it "returns deactivating soon if deactivation_date is in the future" do - FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today + 3.months, scheme:) scheme.reload expect(scheme.status).to eq(:deactivating_soon) end it "returns deactivated if deactivation_date is in the past" do - FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.yesterday, scheme:) scheme.reload expect(scheme.status).to eq(:deactivated) end it "returns deactivated if deactivation_date is today" do - FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today, scheme:) scheme.reload expect(scheme.status).to eq(:deactivated) end it "returns reactivating soon if the scheme has a future reactivation date" do - Timecop.freeze(2022, 6, 8) - FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 9), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today, reactivation_date: Time.zone.tomorrow, scheme:) scheme.save! expect(scheme.status).to eq(:reactivating_soon) end it "returns reactivating soon if the scheme had a deactivation during another deactivation" do - Timecop.freeze(2022, 6, 4) - FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today - 2.months, reactivation_date: Time.zone.today + 2.days, scheme:) scheme.save! expect(scheme.status).to eq(:reactivating_soon) end it "returns activating soon if the scheme has a future startdate" do - scheme.startdate = Time.zone.local(2022, 7, 7) + scheme.startdate = Time.zone.tomorrow scheme.save! expect(scheme.status).to eq(:activating_soon) end @@ -327,20 +318,15 @@ RSpec.describe Scheme, type: :model do before do FactoryBot.create(:location, scheme:) - Timecop.freeze(2022, 6, 7) - end - - after do - Timecop.unfreeze end context "when there have been previous deactivations" do before do - FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 6, 5), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today - 3.days, reactivation_date: Time.zone.today - 2.days, scheme:) end it "returns active if the scheme has no relevant deactivation records" do - expect(scheme.status_at(Time.zone.local(2022, 5, 5))).to eq(:active) + expect(scheme.status_at(Time.zone.today - 1.month)).to eq(:active) end end end