diff --git a/app/validations/household_validations.rb b/app/validations/household_validations.rb index 45e881a4f..0ee9012a3 100644 --- a/app/validations/household_validations.rb +++ b/app/validations/household_validations.rb @@ -71,6 +71,26 @@ module HouseholdValidations def validate_person_1_economic(record) validate_person_age_matches_economic_status(record, 1) + end + + def validate_shared_housing_rooms(record) + unless record.property_unit_type.nil? + if record.property_unit_type == "Bed-sit" && record.property_number_of_bedrooms != 1 + record.errors.add :property_unit_type, "A bedsit can only have one bedroom" + end + + unless record.household_number_of_other_members.nil? + if record.household_number_of_other_members > 0 + if record.property_unit_type.include?("Shared") && !record.property_number_of_bedrooms.to_i.between?(1, 7) + record.errors.add :property_unit_type, "A shared house must have 1 to 7 bedrooms" + end + end + end + + if record.property_unit_type.include?("Shared") && !record.property_number_of_bedrooms.to_i.between?(1, 3) + record.errors.add :property_unit_type, "A shared house with less than two tenants must have 1 to 3 bedrooms" + end + end end private diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 80f45e64a..684488a96 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -97,6 +97,45 @@ RSpec.describe Form, type: :model do end end + context "Shared accomodation bedrooms validation" do + it "you must have more than zero bedrooms" do + expect { + CaseLog.create!(property_unit_type: "Shared house", + property_number_of_bedrooms: 0) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "you must answer less than 8 bedrooms" do + expect { + CaseLog.create!(property_unit_type: "Shared bungalow", + property_number_of_bedrooms: 8, + household_number_of_other_members: 1) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "you must answer less than 8 bedrooms" do + expect { + CaseLog.create!(property_unit_type: "Shared bungalow", + property_number_of_bedrooms: 4, + household_number_of_other_members: 0) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "A bedsit must only have one room" do + expect { + CaseLog.create!(property_unit_type: "Bed-sit", + property_number_of_bedrooms: 2) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "A bedsit must only have one room" do + expect { + CaseLog.create!(property_unit_type: "Bed-sit", + property_number_of_bedrooms: 0) + }.to raise_error(ActiveRecord::RecordInvalid) + end + end + context "outstanding rent or charges validation" do it "must be anwered if answered yes to outstanding rent or charges" do expect {