Browse Source

Merge branch 'main' into CLDC-868-purchase-price-validations

# Conflicts:
#	app/models/validations/sales/sale_information_validations.rb
#	spec/models/validations/sales/sale_information_validations_spec.rb
pull/1225/head
natdeanlewissoftwire 2 years ago
parent
commit
9533659a88
  1. 2
      app/models/form/sales/questions/deposit_amount.rb
  2. 8
      app/models/validations/shared_validations.rb
  3. 2
      app/services/bulk_upload/lettings/csv_parser.rb
  4. 2
      spec/models/form/sales/questions/deposit_amount_spec.rb
  5. 19
      spec/models/validations/shared_validations_spec.rb
  6. 17
      spec/services/bulk_upload/lettings/csv_parser_spec.rb

2
app/models/form/sales/questions/deposit_amount.rb

@ -7,7 +7,7 @@ class Form::Sales::Questions::DepositAmount < ::Form::Question
@type = "numeric"
@min = 0
@width = 5
@max = 9_999_999
@max = 999_999
@prefix = "£"
@hint_text = "Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage"
@derived = true

8
app/models/validations/shared_validations.rb

@ -1,4 +1,6 @@
module Validations::SharedValidations
include ActionView::Helpers::NumberHelper
def validate_other_field(record, value_other = nil, main_field = nil, other_field = nil, main_label = nil, other_label = nil)
return unless main_field || other_field
@ -19,17 +21,19 @@ module Validations::SharedValidations
next unless record[question.id]
field = question.check_answer_label || question.id
min = [question.prefix, number_with_delimiter(question.min, delimiter: ","), question.suffix].join("")
max = [question.prefix, number_with_delimiter(question.max, delimiter: ","), question.suffix].join("")
begin
answer = Float(record.public_send("#{question.id}_before_type_cast"))
rescue ArgumentError
record.errors.add question.id.to_sym, I18n.t("validations.numeric.valid", field:, min: question.min, max: question.max)
record.errors.add question.id.to_sym, I18n.t("validations.numeric.valid", field:, min:, max:)
end
next unless answer
if (question.min && question.min > answer) || (question.max && question.max < answer)
record.errors.add question.id.to_sym, I18n.t("validations.numeric.valid", field:, min: question.min, max: question.max)
record.errors.add question.id.to_sym, I18n.t("validations.numeric.valid", field:, min:, max:)
end
end
end

2
app/services/bulk_upload/lettings/csv_parser.rb

@ -50,7 +50,7 @@ private
def normalised_string
return @normalised_string if @normalised_string
@normalised_string = File.read(path)
@normalised_string = File.read(path, encoding: "bom|utf-8")
@normalised_string.gsub!("\r\n", "\n")
@normalised_string

2
spec/models/form/sales/questions/deposit_amount_spec.rb

@ -48,6 +48,6 @@ RSpec.describe Form::Sales::Questions::DepositAmount, type: :model do
end
it "has correct max" do
expect(question.max).to eq(9_999_999)
expect(question.max).to eq(999_999)
end
end

19
spec/models/validations/shared_validations_spec.rb

@ -5,6 +5,7 @@ RSpec.describe Validations::SharedValidations do
let(:validator_class) { Class.new { include Validations::SharedValidations } }
let(:record) { FactoryBot.create(:lettings_log) }
let(:sales_record) { FactoryBot.create(:sales_log) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
describe "numeric min max validations" do
@ -67,6 +68,24 @@ RSpec.describe Validations::SharedValidations do
expect(record.errors["age6"]).to be_empty
end
end
context "when validating percent" do
it "validates that % suffix is added in the error message" do
sales_record.stairbought = "random"
shared_validator.validate_numeric_min_max(sales_record)
expect(sales_record.errors["stairbought"])
.to include(match I18n.t("validations.numeric.valid", field: "Percentage bought in this staircasing transaction", min: "0 percent", max: "100 percent"))
end
end
context "when validating price" do
it "validates that £ prefix and , is added in the error message" do
sales_record.income1 = "random"
shared_validator.validate_numeric_min_max(sales_record)
expect(sales_record.errors["income1"])
.to include(match I18n.t("validations.numeric.valid", field: "Buyer 1’s gross annual income", min: "£0", max: "£999,999"))
end
end
end
describe "radio options validations" do

17
spec/services/bulk_upload/lettings/csv_parser_spec.rb

@ -35,4 +35,21 @@ RSpec.describe BulkUpload::Lettings::CsvParser do
expect(service.row_parsers[0].field_12).to eql(log.age1)
end
end
context "when parsing with BOM aka byte order mark" do
let(:file) { Tempfile.new }
let(:path) { file.path }
let(:log) { build(:lettings_log, :completed) }
let(:bom) { "\uFEFF" }
before do
file.write(bom)
file.write(BulkUpload::LogToCsv.new(log:, col_offset: 0).to_csv_row)
file.close
end
it "parses csv correctly" do
expect(service.row_parsers[0].field_12).to eql(log.age1)
end
end
end

Loading…
Cancel
Save