From 97ec6c11a162a046e9de5ba530cab6e2f0d8e7b3 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Mon, 14 Feb 2022 17:52:55 +0000 Subject: [PATCH] Net income hard validations --- .../validations/financial_validations.rb | 4 +-- config/locales/en.yml | 4 +-- spec/models/case_log_spec.rb | 24 -------------- .../validations/financial_validations_spec.rb | 32 +++++++++++++++++++ 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index a06328c25..67ac5a38f 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -23,11 +23,11 @@ module Validations::FinancialValidations def validate_net_income(record) if record.ecstat1 && record.weekly_net_income if record.weekly_net_income > record.applicable_income_range.hard_max - record.errors.add :earnings, I18n.t("validations.financial.earnings.under_hard_max", hard_max: record.applicable_income_range.hard_max) + record.errors.add :earnings, I18n.t("validations.financial.earnings.over_hard_max", hard_max: record.applicable_income_range.hard_max) end if record.weekly_net_income < record.applicable_income_range.hard_min - record.errors.add :earnings, I18n.t("validations.financial.earnings.over_hard_min", hard_min: record.applicable_income_range.hard_min) + record.errors.add :earnings, I18n.t("validations.financial.earnings.under_hard_min", hard_min: record.applicable_income_range.hard_min) end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 05d7963b5..a0aad8e60 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -74,8 +74,8 @@ en: benefits: part_or_full_time: "income is from Universal Credit, state pensions or benefits cannot be All if the tenant or the partner works part or full time" earnings: - under_hard_max: "Net income cannot be greater than %{hard_max} given the tenant’s working situation" - over_hard_min: "Net income cannot be less than %{hard_min} given the tenant’s working situation" + over_hard_max: "Net income cannot be greater than %{hard_max} given the tenant’s working situation" + under_hard_min: "Net income cannot be less than %{hard_min} given the tenant’s working situation" freq_missing: "Select how often the household receives income" earnings_missing: "Enter how much income the household has in total" diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 5a4a23b01..8cea4c1c6 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -125,30 +125,6 @@ RSpec.describe CaseLog do end context "when saving income ranges" do - it "validates net income maximum" do - expect { - described_class.create!( - ecstat1: "Full-time - 30 hours or more", - earnings: 5000, - incfreq: "Weekly", - owning_organisation:, - managing_organisation:, - ) - }.to raise_error(ActiveRecord::RecordInvalid) - end - - it "validates net income minimum" do - expect { - described_class.create!( - ecstat1: "Full-time - 30 hours or more", - earnings: 1, - incfreq: "Weekly", - owning_organisation:, - managing_organisation:, - ) - }.to raise_error(ActiveRecord::RecordInvalid) - end - context "with an income in upper soft range" do let(:case_log) do FactoryBot.create(:case_log, diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index e4ce17b9b..a5be733de 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -120,4 +120,36 @@ RSpec.describe Validations::FinancialValidations do end end end + + describe "Net income validations" do + it "validates that the net income is within the expected range for the tenant's employment status" do + record.earnings = 200 + record.incfreq = "Weekly" + record.ecstat1 = "Full-time - 30 hours or more" + financial_validator.validate_net_income(record) + expect(record.errors["earnings"]).to be_empty + end + + context "when the net income is higher than the hard max for their employment status" do + it "adds an error" do + record.earnings = 5000 + record.incfreq = "Weekly" + record.ecstat1 = "Full-time - 30 hours or more" + financial_validator.validate_net_income(record) + expect(record.errors["earnings"]) + .to include(match I18n.t("validations.financial.earnings.over_hard_max", hard_max: 1230)) + end + end + + context "when the net income is lower than the hard min for their employment status" do + it "adds an error" do + record.earnings = 50 + record.incfreq = "Weekly" + record.ecstat1 = "Full-time - 30 hours or more" + financial_validator.validate_net_income(record) + expect(record.errors["earnings"]) + .to include(match I18n.t("validations.financial.earnings.under_hard_min", hard_min: 90)) + end + end + end end