Browse Source

Update a few more model specs

pull/2432/head
Kat 2 years ago
parent
commit
84318b762b
  1. 38
      spec/models/location_deactivation_period_spec.rb
  2. 87
      spec/models/location_spec.rb
  3. 33
      spec/models/scheme_deactivation_period_spec.rb
  4. 42
      spec/models/scheme_spec.rb

38
spec/models/location_deactivation_period_spec.rb

@ -2,31 +2,34 @@ require "rails_helper"
RSpec.describe LocationDeactivationPeriod do RSpec.describe LocationDeactivationPeriod do
let(:validator) { LocationDeactivationPeriodValidator.new } let(:validator) { LocationDeactivationPeriodValidator.new }
let(:location) { FactoryBot.create(:location, startdate: now - 2.years) } let(:previous_collection_start_date) { Time.zone.local(2022, 4, 1) }
let(:record) { FactoryBot.create(:location_deactivation_period, deactivation_date: now, location:) } 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 describe "#validate" do
around do |example| before do
Timecop.freeze(now) do allow(FormHandler.instance).to receive(:previous_collection_start_date).and_return(previous_collection_start_date)
example.run allow(FormHandler.instance).to receive(:current_collection_start_date).and_return(current_collection_start_date)
end
end end
context "when not in a crossover period" do 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 context "with a deactivation date before the current collection period" do
it "adds an error" 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 location.location_deactivation_periods.clear
validator.validate(record) 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
end end
context "with a deactivation date in the current collection period" do context "with a deactivation date in the current collection period" do
it "does not add an error" 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 location.location_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors).to be_empty expect(record.errors).to be_empty
@ -35,11 +38,13 @@ RSpec.describe LocationDeactivationPeriod do
end end
context "when in a crossover period" do 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 context "with a deactivation date before the previous collection period" do
it "does not add an error" 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 location.location_deactivation_periods.clear
validator.validate(record) 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 2022"
@ -48,7 +53,7 @@ RSpec.describe LocationDeactivationPeriod do
context "with a deactivation date in the previous collection period" do context "with a deactivation date in the previous collection period" do
it "does not add an error" 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 location.location_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors).to be_empty 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 context "with a deactivation date in the current collection period" do
it "does not add an error" 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 location.location_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors).to be_empty 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 context "but the location was created in the current collection period" do
let(:location) { FactoryBot.create(:location, startdate:) } 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 context "with a deactivation date in the previous collection period" do
it "adds an error" 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 location.location_deactivation_periods.clear
validator.validate(record) validator.validate(record)
start_date = startdate.to_formatted_s(:govuk_date) start_date = startdate.to_formatted_s(:govuk_date)

87
spec/models/location_spec.rb

