From 206872939e62d407e4777a7efa3a9a035bb53e18 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Sun, 19 Mar 2023 12:39:07 +0000 Subject: [PATCH] write tests for new validations --- .../validations/shared_validations_spec.rb | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/spec/models/validations/shared_validations_spec.rb b/spec/models/validations/shared_validations_spec.rb index 163454e2b..1a5bd69e6 100644 --- a/spec/models/validations/shared_validations_spec.rb +++ b/spec/models/validations/shared_validations_spec.rb @@ -114,6 +114,64 @@ RSpec.describe Validations::SharedValidations do end end + describe "validating level of accuracy or rounding for numeric questions" do + context "when validating a question with a step of 1" do + it "adds an error if input is a decimal" do + sales_log.income1 = 30_000.5 + shared_validator.validate_numeric_step(sales_log) + expect(sales_log.errors[:income1]).to include I18n.t("validations.numeric.whole_number", field: "Buyer 1’s gross annual income") + end + + it "does not add an error if input is an integer" do + sales_log.income1 = 30_000 + shared_validator.validate_numeric_step(sales_log) + expect(sales_log.errors).to be_empty + end + end + + context "when validating a question with a step of 10" do + it "adds an error if input is not a multiple of ten" do + sales_log.savings = 30_005 + shared_validator.validate_numeric_step(sales_log) + expect(sales_log.errors[:savings]).to include I18n.t("validations.numeric.nearest_ten", field: "Buyer’s total savings (to nearest £10) before any deposit paid") + end + + it "does not add an error if input is a multiple of ten" do + sales_log.savings = 30_000 + shared_validator.validate_numeric_step(sales_log) + expect(sales_log.errors).to be_empty + end + end + + context "when validating a question with a step of 0.01" do + it "adds an error if input has more than 2 decimal places" do + sales_log.deposit = 630.7418 + shared_validator.validate_numeric_step(sales_log) + expect(sales_log.errors[:deposit]).to include I18n.t("validations.numeric.nearest_penny", field: "Cash deposit") + end + + it "does not add an error if input has 2 or fewer decimal places" do + sales_log.deposit = 630.74 + shared_validator.validate_numeric_step(sales_log) + expect(sales_log.errors).to be_empty + end + end + end + + describe "validating the format of inputs in numeric questions" do + it "adds an error if number is input in exponent notation" do + sales_log.age2 = "1e1" + shared_validator.validate_numeric_normal_format(sales_log) + expect(sales_log.errors[:age2]).to include I18n.t("validations.numeric.normal_format") + end + + it "does not add an error if number is input in standard notation" do + sales_log.age2 = "11" + shared_validator.validate_numeric_normal_format(sales_log) + expect(sales_log.errors).to be_empty + end + end + describe "radio options validations" do it "allows only possible values" do lettings_log.needstype = 1