Browse Source

Net income hard validations

pull/304/head
baarkerlounger 3 years ago
parent
commit
97ec6c11a1
  1. 4
      app/models/validations/financial_validations.rb
  2. 4
      config/locales/en.yml
  3. 24
      spec/models/case_log_spec.rb
  4. 32
      spec/models/validations/financial_validations_spec.rb

4
app/models/validations/financial_validations.rb

@ -23,11 +23,11 @@ module Validations::FinancialValidations
def validate_net_income(record) def validate_net_income(record)
if record.ecstat1 && record.weekly_net_income if record.ecstat1 && record.weekly_net_income
if record.weekly_net_income > record.applicable_income_range.hard_max 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 end
if record.weekly_net_income < record.applicable_income_range.hard_min 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
end end

4
config/locales/en.yml

@ -74,8 +74,8 @@ en:
benefits: 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" 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: earnings:
under_hard_max: "Net income cannot be greater than %{hard_max} given the tenant’s working situation" over_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" 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" freq_missing: "Select how often the household receives income"
earnings_missing: "Enter how much income the household has in total" earnings_missing: "Enter how much income the household has in total"

24
spec/models/case_log_spec.rb

@ -125,30 +125,6 @@ RSpec.describe CaseLog do
end end
context "when saving income ranges" do 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 context "with an income in upper soft range" do
let(:case_log) do let(:case_log) do
FactoryBot.create(:case_log, FactoryBot.create(:case_log,

32
spec/models/validations/financial_validations_spec.rb

@ -120,4 +120,36 @@ RSpec.describe Validations::FinancialValidations do
end end
end 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 end

Loading…
Cancel
Save