@ -99,11 +99,7 @@ RSpec.describe Location, type: :model do
let(:today) { Time.zone.local(2022, 4, 1) } let(:today) { Time.zone.local(2022, 4, 1) }
before do before do
Timecop.freeze(today) allow(Time).to receive(:now).and_return(today)
end
after do
Timecop.unfreeze
end end
it "returns a list of local authorities" do 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) } let(:today) { Time.zone.local(2023, 5, 1) }
before do before do
Timecop.freeze(today) allow(Time).to receive(:now).and_return(today)
end
after do
Timecop.unfreeze
end end
it "returns a list of local authorities" do it "returns a list of local authorities" do
@ -842,15 +834,7 @@ RSpec.describe Location, type: :model do
end end
describe "status" do describe "status" do
let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 4, 1)) } let(:location) { FactoryBot.build(:location, startdate: Time.zone.today - 2.months) }
before do
Timecop.freeze(2022, 6, 7)
end
after do
Timecop.unfreeze
end
context "when location is not confirmed" do context "when location is not confirmed" do
it "returns incomplete " do it "returns incomplete " do
@ -865,7 +849,7 @@ RSpec.describe Location, type: :model do
end end
it "returns deactivating soon if deactivation_date is in the future" do 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! location.save!
expect(location.status).to eq(:deactivating_soon) expect(location.status).to eq(:deactivating_soon)
end end
@ -876,25 +860,25 @@ RSpec.describe Location, type: :model do
end end
it "returns deactivated if deactivation_date is in the past" do 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! location.save!
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end end
it "returns deactivated if deactivation_date is today" do 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! location.save!
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end end
it "returns reactivating soon if the location has a future reactivation date" do 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! location.save!
expect(location.status).to eq(:reactivating_soon) expect(location.status).to eq(:reactivating_soon)
end end
it "returns activating soon if the location has a future startdate" do 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! location.save!
expect(location.status).to eq(:activating_soon) expect(location.status).to eq(:activating_soon)
end end
@ -902,7 +886,7 @@ RSpec.describe Location, type: :model do
context "when there have been previous deactivations" do context "when there have been previous deactivations" do
before 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! location.save!
end end
@ -911,39 +895,37 @@ RSpec.describe Location, type: :model do
end end
it "returns deactivating soon if deactivation_date is in the future" do 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! location.save!
expect(location.status).to eq(:deactivating_soon) expect(location.status).to eq(:deactivating_soon)
end end
it "returns deactivated if deactivation_date is in the past" do 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! location.save!
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end end
it "returns deactivated if deactivation_date is today" do 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! location.save!
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end end
it "returns reactivating soon if the location has a future reactivation date" do 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.yesterday, reactivation_date: Time.zone.tomorrow, location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 9), location:)
location.save! location.save!
expect(location.status).to eq(:reactivating_soon) expect(location.status).to eq(:reactivating_soon)
end end
it "returns reactivating soon if the location had a deactivation during another deactivation" do 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.today - 1.month, reactivation_date: Time.zone.today + 2.days, location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2), location:)
location.save! location.save!
expect(location.status).to eq(:reactivating_soon) expect(location.status).to eq(:reactivating_soon)
end end
it "returns activating soon if the location has a future startdate" do 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! location.save!
expect(location.status).to eq(:activating_soon) expect(location.status).to eq(:activating_soon)
end end
@ -951,24 +933,16 @@ RSpec.describe Location, type: :model do
end end
describe "status_at" do describe "status_at" do
let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 4, 1)) } let(:location) { FactoryBot.build(:location, startdate: Time.zone.today - 3.months) }
before do
Timecop.freeze(2022, 6, 7)
end
after do
Timecop.unfreeze
end
context "when there have been previous deactivations" do context "when there have been previous deactivations" do
before 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! location.save!
end end
it "returns active if the location has no relevant deactivation records" do 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 end
end end
@ -977,28 +951,23 @@ RSpec.describe Location, type: :model do
let!(:deactivated_organisation) { FactoryBot.create(:organisation, active: false) } let!(:deactivated_organisation) { FactoryBot.create(:organisation, active: false) }
let!(:deactivated_by_organisation_scheme) { FactoryBot.create(:scheme, owning_organisation: deactivated_organisation) } 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!(: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) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.today - 3.months) }
let!(:incomplete_location_with_nil_confirmed) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.local(2022, 4, 1), confirmed: nil) } 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.local(2022, 4, 1)) } let!(:active_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) }
let(:deactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } let(:deactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) }
let(:deactivated_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } let(:deactivated_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) }
let(:reactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } let(:reactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) }
let!(:activating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 7, 7)) } let!(:activating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.today + 1.day) }
before do before do
Timecop.freeze(2022, 6, 7) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today + 1.month, location: deactivating_soon_location)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8), location: deactivating_soon_location)
deactivating_soon_location.save! 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! 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! reactivating_soon_location.save!
end end
after do
Timecop.unfreeze
end
context "when filtering by incomplete status" do context "when filtering by incomplete status" do
it "returns only incomplete locations" do it "returns only incomplete locations" do
expect(described_class.filter_by_status(%w[incomplete]).count).to eq(2) expect(described_class.filter_by_status(%w[incomplete]).count).to eq(2)

33
spec/models/scheme_deactivation_period_spec.rb

@ -2,31 +2,34 @@ require "rails_helper"
RSpec.describe SchemeDeactivationPeriod do RSpec.describe SchemeDeactivationPeriod do
let(:validator) { SchemeDeactivationPeriodValidator.new } let(:validator) { SchemeDeactivationPeriodValidator.new }
let(:scheme) { FactoryBot.create(:scheme, created_at: now - 2.years) } let(:previous_collection_start_date) { Time.zone.local(2022, 4, 1) }
let(:record) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: now, scheme:) } 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 describe "#validate" do
around do |example| before do
Timecop.freeze(now) do allow(FormHandler.instance).to receive(:previous_collection_start_date).and_return(previous_collection_start_date)
example.run allow(FormHandler.instance).to receive(:current_collection_start_date).and_return(current_collection_start_date)
end
end end
context "when not in a crossover period" do 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 context "with a deactivation date before the current collection period" do
it "adds an error" 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 scheme.scheme_deactivation_periods.clear
validator.validate(record) 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
end end
context "with a deactivation date in the current collection period" do context "with a deactivation date in the current collection period" do
it "does not add an error" 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 scheme.scheme_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors[:deactivation_date]).to be_empty expect(record.errors[:deactivation_date]).to be_empty
@ -35,11 +38,13 @@ RSpec.describe SchemeDeactivationPeriod do
end end
context "when in a crossover period" do 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 context "with a deactivation date before the previous collection period" do
it "does not add an error" 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 scheme.scheme_deactivation_periods.clear
validator.validate(record) 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 2022")
@ -48,7 +53,7 @@ RSpec.describe SchemeDeactivationPeriod do
context "with a deactivation date in the previous collection period" do context "with a deactivation date in the previous collection period" do
it "does not add an error" 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 scheme.scheme_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors[:deactivation_date]).to be_empty 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 context "with a deactivation date in the current collection period" do
it "does not add an error" 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 scheme.scheme_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors[:deactivation_date]).to be_empty expect(record.errors[:deactivation_date]).to be_empty

