From 85475513093d5667c6bf4a82b52d8e13d83e3fc4 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 28 Mar 2022 09:32:09 +0100 Subject: [PATCH] Only add errors if charges exist --- .../validations/financial_validations.rb | 2 +- .../validations/financial_validations_spec.rb | 31 ++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 886f7357d..782de55c7 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -122,7 +122,7 @@ private %i[scharge pscharge supcharg].each do |charge| maximum = CHARGE_MAXIMUMS.dig(charge, LANDLORD_VALUES[record.landlord], NEEDSTYPE_VALUES[record.needstype]) - if maximum.present? && !weekly_value_in_range(record, charge, 0, maximum) + if maximum.present? && !weekly_value_in_range(record, charge, 0, maximum) && record[charge].present? record.errors.add charge, I18n.t("validations.financial.rent.#{charge}.#{LANDLORD_VALUES[record.landlord]}.#{NEEDSTYPE_VALUES[record.needstype]}") end end diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 7ea665fc7..371e82d33 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -244,7 +244,7 @@ RSpec.describe Validations::FinancialValidations do period: { label: "every 2 weeks", value: 2 }, charge: { field: "supcharg", value: 81 }, }].each do |test_case| - it "does not allow charges outide the range when period is #{test_case[:period][:label]}" do + it "does not allow charges outside the range when period is #{test_case[:period][:label]}" do record.period = test_case[:period][:value] record[test_case[:charge][:field]] = test_case[:charge][:value] financial_validator.validate_rent_amount(record) @@ -341,7 +341,7 @@ RSpec.describe Validations::FinancialValidations do period: { label: "every 2 weeks", value: 2 }, charge: { field: "supcharg", value: 990 }, }].each do |test_case| - it "does not allow charges outide the range when period is #{test_case[:period][:label]}" do + it "does not allow charges outside the range when period is #{test_case[:period][:label]}" do record.period = test_case[:period][:value] record[test_case[:charge][:field]] = test_case[:charge][:value] financial_validator.validate_rent_amount(record) @@ -440,7 +440,7 @@ RSpec.describe Validations::FinancialValidations do period: { label: "every 2 weeks", value: 2 }, charge: { field: "supcharg", value: 122 }, }].each do |test_case| - it "does not allow charges outide the range when period is #{test_case[:period][:label]}" do + it "does not allow charges outside the range when period is #{test_case[:period][:label]}" do record.period = test_case[:period][:value] record[test_case[:charge][:field]] = test_case[:charge][:value] financial_validator.validate_rent_amount(record) @@ -537,7 +537,7 @@ RSpec.describe Validations::FinancialValidations do period: { label: "every 2 weeks", value: 2 }, charge: { field: "supcharg", value: 241 }, }].each do |test_case| - it "does not allow charges outide the range when period is #{test_case[:period][:label]}" do + it "does not allow charges outside the range when period is #{test_case[:period][:label]}" do record.period = test_case[:period][:value] record[test_case[:charge][:field]] = test_case[:charge][:value] financial_validator.validate_rent_amount(record) @@ -546,6 +546,29 @@ RSpec.describe Validations::FinancialValidations do end end + context "when charges are not given" do + [{ + period: { label: "weekly", value: 1 }, + charge: { field: "scharge", value: nil }, + }, + { + period: { label: "weekly", value: 1 }, + charge: { field: "pscharge", value: nil }, + }, + { + period: { label: "weekly", value: 1 }, + charge: { field: "supcharg", value: nil }, + }].each do |test_case| + it "does not error" do + record.period = test_case[:period][:value] + record[test_case[:charge][:field]] = test_case[:charge][:value] + financial_validator.validate_rent_amount(record) + expect(record.errors[test_case[:charge][:field]]) + .to be_empty + end + end + end + [{ period: { label: "weekly", value: 1 }, charge: { field: "scharge", value: 120.88 },