Browse Source

Update some tests that would fail in 2025 collection (#2875)

* Update some tests that would fail in 2025 collection

* Update derived sales fields tests

* Update a few feature tests
main
kosiakkatrina 2 days ago committed by GitHub
parent
commit
bef5e84b7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 8
      app/models/form_handler.rb
  2. 5
      app/models/sales_log.rb
  3. 2
      spec/factories/sales_log.rb
  4. 10
      spec/features/organisation_spec.rb
  5. 3
      spec/features/sales_log_spec.rb
  6. 25
      spec/helpers/collection_resources_helper_spec.rb
  7. 26
      spec/models/sales_log_derived_fields_spec.rb
  8. 11
      spec/models/sales_log_spec.rb
  9. 18
      spec/models/validations/sales/setup_validations_spec.rb

8
app/models/form_handler.rb

@ -36,6 +36,10 @@ class FormHandler
forms["next_lettings"] forms["next_lettings"]
end end
def archived_lettings_form
forms["archived_lettings"]
end
def current_sales_form def current_sales_form
forms["current_sales"] forms["current_sales"]
end end
@ -44,6 +48,10 @@ class FormHandler
forms["previous_sales"] forms["previous_sales"]
end end
def archived_sales_form
forms["archived_sales"]
end
def next_sales_form def next_sales_form
forms["next_sales"] forms["next_sales"]
end end

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 }

10
spec/features/organisation_spec.rb

@ -132,6 +132,7 @@ RSpec.describe "User Features" do
let!(:other_general_needs_logs) { FactoryBot.create_list(:lettings_log, 4, assigned_to: user, needstype: 1) } let!(:other_general_needs_logs) { FactoryBot.create_list(:lettings_log, 4, assigned_to: user, needstype: 1) }
let!(:other_supported_housing_logs) { FactoryBot.create_list(:lettings_log, 4, assigned_to: user, needstype: 2) } let!(:other_supported_housing_logs) { FactoryBot.create_list(:lettings_log, 4, assigned_to: user, needstype: 2) }
let(:number_of_lettings_logs) { LettingsLog.count } let(:number_of_lettings_logs) { LettingsLog.count }
let(:previous_year) { FormHandler.instance.previous_lettings_form.start_date.year }
before do before do
allow(FormHandler.instance).to receive(:in_crossover_period?).and_return(true) allow(FormHandler.instance).to receive(:in_crossover_period?).and_return(true)
@ -204,9 +205,9 @@ RSpec.describe "User Features" do
end end
it "can filter lettings logs by year" do it "can filter lettings logs by year" do
check("years-2022-field") check("years-#{previous_year}-field")
click_button("Apply filters") click_button("Apply filters")
expect(page).to have_current_path("/organisations/#{org_id}/lettings-logs?%5Byears%5D[]=&years[]=2022&%5Bstatus%5D[]=&%5Bneedstypes%5D[]=&assigned_to=all&user_text_search=&user=&owning_organisation_select=all&owning_organisation_text_search=&owning_organisation=&managing_organisation_select=all&managing_organisation_text_search=&managing_organisation=") expect(page).to have_current_path("/organisations/#{org_id}/lettings-logs?%5Byears%5D[]=&years[]=#{previous_year}&%5Bstatus%5D[]=&%5Bneedstypes%5D[]=&assigned_to=all&user_text_search=&user=&owning_organisation_select=all&owning_organisation_text_search=&owning_organisation=&managing_organisation_select=all&managing_organisation_text_search=&managing_organisation=")
expect(page).not_to have_link first_log.id.to_s, href: "/lettings-logs/#{first_log.id}" expect(page).not_to have_link first_log.id.to_s, href: "/lettings-logs/#{first_log.id}"
end end
@ -226,6 +227,7 @@ RSpec.describe "User Features" do
context "when viewing sales logs for specific organisation" do context "when viewing sales logs for specific organisation" do
let(:first_log) { organisation.sales_logs.first } let(:first_log) { organisation.sales_logs.first }
let(:number_of_sales_logs) { SalesLog.count } let(:number_of_sales_logs) { SalesLog.count }
let(:previous_year) { FormHandler.instance.previous_sales_form.start_date.year }
before do before do
allow(FormHandler.instance).to receive(:in_crossover_period?).and_return(true) allow(FormHandler.instance).to receive(:in_crossover_period?).and_return(true)
@ -254,9 +256,9 @@ RSpec.describe "User Features" do
organisation.sales_logs.map(&:id).each do |sales_log_id| organisation.sales_logs.map(&:id).each do |sales_log_id|
expect(page).to have_link sales_log_id.to_s, href: "/sales-logs/#{sales_log_id}" expect(page).to have_link sales_log_id.to_s, href: "/sales-logs/#{sales_log_id}"
end end
check("years-2022-field") check("years-#{previous_year}-field")
click_button("Apply filters") click_button("Apply filters")
expect(page).to have_current_path("/organisations/#{org_id}/sales-logs?%5Byears%5D[]=&years[]=2022&%5Bstatus%5D[]=&assigned_to=all&user_text_search=&user=&owning_organisation_select=all&owning_organisation_text_search=&owning_organisation=&managing_organisation_select=all&managing_organisation_text_search=&managing_organisation=") expect(page).to have_current_path("/organisations/#{org_id}/sales-logs?%5Byears%5D[]=&years[]=#{previous_year}&%5Bstatus%5D[]=&assigned_to=all&user_text_search=&user=&owning_organisation_select=all&owning_organisation_text_search=&owning_organisation=&managing_organisation_select=all&managing_organisation_text_search=&managing_organisation=")
expect(page).not_to have_link first_log.id.to_s, href: "/sales-logs/#{first_log.id}" expect(page).not_to have_link first_log.id.to_s, href: "/sales-logs/#{first_log.id}"
end end
end end

