Browse Source

CLDC-859 Sales validation - bedsits cannot have more than 1 bed (#1188)

* feat: add sales validation to check bedsits have <=1 beds

* test: sales validation to check bedsits have <=1 beds

* fix: typo in property validation method name

* feat: add same bedsit validation to number of bedrooms question

* test: fix typo propert -> property in property validations tests

* test: add test for validate_property_number_of_bedrooms (sales)

* feat: update wording for 'number of bedrooms' validation when bedsit

* test: condense sales property validations tests

* test: strengthen sales property validations error addition test

* refactor: simplify sales property validations into one method

* fix: update error message content to match Beth's choices

* chore: lint

* test: add requests test for invalid sales log params when posting

* refactor: use if rather than unless... not in property validation

* refactor: write method is_bedsit? on SalesLog

* test: check bedsit error not added if proptype or beds is nil

* lint: use update! not update

* feat: update validation messages to improve readability

* fix: provide valid date in request test for invalid proptype/beds

* feat: make is_bedsit and validate_bedsit_number_of_beds more readable
pull/1302/head v0.2.39
SamSeed-Softwire 2 years ago committed by GitHub
parent
commit
92029ea2a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/models/sales_log.rb
  2. 9
      app/models/validations/sales/property_validations.rb
  3. 3
      config/locales/en.yml
  4. 36
      spec/models/validations/sales/property_validations_spec.rb
  5. 25
      spec/requests/sales_logs_controller_spec.rb

4
app/models/sales_log.rb

@ -223,6 +223,10 @@ class SalesLog < Log
type == 24
end
def is_bedsit?
proptype == 2
end
def shared_ownership_scheme?
ownershipsch == 1
end

9
app/models/validations/sales/property_validations.rb

@ -7,4 +7,13 @@ module Validations::Sales::PropertyValidations
record.errors.add :ppostcode_full, I18n.t("validations.property.postcode.must_match_previous")
end
end
def validate_bedsit_number_of_beds(record)
return unless record.proptype.present? && record.beds.present?
if record.is_bedsit? && record.beds > 1
record.errors.add :proptype, I18n.t("validations.property.proptype.bedsits_have_max_one_bedroom")
record.errors.add :beds, I18n.t("validations.property.beds.bedsits_have_max_one_bedroom")
end
end
end

3
config/locales/en.yml

@ -202,6 +202,9 @@ en:
beds:
non_positive: "Number of bedrooms has to be greater than 0"
over_max: "Number of bedrooms cannot be more than 12"
bedsits_have_max_one_bedroom: "Number of bedrooms must be 1 if the property is a bedsit"
proptype:
bedsits_have_max_one_bedroom: "Answer cannot be 'Bedsit' if the property has 2 or more bedrooms"
postcode:
must_match_previous: "Buyer's last accommodation and discounted ownership postcodes must match"

36
spec/models/validations/sales/property_validations_spec.rb

@ -48,4 +48,40 @@ RSpec.describe Validations::Sales::PropertyValidations do
end
end
end
describe "#validate_property_unit_type" do
context "when number of bedrooms is 1" do
let(:record) { FactoryBot.build(:sales_log, beds: 1, proptype: 2) }
it "does not add an error if it's a bedsit" do
property_validator.validate_bedsit_number_of_beds(record)
expect(record.errors).not_to be_present
end
end
context "when number of bedrooms is > 1" do
let(:record) { FactoryBot.build(:sales_log, beds: 2, proptype: 2) }
it "does add an error if it's a bedsit" do
property_validator.validate_bedsit_number_of_beds(record)
expect(record.errors.added?(:proptype, "Answer cannot be 'Bedsit' if the property has 2 or more bedrooms")).to be true
expect(record.errors.added?(:beds, "Number of bedrooms must be 1 if the property is a bedsit")).to be true
end
it "does not add an error if proptype is undefined" do
record.update!(proptype: nil)
property_validator.validate_bedsit_number_of_beds(record)
expect(record.errors).not_to be_present
end
end
context "when number of bedrooms is undefined" do
let(:record) { FactoryBot.build(:sales_log, beds: nil, proptype: 2) }
it "does not add an error if it's a bedsit" do
property_validator.validate_bedsit_number_of_beds(record)
expect(record.errors).not_to be_present
end
end
end
end

25
spec/requests/sales_logs_controller_spec.rb

@ -61,6 +61,31 @@ RSpec.describe SalesLogsController, type: :request do
expect(response).to have_http_status(:unauthorized)
end
end
context "with a request containing invalid json parameters" do
let(:params) do
{
"saledate": Date.new(2022, 4, 1),
"purchid": "1",
"ownershipsch": 1,
"type": 2,
"jointpur": 1,
"jointmore": 1,
"beds": 2,
"proptype": 2,
}
end
before do
post "/sales-logs", headers:, params: params.to_json
end
it "validates sales log parameters" do
json_response = JSON.parse(response.body)
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response["errors"]).to match_array([["beds", ["Number of bedrooms must be 1 if the property is a bedsit"]], ["proptype", ["Answer cannot be 'Bedsit' if the property has 2 or more bedrooms"]]])
end
end
end
context "when UI" do

Loading…
Cancel
Save