Browse Source

further refactoring of various tests to reduce database interactions and improve speed

pull/2389/head
Arthur Campbell 2 years ago
parent
commit
7a0e75f19c
  1. 256
      spec/models/lettings_log_derived_fields_spec.rb

256
spec/models/lettings_log_derived_fields_spec.rb

@ -2,7 +2,7 @@ require "rails_helper"
require "shared/shared_examples_for_derived_fields" require "shared/shared_examples_for_derived_fields"
RSpec.describe LettingsLog, type: :model do RSpec.describe LettingsLog, type: :model do
let(:organisation) { build(:organisation, name: "derived fields org") } let(:organisation) { build(:organisation, name: "derived fields org", skip_rent_period_creation: true) }
let(:user) { build(:user, organisation:) } let(:user) { build(:user, organisation:) }
let(:log) { build(:lettings_log, :startdate_today, assigned_to: user) } let(:log) { build(:lettings_log, :startdate_today, assigned_to: user) }
@ -937,226 +937,206 @@ RSpec.describe LettingsLog, type: :model do
end end
describe "variables dependent on whether a letting is a renewal" do describe "variables dependent on whether a letting is a renewal" do
let(:lettings_log) { create(:lettings_log, :setup_completed) } let(:organisation) { create(:organisation, skip_rent_period_creation: true) }
let(:user) { create(:user, organisation:) }
let(:startdate) { Time.zone.today }
let(:persisted_renewal_lettings_log) { create(:lettings_log, :setup_completed, startdate:, renewal: 1, assigned_to: user) }
[ it "derives waityear offered referral first_time_property_let_as_social_housing rsnvac when renewal" do
{ log.renewal = 1
test_title: "correctly derives the length of time on local authority waiting list", expect { log.set_derived_fields! }
field: :waityear, .to change(log, :waityear).to(2)
value: 2, .and change(log, :offered).to(0)
}, .and change(log, :referral).to(1)
{ .and change(log, :first_time_property_let_as_social_housing).to(0)
test_title: "correctly derives the number of times previously offered since becoming available", .and change(log, :rsnvac).to(14)
field: :offered,
value: 0,
},
{
test_title: "correctly derives referral if the letting is a renewal and clears it if it is not",
field: :referral,
value: 1,
},
{
test_title: "correctly derives first_time_property_let_as_social_housing and clears it if it is not",
field: :first_time_property_let_as_social_housing,
value: 0,
},
{
test_title: "correctly derives vacancy reason and clears it if it is not",
field: :rsnvac,
value: 14,
},
].each do |test_case|
it test_case[:test_title] do
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, test_case[:field]).to test_case[:value]
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, test_case[:field]).from(test_case[:value]).to nil
end end
it "clears waityear offered referral first_time_property_let_as_social_housing rsnvac when not a renewal" do
expect { persisted_renewal_lettings_log.update!(renewal: 0) }
.to change(persisted_renewal_lettings_log, :waityear).from(2).to(nil)
.and change(persisted_renewal_lettings_log, :offered).from(0).to(nil)
.and change(persisted_renewal_lettings_log, :referral).from(1).to(nil)
.and change(persisted_renewal_lettings_log, :first_time_property_let_as_social_housing).from(0).to(nil)
.and change(persisted_renewal_lettings_log, :rsnvac).from(14).to(nil)
end end
describe "deriving voiddate from startdate" do
let(:startdate) { Time.zone.now }
it "correctly derives voiddate if the letting is a renewal and clears it if it is not" do it "correctly derives voiddate if the letting is a renewal and clears it if it is not" do
startdate = Time.zone.now log.assign_attributes(renewal: 1, startdate:)
lettings_log.update!(startdate:)
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :voiddate).to startdate expect { log.set_derived_fields! }.to change(log, :voiddate).to startdate
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :voiddate).from(startdate).to nil end
it "clears voiddate if the letting is no longer a renewal" do
expect { persisted_renewal_lettings_log.update!(renewal: 0) }.to change(persisted_renewal_lettings_log, :voiddate).from(startdate).to nil
end
end end
it "derives values for local authority and previous location if postcode is set and log is a renewal" do it "derives values for local authority and previous location if postcode is set and log is a renewal" do
postcode = "SW1A 1AA"
expected_la = "E09000033" expected_la = "E09000033"
expect { lettings_log.update!(renewal: 1, postcode_full: postcode, postcode_known: 1) } postcode = "SW1A 1AA"
.to change(lettings_log, :la).to(expected_la) log.assign_attributes(postcode_known: 1, postcode_full: postcode, renewal: 1)
.and change(lettings_log, :ppostcode_full).to(postcode)
.and change(lettings_log, :ppcodenk).to(0) expect { log.send :process_postcode_changes! }.to change(log, :la).to(expected_la)
.and change(lettings_log, :prevloc).to(expected_la) expect { log.set_derived_fields! }
.to change(log, :ppostcode_full).to(postcode)
.and change(log, :ppcodenk).to(0)
.and change(log, :prevloc).to(expected_la)
end end
context "when the log is general needs" do context "when the log is general needs" do
context "and the managing organisation is a private registered provider" do context "and the managing organisation is a private registered provider" do
before do before do
lettings_log.managing_organisation.update!(provider_type: "PRP") log.managing_organisation.provider_type = "PRP"
lettings_log.update!(needstype: 1, renewal: 1) log.renewal = 1
end end
it "correctly derives prevten" do it "correctly derives prevten" do
expect(lettings_log.prevten).to be 32 log.needstype = 1
log.set_derived_fields!
expect(log.prevten).to be 32
end end
it "clears prevten if the log is marked as supported housing" do it "clears prevten if the log is marked as supported housing" do
lettings_log.update!(needstype: 2) log.needstype = 2
expect(lettings_log.prevten).to be nil log.set_derived_fields!
expect(log.prevten).to be nil
end end
it "clears prevten if renewal is update to no" do it "clears prevten if renewal is update to no" do
lettings_log.update!(renewal: 0) log.renewal = 0
expect(lettings_log.prevten).to be nil log.set_derived_fields!
expect(log.prevten).to be nil
end end
end end
context "and the managing organisation is a local authority" do context "and the managing organisation is a local authority" do
before do before do
lettings_log.managing_organisation.update!(provider_type: "LA") log.managing_organisation.provider_type = "LA"
lettings_log.update!(needstype: 1, renewal: 1) log.renewal = 1
end end
it "correctly derives prevten" do it "correctly derives prevten if the log is general needs" do
expect(lettings_log.prevten).to be 30 log.needstype = 1
log.set_derived_fields!
expect(log.prevten).to be 30
end end
it "clears prevten if the log is marked as supported housing" do it "clears prevten if the log is marked as supported housing" do
expect { lettings_log.update!(needstype: 2) }.to change(lettings_log, :prevten).to nil log.needstype = 2
log.set_derived_fields!
expect(log.prevten).to be nil
end end
it "clears prevten if renewal is update to no" do it "clears prevten if renewal is update to no" do
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :prevten).to nil log.renewal = 0
log.set_derived_fields!
expect(log.prevten).to be nil
end end
end end
end end
context "and updating rent_type" do context "and updating rent_type" do
let(:irproduct_other) { nil } let(:irproduct_other) { nil }
let(:persisted_renewal_lettings_log) { create(:lettings_log, :setup_completed, assigned_to: user, rent_type:, irproduct_other:, renewal: 1) }
around do |example|
Timecop.freeze(now) do
Singleton.__init__(FormHandler)
lettings_log.update!(rent_type:, irproduct_other:, startdate: now)
example.run
end
end
context "when collection year is 2022/23 or earlier" do
let(:now) { Time.zone.local(2023, 1, 1) }
context "when rent_type is Social Rent" do context "when rent_type is Social Rent" do
let(:rent_type) { 0 } let(:rent_type) { 0 }
let(:expected_unitletas) { 1 }
it "derives the most recent let type as Social Rent basis if it is a renewal and clears it if it is not" do it "derives the most recent let type as Social Rent basis if it is a renewal" do
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 1 log.assign_attributes(renewal: 1, rent_type:)
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(1).to nil
end
end
context "when rent_type is Affordable Rent" do expect { log.set_derived_fields! }.to change(log, :unitletas).to expected_unitletas
let(:rent_type) { 1 }
it "derives the most recent let type as Affordable Rent basis if it is a renewal and clears it if it is not" do
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 2
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(2).to nil
end
end end
context "when rent_type is London Affordable Rent" do it "clears the most recent let type if it is not a renewal" do
let(:rent_type) { 2 } expect { persisted_renewal_lettings_log.update!(renewal: 0) }.to change(persisted_renewal_lettings_log, :unitletas).from(expected_unitletas).to nil
it "derives the most recent let type as Affordable Rent basis if it is a renewal and clears it if it is not" do
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 2
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(2).to nil
end end
end end
context "when rent_type is Rent to Buy" do context "when rent_type is Affordable Rent" do
let(:rent_type) { 3 } let(:rent_type) { 1 }
let(:expected_unitletas) { 2 }
it "derives the most recent let type as Intermediate Rent basis if it is a renewal and clears it if it is not" do
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 4
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(4).to nil
end
end
context "when rent_type is London Living Rent" do it "derives the most recent let type as Affordable Rent basis if it is a renewal" do
let(:rent_type) { 4 } log.assign_attributes(renewal: 1, rent_type:)
it "derives the most recent let type as Intermediate Rent basis if it is a renewal and clears it if it is not" do expect { log.set_derived_fields! }.to change(log, :unitletas).to expected_unitletas
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 4
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(4).to nil
end end
end
context "when rent_type is Other intermediate rent product" do
let(:rent_type) { 5 }
let(:irproduct_other) { "Rent first" }
it "derives the most recent let type as Intermediate Rent basis if it is a renewal and clears it if it is not" do it "clears the most recent let type if it is not a renewal" do
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 4 expect { persisted_renewal_lettings_log.update!(renewal: 0) }.to change(persisted_renewal_lettings_log, :unitletas).from(expected_unitletas).to nil
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(4).to nil
end end
end end
end
context "when collection year is 2023/24 or later" do
let(:now) { Time.zone.local(2024, 1, 1) }
context "when rent_type is Social Rent" do context "when rent_type is London Affordable Rent" do
let(:rent_type) { 0 } let(:rent_type) { 2 }
let(:expected_unitletas) { 5 }
it "derives the most recent let type as Social Rent basis if it is a renewal and clears it if it is not" do
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 1
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(1).to nil
end
end
context "when rent_type is Affordable Rent" do it "derives the most recent let type as London Affordable Rent basis if it is a renewal" do
let(:rent_type) { 1 } log.assign_attributes(renewal: 1, rent_type:)
it "derives the most recent let type as Affordable Rent basis if it is a renewal and clears it if it is not" do expect { log.set_derived_fields! }.to change(log, :unitletas).to expected_unitletas
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 2
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(2).to nil
end
end end
context "when rent_type is London Affordable Rent" do it "clears the most recent let type if it is not a renewal" do
let(:rent_type) { 2 } expect { persisted_renewal_lettings_log.update!(renewal: 0) }.to change(persisted_renewal_lettings_log, :unitletas).from(expected_unitletas).to nil
it "derives the most recent let type as London Affordable Rent basis if it is a renewal and clears it if it is not" do
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 5
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(5).to nil
end end
end end
context "when rent_type is Rent to Buy" do context "when rent_type is Rent to Buy" do
let(:rent_type) { 3 } let(:rent_type) { 3 }
let(:expected_unitletas) { 6 }
it "derives the most recent let type as Rent to Buy basis if it is a renewal" do
log.assign_attributes(renewal: 1, rent_type:)
expect { log.set_derived_fields! }.to change(log, :unitletas).to expected_unitletas
end
it "derives the most recent let type as Rent to Buy basis if it is a renewal and clears it if it is not" do it "clears the most recent let type if it is not a renewal" do
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 6 expect { persisted_renewal_lettings_log.update!(renewal: 0) }.to change(persisted_renewal_lettings_log, :unitletas).from(expected_unitletas).to nil
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(6).to nil
end end
end end
context "when rent_type is London Living Rent" do context "when rent_type is London Living Rent" do
let(:rent_type) { 4 } let(:rent_type) { 4 }
let(:expected_unitletas) { 7 }
it "derives the most recent let type as London Living Rent basis if it is a renewal and clears it if it is not" do it "derives the most recent let type as London Living Rent basis if it is a renewal" do
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 7 log.assign_attributes(renewal: 1, rent_type:)
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(7).to nil
expect { log.set_derived_fields! }.to change(log, :unitletas).to expected_unitletas
end
it "clears the most recent let type if it is not a renewal" do
expect { persisted_renewal_lettings_log.update!(renewal: 0) }.to change(persisted_renewal_lettings_log, :unitletas).from(expected_unitletas).to nil
end end
end end
context "when rent_type is Other intermediate rent product" do context "when rent_type is Other intermediate rent product" do
let(:rent_type) { 5 } let(:rent_type) { 5 }
let(:irproduct_other) { "Rent first" } let(:irproduct_other) { "Rent first" }
let(:expected_unitletas) { 8 }
it "derives the most recent let type as Another Intermediate Rent basis if it is a renewal and clears it if it is not" do it "derives the most recent let type as London Living Rent basis if it is a renewal" do
expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 8 log.assign_attributes(renewal: 1, rent_type:, irproduct_other:)
expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(8).to nil
expect { log.set_derived_fields! }.to change(log, :unitletas).to expected_unitletas
end end
it "clears the most recent let type if it is not a renewal" do
expect { persisted_renewal_lettings_log.update!(renewal: 0) }.to change(persisted_renewal_lettings_log, :unitletas).from(expected_unitletas).to nil
end end
end end
end end

Loading…
Cancel
Save