From b6760c4af6d7984f18afd14d684eee99e6df543e Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 1 Feb 2024 15:38:41 +0000 Subject: [PATCH] Deduplicate validations --- .../sales/financial_validations.rb | 13 - .../sales/sale_information_validations.rb | 12 +- config/locales/en.yml | 3 +- .../sales/financial_validations_spec.rb | 240 ------------------ .../sale_information_validations_spec.rb | 222 ++++++++++------ 5 files changed, 146 insertions(+), 344 deletions(-) diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index bbc99f361..410f6d250 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -92,19 +92,6 @@ module Validations::Sales::FinancialValidations end end - def validate_discounted_ownership_value(record) - return unless record.saledate && record.form.start_year_after_2024? - return unless record.value && record.deposit && record.ownershipsch - return unless record.mortgage || record.mortgageused == 2 || record.mortgageused == 3 - return unless record.discount || record.grant || record.type == 29 - - if record.mortgage_deposit_and_grant_total != record.value_with_discount && record.discounted_ownership_sale? - %i[mortgageused mortgage value deposit ownershipsch discount grant].each do |field| - record.errors.add field, I18n.t("validations.financial.discounted_sale_value", mortgage_deposit_and_grant_total: record.field_formatted_as_currency("mortgage_deposit_and_grant_total"), value_with_discount: record.field_formatted_as_currency("value_with_discount")) - end - end - end - private def is_relationship_child?(relationship) diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 3f4d06a30..5b036580d 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -35,20 +35,14 @@ module Validations::Sales::SaleInformationValidations end def validate_discounted_ownership_value(record) - return unless record.saledate && collection_start_year_for_date(record.saledate) >= 2024 + return unless record.saledate && record.form.start_year_after_2024? return unless record.value && record.deposit && record.ownershipsch return unless record.mortgage || record.mortgageused == 2 || record.mortgageused == 3 return unless record.discount || record.grant || record.type == 29 if record.mortgage_deposit_and_grant_total != record.value_with_discount && record.discounted_ownership_sale? - %i[mortgage deposit grant value discount ownershipsch].each do |field| - record.errors.add( - field, - I18n.t( - "validations.sale_information.discounted_ownership_value", - value_with_discount: format_as_currency(record.value_with_discount), - ), - ) + %i[mortgageused mortgage value deposit ownershipsch discount grant].each do |field| + record.errors.add field, I18n.t("validations.sale_information.discounted_ownership_value", mortgage_deposit_and_grant_total: record.field_formatted_as_currency("mortgage_deposit_and_grant_total"), value_with_discount: record.field_formatted_as_currency("value_with_discount")) end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index f5cb94bca..ca910b75e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -433,7 +433,6 @@ en: under_min: "The minimum initial equity stake for this type of shared ownership sale is %{min_equity}%" over_max: "The maximum initial equity stake is %{max_equity}%" mortgage: "Mortgage value cannot be £0 if a mortgage was used for the purchase of this property" - discounted_sale_value: "The mortgage, deposit, and grant when added together is %{mortgage_deposit_and_grant_total}, and the purchase purchase price times by the discount is %{value_with_discount}. These figures should be the same" household: reasonable_preference_reason: @@ -609,7 +608,7 @@ en: must_be_after_hodate: "Sale completion date must be after practical completion or handover date" previous_property_type: property_type_bedsit: "A bedsit cannot have more than 1 bedroom" - discounted_ownership_value: "Mortgage, deposit, and grant total must equal %{value_with_discount}" + discounted_ownership_value: "The mortgage, deposit, and grant when added together is %{mortgage_deposit_and_grant_total}, and the purchase purchase price times by the discount is %{value_with_discount}. These figures should be the same" monthly_rent: higher_than_expected: "Basic monthly rent must be between £0.00 and £9,999.00" merge_request: diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index 6f58e4917..68cf19a14 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -339,244 +339,4 @@ RSpec.describe Validations::Sales::FinancialValidations do end end end - - describe "#validate_discounted_ownership_value" do - let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 8, saledate: now) } - - around do |example| - Timecop.freeze(now) do - example.run - end - Timecop.return - end - - context "with a log in the 24/25 collection year" do - let(:now) { Time.zone.local(2024, 4, 1) } - - context "when grant is routed to" do - context "and not provided" do - before do - record.grant = nil - end - - it "returns false" do - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to be_empty - expect(record.errors["mortgage"]).to be_empty - expect(record.errors["value"]).to be_empty - expect(record.errors["deposit"]).to be_empty - expect(record.errors["ownershipsch"]).to be_empty - expect(record.errors["discount"]).to be_empty - expect(record.errors["grant"]).to be_empty - end - end - - context "and is provided" do - it "returns true if mortgage, deposit and grant total does not equal market value" do - record.grant = 3_000 - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - end - - it "returns false if mortgage, deposit and grant total equals market value" do - record.grant = 15_000 - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to be_empty - expect(record.errors["mortgage"]).to be_empty - expect(record.errors["value"]).to be_empty - expect(record.errors["deposit"]).to be_empty - expect(record.errors["ownershipsch"]).to be_empty - expect(record.errors["discount"]).to be_empty - expect(record.errors["grant"]).to be_empty - end - end - end - - context "when discount is routed to" do - let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 9, saledate: now) } - - context "and not provided" do - before do - record.discount = nil - end - - it "returns false" do - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to be_empty - expect(record.errors["mortgage"]).to be_empty - expect(record.errors["value"]).to be_empty - expect(record.errors["deposit"]).to be_empty - expect(record.errors["ownershipsch"]).to be_empty - expect(record.errors["discount"]).to be_empty - expect(record.errors["grant"]).to be_empty - end - end - - context "and is provided" do - it "returns true if mortgage and deposit total does not equal market value - discount" do - record.discount = 10 - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") - expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") - expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") - expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") - expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") - expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") - expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") - end - - it "returns false if mortgage and deposit total equals market value - discount" do - record.discount = 50 - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to be_empty - expect(record.errors["mortgage"]).to be_empty - expect(record.errors["value"]).to be_empty - expect(record.errors["deposit"]).to be_empty - expect(record.errors["ownershipsch"]).to be_empty - expect(record.errors["discount"]).to be_empty - expect(record.errors["grant"]).to be_empty - end - end - end - - context "when neither discount nor grant is routed to" do - let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, value: 30_000, ownershipsch: 2, type: 29, saledate: now) } - - it "returns true if mortgage and deposit total does not equal market value" do - record.deposit = 2_000 - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") - end - - it "returns false if mortgage and deposit total equals market value" do - record.deposit = 20_000 - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to be_empty - expect(record.errors["mortgage"]).to be_empty - expect(record.errors["value"]).to be_empty - expect(record.errors["deposit"]).to be_empty - expect(record.errors["ownershipsch"]).to be_empty - expect(record.errors["discount"]).to be_empty - expect(record.errors["grant"]).to be_empty - end - end - - context "when mortgage is routed to" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: now) } - - context "and not provided" do - before do - record.mortgage = nil - end - - it "returns false" do - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to be_empty - expect(record.errors["mortgage"]).to be_empty - expect(record.errors["value"]).to be_empty - expect(record.errors["deposit"]).to be_empty - expect(record.errors["ownershipsch"]).to be_empty - expect(record.errors["discount"]).to be_empty - expect(record.errors["grant"]).to be_empty - end - end - - context "and is provided" do - it "returns true if mortgage, grant and deposit total does not equal market value - discount" do - record.mortgage = 10 - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - end - - it "returns false if mortgage, grant and deposit total equals market value - discount" do - record.mortgage = 10_000 - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to be_empty - expect(record.errors["mortgage"]).to be_empty - expect(record.errors["value"]).to be_empty - expect(record.errors["deposit"]).to be_empty - expect(record.errors["ownershipsch"]).to be_empty - expect(record.errors["discount"]).to be_empty - expect(record.errors["grant"]).to be_empty - end - end - end - - context "when mortgage is not routed to" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: now) } - - it "returns true if grant and deposit total does not equal market value - discount" do - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") - end - - it "returns false if mortgage, grant and deposit total equals market value - discount" do - record.grant = 13_000 - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to be_empty - expect(record.errors["mortgage"]).to be_empty - expect(record.errors["value"]).to be_empty - expect(record.errors["deposit"]).to be_empty - expect(record.errors["ownershipsch"]).to be_empty - expect(record.errors["discount"]).to be_empty - expect(record.errors["grant"]).to be_empty - end - end - - context "when ownership is not discounted" do - let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 1, saledate: now) } - - it "returns false" do - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to be_empty - expect(record.errors["mortgage"]).to be_empty - expect(record.errors["value"]).to be_empty - expect(record.errors["deposit"]).to be_empty - expect(record.errors["ownershipsch"]).to be_empty - expect(record.errors["discount"]).to be_empty - expect(record.errors["grant"]).to be_empty - end - end - - context "when it is a 2023 log" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2023, 4, 1)) } - - it "returns false" do - record.mortgage = 10 - financial_validator.validate_discounted_ownership_value(record) - expect(record.errors["mortgageused"]).to be_empty - expect(record.errors["mortgage"]).to be_empty - expect(record.errors["value"]).to be_empty - expect(record.errors["deposit"]).to be_empty - expect(record.errors["ownershipsch"]).to be_empty - expect(record.errors["discount"]).to be_empty - expect(record.errors["grant"]).to be_empty - end - end - end - end end diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index dbd4c1e49..392686ac7 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -172,179 +172,241 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end describe "#validate_discounted_ownership_value" do - context "when sale is on or after 24/25 collection window" do - context "when grant is routed to" do - let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 8, saledate: Time.zone.local(2024, 4, 3)) } + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 8, saledate: now) } + + around do |example| + Timecop.freeze(now) do + example.run + end + Timecop.return + end + + context "with a log in the 24/25 collection year" do + let(:now) { Time.zone.local(2024, 4, 1) } + context "when grant is routed to" do context "and not provided" do before do record.grant = nil end - it "does not add an error" do + it "returns false" do sale_information_validator.validate_discounted_ownership_value(record) - - expect(record.errors).to be_empty + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty end end context "and is provided" do - it "adds an error if mortgage, deposit and grant total does not equal market value" do + it "returns true if mortgage, deposit and grant total does not equal market value" do record.grant = 3_000 sale_information_validator.validate_discounted_ownership_value(record) - expected_message = ["Mortgage, deposit, and grant total must equal £30,000.00"] - - expect(record.errors[:mortgage]).to eq(expected_message) - expect(record.errors[:deposit]).to eq(expected_message) - expect(record.errors[:grant]).to eq(expected_message) - expect(record.errors[:value]).to eq(expected_message) - expect(record.errors[:discount]).to eq(expected_message) + expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") + expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") + expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") + expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") + expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") + expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") + expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") end - it "does not add an error if mortgage, deposit and grant total equals market value" do + it "returns false if mortgage, deposit and grant total equals market value" do record.grant = 15_000 sale_information_validator.validate_discounted_ownership_value(record) - expect(record.errors).to be_empty + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty end end end context "when discount is routed to" do - let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 9, saledate: Time.zone.local(2024, 4, 3)) } + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 9, saledate: now) } context "and not provided" do before do record.discount = nil end - it "does not add an error" do + it "returns false" do sale_information_validator.validate_discounted_ownership_value(record) - - expect(record.errors).to be_empty + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty end end context "and is provided" do - it "adds an error if mortgage and deposit total does not equal market value - discount" do + it "returns true if mortgage and deposit total does not equal market value - discount" do record.discount = 10 sale_information_validator.validate_discounted_ownership_value(record) - - expected_message = ["Mortgage, deposit, and grant total must equal £27,000.00"] - - expect(record.errors[:mortgage]).to eq(expected_message) - expect(record.errors[:deposit]).to eq(expected_message) - expect(record.errors[:grant]).to eq(expected_message) - expect(record.errors[:value]).to eq(expected_message) - expect(record.errors[:discount]).to eq(expected_message) + expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") + expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") + expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") + expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") + expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") + expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") + expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same") end - it "does not add an error if mortgage and deposit total equals market value - discount" do + it "returns false if mortgage and deposit total equals market value - discount" do record.discount = 50 sale_information_validator.validate_discounted_ownership_value(record) - expect(record.errors).to be_empty + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty end end end context "when neither discount nor grant is routed to" do - let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, value: 30_000, ownershipsch: 2, type: 29, saledate: Time.zone.local(2024, 4, 3)) } + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, value: 30_000, ownershipsch: 2, type: 29, saledate: now) } - it "adds an error if mortgage and deposit total does not equal market value" do + it "returns true if mortgage and deposit total does not equal market value" do record.deposit = 2_000 sale_information_validator.validate_discounted_ownership_value(record) - - expected_message = ["Mortgage, deposit, and grant total must equal £30,000.00"] - - expect(record.errors[:mortgage]).to eq(expected_message) - expect(record.errors[:deposit]).to eq(expected_message) - expect(record.errors[:grant]).to eq(expected_message) - expect(record.errors[:value]).to eq(expected_message) - expect(record.errors[:discount]).to eq(expected_message) + expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") + expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") + expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") + expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") + expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") + expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") + expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") end - it "does not add an error if mortgage and deposit total equals market value" do + it "returns false if mortgage and deposit total equals market value" do record.deposit = 20_000 sale_information_validator.validate_discounted_ownership_value(record) - expect(record.errors).to be_empty + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty end end context "when mortgage is routed to" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2024, 4, 3)) } + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: now) } context "and not provided" do before do record.mortgage = nil end - it "does not add an error" do + it "returns false" do sale_information_validator.validate_discounted_ownership_value(record) - - expect(record.errors).to be_empty + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty end end context "and is provided" do - it "adds an error if mortgage, grant and deposit total does not equal market value - discount" do + it "returns true if mortgage, grant and deposit total does not equal market value - discount" do record.mortgage = 10 sale_information_validator.validate_discounted_ownership_value(record) - - expected_message = ["Mortgage, deposit, and grant total must equal £18,000.00"] - - expect(record.errors[:mortgage]).to eq(expected_message) - expect(record.errors[:deposit]).to eq(expected_message) - expect(record.errors[:grant]).to eq(expected_message) - expect(record.errors[:value]).to eq(expected_message) - expect(record.errors[:discount]).to eq(expected_message) + expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") + expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") + expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") + expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") + expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") + expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") + expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") end - it "does not add an error if mortgage, grant and deposit total equals market value - discount" do + it "returns false if mortgage, grant and deposit total equals market value - discount" do record.mortgage = 10_000 sale_information_validator.validate_discounted_ownership_value(record) - expect(record.errors).to be_empty + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty end end end context "when mortgage is not routed to" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2024, 4, 3)) } + let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: now) } - it "adds an error if grant and deposit total does not equal market value - discount" do + it "returns true if grant and deposit total does not equal market value - discount" do sale_information_validator.validate_discounted_ownership_value(record) - - expected_message = ["Mortgage, deposit, and grant total must equal £18,000.00"] - expect(record.errors[:mortgage]).to eq(expected_message) - expect(record.errors[:deposit]).to eq(expected_message) - expect(record.errors[:grant]).to eq(expected_message) - expect(record.errors[:value]).to eq(expected_message) - expect(record.errors[:discount]).to eq(expected_message) + expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") + expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") + expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") + expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") + expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") + expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") + expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same") end - it "does not add an error if mortgage, grant and deposit total equals market value - discount" do + it "returns false if mortgage, grant and deposit total equals market value - discount" do record.grant = 13_000 sale_information_validator.validate_discounted_ownership_value(record) - expect(record.errors).to be_empty + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty end end context "when ownership is not discounted" do - let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 1, saledate: Time.zone.local(2024, 4, 3)) } + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 1, saledate: now) } - it "does not add an error" do + it "returns false" do sale_information_validator.validate_discounted_ownership_value(record) - - expect(record.errors).to be_empty + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty end end - end - - context "when sale is before 24/25 collection" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2023, 4, 4)) } - it "does not add an error" do - sale_information_validator.validate_discounted_ownership_value(record) + context "when it is a 2023 log" do + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2023, 4, 1)) } - expect(record.errors).to be_empty + it "returns false" do + record.mortgage = 10 + sale_information_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty + end end end end