42
spec/models/scheme_spec.rb

@ -216,11 +216,6 @@ RSpec.describe Scheme, type: :model do
before do before do
FactoryBot.create(:location, scheme:) FactoryBot.create(:location, scheme:)
Timecop.freeze(2022, 6, 7)
end
after do
Timecop.unfreeze
end end
context "when there have not been any previous deactivations" do context "when there have not been any previous deactivations" do
@ -229,7 +224,7 @@ RSpec.describe Scheme, type: :model do
end end
it "returns deactivating soon if deactivation_date is in the future" do 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 scheme.reload
expect(scheme.status).to eq(:deactivating_soon) expect(scheme.status).to eq(:deactivating_soon)
end end
@ -240,34 +235,32 @@ RSpec.describe Scheme, type: :model do
end end
it "returns deactivated if deactivation_date is in the past" do 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 scheme.reload
expect(scheme.status).to eq(:deactivated) expect(scheme.status).to eq(:deactivated)
end end
it "returns deactivated if deactivation_date is today" do 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 scheme.reload
expect(scheme.status).to eq(:deactivated) expect(scheme.status).to eq(:deactivated)
end end
it "returns reactivating soon if the scheme has a future reactivation date" do 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! scheme.save!
expect(scheme.status).to eq(:reactivating_soon) expect(scheme.status).to eq(:reactivating_soon)
end end
it "returns activating soon if the scheme has a future startdate" do it "returns activating soon if the scheme has a future startdate" do
Timecop.freeze(2022, 6, 4) scheme.startdate = Time.zone.today + 2.weeks
scheme.startdate = Time.zone.local(2022, 7, 7)
scheme.save!
expect(scheme.status).to eq(:activating_soon) expect(scheme.status).to eq(:activating_soon)
end end
end end
context "when there have been previous deactivations" do context "when there have been previous deactivations" do
before 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 end
it "returns active if the scheme has no relevant deactivation records" do it "returns active if the scheme has no relevant deactivation records" do
@ -275,39 +268,37 @@ RSpec.describe Scheme, type: :model do
end end
it "returns deactivating soon if deactivation_date is in the future" do 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 scheme.reload
expect(scheme.status).to eq(:deactivating_soon) expect(scheme.status).to eq(:deactivating_soon)
end end
it "returns deactivated if deactivation_date is in the past" do 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 scheme.reload
expect(scheme.status).to eq(:deactivated) expect(scheme.status).to eq(:deactivated)
end end
it "returns deactivated if deactivation_date is today" do 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 scheme.reload
expect(scheme.status).to eq(:deactivated) expect(scheme.status).to eq(:deactivated)
end end
it "returns reactivating soon if the scheme has a future reactivation date" do 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.today, reactivation_date: Time.zone.tomorrow, scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 9), scheme:)
scheme.save! scheme.save!
expect(scheme.status).to eq(:reactivating_soon) expect(scheme.status).to eq(:reactivating_soon)
end end
it "returns reactivating soon if the scheme had a deactivation during another deactivation" do 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.today - 2.months, reactivation_date: Time.zone.today + 2.days, scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2), scheme:)
scheme.save! scheme.save!
expect(scheme.status).to eq(:reactivating_soon) expect(scheme.status).to eq(:reactivating_soon)
end end
it "returns activating soon if the scheme has a future startdate" do 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! scheme.save!
expect(scheme.status).to eq(:activating_soon) expect(scheme.status).to eq(:activating_soon)
end end
@ -327,20 +318,15 @@ RSpec.describe Scheme, type: :model do
before do before do
FactoryBot.create(:location, scheme:) FactoryBot.create(:location, scheme:)
Timecop.freeze(2022, 6, 7)
end
after do
Timecop.unfreeze
end end
context "when there have been previous deactivations" do context "when there have been previous deactivations" do
before 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 end
it "returns active if the scheme has no relevant deactivation records" do 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 end
end end

Loading…
Cancel
Save