Browse Source

refactor validation and tests

pull/388/head
Kat 3 years ago
parent
commit
80776e8e33
  1. 89
      app/models/validations/financial_validations.rb
  2. 444
      spec/models/validations/financial_validations_spec.rb

89
app/models/validations/financial_validations.rb

@ -59,14 +59,62 @@ module Validations::FinancialValidations
end end
end end
SCHARGE_RANGES = { this_landlord_general_needs: SCHARGE_RANGES = {
{ min: 0, max: 55, error: I18n.t("validations.financial.rent.scharge.this_landlord.general_needs") }, this_landlord: {
this_landlord_supported_housing: general_needs: {
{ min: 0, max: 280, error: I18n.t("validations.financial.rent.scharge.this_landlord.supported_housing") }, min: 0,
other_landlord_general_needs: max: 55,
{ min: 0, max: 45, error: I18n.t("validations.financial.rent.scharge.other_landlord.general_needs") }, error: I18n.t("validations.financial.rent.scharge.this_landlord.general_needs"),
other_landlord_supported_housing: },
{ min: 0, max: 165, error: I18n.t("validations.financial.rent.scharge.other_landlord.supported_housing") } }.freeze 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) def validate_rent_amount(record)
if record.brent.present? && record.tshortfall.present? && record.brent < record.tshortfall * 2 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") record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.more_than_rent")
end end
validate_scharge(record) validate_charges(record)
end end
private private
def validate_scharge(record) def validate_charges(record)
scharge_range = if record.this_landlord? scharge_range = SCHARGE_RANGES.dig(LANDLORD_VALUES[record.landlord], NEEDSTYPE_VALUES[record.needstype])
if record.is_general_needs? pscharge_range = PSCHARGE_RANGES.dig(LANDLORD_VALUES[record.landlord], NEEDSTYPE_VALUES[record.needstype])
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
if scharge_range.present? && !weekly_value_in_range(record, "scharge", scharge_range[:min], scharge_range[:max]) if scharge_range.present? && !weekly_value_in_range(record, "scharge", scharge_range[:min], scharge_range[:max])
record.errors.add :scharge, scharge_range[:error] record.errors.add :scharge, scharge_range[:error]
end 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 end
def weekly_value_in_range(record, field, min, max) 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
end end

444
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 the landlord is this landlord" do
context "when needstype is general needs" 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.needstype = 1
record.landlord = 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 end
it "does allow the scharge to be between of 0 and 55 per week when period is weekly" do [{
record.needstype = 1 period: { label: "weekly", value: 1 },
record.landlord = 1 charge: { field: "scharge", value: 56 },
record.period = 1 },
record.scharge = 54 {
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) financial_validator.validate_rent_amount(record)
expect(record.errors["scharge"]) expect(record.errors[test_case[:charge][:field]])
.to be_empty .to include(match I18n.t("validations.financial.rent.#{test_case[:charge][:field]}.this_landlord.general_needs"))
end end
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 period: { label: "weekly", value: 1 },
record.period = 4 charge: { field: "scharge", value: 54 },
record.scharge = 300 },
{
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) financial_validator.validate_rent_amount(record)
expect(record.errors["scharge"]) expect(record.errors[test_case[:charge][:field]])
.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 .to be_empty
end 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"))
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
end end
end end
context "when needstype is supported housing" do 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.needstype = 0
record.landlord = 1 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 end
it "does allow the scharge to be between of 0 and 280 per week when period is weekly" do [{
record.needstype = 0 period: { label: "weekly", value: 1 },
record.landlord = 1 charge: { field: "scharge", value: 281 },
record.period = 1 },
record.scharge = 280 {
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) financial_validator.validate_rent_amount(record)
expect(record.errors["scharge"]) expect(record.errors[test_case[:charge][:field]])
.to be_empty .to include(match I18n.t("validations.financial.rent.#{test_case[:charge][:field]}.this_landlord.supported_housing"))
end end
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 period: { label: "weekly", value: 1 },
record.period = 4 charge: { field: "scharge", value: 280 },
record.scharge = 1225 },
{
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) financial_validator.validate_rent_amount(record)
expect(record.errors["scharge"]) expect(record.errors[test_case[:charge][:field]])
.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 .to be_empty
end 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"))
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
end end
end end
end end
context "when the landlord is another RP" do context "when the landlord is another RP" do
context "when needstype is general needs" 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.needstype = 1
record.landlord = 2 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 end
it "does allow the scharge to be between of 0 and 45 per week when period is weekly" do [{
record.needstype = 1 period: { label: "weekly", value: 1 },
record.landlord = 2 charge: { field: "scharge", value: 46 },
record.period = 1 },
record.scharge = 44 {
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) financial_validator.validate_rent_amount(record)
expect(record.errors["scharge"]) expect(record.errors[test_case[:charge][:field]])
.to be_empty .to include(match I18n.t("validations.financial.rent.#{test_case[:charge][:field]}.other_landlord.general_needs"))
end end
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 period: { label: "weekly", value: 1 },
record.period = 4 charge: { field: "scharge", value: 44 },
record.scharge = 200 },
{
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) financial_validator.validate_rent_amount(record)
expect(record.errors["scharge"]) expect(record.errors[test_case[:charge][:field]])
.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 .to be_empty
end 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"))
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
end end
end end
context "when needstype is supported housing" do 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.needstype = 0
record.landlord = 2 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 end
it "does allow the scharge to be between of 0 and 165 per week when period is weekly" do [{
record.needstype = 0 period: { label: "weekly", value: 1 },
record.landlord = 2 charge: { field: "scharge", value: 165.90 },
record.period = 1 },
record.scharge = 120.88 {
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) financial_validator.validate_rent_amount(record)
expect(record.errors["scharge"]) expect(record.errors[test_case[:charge][:field]])
.to be_empty .to include(match I18n.t("validations.financial.rent.#{test_case[:charge][:field]}.other_landlord.supported_housing"))
end end
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 period: { label: "weekly", value: 1 },
record.period = 4 charge: { field: "scharge", value: 120.88 },
record.scharge = 750 },
{
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) financial_validator.validate_rent_amount(record)
expect(record.errors["scharge"]) expect(record.errors[test_case[:charge][:field]])
.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 .to be_empty
end 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"))
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
end end
end end
end end

Loading…
Cancel
Save