Browse Source

Fix tests

CLDC-4146-infer-no-other-partners
oscar-richardson-softwire 3 days ago
parent
commit
8bd27a2b87
  1. 2
      app/models/form.rb
  2. 2
      app/models/form/lettings/pages/person_lead_partner.rb
  3. 6
      spec/fixtures/files/lettings_log_csv_export_codes_26.csv
  4. 6
      spec/fixtures/files/lettings_log_csv_export_labels_26.csv
  5. 127
      spec/models/form/lettings/pages/person_lead_partner_spec.rb
  6. 124
      spec/models/form/lettings/questions/person_partner_spec.rb
  7. 15
      spec/models/form_spec.rb
  8. 46
      spec/models/lettings_log_derived_fields_spec.rb
  9. 18
      spec/models/lettings_log_spec.rb

2
app/models/form.rb

@ -325,7 +325,7 @@ class Form
operator = value["operator"]
operand = value["operand"]
if operator == "!=" # This branch is needed as `nil` does not behave as expected with the default logic (`nil&.send("!=", operand)` => `nil` i.e., `false`).
if operator == "!=" # This branch is needed as `nil` does not behave as expected with the default logic (`nil&.send("!=", nil)` => `nil` i.e., `false`).
log[question] != operand
else
log[question]&.send(operator, operand)

2
app/models/form/lettings/pages/person_lead_partner.rb

@ -30,6 +30,6 @@ class Form::Lettings::Pages::PersonLeadPartner < ::Form::Page
end
def skip_page_in_form_flow?(log)
log.is_another_person_partner?(@person_index)
form.start_year_2026_or_later? && log.is_another_person_partner?(@person_index)
end
end

6
spec/fixtures/files/lettings_log_csv_export_codes_26.csv vendored

File diff suppressed because one or more lines are too long

6
spec/fixtures/files/lettings_log_csv_export_labels_26.csv vendored

File diff suppressed because one or more lines are too long

127
spec/models/form/lettings/pages/person_lead_partner_spec.rb

