From ea58c8ba954c58caaed9d5b0a803fd2345220f14 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 30 Jan 2023 11:54:26 +0000 Subject: [PATCH] CLDC-845 Update validation type from hard validation to soft validation (#1224) * Add old_persons_shared_ownership_value_check to sales table * Move hard validation method to soft validation * Add old persons shared ownership value check page and question * Add pages to the household characteristics * Add soft validation after ownership type * update a test * make old_persons_shared_ownership_value_check optional because it's in the setup section * typo --- ...ld_persons_shared_ownership_value_check.rb | 21 ++++ ...ld_persons_shared_ownership_value_check.rb | 23 ++++ .../subsections/household_characteristics.rb | 2 + app/models/form/sales/subsections/setup.rb | 1 + app/models/sales_log.rb | 12 +- .../sales/household_validations.rb | 18 --- config/locales/en.yml | 4 +- ...ld_persons_shared_ownership_value_check.rb | 7 ++ db/schema.rb | 1 + ...rsons_shared_ownership_value_check_spec.rb | 44 +++++++ ...rsons_shared_ownership_value_check_spec.rb | 57 +++++++++ .../household_characteristics_spec.rb | 2 + .../form/sales/subsections/setup_spec.rb | 1 + spec/models/form_handler_spec.rb | 4 +- spec/models/form_spec.rb | 4 +- spec/models/sales_log_spec.rb | 2 +- .../sales/household_validations_spec.rb | 115 ------------------ .../validations/soft_validations_spec.rb | 74 +++++++++++ 18 files changed, 251 insertions(+), 141 deletions(-) create mode 100644 app/models/form/sales/pages/old_persons_shared_ownership_value_check.rb create mode 100644 app/models/form/sales/questions/old_persons_shared_ownership_value_check.rb create mode 100644 db/migrate/20230124105247_add_old_persons_shared_ownership_value_check.rb create mode 100644 spec/models/form/sales/pages/old_persons_shared_ownership_value_check_spec.rb create mode 100644 spec/models/form/sales/questions/old_persons_shared_ownership_value_check_spec.rb diff --git a/app/models/form/sales/pages/old_persons_shared_ownership_value_check.rb b/app/models/form/sales/pages/old_persons_shared_ownership_value_check.rb new file mode 100644 index 000000000..9198af18a --- /dev/null +++ b/app/models/form/sales/pages/old_persons_shared_ownership_value_check.rb @@ -0,0 +1,21 @@ +class Form::Sales::Pages::OldPersonsSharedOwnershipValueCheck < ::Form::Page + def initialize(id, hsh, subsection) + super + @depends_on = [ + { + "buyers_age_for_old_persons_shared_ownership_invalid?" => true, + }, + ] + @title_text = { + "translation" => "soft_validations.old_persons_shared_ownership", + "arguments" => [], + } + @informative_text = {} + end + + def questions + @questions ||= [ + Form::Sales::Questions::OldPersonsSharedOwnershipValueCheck.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/sales/questions/old_persons_shared_ownership_value_check.rb b/app/models/form/sales/questions/old_persons_shared_ownership_value_check.rb new file mode 100644 index 000000000..c869dd3be --- /dev/null +++ b/app/models/form/sales/questions/old_persons_shared_ownership_value_check.rb @@ -0,0 +1,23 @@ +class Form::Sales::Questions::OldPersonsSharedOwnershipValueCheck < ::Form::Question + def initialize(id, hsh, page) + super + @id = "old_persons_shared_ownership_value_check" + @check_answer_label = "Shared ownership confirmation" + @header = "Are you sure this is correct?" + @type = "interruption_screen" + @answer_options = { + "0" => { "value" => "Yes" }, + "1" => { "value" => "No" }, + } + @hidden_in_check_answers = { + "depends_on" => [ + { + "old_persons_shared_ownership_value_check" => 0, + }, + { + "old_persons_shared_ownership_value_check" => 1, + }, + ], + } + end +end diff --git a/app/models/form/sales/subsections/household_characteristics.rb b/app/models/form/sales/subsections/household_characteristics.rb index b4c8badcf..bfccf0fa2 100644 --- a/app/models/form/sales/subsections/household_characteristics.rb +++ b/app/models/form/sales/subsections/household_characteristics.rb @@ -13,6 +13,7 @@ class Form::Sales::Subsections::HouseholdCharacteristics < ::Form::Subsection Form::Sales::Pages::Age1.new(nil, nil, self), Form::Sales::Pages::RetirementValueCheck.new("age_1_retirement_value_check", nil, self, person_index: 1), Form::Sales::Pages::RetirementValueCheck.new("age_1_retirement_value_check_joint_purchase", nil, self, person_index: 1), + Form::Sales::Pages::OldPersonsSharedOwnershipValueCheck.new("age_1_old_persons_shared_ownership_value_check", nil, self), Form::Sales::Pages::GenderIdentity1.new(nil, nil, self), Form::Sales::Pages::RetirementValueCheck.new("gender_1_retirement_value_check", nil, self, person_index: 1), Form::Sales::Pages::RetirementValueCheck.new("gender_1_retirement_value_check_joint_purchase", nil, self, person_index: 1), @@ -31,6 +32,7 @@ class Form::Sales::Subsections::HouseholdCharacteristics < ::Form::Subsection Form::Sales::Pages::Buyer2RelationshipToBuyer1.new(nil, nil, self), Form::Sales::Pages::Age2.new(nil, nil, self), Form::Sales::Pages::RetirementValueCheck.new("age_2_retirement_value_check_joint_purchase", nil, self, person_index: 2), + Form::Sales::Pages::OldPersonsSharedOwnershipValueCheck.new("age_2_old_persons_shared_ownership_value_check", nil, self), Form::Sales::Pages::GenderIdentity2.new(nil, nil, self), Form::Sales::Pages::RetirementValueCheck.new("gender_2_retirement_value_check_joint_purchase", nil, self, person_index: 2), Form::Sales::Pages::Buyer2WorkingSituation.new(nil, nil, self), diff --git a/app/models/form/sales/subsections/setup.rb b/app/models/form/sales/subsections/setup.rb index d2261a33b..cb147e9c2 100644 --- a/app/models/form/sales/subsections/setup.rb +++ b/app/models/form/sales/subsections/setup.rb @@ -15,6 +15,7 @@ class Form::Sales::Subsections::Setup < ::Form::Subsection Form::Sales::Pages::SharedOwnershipType.new(nil, nil, self), Form::Sales::Pages::DiscountedOwnershipType.new(nil, nil, self), Form::Sales::Pages::OutrightOwnershipType.new(nil, nil, self), + Form::Sales::Pages::OldPersonsSharedOwnershipValueCheck.new("ownership_type_old_persons_shared_ownership_value_check", nil, self), Form::Sales::Pages::BuyerCompany.new(nil, nil, self), Form::Sales::Pages::BuyerLive.new(nil, nil, self), Form::Sales::Pages::JointPurchase.new(nil, nil, self), diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index daea6f060..b78dd2321 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -34,7 +34,7 @@ class SalesLog < Log scope :search_by, ->(param) { filter_by_id(param) } scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) } - OPTIONAL_FIELDS = %w[purchid].freeze + OPTIONAL_FIELDS = %w[purchid old_persons_shared_ownership_value_check].freeze RETIREMENT_AGES = { "M" => 65, "F" => 60, "X" => 65 }.freeze def startdate @@ -221,4 +221,14 @@ class SalesLog < Log def shared_owhership_scheme? ownershipsch == 1 end + + def buyers_age_for_old_persons_shared_ownership_invalid? + return unless old_persons_shared_ownership? + + (joint_purchase? && ages_unknown_or_under_64?([1, 2])) || (not_joint_purchase? && ages_unknown_or_under_64?([1])) + end + + def ages_unknown_or_under_64?(person_indexes) + person_indexes.all? { |person_num| self["age#{person_num}"].present? && self["age#{person_num}"] < 64 || self["age#{person_num}_known"] == 1 } + end end diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index 8c03d9db9..4f2290268 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -18,20 +18,6 @@ module Validations::Sales::HouseholdValidations shared_validate_partner_count(record, 6) end - def validate_buyers_age_for_old_persons_shared_ownership(record) - if record.old_persons_shared_ownership? - if record.joint_purchase? && ages_unknown_or_under_64?(record, [1, 2]) - record.errors.add :age1, I18n.t("validations.household.old_persons_shared_ownership") - record.errors.add :age2, I18n.t("validations.household.old_persons_shared_ownership") - record.errors.add :type, I18n.t("validations.household.old_persons_shared_ownership") - end - if record.not_joint_purchase? && ages_unknown_or_under_64?(record, [1]) - record.errors.add :age1, I18n.t("validations.household.old_persons_shared_ownership") - record.errors.add :type, I18n.t("validations.household.old_persons_shared_ownership") - end - end - end - def validate_previous_postcode(record) return unless record.postcode_full && record.ppostcode_full && record.discounted_ownership_sale? @@ -116,8 +102,4 @@ private def tenant_is_economic_child?(economic_status) economic_status == 9 end - - def ages_unknown_or_under_64?(record, person_indexes) - person_indexes.all? { |person_num| record["age#{person_num}"].present? && record["age#{person_num}"] < 64 || record["age#{person_num}_known"] == 1 } - end end diff --git a/config/locales/en.yml b/config/locales/en.yml index f9c84253e..445dfc09f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -368,10 +368,9 @@ en: not_internal_transfer: "Answer cannot be ‘permanently decanted from another property owned by this landlord’ as you told us the source of referral for this tenancy was not an internal transfer" condition_effects: no_choices: "You cannot answer this question as you told us nobody in the household has a physical or mental health condition (or other illness) expected to last 12 months or more" - old_persons_shared_ownership: "Are you sure? At least one buyer should be aged over 64 for Older persons‘ shared ownership scheme" postcode: discounted_ownership: "Last settled accommodation and discounted ownership property postcodes must match" - + tenancy: length: fixed_term_not_required: "You must only answer the length of the tenancy if it's fixed-term" @@ -463,6 +462,7 @@ en: title_text: "You told us the time between the start of the tenancy and the void date is more than 2 years" shared_owhership_deposit: title_text: "Mortgage, deposit and cash discount total should equal £%{expected_shared_ownership_deposit_value}" + old_persons_shared_ownership: "At least one buyer should be aged over 64 for Older persons’ shared ownership scheme" devise: two_factor_authentication: diff --git a/db/migrate/20230124105247_add_old_persons_shared_ownership_value_check.rb b/db/migrate/20230124105247_add_old_persons_shared_ownership_value_check.rb new file mode 100644 index 000000000..ad5a0c70f --- /dev/null +++ b/db/migrate/20230124105247_add_old_persons_shared_ownership_value_check.rb @@ -0,0 +1,7 @@ +class AddOldPersonsSharedOwnershipValueCheck < ActiveRecord::Migration[7.0] + def change + change_table :sales_logs, bulk: true do |t| + t.column :old_persons_shared_ownership_value_check, :integer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 4b5e65a4e..6f21c94de 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -506,6 +506,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_26_145529) do t.integer "grant_value_check" t.integer "hodate_check" t.integer "extrabor_value_check" + t.integer "old_persons_shared_ownership_value_check" t.index ["bulk_upload_id"], name: "index_sales_logs_on_bulk_upload_id" t.integer "shared_ownership_deposit_value_check" t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id" diff --git a/spec/models/form/sales/pages/old_persons_shared_ownership_value_check_spec.rb b/spec/models/form/sales/pages/old_persons_shared_ownership_value_check_spec.rb new file mode 100644 index 000000000..05b6f7734 --- /dev/null +++ b/spec/models/form/sales/pages/old_persons_shared_ownership_value_check_spec.rb @@ -0,0 +1,44 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Pages::OldPersonsSharedOwnershipValueCheck, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { "old_persons_shared_ownership_value_check" } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + it "has correct subsection" do + expect(page.subsection).to eq(subsection) + end + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[old_persons_shared_ownership_value_check]) + end + + it "has the correct id" do + expect(page.id).to eq("old_persons_shared_ownership_value_check") + end + + it "has the correct header" do + expect(page.header).to be_nil + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([ + { + "buyers_age_for_old_persons_shared_ownership_invalid?" => true, + }, + ]) + end + + it "has the correct title_text" do + expect(page.title_text).to eq({ + "translation" => "soft_validations.old_persons_shared_ownership", + "arguments" => [], + }) + end + + it "has the correct informative_text" do + expect(page.informative_text).to eq({}) + end +end diff --git a/spec/models/form/sales/questions/old_persons_shared_ownership_value_check_spec.rb b/spec/models/form/sales/questions/old_persons_shared_ownership_value_check_spec.rb new file mode 100644 index 000000000..5ac066e31 --- /dev/null +++ b/spec/models/form/sales/questions/old_persons_shared_ownership_value_check_spec.rb @@ -0,0 +1,57 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::OldPersonsSharedOwnershipValueCheck, type: :model do + subject(:question) { described_class.new(question_id, question_definition, page) } + + let(:question_id) { nil } + let(:question_definition) { nil } + let(:page) { instance_double(Form::Page) } + + it "has correct page" do + expect(question.page).to eq(page) + end + + it "has the correct id" do + expect(question.id).to eq("old_persons_shared_ownership_value_check") + end + + it "has the correct header" do + expect(question.header).to eq("Are you sure this is correct?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Shared ownership confirmation") + end + + it "has the correct type" do + expect(question.type).to eq("interruption_screen") + end + + it "is not marked as derived" do + expect(question.derived?).to be false + end + + it "has the correct hint" do + expect(question.hint_text).to be_nil + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq({ + "0" => { "value" => "Yes" }, + "1" => { "value" => "No" }, + }) + end + + it "has the correct hidden_in_check_answers" do + expect(question.hidden_in_check_answers).to eq({ + "depends_on" => [ + { + "old_persons_shared_ownership_value_check" => 0, + }, + { + "old_persons_shared_ownership_value_check" => 1, + }, + ], + }) + end +end diff --git a/spec/models/form/sales/subsections/household_characteristics_spec.rb b/spec/models/form/sales/subsections/household_characteristics_spec.rb index 34b1a7536..f99ebe0aa 100644 --- a/spec/models/form/sales/subsections/household_characteristics_spec.rb +++ b/spec/models/form/sales/subsections/household_characteristics_spec.rb @@ -19,6 +19,7 @@ RSpec.describe Form::Sales::Subsections::HouseholdCharacteristics, type: :model buyer_1_age age_1_retirement_value_check age_1_retirement_value_check_joint_purchase + age_1_old_persons_shared_ownership_value_check buyer_1_gender_identity gender_1_retirement_value_check gender_1_retirement_value_check_joint_purchase @@ -37,6 +38,7 @@ RSpec.describe Form::Sales::Subsections::HouseholdCharacteristics, type: :model buyer_2_relationship_to_buyer_1 buyer_2_age age_2_retirement_value_check_joint_purchase + age_2_old_persons_shared_ownership_value_check buyer_2_gender_identity gender_2_retirement_value_check_joint_purchase buyer_2_working_situation diff --git a/spec/models/form/sales/subsections/setup_spec.rb b/spec/models/form/sales/subsections/setup_spec.rb index 36fbf247a..4bb95e391 100644 --- a/spec/models/form/sales/subsections/setup_spec.rb +++ b/spec/models/form/sales/subsections/setup_spec.rb @@ -22,6 +22,7 @@ RSpec.describe Form::Sales::Subsections::Setup, type: :model do shared_ownership_type discounted_ownership_type outright_ownership_type + ownership_type_old_persons_shared_ownership_value_check buyer_company buyer_live joint_purchase diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index e51d5ea59..f08e79ca4 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -52,14 +52,14 @@ RSpec.describe FormHandler do it "is able to load a current sales form" do form = form_handler.get_form("current_sales") expect(form).to be_a(Form) - expect(form.pages.count).to eq(191) + expect(form.pages.count).to eq(194) expect(form.name).to eq("2022_2023_sales") end it "is able to load a previous sales form" do form = form_handler.get_form("previous_sales") expect(form).to be_a(Form) - expect(form.pages.count).to eq(191) + expect(form.pages.count).to eq(194) expect(form.name).to eq("2021_2022_sales") end end diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index 110a521e2..fee42f1bc 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -218,9 +218,9 @@ RSpec.describe Form, type: :model do expect(form.sections[0].class).to eq(Form::Sales::Sections::Setup) expect(form.subsections.count).to eq(1) expect(form.subsections.first.id).to eq("setup") - expect(form.pages.count).to eq(12) + expect(form.pages.count).to eq(13) expect(form.pages.first.id).to eq("organisation") - expect(form.questions.count).to eq(13) + expect(form.questions.count).to eq(14) expect(form.questions.first.id).to eq("owning_organisation_id") expect(form.start_date).to eq(Time.zone.parse("2022-04-01")) expect(form.end_date).to eq(Time.zone.parse("2023-07-01")) diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index e49e56e01..3924feb65 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -47,7 +47,7 @@ RSpec.describe SalesLog, type: :model do let(:sales_log) { build(:sales_log) } it "returns optional fields" do - expect(sales_log.optional_fields).to eq(%w[purchid]) + expect(sales_log.optional_fields).to eq(%w[purchid old_persons_shared_ownership_value_check]) end end diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 5b227c36d..26d369a35 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -164,121 +164,6 @@ RSpec.describe Validations::Sales::HouseholdValidations do expect(record.errors["ecstat2"]) .to include(match I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19")) end - - context "when it is a joint purchase and both buyers are over 64" do - let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 65, age2: 66, type: 24) } - - it "does not add an error" do - household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) - - expect(record.errors).not_to be_present - end - end - - context "when it is a joint purchase and first buyer is over 64" do - let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 65, age2: 40, type: 24) } - - it "does not add an error" do - household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) - - expect(record.errors).not_to be_present - end - end - - context "when it is a joint purchase and second buyer is over 64" do - let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 43, age2: 64, type: 24) } - - it "does not add an error" do - household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) - - expect(record.errors).not_to be_present - end - end - - context "when it is a joint purchase and neither of the buyers are over 64" do - let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 43, age2: 33, type: 24) } - - it "adds an error" do - household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) - - expect(record.errors["age1"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - expect(record.errors["age2"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - expect(record.errors["type"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - end - end - - context "when it is a joint purchase and first buyer is under 64 and the second buyers' age is unknown" do - let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 43, age2_known: 1, type: 24) } - - it "adds an error" do - household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) - - expect(record.errors["age1"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - expect(record.errors["age2"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - expect(record.errors["type"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - end - end - - context "when it is a joint purchase and neither of the buyers ages are known" do - let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1_known: 1, age2_known: 1, type: 24) } - - it "adds an error" do - household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) - - expect(record.errors["age1"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - expect(record.errors["age2"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - expect(record.errors["type"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - end - end - - context "when it is not a joint purchase and the buyer is over 64" do - let(:record) { FactoryBot.build(:sales_log, jointpur: 2, age1: 70, type: 24) } - - it "does not add an error" do - household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) - - expect(record.errors).not_to be_present - end - end - - context "when it is not a joint purchase and the buyer is under 64" do - let(:record) { FactoryBot.build(:sales_log, jointpur: 2, age1: 20, type: 24) } - - it "adds an error" do - household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) - - expect(record.errors["age1"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - expect(record.errors["age2"]) - .to be_empty - expect(record.errors["type"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - end - end - - context "when it is not a joint purchase and the buyers age is not known" do - let(:record) { FactoryBot.build(:sales_log, jointpur: 2, age1_known: 1, type: 24) } - - it "adds an error" do - household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) - - expect(record.errors["age1"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - expect(record.errors["age2"]) - .to be_empty - expect(record.errors["type"]) - .to include(match I18n.t("validations.household.old_persons_shared_ownership")) - end - end end describe "previous postcode validations" do diff --git a/spec/models/validations/soft_validations_spec.rb b/spec/models/validations/soft_validations_spec.rb index 93ed01a20..9ca9436fb 100644 --- a/spec/models/validations/soft_validations_spec.rb +++ b/spec/models/validations/soft_validations_spec.rb @@ -237,4 +237,78 @@ RSpec.describe Validations::SoftValidations do end end end + + describe "old persons shared ownership soft validations" do + context "when it is a joint purchase and both buyers are over 64" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 65, age2: 66, type: 24) } + + it "returns false" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be false + end + end + + context "when it is a joint purchase and first buyer is over 64" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 65, age2: 40, type: 24) } + + it "returns false" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be false + end + end + + context "when it is a joint purchase and second buyer is over 64" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 43, age2: 64, type: 24) } + + it "returns false" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be false + end + end + + context "when it is a joint purchase and neither of the buyers are over 64" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 43, age2: 33, type: 24) } + + it "returns true" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be true + end + end + + context "when it is a joint purchase and first buyer is under 64 and the second buyers' age is unknown" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 43, age2_known: 1, type: 24) } + + it "returns true" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be true + end + end + + context "when it is a joint purchase and neither of the buyers ages are known" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1_known: 1, age2_known: 1, type: 24) } + + it "returns true" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be true + end + end + + context "when it is not a joint purchase and the buyer is over 64" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 2, age1: 70, type: 24) } + + it "returns false" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be false + end + end + + context "when it is not a joint purchase and the buyer is under 64" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 2, age1: 20, type: 24) } + + it "returns true" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be true + end + end + + context "when it is not a joint purchase and the buyers age is not known" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 2, age1_known: 1, type: 24) } + + it "returns true" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be true + end + end + end end