Browse Source

add total charge calculations

pull/388/head
Kat 3 years ago
parent
commit
b26a7ae572
  1. 11
      app/models/validations/financial_validations.rb
  2. 122
      spec/models/validations/financial_validations_spec.rb

11
app/models/validations/financial_validations.rb

@ -101,6 +101,17 @@ 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
if record.tcharge.present? && weekly_value_in_range(record, "tcharge", 9.99)
record.errors.add :tcharge, I18n.t("validations.financial.tcharge.under_10")
end
answered_questions = [record.tcharge, record.chcharge].concat(record.household_charge.zero? ? [record.household_charge] : [])
if answered_questions.count(&:present?) > 1
record.errors.add :tcharge, I18n.t("validations.financial.tcharge.complete_1_of_3") if record.tcharge.present?
record.errors.add :chcharge, I18n.t("validations.financial.chcharge.complete_1_of_3") if record.chcharge.present?
record.errors.add :household_charge, I18n.t("validations.financial.household_charge.complete_1_of_3") if record.household_charge.present?
end
validate_charges(record) validate_charges(record)
end end

122
spec/models/validations/financial_validations_spec.rb

@ -591,6 +591,128 @@ RSpec.describe Validations::FinancialValidations do
end end
end end
end end
context "when period is weekly" do
it "validates that total charge is at least 10 per week" do
record.period = 1
record.tcharge = 9
financial_validator.validate_rent_amount(record)
expect(record.errors["tcharge"])
.to include(match I18n.t("validations.financial.tcharge.under_10"))
end
it "allows the total charge to be over 10 per week" do
record.period = 1
record.tcharge = 10
financial_validator.validate_rent_amount(record)
expect(record.errors["tcharge"])
.to be_empty
end
end
context "when period is every 2 weeks" do
it "validates that total charge is at least 10 per week" do
record.period = 2
record.tcharge = 19.99
financial_validator.validate_rent_amount(record)
expect(record.errors["tcharge"])
.to include(match I18n.t("validations.financial.tcharge.under_10"))
end
it "allows the total charge to be over 10 per week" do
record.period = 2
record.tcharge = 20
financial_validator.validate_rent_amount(record)
expect(record.errors["tcharge"])
.to be_empty
end
end
context "when entering charges" do
it "returns an error for 3 charge types selected" do
record.tcharge = 19.99
record.chcharge = 20
record.household_charge = 0
financial_validator.validate_rent_amount(record)
expect(record.errors["tcharge"])
.to include(match I18n.t("validations.financial.tcharge.complete_1_of_3"))
expect(record.errors["chcharge"])
.to include(match I18n.t("validations.financial.chcharge.complete_1_of_3"))
expect(record.errors["household_charge"])
.to include(match I18n.t("validations.financial.household_charge.complete_1_of_3"))
end
it "returns an error for tcharge and chcharge types selected" do
record.tcharge = 19.99
record.chcharge = 20
financial_validator.validate_rent_amount(record)
expect(record.errors["household_charge"])
.to be_empty
expect(record.errors["tcharge"])
.to include(match I18n.t("validations.financial.tcharge.complete_1_of_3"))
expect(record.errors["chcharge"])
.to include(match I18n.t("validations.financial.chcharge.complete_1_of_3"))
end
it "returns an error for tcharge and household_charge types selected" do
record.tcharge = 19.99
record.household_charge = 0
financial_validator.validate_rent_amount(record)
expect(record.errors["chcharge"])
.to be_empty
expect(record.errors["tcharge"])
.to include(match I18n.t("validations.financial.tcharge.complete_1_of_3"))
expect(record.errors["household_charge"])
.to include(match I18n.t("validations.financial.household_charge.complete_1_of_3"))
end
it "returns an error for chcharge and household_charge types selected" do
record.chcharge = 20
record.household_charge = 0
financial_validator.validate_rent_amount(record)
expect(record.errors["tcharge"])
.to be_empty
expect(record.errors["chcharge"])
.to include(match I18n.t("validations.financial.chcharge.complete_1_of_3"))
expect(record.errors["household_charge"])
.to include(match I18n.t("validations.financial.household_charge.complete_1_of_3"))
end
end
it "does not return an error for household_charge being yes" do
record.household_charge = 0
financial_validator.validate_rent_amount(record)
expect(record.errors["tcharge"])
.to be_empty
expect(record.errors["chcharge"])
.to be_empty
expect(record.errors["household_charge"])
.to be_empty
end
it "does not return an error for chcharge being selected" do
record.household_charge = 1
record.chcharge = 20
financial_validator.validate_rent_amount(record)
expect(record.errors["tcharge"])
.to be_empty
expect(record.errors["chcharge"])
.to be_empty
expect(record.errors["household_charge"])
.to be_empty
end
it "does not return an error for tcharge being selected" do
record.household_charge = 1
record.tcharge = 19.99
financial_validator.validate_rent_amount(record)
expect(record.errors["tcharge"])
.to be_empty
expect(record.errors["chcharge"])
.to be_empty
expect(record.errors["household_charge"])
.to be_empty
end
end end
end end
end end

Loading…
Cancel
Save