Browse Source

Update some tests that would fail in 2025 collection

pull/2875/head
Kat 8 months ago
parent
commit
f67bb30de8
  1. 5
      app/models/sales_log.rb
  2. 2
      spec/factories/sales_log.rb
  3. 25
      spec/helpers/collection_resources_helper_spec.rb
  4. 11
      spec/models/sales_log_spec.rb
  5. 18
      spec/models/validations/sales/setup_validations_spec.rb

5
app/models/sales_log.rb

@ -61,13 +61,14 @@ class SalesLog < Log
[by_postcode, param_without_spaces, param_without_spaces]) [by_postcode, param_without_spaces, param_without_spaces])
} }
scope :age1_answered, -> { where.not(age1: nil).or(where(age1_known: [1, 2])) } 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| scope :duplicate_logs, lambda { |log|
visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES))
.where.not(id: log.id) .where.not(id: log.id)
.where.not(saledate: nil) .where.not(saledate: nil)
.where.not(sex1: nil) .where.not(sex1: nil)
.where.not(ecstat1: nil)
.where.not(postcode_full: nil) .where.not(postcode_full: nil)
.ecstat1_answered
.age1_answered .age1_answered
} }
scope :after_date, ->(date) { where("saledate >= ?", date) } scope :after_date, ->(date) { where("saledate >= ?", date) }
@ -77,9 +78,9 @@ class SalesLog < Log
.group(*DUPLICATE_LOG_ATTRIBUTES) .group(*DUPLICATE_LOG_ATTRIBUTES)
.where.not(saledate: nil) .where.not(saledate: nil)
.where.not(sex1: nil) .where.not(sex1: nil)
.where.not(ecstat1: nil)
.where.not(postcode_full: nil) .where.not(postcode_full: nil)
.age1_answered .age1_answered
.ecstat1_answered
.having("COUNT(*) > 1") .having("COUNT(*) > 1")
if assigned_to_id if assigned_to_id

2
spec/factories/sales_log.rb

@ -33,6 +33,7 @@ FactoryBot.define do
noint { 2 } noint { 2 }
privacynotice { 1 } privacynotice { 1 }
purchid { rand(999_999_999).to_s } purchid { rand(999_999_999).to_s }
staircase { 1 }
end end
trait :discounted_ownership_setup_complete do trait :discounted_ownership_setup_complete do
saledate_today saledate_today
@ -66,6 +67,7 @@ FactoryBot.define do
postcode_full { "A1 1AA" } postcode_full { "A1 1AA" }
noint { 2 } noint { 2 }
uprn_known { 0 } uprn_known { 0 }
staircase { 1 }
end end
trait :completed do trait :completed do
purchid { rand(999_999_999).to_s } purchid { rand(999_999_999).to_s }

25
spec/helpers/collection_resources_helper_spec.rb

@ -4,10 +4,18 @@ RSpec.describe CollectionResourcesHelper do
let(:current_user) { create(:user, :data_coordinator) } let(:current_user) { create(:user, :data_coordinator) }
let(:user) { create(:user, :data_coordinator) } let(:user) { create(:user, :data_coordinator) }
let(:storage_service) { instance_double(Storage::S3Service, get_file_metadata: nil) } let(:storage_service) { instance_double(Storage::S3Service, get_file_metadata: nil) }
let(:current_date) { Time.zone.local(2024, 8, 8) }
before do before do
allow(Storage::S3Service).to receive(:new).and_return(storage_service) 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")) 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 end
describe "when displaying file metadata" do describe "when displaying file metadata" do
@ -34,9 +42,10 @@ RSpec.describe CollectionResourcesHelper do
describe "#editable_collection_resource_years" do describe "#editable_collection_resource_years" do
context "when in crossover period" do context "when in crossover period" do
let(:current_date) { Time.zone.local(2024, 4, 8) }
before do before do
allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true) 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 end
it "returns previous and current years" do it "returns previous and current years" do
@ -50,9 +59,7 @@ RSpec.describe CollectionResourcesHelper do
end end
context "and after 1st January" do context "and after 1st January" do
before do let(:current_date) { Time.zone.local(2025, 2, 1) }
allow(Time.zone).to receive(:today).and_return(Time.zone.local(2025, 2, 1))
end
it "returns current and next years" do it "returns current and next years" do
expect(editable_collection_resource_years).to match_array([2024, 2025]) expect(editable_collection_resource_years).to match_array([2024, 2025])
@ -60,9 +67,7 @@ RSpec.describe CollectionResourcesHelper do
end end
context "and before 1st January" do context "and before 1st January" do
before do let(:current_date) { Time.zone.local(2024, 12, 1) }
allow(Time.zone).to receive(:today).and_return(Time.zone.local(2024, 12, 1))
end
it "returns current year" do it "returns current year" do
expect(editable_collection_resource_years).to eq([2024]) expect(editable_collection_resource_years).to eq([2024])
@ -73,9 +78,10 @@ RSpec.describe CollectionResourcesHelper do
describe "#displayed_collection_resource_years" do describe "#displayed_collection_resource_years" do
context "when in crossover period" do context "when in crossover period" do
let(:current_date) { Time.zone.local(2024, 4, 8) }
before do before do
allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true) 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 end
it "returns previous and current years" do it "returns previous and current years" do
@ -188,9 +194,10 @@ RSpec.describe CollectionResourcesHelper do
end end
context "when next year is editable" do context "when next year is editable" do
let(:current_date) { Time.zone.local(2025, 1, 1) }
before do before do
allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(false) 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 end
it "returns true" do it "returns true" do

11
spec/models/sales_log_spec.rb

@ -297,9 +297,18 @@ RSpec.describe SalesLog, type: :model do
end end
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) } 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 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) expect(described_class.duplicate_logs(log)).not_to include(different_ecstat1)
end end

18
spec/models/validations/sales/setup_validations_spec.rb

@ -4,6 +4,8 @@ RSpec.describe Validations::Sales::SetupValidations do
subject(:setup_validator) { validator_class.new } subject(:setup_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::Sales::SetupValidations } } 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 describe "#validate_saledate_collection_year" do
context "with sales_in_crossover_period == false" do context "with sales_in_crossover_period == false" do
@ -68,7 +70,7 @@ RSpec.describe Validations::Sales::SetupValidations do
end end
context "when saledate is in an open previous collection year" do 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 before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true) 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 it "adds error" do
setup_validator.validate_saledate_collection_year(record) 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
end end
context "when saledate is after an open collection year" do 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 before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true) 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 it "adds error" do
setup_validator.validate_saledate_collection_year(record) 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
end end
context "when current time is after the new logs end date but before edit end date for the previous period" do 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) } 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 it "cannot create new logs for the archived collection year" do
record.saledate = Time.zone.local(2023, 1, 1) record.saledate = Time.zone.local(2023, 1, 1)
setup_validator.validate_saledate_collection_year(record) 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 end
it "can edit already created logs for the previous collection year" do 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.save!(validate: false)
record.saledate = Time.zone.local(2024, 1, 1) record.saledate = Time.zone.local(2024, 1, 1)
setup_validator.validate_saledate_collection_year(record) 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
end end

Loading…
Cancel
Save