From 80776e8e33b48f695c62f0bd0a58bd4e72c0acfd Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 16 Mar 2022 14:33:47 +0000 Subject: [PATCH] refactor validation and tests --- .../validations/financial_validations.rb | 91 +++- .../validations/financial_validations_spec.rb | 460 ++++++++++-------- 2 files changed, 318 insertions(+), 233 deletions(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 59ae2de83..1c45e9a29 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -59,14 +59,62 @@ module Validations::FinancialValidations end end - SCHARGE_RANGES = { this_landlord_general_needs: - { min: 0, max: 55, error: I18n.t("validations.financial.rent.scharge.this_landlord.general_needs") }, - this_landlord_supported_housing: - { min: 0, max: 280, error: I18n.t("validations.financial.rent.scharge.this_landlord.supported_housing") }, - other_landlord_general_needs: - { min: 0, max: 45, error: I18n.t("validations.financial.rent.scharge.other_landlord.general_needs") }, - other_landlord_supported_housing: - { min: 0, max: 165, error: I18n.t("validations.financial.rent.scharge.other_landlord.supported_housing") } }.freeze + SCHARGE_RANGES = { + this_landlord: { + general_needs: { + min: 0, + max: 55, + error: I18n.t("validations.financial.rent.scharge.this_landlord.general_needs"), + }, + supported_housing: { + min: 0, + max: 280, + error: I18n.t("validations.financial.rent.scharge.this_landlord.supported_housing"), + }, + }, + other_landlord: { + general_needs: { + min: 0, + max: 45, + error: I18n.t("validations.financial.rent.scharge.other_landlord.general_needs"), + }, + supported_housing: { + min: 0, + max: 165, + error: I18n.t("validations.financial.rent.scharge.other_landlord.supported_housing"), + }, + }, + }.freeze + + PSCHARGE_RANGES = { + this_landlord: { + general_needs: { + min: 0, + max: 30, + error: I18n.t("validations.financial.rent.pscharge.this_landlord.general_needs"), + }, + supported_housing: { + min: 0, + max: 200, + error: I18n.t("validations.financial.rent.pscharge.this_landlord.supported_housing"), + }, + }, + other_landlord: { + general_needs: { + min: 0, + max: 35, + error: I18n.t("validations.financial.rent.pscharge.other_landlord.general_needs"), + }, + supported_housing: { + min: 0, + max: 75, + error: I18n.t("validations.financial.rent.pscharge.other_landlord.supported_housing"), + }, + }, + }.freeze + + LANDLORD_VALUES = { 1 => :this_landlord, 2 => :other_landlord }.freeze + NEEDSTYPE_VALUES = { 0 => :supported_housing, 1 => :general_needs }.freeze def validate_rent_amount(record) if record.brent.present? && record.tshortfall.present? && record.brent < record.tshortfall * 2 @@ -74,32 +122,25 @@ module Validations::FinancialValidations record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.more_than_rent") end - validate_scharge(record) + validate_charges(record) end private - def validate_scharge(record) - scharge_range = if record.this_landlord? - if record.is_general_needs? - SCHARGE_RANGES[:this_landlord_general_needs] - elsif record.is_supported_housing? - SCHARGE_RANGES[:this_landlord_supported_housing] - end - elsif record.other_landlord? - if record.is_general_needs? - SCHARGE_RANGES[:other_landlord_general_needs] - elsif record.is_supported_housing? - SCHARGE_RANGES[:other_landlord_supported_housing] - end - end + def validate_charges(record) + scharge_range = SCHARGE_RANGES.dig(LANDLORD_VALUES[record.landlord], NEEDSTYPE_VALUES[record.needstype]) + pscharge_range = PSCHARGE_RANGES.dig(LANDLORD_VALUES[record.landlord], NEEDSTYPE_VALUES[record.needstype]) if scharge_range.present? && !weekly_value_in_range(record, "scharge", scharge_range[:min], scharge_range[:max]) record.errors.add :scharge, scharge_range[:error] end + + if pscharge_range.present? && !weekly_value_in_range(record, "pscharge", pscharge_range[:min], pscharge_range[:max]) + record.errors.add :pscharge, pscharge_range[:error] + end end - + def weekly_value_in_range(record, field, min, max) - record.scharge.present? && record.weekly_value(record.scharge).present? && record.weekly_value(record[field]).between?(min, max) + record[field].present? && record.weekly_value(record[field]).present? && record.weekly_value(record[field]).between?(min, max) end end diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 8542283c9..06f024e26 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -203,252 +203,296 @@ RSpec.describe Validations::FinancialValidations do context "when the landlord is this landlord" do context "when needstype is general needs" do - it "does not allow the scharge to be outside of 0 and 55 range per week when period is weekly" do + before do record.needstype = 1 record.landlord = 1 - record.period = 1 - record.scharge = 56 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to include(match I18n.t("validations.financial.rent.scharge.this_landlord.general_needs")) end - it "does allow the scharge to be between of 0 and 55 per week when period is weekly" do - record.needstype = 1 - record.landlord = 1 - record.period = 1 - record.scharge = 54 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to be_empty - end - - it "does not allow the scharge to be outside of 0 and 55 range per week when period is monthly" do - record.needstype = 1 - record.landlord = 1 - record.period = 4 - record.scharge = 300 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to include(match I18n.t("validations.financial.rent.scharge.this_landlord.general_needs")) - end - - it "does allow the scharge to be between of 0 and 55 per week when period is monthly" do - record.needstype = 1 - record.landlord = 1 - record.period = 4 - record.scharge = 220 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to be_empty - end - - it "does not allow the scharge to be outside of 0 and 55 range per week when period is every 2 weeks" do - record.needstype = 1 - record.landlord = 1 - record.period = 2 - record.scharge = 111 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to include(match I18n.t("validations.financial.rent.scharge.this_landlord.general_needs")) + [{ + period: { label: "weekly", value: 1 }, + charge: { field: "scharge", value: 56 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "scharge", value: 300 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "scharge", value: 111 }, + }, + { + period: { label: "weekly", value: 1 }, + charge: { field: "pscharge", value: 31 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "pscharge", value: 150 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "pscharge", value: 61 }, + }].each do |test_case| + it "does not allow charges outide 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) + expect(record.errors[test_case[:charge][:field]]) + .to include(match I18n.t("validations.financial.rent.#{test_case[:charge][:field]}.this_landlord.general_needs")) + end end - it "does allow the scharge to be between of 0 and 55 per week when period is every 2 weeks" do - record.needstype = 1 - record.landlord = 1 - record.period = 2 - record.scharge = 109 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to be_empty + [{ + period: { label: "weekly", value: 1 }, + charge: { field: "scharge", value: 54 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "scharge", value: 220 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "scharge", value: 109 }, + }, + { + period: { label: "weekly", value: 1 }, + charge: { field: "pscharge", value: 30 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "pscharge", value: 120 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "pscharge", value: 59 }, + }].each do |test_case| + it "does allow charges inside 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) + expect(record.errors[test_case[:charge][:field]]) + .to be_empty + end end end context "when needstype is supported housing" do - it "does not allow the scharge to be outside of 0 and 280 range per week when period is weekly" do + before do record.needstype = 0 record.landlord = 1 - record.period = 1 - record.scharge = 281 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to include(match I18n.t("validations.financial.rent.scharge.this_landlord.supported_housing")) end - it "does allow the scharge to be between of 0 and 280 per week when period is weekly" do - record.needstype = 0 - record.landlord = 1 - record.period = 1 - record.scharge = 280 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to be_empty - end - - it "does not allow the scharge to be outside of 0 and 280 range per week when period is monthly" do - record.needstype = 0 - record.landlord = 1 - record.period = 4 - record.scharge = 1225 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to include(match I18n.t("validations.financial.rent.scharge.this_landlord.supported_housing")) - end - - it "does allow the scharge to be between of 0 and 280 per week when period is monthly" do - record.needstype = 0 - record.landlord = 1 - record.period = 4 - record.scharge = 1200 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to be_empty - end - - it "does not allow the scharge to be outside of 0 and 280 range per week when period is every 2 weeks" do - record.needstype = 0 - record.landlord = 1 - record.period = 2 - record.scharge = 561 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to include(match I18n.t("validations.financial.rent.scharge.this_landlord.supported_housing")) + [{ + period: { label: "weekly", value: 1 }, + charge: { field: "scharge", value: 281 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "scharge", value: 1225 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "scharge", value: 561 }, + }, + { + period: { label: "weekly", value: 1 }, + charge: { field: "pscharge", value: 201 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "pscharge", value: 1000 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "pscharge", value: 400.80 }, + }].each do |test_case| + it "does not allow charges outide 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) + expect(record.errors[test_case[:charge][:field]]) + .to include(match I18n.t("validations.financial.rent.#{test_case[:charge][:field]}.this_landlord.supported_housing")) + end end - it "does allow the scharge to be between of 0 and 280 per week when period is every 2 weeks" do - record.needstype = 0 - record.landlord = 1 - record.period = 2 - record.scharge = 559 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to be_empty + [{ + period: { label: "weekly", value: 1 }, + charge: { field: "scharge", value: 280 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "scharge", value: 1200 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "scharge", value: 559 }, + }, + { + period: { label: "weekly", value: 1 }, + charge: { field: "pscharge", value: 199.99 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "pscharge", value: 800 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "pscharge", value: 400 }, + }].each do |test_case| + it "does allow charges inside 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) + expect(record.errors[test_case[:charge][:field]]) + .to be_empty + end end end end context "when the landlord is another RP" do context "when needstype is general needs" do - it "does not allow the scharge to be outside of 0 and 45 range per week when period is weekly" do + before do record.needstype = 1 record.landlord = 2 - record.period = 1 - record.scharge = 46 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to include(match I18n.t("validations.financial.rent.scharge.other_landlord.general_needs")) end - it "does allow the scharge to be between of 0 and 45 per week when period is weekly" do - record.needstype = 1 - record.landlord = 2 - record.period = 1 - record.scharge = 44 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to be_empty - end - - it "does not allow the scharge to be outside of 0 and 45 range per week when period is monthly" do - record.needstype = 1 - record.landlord = 2 - record.period = 4 - record.scharge = 200 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to include(match I18n.t("validations.financial.rent.scharge.other_landlord.general_needs")) - end - - it "does allow the scharge to be between of 0 and 45 per week when period is monthly" do - record.needstype = 1 - record.landlord = 2 - record.period = 4 - record.scharge = 160 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to be_empty - end - - it "does not allow the scharge to be outside of 0 and 45 range per week when period is every 2 weeks" do - record.needstype = 1 - record.landlord = 2 - record.period = 2 - record.scharge = 91 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to include(match I18n.t("validations.financial.rent.scharge.other_landlord.general_needs")) + [{ + period: { label: "weekly", value: 1 }, + charge: { field: "scharge", value: 46 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "scharge", value: 200 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "scharge", value: 91 }, + }, + { + period: { label: "weekly", value: 1 }, + charge: { field: "pscharge", value: 36 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "pscharge", value: 190 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "pscharge", value: 71 }, + }].each do |test_case| + it "does not allow charges outide 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) + expect(record.errors[test_case[:charge][:field]]) + .to include(match I18n.t("validations.financial.rent.#{test_case[:charge][:field]}.other_landlord.general_needs")) + end end - it "does allow the scharge to be between of 0 and 45 per week when period is every 2 weeks" do - record.needstype = 1 - record.landlord = 2 - record.period = 2 - record.scharge = 89 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to be_empty + [{ + period: { label: "weekly", value: 1 }, + charge: { field: "scharge", value: 44 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "scharge", value: 160 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "scharge", value: 89 }, + }, + { + period: { label: "weekly", value: 1 }, + charge: { field: "pscharge", value: 34 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "pscharge", value: 140 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "pscharge", value: 69 }, + }].each do |test_case| + it "does allow charges inside 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) + expect(record.errors[test_case[:charge][:field]]) + .to be_empty + end end end context "when needstype is supported housing" do - it "does not allow the scharge to be outside of 0 and 165 range per week when period is weekly" do + before do record.needstype = 0 record.landlord = 2 - record.period = 1 - record.scharge = 165.90 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to include(match I18n.t("validations.financial.rent.scharge.other_landlord.supported_housing")) end - it "does allow the scharge to be between of 0 and 165 per week when period is weekly" do - record.needstype = 0 - record.landlord = 2 - record.period = 1 - record.scharge = 120.88 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to be_empty - end - - it "does not allow the scharge to be outside of 0 and 165 range per week when period is monthly" do - record.needstype = 0 - record.landlord = 2 - record.period = 4 - record.scharge = 750 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to include(match I18n.t("validations.financial.rent.scharge.other_landlord.supported_housing")) - end - - it "does allow the scharge to be between of 0 and 165 per week when period is monthly" do - record.needstype = 0 - record.landlord = 2 - record.period = 4 - record.scharge = 608 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to be_empty - end - - it "does not allow the scharge to be outside of 0 and 165 range per week when period is every 2 weeks" do - record.needstype = 0 - record.landlord = 2 - record.period = 2 - record.scharge = 330.50 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to include(match I18n.t("validations.financial.rent.scharge.other_landlord.supported_housing")) + [{ + period: { label: "weekly", value: 1 }, + charge: { field: "scharge", value: 165.90 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "scharge", value: 750 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "scharge", value: 330.50 }, + }, + { + period: { label: "weekly", value: 1 }, + charge: { field: "pscharge", value: 76 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "pscharge", value: 400 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "pscharge", value: 151 }, + }].each do |test_case| + it "does not allow charges outide 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) + expect(record.errors[test_case[:charge][:field]]) + .to include(match I18n.t("validations.financial.rent.#{test_case[:charge][:field]}.other_landlord.supported_housing")) + end end - it "does allow the scharge to be between of 0 and 165 per week when period is every 2 weeks" do - record.needstype = 0 - record.landlord = 2 - record.period = 2 - record.scharge = 329.99 - financial_validator.validate_rent_amount(record) - expect(record.errors["scharge"]) - .to be_empty + [{ + period: { label: "weekly", value: 1 }, + charge: { field: "scharge", value: 120.88 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "scharge", value: 608 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "scharge", value: 329.99 }, + }, + { + period: { label: "weekly", value: 1 }, + charge: { field: "pscharge", value: 74 }, + }, + { + period: { label: "monthly", value: 4 }, + charge: { field: "pscharge", value: 210 }, + }, + { + period: { label: "every 2 weeks", value: 2 }, + charge: { field: "pscharge", value: 149 }, + }].each do |test_case| + it "does allow charges inside 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) + expect(record.errors[test_case[:charge][:field]]) + .to be_empty + end end end end