@ -9,6 +9,8 @@ RSpec.describe Form::Lettings::Pages::PersonLeadPartner, type: :model do
let(:form) { instance_double(Form, start_date: Time.zone.local(2024, 4, 1), start_year_2026_or_later?: start_year_2026_or_later?, person_question_count:) }
let(:subsection) { instance_double(Form::Subsection, form:) }
let(:person_index) { 2 }
let(:is_another_person_partner?) { false }
let(:log) { instance_double(LettingsLog, is_another_person_partner?: is_another_person_partner?) }
it "has correct subsection" do
expect(page.subsection).to eq(subsection)
@ -18,151 +20,116 @@ RSpec.describe Form::Lettings::Pages::PersonLeadPartner, type: :model do
expect(page.description).to be_nil
end
context "with person 2" do
it "has the correct id" do
expect(page.id).to eq("person_2_lead_partner")
end
describe "#skip_page_in_form_flow?" do
context "with start year < 2026", metadata: { year: 25 } do
let(:person_question_count) { 4 }
context "when no other person is the partner of the lead tenant" do
let(:is_another_person_partner?) { false }
it "has correct questions" do
expect(page.questions.map(&:id)).to eq(%w[relat2])
it "returns false" do
expect(page.skip_page_in_form_flow?(log)).to be false
end
end
it "has correct depends_on" do
expect(page.depends_on).to eq(
[{ "details_known_2" => 0 }],
)
context "when another person is the partner of the lead tenant" do
let(:is_another_person_partner?) { true }
it "returns false" do
expect(page.skip_page_in_form_flow?(log)).to be false
end
end
end
context "with start year >= 2026", metadata: { year: 26 } do
let(:start_year_2026_or_later?) { true }
let(:person_question_count) { 5 }
it "has correct questions" do
expect(page.questions.map(&:id)).to eq(%w[relat2])
context "when no other person is the partner of the lead tenant" do
let(:is_another_person_partner?) { false }
it "returns false" do
expect(page.skip_page_in_form_flow?(log)).to be false
end
end
it "has correct depends_on" do
expect(page.depends_on).to eq(
[
{
"details_known_2" => 0,
"age2" => {
"operator" => ">=",
"operand" => 16,
},
},
{ "details_known_2" => 0, "age2" => nil },
],
)
context "when another person is the partner of the lead tenant" do
let(:is_another_person_partner?) { true }
it "returns true" do
expect(page.skip_page_in_form_flow?(log)).to be true
end
end
end
end
context "with person 3" do
let(:person_index) { 3 }
context "with person 2" do
it "has correct questions" do
expect(page.questions.map(&:id)).to eq(%w[relat2])
end
it "has the correct id" do
expect(page.id).to eq("person_3_lead_partner")
expect(page.id).to eq("person_2_lead_partner")
end
context "with start year < 2026", metadata: { year: 25 } do
let(:person_question_count) { 4 }
it "has correct questions" do
expect(page.questions.map(&:id)).to eq(%w[relat3])
end
it "has correct depends_on" do
expect(page.depends_on).to eq(
[{ "details_known_3" => 0 }],
[{ "details_known_2" => 0 }],
)
end
end
context "with start year >= 2026", metadata: { year: 26 } do
let(:start_year_2026_or_later?) { true }
let(:person_question_count) { 5 }
it "has correct questions" do
expect(page.questions.map(&:id)).to eq(%w[relat3])
end
it "has correct depends_on" do
expect(page.depends_on).to eq(
[
{
"details_known_3" => 0,
"age3" => {
"details_known_2" => 0,
"age2" => {
"operator" => ">=",
"operand" => 16,
},
"relat2" => { "operator" => "!=", "operand" => "P" },
},
{
"details_known_3" => 0,
"age3" => nil,
"relat2" => { "operator" => "!=", "operand" => "P" },
},
{ "details_known_2" => 0, "age2" => nil },
],
)
end
end
end
context "with person 4" do
let(:person_index) { 4 }
context "with person 3" do
let(:person_index) { 3 }
it "has correct questions" do
expect(page.questions.map(&:id)).to eq(%w[relat3])
end
it "has the correct id" do
expect(page.id).to eq("person_4_lead_partner")
expect(page.id).to eq("person_3_lead_partner")
end
context "with start year < 2026", metadata: { year: 25 } do
before do
allow(form).to receive(:start_year_2026_or_later?).and_return(false)
end
it "has correct questions" do
expect(page.questions.map(&:id)).to eq(%w[relat4])
end
it "has correct depends_on" do
expect(page.depends_on).to eq(
[{ "details_known_4" => 0 }],
[{ "details_known_3" => 0 }],
)
end
end
context "with start year >= 2026", metadata: { year: 26 } do
before do
allow(form).to receive(:start_year_2026_or_later?).and_return(true)
end
it "has correct questions" do
expect(page.questions.map(&:id)).to eq(%w[relat4])
end
let(:start_year_2026_or_later?) { true }
it "has correct depends_on" do
expect(page.depends_on).to eq(
[
{
"details_known_4" => 0,
"age4" => {
"details_known_3" => 0,
"age3" => {
"operator" => ">=",
"operand" => 16,
},
"relat2" => { "operator" => "!=", "operand" => "P" },
"relat3" => { "operator" => "!=", "operand" => "P" },
},
{
"details_known_4" => 0,
"age4" => nil,
"relat2" => { "operator" => "!=", "operand" => "P" },
"relat3" => { "operator" => "!=", "operand" => "P" },
},
{ "details_known_3" => 0, "age3" => nil },
],
)
end

124
spec/models/form/lettings/questions/person_partner_spec.rb

@ -123,129 +123,5 @@ RSpec.describe Form::Lettings::Questions::PersonPartner, type: :model do
it "has the correct check_answers_card_number" do
expect(question.check_answers_card_number).to eq(3)
end
context "when the log has a preceding value of `relat` as 'P'" do
let(:log) { build(:lettings_log, relat2: "P") }
context "and in 2025", metadata: { year: 25 } do
let(:year) { 2025 }
it "has the correct hidden_in_check_answers?" do
expect(question.hidden_in_check_answers?(log)).to eq(false)
end
it "is not marked as derived" do
expect(question.derived?(log)).to be false
end
end
context "and in 2026", metadata: { year: 26 } do
let(:year) { 2026 }
it "has the correct hidden_in_check_answers?" do
expect(question.hidden_in_check_answers?(log)).to eq(true)
end
it "is marked as derived" do
expect(question.derived?(log)).to be true
end
end
end
context "when the log does not have a preceding value of `relat` as 'P'" do
let(:log) { build(:lettings_log, relat2: "X") }
context "and in 2025", metadata: { year: 25 } do
let(:year) { 2025 }
it "has the correct hidden_in_check_answers?" do
expect(question.hidden_in_check_answers?(log)).to eq(false)
end
it "is not marked as derived" do
expect(question.derived?(log)).to be false
end
end
context "and in 2026", metadata: { year: 26 } do
let(:year) { 2026 }
it "has the correct hidden_in_check_answers?" do
expect(question.hidden_in_check_answers?(log)).to eq(false)
end
it "is not marked as derived" do
expect(question.derived?(log)).to be false
end
end
end
end
context "with person 4" do
let(:person_index) { 4 }
it "has the correct id" do
expect(question.id).to eq("relat4")
end
it "has the correct check_answers_card_number" do
expect(question.check_answers_card_number).to eq(4)
end
context "when the log has a preceding value of `relat` as 'P'" do
let(:log) { build(:lettings_log, relat2: "P", relat3: "X") }
context "and in 2025", metadata: { year: 25 } do
let(:year) { 2025 }
it "has the correct hidden_in_check_answers?" do
expect(question.hidden_in_check_answers?(log)).to eq(false)
end
it "is not marked as derived" do
expect(question.derived?(log)).to be false
end
end
context "and in 2026", metadata: { year: 26 } do
let(:year) { 2026 }
it "has the correct hidden_in_check_answers?" do
expect(question.hidden_in_check_answers?(log)).to eq(true)
end
it "is marked as derived" do
expect(question.derived?(log)).to be true
end
end
end
context "when the log does not have a preceding value of `relat` as 'P'" do
let(:log) { build(:lettings_log, relat2: "R", relat3: "X") }
context "and in 2025", metadata: { year: 25 } do
let(:year) { 2025 }
it "has the correct hidden_in_check_answers?" do
expect(question.hidden_in_check_answers?(log)).to eq(false)
end
it "is not marked as derived" do
expect(question.derived?(log)).to be false
end
end
context "and in 2026", metadata: { year: 26 } do
let(:year) { 2026 }
it "has the correct hidden_in_check_answers?" do
expect(question.hidden_in_check_answers?(log)).to eq(false)
end
it "is not marked as derived" do
expect(question.derived?(log)).to be false
end
end
end
end
end

15
spec/models/form_spec.rb

@ -23,6 +23,21 @@ RSpec.describe Form, type: :model do
expect(form.next_page_id(previous_page_id, lettings_log, user)).to eq("person_1_gender")
end
context "when the next page's `skip_page_in_form_flow?` returns `true`" do
let(:next_page) { form.get_page("person_1_gender") }
before do
allow(next_page).to receive(:skip_page_in_form_flow?)
.with(lettings_log)
.and_return(true)
end
it "returns the page after next given the previous" do
puts next_page.skip_page_in_form_flow?(lettings_log)
expect(form.next_page_id(previous_page_id, lettings_log, user)).to eq("person_1_working_situation")
end
end
context "when the current page is a value check page" do
before do
lettings_log.hhmemb = 1

46
spec/models/lettings_log_derived_fields_spec.rb

@ -1754,6 +1754,33 @@ RSpec.describe LettingsLog, type: :model do
end
end
context "and a new relationship is added to an later person than the existing one" do
before do
log.relat6 = nil # This is necessary because `log.set_derived_fields!` runs when the log is created from the factory, which sets `relat6` to "X".
log.relat5 = "P"
end
it "infers no to the existing relationship" do
expect { log.set_derived_fields! }.to change(log, :relat3).to "X"
end
it "infers no to unanswered questions" do
expect { log.set_derived_fields! }.to change(log, :relat6).to "X"
end
it "does not change relationship answers for people not in the household" do
expect { log.set_derived_fields! }
.to not_change(log, :relat7)
.and not_change(log, :relat8)
end
it "does not change relationship answers of no or prefer not to say" do
expect { log.set_derived_fields! }
.to not_change(log, :relat2)
.and not_change(log, :relat4)
end
end
it "does not change other relationship values if no is changed to prefer not to say" do
log.relat2 = "R"
expect { log.set_derived_fields! }
@ -1781,18 +1808,15 @@ RSpec.describe LettingsLog, type: :model do
log.relat3 = "X"
end
it "does not reset answers of no that come before the removed relationship to nil" do
expect { log.set_derived_fields! }.to not_change(log, :relat2)
end
it "resets answers of no that come after the removed relationship to nil, regardless of whether they were inferred or not" do
it "does not reset any answers" do
expect { log.set_derived_fields! }
.to change(log, :relat5).to(nil) # `relat5` was set to "X" explicitly.
.and change(log, :relat6).to(nil) # `relat6` was inferred as "X" when we created the log from the factory.
end
it "does not reset answers of prefer not to say" do
expect { log.set_derived_fields! }.to not_change(log, :relat4)
.to not_change(log, :relat2)
.and not_change(log, :relat3)
.and not_change(log, :relat4)
.and not_change(log, :relat5)
.and not_change(log, :relat6)
.and not_change(log, :relat7)
.and not_change(log, :relat8)
end
end
end

18
spec/models/lettings_log_spec.rb

@ -2349,5 +2349,23 @@ RSpec.describe LettingsLog do
end
end
end
describe "#is_another_person_partner?" do
context "when no other person is the partner of the lead tenant" do
let(:log) { create(:lettings_log, :completed, hhmemb: 4, relat2: "X", relat3: "R") }
it "returns false" do
expect(log.is_another_person_partner?(4)).to be false
end
end
context "when another person is the partner of the lead tenant" do
let(:log) { create(:lettings_log, :completed, hhmemb: 4, relat2: "X", relat3: "P") }
it "returns true" do
expect(log.is_another_person_partner?(4)).to be true
end
end
end
end
# rubocop:enable RSpec/MessageChain

Loading…
Cancel
Save