Browse Source

Add tolerance to soft validation (#2240)

pull/2268/head
kosiakkatrina 10 months ago committed by GitHub
parent
commit
3856a175af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 8
      app/models/validations/sales/soft_validations.rb
  2. 23
      spec/models/validations/sales/soft_validations_spec.rb

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

@ -89,9 +89,11 @@ module Validations::Sales::SoftValidations
return unless cashdis || !is_type_discount?
return unless deposit && value && equity
cash_discount = cashdis || 0
mortgage_value = mortgage || 0
mortgage_value + deposit + cash_discount != value * equity / 100
!within_tolerance?(mortgage_deposit_and_discount_total, value * equity / 100, 1)
end
def within_tolerance?(expected, actual, tolerance)
(expected - actual).abs <= tolerance
end
def mortgage_plus_deposit_less_than_discounted_value?

23
spec/models/validations/sales/soft_validations_spec.rb

@ -415,6 +415,17 @@ RSpec.describe Validations::Sales::SoftValidations do
.not_to be_shared_ownership_deposit_invalid
end
it "returns false if MORTGAGE + DEPOSIT + CASHDIS are within 1£ of VALUE * EQUITY/100" do
record.mortgage = 500
record.deposit = 500
record.cashdis = 500
record.value = 3001
record.equity = 50
expect(record)
.not_to be_shared_ownership_deposit_invalid
end
it "returns false if mortgage is used and no mortgage is given" do
record.mortgage = nil
record.deposit = 1000
@ -473,6 +484,18 @@ RSpec.describe Validations::Sales::SoftValidations do
.to be_shared_ownership_deposit_invalid
end
it "returns false if no cashdis not routed to and MORTGAGE + DEPOSIT are within 1£ of VALUE * EQUITY/100" do
record.mortgage = 500
record.deposit = 500
record.type = 2
record.cashdis = nil
record.value = 1999
record.equity = 50
expect(record)
.not_to be_shared_ownership_deposit_invalid
end
it "returns false if no value is given" do
record.mortgage = 1000
record.deposit = 1000

Loading…
Cancel
Save