Browse Source

CLDC-3447: Make tolerance inclusive

pull/2427/head
Rachael Booth 2 years ago
parent
commit
dfbf4bf2d9
  1. 8
      app/models/validations/sales/sale_information_validations.rb
  2. 20
      spec/models/validations/sales/sale_information_validations_spec.rb

8
app/models/validations/sales/sale_information_validations.rb

@ -50,7 +50,7 @@ module Validations::Sales::SaleInformationValidations
# When a percentage discount is used, a percentage tolerance is needed to account for rounding errors # When a percentage discount is used, a percentage tolerance is needed to account for rounding errors
tolerance = record.discount ? record.value * 0.05 / 100 : 1 tolerance = record.discount ? record.value * 0.05 / 100 : 1
if over_tolerance?(record.mortgage_deposit_and_grant_total, record.value_with_discount, tolerance) && record.discounted_ownership_sale? if over_tolerance?(record.mortgage_deposit_and_grant_total, record.value_with_discount, tolerance, true) && record.discounted_ownership_sale?
%i[mortgageused mortgage value deposit ownershipsch discount grant].each do |field| %i[mortgageused mortgage value deposit ownershipsch discount grant].each do |field|
record.errors.add field, I18n.t("validations.sale_information.discounted_ownership_value", mortgage_deposit_and_grant_total: record.field_formatted_as_currency("mortgage_deposit_and_grant_total"), value_with_discount: record.field_formatted_as_currency("value_with_discount")) record.errors.add field, I18n.t("validations.sale_information.discounted_ownership_value", mortgage_deposit_and_grant_total: record.field_formatted_as_currency("mortgage_deposit_and_grant_total"), value_with_discount: record.field_formatted_as_currency("value_with_discount"))
end end
@ -250,7 +250,11 @@ module Validations::Sales::SaleInformationValidations
end end
end end
def over_tolerance?(expected, actual, tolerance) def over_tolerance?(expected, actual, tolerance, strict = false)
if strict
(expected - actual).abs > tolerance
else
(expected - actual).abs >= tolerance (expected - actual).abs >= tolerance
end end
end
end end

20
spec/models/validations/sales/sale_information_validations_spec.rb

@ -323,7 +323,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
it "does not add errors if mortgage and deposit total is within a 0.05% x market value tolerance of market value - discount" do it "does not add errors if mortgage and deposit total is within a 0.05% x market value tolerance of market value - discount" do
record.value = 123_000 record.value = 123_000
record.mortgage = 66_112 record.mortgage = 66_112
record.deposit = 0 # This would be a 46.25% discount record.deposit = 0
record.discount = 46.3 record.discount = 46.3
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
@ -353,6 +353,24 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £66,113.00, and the purchase price times by the discount is £66,051.00. These figures should be the same") expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £66,113.00, and the purchase price times by the discount is £66,051.00. These figures should be the same")
expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £66,113.00, and the purchase price times by the discount is £66,051.00. These figures should be the same") expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £66,113.00, and the purchase price times by the discount is £66,051.00. These figures should be the same")
end end
it "does not add errors if mortgage and deposit total is exactly 0.05% x market value away from market value - discount" do
record.value = 120_000
record.mortgage = 64_500
record.deposit = 0
record.discount = 46.3
sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to be_empty
expect(record.errors["mortgage"]).to be_empty
expect(record.errors["value"]).to be_empty
expect(record.errors["deposit"]).to be_empty
expect(record.errors["ownershipsch"]).to be_empty
expect(record.errors["discount"]).to be_empty
expect(record.errors["grant"]).to be_empty
end
end end
end end

Loading…
Cancel
Save