Browse Source

Update optional fields

CLDC-2068-request-and-validate-UPRN
Jack S 2 years ago
parent
commit
63ec77c93b
  1. 2
      app/models/form.rb
  2. 2
      app/models/lettings_log.rb
  3. 2
      app/models/sales_log.rb
  4. 165
      spec/models/lettings_log_spec.rb
  5. 38
      spec/models/sales_log_spec.rb

2
app/models/form.rb

@ -160,7 +160,7 @@ class Form
def invalidated_page_questions(log, current_user = nil) def invalidated_page_questions(log, current_user = nil)
# we're already treating these fields as a special case and reset their values upon saving a log # we're already treating these fields as a special case and reset their values upon saving a log
callback_questions = %w[postcode_known la ppcodenk previous_la_known prevloc postcode_full ppostcode_full location_id] callback_questions = %w[postcode_known la ppcodenk previous_la_known prevloc postcode_full ppostcode_full location_id address_line1 address_line2 town_or_city county]
questions.reject { |q| q.page.routed_to?(log, current_user) || q.derived? || callback_questions.include?(q.id) } || [] questions.reject { |q| q.page.routed_to?(log, current_user) || q.derived? || callback_questions.include?(q.id) } || []
end end

2
app/models/lettings_log.rb

@ -562,6 +562,8 @@ private
not_required << "tshortfall" if tshortfall_unknown? not_required << "tshortfall" if tshortfall_unknown?
not_required << "tenancylength" if tenancylength_optional? not_required << "tenancylength" if tenancylength_optional?
not_required |= %w[address_line2 county postcode_full] if startdate && startdate.year >= 2023
not_required not_required
end end

2
app/models/sales_log.rb

@ -78,6 +78,8 @@ class SalesLog < Log
not_required = [] not_required = []
not_required << "proplen" if proplen_optional? not_required << "proplen" if proplen_optional?
not_required |= %w[address_line2 county postcode_full] if saledate && saledate.year >= 2023
not_required not_required
end end

165
spec/models/lettings_log_spec.rb

