diff --git a/app/validations/household_validations.rb b/app/validations/household_validations.rb index 8ff4e684a..769266748 100644 --- a/app/validations/household_validations.rb +++ b/app/validations/household_validations.rb @@ -56,6 +56,26 @@ module HouseholdValidations end 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 def women_of_child_bearing_age_in_household(record) diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 5e42ebce9..166f377b5 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 {