3
spec/features/sales_log_spec.rb

@ -142,6 +142,7 @@ RSpec.describe "Sales Log Features" do
context "when downloading logs" do context "when downloading logs" do
let(:user) { create(:user, :support) } let(:user) { create(:user, :support) }
let(:other_user) { create(:user, organisation: user.organisation) } let(:other_user) { create(:user, organisation: user.organisation) }
let(:current_year) { FormHandler.instance.current_sales_form.start_date.year }
context "when I am signed in" do context "when I am signed in" do
before do before do
@ -191,7 +192,7 @@ RSpec.describe "Sales Log Features" do
context "when one year filter is selected" do context "when one year filter is selected" do
before do before do
check("2024 to 2025") check("#{current_year} to #{current_year + 1}")
click_button("Apply filters") click_button("Apply filters")
end end

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

26
spec/models/sales_log_derived_fields_spec.rb

@ -67,13 +67,13 @@ RSpec.describe SalesLog, type: :model do
end end
it "clears mortgage value if mortgage used is changed from no to don't know" do it "clears mortgage value if mortgage used is changed from no to don't know" do
log = create(:sales_log, :outright_sale_setup_complete, mortgage: 0, mortgageused: 2) log = create(:sales_log, :shared_ownership_setup_complete, stairowned: 100, mortgage: 0, mortgageused: 2)
log.mortgageused = 3 log.mortgageused = 3
expect { log.set_derived_fields! }.to change(log, :mortgage).from(0).to(nil) expect { log.set_derived_fields! }.to change(log, :mortgage).from(0).to(nil)
end end
it "clears mortgage value if mortgage used is changed from yes to don't know" do it "clears mortgage value if mortgage used is changed from yes to don't know" do
log = create(:sales_log, :outright_sale_setup_complete, mortgage: 50_000, mortgageused: 1) log = create(:sales_log, :shared_ownership_setup_complete, staircase: 2, mortgage: 50_000, mortgageused: 1)
log.mortgageused = 3 log.mortgageused = 3
expect { log.set_derived_fields! }.to change(log, :mortgage).from(50_000).to(nil) expect { log.set_derived_fields! }.to change(log, :mortgage).from(50_000).to(nil)
end end
@ -101,30 +101,36 @@ RSpec.describe SalesLog, type: :model do
expect { log.set_derived_fields! }.to change(log, :deposit).from(0).to(nil) expect { log.set_derived_fields! }.to change(log, :deposit).from(0).to(nil)
end end
context "with outright sales log" do
before do
allow(Time).to receive(:now).and_return(Time.zone.local(2024, 5, 4))
end
it "clears derived deposit when setting mortgage used to yes" do it "clears derived deposit when setting mortgage used to yes" do
log = create(:sales_log, :outright_sale_setup_complete, value: 123_400, deposit: 123_400, mortgageused: 2) log = create(:sales_log, :outright_sale_setup_complete, value: 123_400, deposit: 123_400, mortgageused: 2)
log.mortgageused = 1 log.mortgageused = 1
expect { log.set_derived_fields! }.to change(log, :deposit).from(123_400).to(nil) expect { log.set_derived_fields! }.to change(log, :deposit).from(123_400).to(nil)
end end
it "clears that buyer 1 will live in the property if joint purchase is updated" do
log = create(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2)
log.jointpur = 1
expect { log.set_derived_fields! }.to change(log, :buy1livein).from(1).to(nil)
end
end
context "when buyers will live in the property" do context "when buyers will live in the property" do
context "and the sale is not a joint purchase" do context "and the sale is not a joint purchase" do
it "derives that buyer 1 will live in the property" do it "derives that buyer 1 will live in the property" do
log = build(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2) log = build(:sales_log, :shared_ownership_setup_complete, staircase: 2, buylivein: 1, jointpur: 2)
expect { log.set_derived_fields! }.to change(log, :buy1livein).from(nil).to(1) expect { log.set_derived_fields! }.to change(log, :buy1livein).from(nil).to(1)
end end
it "does not derive a value for whether buyer 2 will live in the property" do it "does not derive a value for whether buyer 2 will live in the property" do
log = build(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2) log = build(:sales_log, :shared_ownership_setup_complete, staircase: 2, buylivein: 1, jointpur: 2)
log.set_derived_fields! log.set_derived_fields!
expect(log.buy2livein).to be_nil expect(log.buy2livein).to be_nil
end end
it "clears that buyer 1 will live in the property if joint purchase is updated" do
log = create(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2)
log.jointpur = 1
expect { log.set_derived_fields! }.to change(log, :buy1livein).from(1).to(nil)
end
end end
context "and the sale is a joint purchase" do context "and the sale is a joint purchase" do

11
spec/models/sales_log_spec.rb

@ -281,9 +281,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