@ -2,8 +2,8 @@ require "rails_helper"
require "shared/shared_examples_for_derived_fields" require "shared/shared_examples_for_derived_fields"
RSpec.describe LettingsLog do RSpec.describe LettingsLog do
let(:different_managing_organisation) { FactoryBot.create(:organisation) } let(:different_managing_organisation) { create(:organisation) }
let(:created_by_user) { FactoryBot.create(:user) } let(:created_by_user) { create(:user) }
let(:owning_organisation) { created_by_user.organisation } let(:owning_organisation) { created_by_user.organisation }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
@ -19,19 +19,19 @@ RSpec.describe LettingsLog do
end end
it "is a not a sales log" do it "is a not a sales log" do
lettings_log = FactoryBot.build(:lettings_log, created_by: created_by_user) lettings_log = build(:lettings_log, created_by: created_by_user)
expect(lettings_log.sales?).to be false expect(lettings_log.sales?).to be false
end end
it "is a lettings log" do it "is a lettings log" do
lettings_log = FactoryBot.build(:lettings_log, created_by: created_by_user) lettings_log = build(:lettings_log, created_by: created_by_user)
expect(lettings_log).to be_lettings expect(lettings_log).to be_lettings
end end
describe "#form" do describe "#form" do
let(:lettings_log) { FactoryBot.build(:lettings_log, created_by: created_by_user) } let(:lettings_log) { build(:lettings_log, created_by: created_by_user) }
let(:lettings_log_2) { FactoryBot.build(:lettings_log, startdate: Time.zone.local(2022, 1, 1), created_by: created_by_user) } let(:lettings_log_2) { build(:lettings_log, startdate: Time.zone.local(2022, 1, 1), created_by: created_by_user) }
let(:lettings_log_year_2) { FactoryBot.build(:lettings_log, startdate: Time.zone.local(2023, 5, 1), created_by: created_by_user) } let(:lettings_log_year_2) { build(:lettings_log, startdate: Time.zone.local(2023, 5, 1), created_by: created_by_user) }
it "has returns the correct form based on the start date" do it "has returns the correct form based on the start date" do
expect(lettings_log.form_name).to be_nil expect(lettings_log.form_name).to be_nil
@ -43,7 +43,7 @@ RSpec.describe LettingsLog do
end end
context "when a date outside the collection window is passed" do context "when a date outside the collection window is passed" do
let(:lettings_log) { FactoryBot.build(:lettings_log, startdate: Time.zone.local(2015, 1, 1), created_by: created_by_user) } let(:lettings_log) { build(:lettings_log, startdate: Time.zone.local(2015, 1, 1), created_by: created_by_user) }
it "returns the first form" do it "returns the first form" do
expect(lettings_log.form).to be_a(Form) expect(lettings_log.form).to be_a(Form)
@ -70,7 +70,7 @@ RSpec.describe LettingsLog do
end end
describe "#update" do describe "#update" do
let(:lettings_log) { FactoryBot.create(:lettings_log, created_by: created_by_user) } let(:lettings_log) { create(:lettings_log, created_by: created_by_user) }
let(:validator) { lettings_log._validators[nil].first } let(:validator) { lettings_log._validators[nil].first }
after do after do
@ -168,9 +168,9 @@ RSpec.describe LettingsLog do
end end
describe "status" do describe "status" do
let!(:empty_lettings_log) { FactoryBot.create(:lettings_log) } let!(:empty_lettings_log) { create(:lettings_log) }
let!(:in_progress_lettings_log) { FactoryBot.create(:lettings_log, :in_progress) } let!(:in_progress_lettings_log) { create(:lettings_log, :in_progress) }
let!(:completed_lettings_log) { FactoryBot.create(:lettings_log, :completed) } let!(:completed_lettings_log) { create(:lettings_log, :completed) }
it "is set to not started for an empty lettings log" do it "is set to not started for an empty lettings log" do
expect(empty_lettings_log.not_started?).to be(true) expect(empty_lettings_log.not_started?).to be(true)
@ -208,7 +208,7 @@ RSpec.describe LettingsLog do
describe "weekly_net_income" do describe "weekly_net_income" do
let(:net_income) { 5000 } let(:net_income) { 5000 }
let(:lettings_log) { FactoryBot.build(:lettings_log, earnings: net_income) } let(:lettings_log) { build(:lettings_log, earnings: net_income) }
it "returns input income if frequency is already weekly" do it "returns input income if frequency is already weekly" do
lettings_log.incfreq = 1 lettings_log.incfreq = 1
@ -1860,8 +1860,8 @@ RSpec.describe LettingsLog do
end end
context "and a scheme with a single log is selected" do context "and a scheme with a single log is selected" do
let(:scheme) { FactoryBot.create(:scheme) } let(:scheme) { create(:scheme) }
let!(:location) { FactoryBot.create(:location, scheme:) } let!(:location) { create(:location, scheme:) }
before { lettings_log.update!(startdate: Time.zone.local(2022, 4, 2), scheme:) } before { lettings_log.update!(startdate: Time.zone.local(2022, 4, 2), scheme:) }
@ -1873,8 +1873,8 @@ RSpec.describe LettingsLog do
end end
context "and not renewal" do context "and not renewal" do
let(:scheme) { FactoryBot.create(:scheme) } let(:scheme) { create(:scheme) }
let(:location) { FactoryBot.create(:location, scheme:, postcode: "M11AE", type_of_unit: 1, mobility_type: "W") } let(:location) { create(:location, scheme:, postcode: "M11AE", type_of_unit: 1, mobility_type: "W") }
let(:supported_housing_lettings_log) do let(:supported_housing_lettings_log) do
described_class.create!({ described_class.create!({
@ -1916,8 +1916,8 @@ RSpec.describe LettingsLog do
end end
context "and renewal" do context "and renewal" do
let(:scheme) { FactoryBot.create(:scheme) } let(:scheme) { create(:scheme) }
let(:location) { FactoryBot.create(:location, scheme:) } let(:location) { create(:location, scheme:) }
let!(:supported_housing_lettings_log) do let!(:supported_housing_lettings_log) do
described_class.create!({ described_class.create!({
@ -2035,7 +2035,7 @@ RSpec.describe LettingsLog do
end end
describe "optional fields" do describe "optional fields" do
let(:lettings_log) { FactoryBot.create(:lettings_log) } let(:lettings_log) { create(:lettings_log) }
context "when tshortfall is marked as not known" do context "when tshortfall is marked as not known" do
it "makes tshortfall optional" do it "makes tshortfall optional" do
@ -2043,13 +2043,42 @@ RSpec.describe LettingsLog do
expect(lettings_log.optional_fields).to include("tshortfall") expect(lettings_log.optional_fields).to include("tshortfall")
end end
end end
context "when saledate is before 2023" do
let(:lettings_log) { build(:lettings_log, startdate: Time.zone.parse("2022-07-01")) }
it "returns optional fields" do
expect(lettings_log.optional_fields).to eq(%w[
first_time_property_let_as_social_housing
tenancycode
propcode
tenancylength
])
end
end
context "when saledate is after 2023" do
let(:lettings_log) { build(:lettings_log, startdate: Time.zone.parse("2023-07-01")) }
it "returns optional fields" do
expect(lettings_log.optional_fields).to eq(%w[
first_time_property_let_as_social_housing
tenancycode
propcode
tenancylength
address_line2
county
postcode_full
])
end
end
end end
describe "resetting invalidated fields" do describe "resetting invalidated fields" do
let(:scheme) { FactoryBot.create(:scheme, owning_organisation: created_by_user.organisation) } let(:scheme) { create(:scheme, owning_organisation: created_by_user.organisation) }
let(:location) { FactoryBot.create(:location, location_code: "E07000223", scheme:) } let(:location) { create(:location, location_code: "E07000223", scheme:) }
let(:lettings_log) do let(:lettings_log) do
FactoryBot.create( create(
:lettings_log, :lettings_log,
renewal: 0, renewal: 0,
rsnvac: 5, rsnvac: 5,
@ -2084,14 +2113,14 @@ RSpec.describe LettingsLog do
end end
context "when a question that has already been answered, no longer has met dependencies" do context "when a question that has already been answered, no longer has met dependencies" do
let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, cbl: 1, preg_occ: 2, wchair: 2) } let(:lettings_log) { create(:lettings_log, :in_progress, cbl: 1, preg_occ: 2, wchair: 2) }
it "clears the answer" do it "clears the answer" do
expect { lettings_log.update!(preg_occ: nil) }.to change(lettings_log, :cbl).from(1).to(nil) expect { lettings_log.update!(preg_occ: nil) }.to change(lettings_log, :cbl).from(1).to(nil)
end end
context "when the question type does not have answer options" do context "when the question type does not have answer options" do
let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, housingneeds_a: 1, age1: 19) } let(:lettings_log) { create(:lettings_log, :in_progress, housingneeds_a: 1, age1: 19) }
it "clears the answer" do it "clears the answer" do
expect { lettings_log.update!(housingneeds_a: 0) }.to change(lettings_log, :age1).from(19).to(nil) expect { lettings_log.update!(housingneeds_a: 0) }.to change(lettings_log, :age1).from(19).to(nil)
@ -2099,7 +2128,7 @@ RSpec.describe LettingsLog do
end end
context "when the question type has answer options" do context "when the question type has answer options" do
let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, illness: 1, illness_type_1: 1) } let(:lettings_log) { create(:lettings_log, :in_progress, illness: 1, illness_type_1: 1) }
it "clears the answer" do it "clears the answer" do
expect { lettings_log.update!(illness: 2) }.to change(lettings_log, :illness_type_1).from(1).to(nil) expect { lettings_log.update!(illness: 2) }.to change(lettings_log, :illness_type_1).from(1).to(nil)
@ -2108,7 +2137,7 @@ RSpec.describe LettingsLog do
end end
context "with two pages having the same question key, only one's dependency is met" do context "with two pages having the same question key, only one's dependency is met" do
let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, cbl: 0, preg_occ: 2, wchair: 2) } let(:lettings_log) { create(:lettings_log, :in_progress, cbl: 0, preg_occ: 2, wchair: 2) }
it "does not clear the value for answers that apply to both pages" do it "does not clear the value for answers that apply to both pages" do
expect(lettings_log.cbl).to eq(0) expect(lettings_log.cbl).to eq(0)
@ -2123,7 +2152,7 @@ RSpec.describe LettingsLog do
end end
context "when a non select question associated with several pages is routed to" do context "when a non select question associated with several pages is routed to" do
let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, period: 2, needstype: 1, renewal: 0) } let(:lettings_log) { create(:lettings_log, :in_progress, period: 2, needstype: 1, renewal: 0) }
it "does not clear the answer value" do it "does not clear the answer value" do
lettings_log.update!({ unitletas: 1 }) lettings_log.update!({ unitletas: 1 })
@ -2133,7 +2162,7 @@ RSpec.describe LettingsLog do
end end
context "when the lettings log does not have a valid form set yet" do context "when the lettings log does not have a valid form set yet" do
let(:lettings_log) { FactoryBot.create(:lettings_log) } let(:lettings_log) { create(:lettings_log) }
it "does not throw an error" do it "does not throw an error" do
expect { lettings_log.update(startdate: Time.zone.local(2015, 1, 1)) }.not_to raise_error expect { lettings_log.update(startdate: Time.zone.local(2015, 1, 1)) }.not_to raise_error
@ -2141,7 +2170,7 @@ RSpec.describe LettingsLog do
end end
context "when it changes from a renewal to not a renewal" do context "when it changes from a renewal to not a renewal" do
let(:lettings_log) { FactoryBot.create(:lettings_log) } let(:lettings_log) { create(:lettings_log) }
it "resets inferred waityear value" do it "resets inferred waityear value" do
lettings_log.update!({ renewal: 1 }) lettings_log.update!({ renewal: 1 })
@ -2173,8 +2202,8 @@ RSpec.describe LettingsLog do
end end
context "when it changes from a supported housing to not a supported housing" do context "when it changes from a supported housing to not a supported housing" do
let(:location) { FactoryBot.create(:location, mobility_type: "A", postcode: "SW1P 4DG") } let(:location) { create(:location, mobility_type: "A", postcode: "SW1P 4DG") }
let(:lettings_log) { FactoryBot.create(:lettings_log, location:) } let(:lettings_log) { create(:lettings_log, location:) }
it "resets inferred wchair value" do it "resets inferred wchair value" do
lettings_log.update!({ needstype: 2 }) lettings_log.update!({ needstype: 2 })
@ -2203,7 +2232,7 @@ RSpec.describe LettingsLog do
end end
context "when it is not a renewal" do context "when it is not a renewal" do
let(:lettings_log) { FactoryBot.create(:lettings_log) } let(:lettings_log) { create(:lettings_log) }
it "saves waityear value" do it "saves waityear value" do
lettings_log.update!({ renewal: 0, waityear: 2 }) lettings_log.update!({ renewal: 0, waityear: 2 })
@ -2215,13 +2244,13 @@ RSpec.describe LettingsLog do
end end
context "when a support user changes the owning organisation of the log" do context "when a support user changes the owning organisation of the log" do
let(:lettings_log) { FactoryBot.create(:lettings_log, created_by: created_by_user) } let(:lettings_log) { create(:lettings_log, created_by: created_by_user) }
let(:organisation_2) { FactoryBot.create(:organisation) } let(:organisation_2) { create(:organisation) }
context "when the organisation selected doesn't match the scheme set" do context "when the organisation selected doesn't match the scheme set" do
let(:scheme) { FactoryBot.create(:scheme, owning_organisation: created_by_user.organisation) } let(:scheme) { create(:scheme, owning_organisation: created_by_user.organisation) }
let(:location) { FactoryBot.create(:location, scheme:) } let(:location) { create(:location, scheme:) }
let(:lettings_log) { FactoryBot.create(:lettings_log, owning_organisation: nil, needstype: 2, scheme_id: scheme.id) } let(:lettings_log) { create(:lettings_log, owning_organisation: nil, needstype: 2, scheme_id: scheme.id) }
it "clears the scheme value" do it "clears the scheme value" do
lettings_log.update!(owning_organisation: organisation_2) lettings_log.update!(owning_organisation: organisation_2)
@ -2230,9 +2259,9 @@ RSpec.describe LettingsLog do
end end
context "when the organisation selected still matches the scheme set" do context "when the organisation selected still matches the scheme set" do
let(:scheme) { FactoryBot.create(:scheme, owning_organisation: organisation_2) } let(:scheme) { create(:scheme, owning_organisation: organisation_2) }
let(:location) { FactoryBot.create(:location, scheme:) } let(:location) { create(:location, scheme:) }
let(:lettings_log) { FactoryBot.create(:lettings_log, owning_organisation: nil, needstype: 2, scheme_id: scheme.id) } let(:lettings_log) { create(:lettings_log, owning_organisation: nil, needstype: 2, scheme_id: scheme.id) }
it "does not clear the scheme value" do it "does not clear the scheme value" do
lettings_log.update!(owning_organisation: organisation_2) lettings_log.update!(owning_organisation: organisation_2)
@ -2319,7 +2348,7 @@ RSpec.describe LettingsLog do
describe "tshortfall_unknown?" do describe "tshortfall_unknown?" do
context "when tshortfall is nil" do context "when tshortfall is nil" do
let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, tshortfall_known: nil) } let(:lettings_log) { create(:lettings_log, :in_progress, tshortfall_known: nil) }
it "returns false" do it "returns false" do
expect(lettings_log.tshortfall_unknown?).to be false expect(lettings_log.tshortfall_unknown?).to be false
@ -2327,7 +2356,7 @@ RSpec.describe LettingsLog do
end end
context "when tshortfall is No" do context "when tshortfall is No" do
let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, tshortfall_known: 1) } let(:lettings_log) { create(:lettings_log, :in_progress, tshortfall_known: 1) }
it "returns false" do it "returns false" do
expect(lettings_log.tshortfall_unknown?).to be true expect(lettings_log.tshortfall_unknown?).to be true
@ -2335,7 +2364,7 @@ RSpec.describe LettingsLog do
end end
context "when tshortfall is Yes" do context "when tshortfall is Yes" do
let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, tshortfall_known: 0) } let(:lettings_log) { create(:lettings_log, :in_progress, tshortfall_known: 0) }
it "returns false" do it "returns false" do
expect(lettings_log.tshortfall_unknown?).to be false expect(lettings_log.tshortfall_unknown?).to be false
@ -2344,7 +2373,7 @@ RSpec.describe LettingsLog do
end end
describe "paper trail" do describe "paper trail" do
let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress) } let(:lettings_log) { create(:lettings_log, :in_progress) }
it "creates a record of changes to a log" do it "creates a record of changes to a log" do
expect { lettings_log.update!(age1: 64) }.to change(lettings_log.versions, :count).by(1) expect { lettings_log.update!(age1: 64) }.to change(lettings_log.versions, :count).by(1)
@ -2357,7 +2386,7 @@ RSpec.describe LettingsLog do
end end
describe "soft values for period" do describe "soft values for period" do
let(:lettings_log) { FactoryBot.create(:lettings_log) } let(:lettings_log) { create(:lettings_log) }
before do before do
LaRentRange.create!( LaRentRange.create!(
@ -2404,12 +2433,12 @@ RSpec.describe LettingsLog do
end end
describe "scopes" do describe "scopes" do
let!(:lettings_log_1) { FactoryBot.create(:lettings_log, :in_progress, startdate: Time.utc(2021, 5, 3), created_by: created_by_user) } let!(:lettings_log_1) { create(:lettings_log, :in_progress, startdate: Time.utc(2021, 5, 3), created_by: created_by_user) }
let!(:lettings_log_2) { FactoryBot.create(:lettings_log, :completed, startdate: Time.utc(2021, 5, 3), created_by: created_by_user) } let!(:lettings_log_2) { create(:lettings_log, :completed, startdate: Time.utc(2021, 5, 3), created_by: created_by_user) }
before do before do
Timecop.freeze(Time.utc(2022, 6, 3)) Timecop.freeze(Time.utc(2022, 6, 3))
FactoryBot.create(:lettings_log, startdate: Time.utc(2022, 6, 3)) create(:lettings_log, startdate: Time.utc(2022, 6, 3))
end end
after do after do
@ -2417,10 +2446,10 @@ RSpec.describe LettingsLog do
end end
context "when searching logs" do context "when searching logs" do
let!(:lettings_log_to_search) { FactoryBot.create(:lettings_log, :completed) } let!(:lettings_log_to_search) { create(:lettings_log, :completed) }
before do before do
FactoryBot.create_list(:lettings_log, 5, :completed) create_list(:lettings_log, 5, :completed)
end end
describe "#filter_by_id" do describe "#filter_by_id" do
@ -2475,7 +2504,7 @@ RSpec.describe LettingsLog do
end end
context "when lettings log is supported housing" do context "when lettings log is supported housing" do
let(:location) { FactoryBot.create(:location, postcode: "W6 0ST") } let(:location) { create(:location, postcode: "W6 0ST") }
before do before do
lettings_log_to_search.update!(needstype: 2, location:) lettings_log_to_search.update!(needstype: 2, location:)
@ -2515,7 +2544,7 @@ RSpec.describe LettingsLog do
end end
context "when lettings log is supported housing" do context "when lettings log is supported housing" do
let(:location) { FactoryBot.create(:location, postcode: "W6 0ST") } let(:location) { create(:location, postcode: "W6 0ST") }
before do before do
lettings_log_to_search.update!(needstype: 2, location:) lettings_log_to_search.update!(needstype: 2, location:)
@ -2573,15 +2602,15 @@ RSpec.describe LettingsLog do
end end
context "when filtering by organisation" do context "when filtering by organisation" do
let(:organisation_1) { FactoryBot.create(:organisation) } let(:organisation_1) { create(:organisation) }
let(:organisation_2) { FactoryBot.create(:organisation) } let(:organisation_2) { create(:organisation) }
let(:organisation_3) { FactoryBot.create(:organisation) } let(:organisation_3) { create(:organisation) }
before do before do
FactoryBot.create(:lettings_log, :in_progress, owning_organisation: organisation_1, managing_organisation: organisation_1, created_by: nil) create(:lettings_log, :in_progress, owning_organisation: organisation_1, managing_organisation: organisation_1, created_by: nil)
FactoryBot.create(:lettings_log, :completed, owning_organisation: organisation_1, managing_organisation: organisation_2, created_by: nil) create(:lettings_log, :completed, owning_organisation: organisation_1, managing_organisation: organisation_2, created_by: nil)
FactoryBot.create(:lettings_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_1, created_by: nil) create(:lettings_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_1, created_by: nil)
FactoryBot.create(:lettings_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_2, created_by: nil) create(:lettings_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_2, created_by: nil)
end end
it "filters by given organisation" do it "filters by given organisation" do
@ -2623,7 +2652,7 @@ RSpec.describe LettingsLog do
describe "#retirement_age_for_person" do describe "#retirement_age_for_person" do
context "when a person gender is Male" do context "when a person gender is Male" do
let(:lettings_log) { FactoryBot.build(:lettings_log, sex1: "M") } let(:lettings_log) { build(:lettings_log, sex1: "M") }
it "returns the expected retirement age" do it "returns the expected retirement age" do
expect(lettings_log.retirement_age_for_person_1).to eq(67) expect(lettings_log.retirement_age_for_person_1).to eq(67)
@ -2635,7 +2664,7 @@ RSpec.describe LettingsLog do
end end
context "when a person gender is Female" do context "when a person gender is Female" do
let(:lettings_log) { FactoryBot.build(:lettings_log, sex2: "F") } let(:lettings_log) { build(:lettings_log, sex2: "F") }
it "returns the expected retirement age" do it "returns the expected retirement age" do
expect(lettings_log.retirement_age_for_person_2).to eq(60) expect(lettings_log.retirement_age_for_person_2).to eq(60)
@ -2647,7 +2676,7 @@ RSpec.describe LettingsLog do
end end
context "when a person gender is Non-Binary" do context "when a person gender is Non-Binary" do
let(:lettings_log) { FactoryBot.build(:lettings_log, sex3: "X") } let(:lettings_log) { build(:lettings_log, sex3: "X") }
it "returns the expected retirement age" do it "returns the expected retirement age" do
expect(lettings_log.retirement_age_for_person_3).to eq(67) expect(lettings_log.retirement_age_for_person_3).to eq(67)
@ -2659,7 +2688,7 @@ RSpec.describe LettingsLog do
end end
context "when the person gender is not set" do context "when the person gender is not set" do
let(:lettings_log) { FactoryBot.build(:lettings_log) } let(:lettings_log) { build(:lettings_log) }
it "returns nil" do it "returns nil" do
expect(lettings_log.retirement_age_for_person_3).to be_nil expect(lettings_log.retirement_age_for_person_3).to be_nil
@ -2671,7 +2700,7 @@ RSpec.describe LettingsLog do
end end
context "when a postcode contains unicode characters" do context "when a postcode contains unicode characters" do
let(:lettings_log) { FactoryBot.build(:lettings_log, postcode_full: "SR81LS\u00A0") } let(:lettings_log) { build(:lettings_log, postcode_full: "SR81LS\u00A0") }
it "triggers a validation error" do it "triggers a validation error" do
expect { lettings_log.save! }.to raise_error(ActiveRecord::RecordInvalid, /Enter a postcode in the correct format/) expect { lettings_log.save! }.to raise_error(ActiveRecord::RecordInvalid, /Enter a postcode in the correct format/)
@ -2680,9 +2709,9 @@ RSpec.describe LettingsLog do
end end
describe "csv download" do describe "csv download" do
let(:scheme) { FactoryBot.create(:scheme) } let(:scheme) { create(:scheme) }
let(:location) { FactoryBot.create(:location, :export, scheme:, type_of_unit: 6, postcode: "SE11TE", startdate: Time.zone.local(2021, 10, 1)) } let(:location) { create(:location, :export, scheme:, type_of_unit: 6, postcode: "SE11TE", startdate: Time.zone.local(2021, 10, 1)) }
let(:user) { FactoryBot.create(:user, organisation: location.scheme.owning_organisation) } let(:user) { create(:user, organisation: location.scheme.owning_organisation) }
let(:expected_content) { csv_export_file.read } let(:expected_content) { csv_export_file.read }
after do after do

38
spec/models/sales_log_spec.rb

@ -36,7 +36,7 @@ RSpec.describe SalesLog, type: :model do
end end
describe "#update" do describe "#update" do
let(:sales_log) { FactoryBot.create(:sales_log, created_by: created_by_user) } let(:sales_log) { create(:sales_log, created_by: created_by_user) }
let(:validator) { sales_log._validators[nil].first } let(:validator) { sales_log._validators[nil].first }
after do after do
@ -49,10 +49,34 @@ RSpec.describe SalesLog, type: :model do
end end
describe "#optional_fields" do describe "#optional_fields" do
let(:sales_log) { build(:sales_log) } context "when saledate is before 2023" do
let(:sales_log) { build(:sales_log, saledate: Time.zone.parse("2022-07-01")) }
it "returns optional fields" do it "returns optional fields" do
expect(sales_log.optional_fields).to eq(%w[saledate_check purchid monthly_charges_value_check old_persons_shared_ownership_value_check]) expect(sales_log.optional_fields).to eq(%w[
saledate_check
purchid
monthly_charges_value_check
old_persons_shared_ownership_value_check
proplen
])
end
end
context "when saledate is after 2023" do
let(:sales_log) { build(:sales_log, saledate: Time.zone.parse("2023-07-01")) }
it "returns optional fields" do
expect(sales_log.optional_fields).to eq(%w[
saledate_check
purchid
monthly_charges_value_check
old_persons_shared_ownership_value_check
address_line2
county
postcode_full
])
end
end end
end end
@ -135,7 +159,7 @@ RSpec.describe SalesLog, type: :model do
end end
describe "derived variables" do describe "derived variables" do
let(:sales_log) { FactoryBot.create(:sales_log, :completed) } let(:sales_log) { create(:sales_log, :completed) }
it "correctly derives and saves exday, exmonth and exyear" do it "correctly derives and saves exday, exmonth and exyear" do
sales_log.update!(exdate: Time.gm(2022, 5, 4), saledate: Time.gm(2022, 7, 4), ownershipsch: 1, staircase: 2, resale: 2) sales_log.update!(exdate: Time.gm(2022, 5, 4), saledate: Time.gm(2022, 7, 4), ownershipsch: 1, staircase: 2, resale: 2)
@ -192,7 +216,7 @@ RSpec.describe SalesLog, type: :model do
end end
let!(:address_sales_log) do let!(:address_sales_log) do
FactoryBot.create( create(
:sales_log, :sales_log,
:completed, :completed,
owning_organisation:, owning_organisation:,
@ -336,7 +360,7 @@ RSpec.describe SalesLog, type: :model do
context "when deriving household variables" do context "when deriving household variables" do
let!(:sales_log) do let!(:sales_log) do
FactoryBot.create( create(
:sales_log, :sales_log,
:completed, :completed,
jointpur: 1, jointpur: 1,
@ -463,7 +487,7 @@ RSpec.describe SalesLog, type: :model do
end end
describe "#field_formatted_as_currency" do describe "#field_formatted_as_currency" do
let(:completed_sales_log) { FactoryBot.create(:sales_log, :completed) } let(:completed_sales_log) { create(:sales_log, :completed) }
it "returns small numbers correctly formatted as currency" do it "returns small numbers correctly formatted as currency" do
completed_sales_log.update!(savings: 4) completed_sales_log.update!(savings: 4)

Loading…
Cancel
Save