From 8f51dd55248cecbca5427037e8c2b904f6c8abed Mon Sep 17 00:00:00 2001 From: Mo Seedat Date: Sun, 16 Oct 2022 01:12:42 +0100 Subject: [PATCH] Add appropriate specs --- app/models/sales_log.rb | 32 ++--------- .../sales/property_information_validations.rb | 7 ++- config/locales/en.yml | 4 +- spec/models/sales_log_spec.rb | 54 ++++++++++++++----- 4 files changed, 52 insertions(+), 45 deletions(-) diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index c96589644..fc7c2b1af 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -1,32 +1,5 @@ class SalesLogValidator < ActiveModel::Validator - - # included do - # validates :beds, numericality: { only_integer: true }, presence: true, comparison: { greater_than: 0, less_than: 10 } - # end - def self.included(klass) - #klass.extend(ClassMethods) - puts "INCLUDING VALIDATIONS" - validates :beds, numericality: { only_integer: true }, comparison: { greater_than: 0, less_than: 10 } - - - end - - SalesLogValidator.class_eval do - p "class_eval - self is: " + self.to_s - def frontend - p "inside a method self is: " + self.to_s - end - validates :beds, numericality: { only_integer: true }, comparison: { greater_than: 0, less_than: 10 } - - end - - - - # Validations methods need to be called 'validate_' to run on model save - # or form page submission include Validations::Sales::PropertyInformationValidations - #extend ActiveSupport::Concern - def validate(record) validation_methods = public_methods.select { |method| method.starts_with?("validate_") } @@ -41,7 +14,10 @@ class SalesLog < Log has_paper_trail - #validates :beds, numericality: { only_integer: true }, presence: true, comparison: { greater_than: 0, less_than: 10 } + ## Regular validations + validates :beds, numericality: { only_integer: true }, comparison: { greater_than: 0, less_than: 10 }, if: -> { beds.present? } + + ## Custom validations validates_with SalesLogValidator before_validation :set_derived_fields! diff --git a/app/models/validations/sales/property_information_validations.rb b/app/models/validations/sales/property_information_validations.rb index cfed71d22..076facc0d 100644 --- a/app/models/validations/sales/property_information_validations.rb +++ b/app/models/validations/sales/property_information_validations.rb @@ -1,8 +1,11 @@ module Validations::Sales::PropertyInformationValidations - # CLDC-858 + # Error must be defined for both :proptype and :beds + # to ensure the appropriate error is shown when selecting + # property type and number of rooms def validate_bedsit_has_one_room(record) if record.bedsit? && record.beds > 1 - record.errors.add(:beds, :non_bedsit_max) + record.errors.add(:proptype, :bedsit_max) + record.errors.add(:beds, :bedsit_max) end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index d9ded75d4..5e2ca87b9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -98,7 +98,9 @@ en: not_an_integer: "Number of bedrooms must be a whole number between 1 and 9" greater_than: "Number of bedrooms must be between 1 and 9" less_than: "Number of bedrooms must be between 1 and 9" - non_bedsit_max: "A bedsit can not have more than 1 bedroom" + bedsit_max: "A bedsit can not have more than 1 bedroom" + proptype: + bedsit_max: "A bedsit can not have more than 1 bedroom" validations: organisation: diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index a3fae265a..4b2b9edfd 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -94,28 +94,54 @@ RSpec.describe SalesLog, type: :model do # Rails helper validations only. Custom validations belong in their respective # validations spec describe "validations" do - describe "beds" do - context "when valid" do - it "is between 1 and 9" do - sales_log = FactoryBot.build(:sales_log, beds: 4) + describe "#beds" do + before do + expect(sales_log).to be_valid + end + + context "when property is a bedsit" do + let(:sales_log) {FactoryBot.build(:sales_log, :completed, :bedsit) } + + # Set error for :beds and :proptype to ensure message is shown in + # both bedroom number selection and property type selection + it "must only have 1 bedroom" do + sales_log.beds = 3 - expect(sales_log).to be_valid + expect(sales_log).to_not be_valid + expect(sales_log.errors[:beds]).to eq ["A bedsit can not have more than 1 bedroom"] + expect(sales_log.errors[:proptype]).to eq ["A bedsit can not have more than 1 bedroom"] end end - context "when invalid" do - it "is not an integer" do - sales_log = FactoryBot.build(:sales_log, beds: "Four") + context "when property is not a bedsit" do + let(:sales_log) { FactoryBot.build(:sales_log, :completed, beds: 4, proptype: 3) } - expect(sales_log).to_not be_valid - expect(sales_log.errors.message[:beds]).to eq ["is invalid"] + it "must have 1 to 9 bedrooms", aggregate_failures: true do + [0, 10].each do |num_beds| + sales_log.beds = num_beds + + expect(sales_log).to_not be_valid + expect(sales_log.errors[:beds]).to eq ["Number of bedrooms must be between 1 and 9"] + end end + end - it "is a negative number" do - sales_log = FactoryBot.build(:sales_log, beds: -4) + context "when given invalid data" do + let(:sales_log) { FactoryBot.build(:sales_log, :completed) } - expect(sales_log).to_not be_valid - expect(sales_log.errors.message[:beds]).to eq ["is invalid"] + it "fails with appropriate error message", aggregate_failures: true do + invalid_beds_values = { + "Four" => ["Number of bedrooms must be between 1 and 9", "Number of bedrooms must be between 1 and 9"], + -2 => ["Number of bedrooms must be between 1 and 9"], + 2.5 => ["Number of bedrooms must be a whole number between 1 and 9"] + } + + invalid_beds_values.each do |beds_value, expected_error| + sales_log.beds = beds_value + + expect(sales_log).to_not be_valid + expect(sales_log.errors[:beds]).to eq expected_error + end end end end