Browse Source

Add compound tenancy validations to both fields

pull/296/head
baarkerlounger 3 years ago
parent
commit
c681dee10c
  1. 7
      app/models/validations/tenancy_validations.rb
  2. 55
      spec/models/case_log_spec.rb
  3. 92
      spec/models/validations/tenancy_validations_spec.rb

7
app/models/validations/tenancy_validations.rb

@ -12,7 +12,12 @@ module Validations::TenancyValidations
{ condition: is_secure && (!is_in_range && is_present), error: I18n.t("validations.tenancy.length.secure") },
]
conditions.each { |condition| condition[:condition] ? (record.errors.add :tenancylength, condition[:error]) : nil }
conditions.each do |condition|
next unless condition[:condition]
record.errors.add :tenancylength, condition[:error]
record.errors.add :tenancy, condition[:error]
end
end
def validate_other_tenancy_type(record)

55
spec/models/case_log_spec.rb

@ -397,7 +397,7 @@ RSpec.describe CaseLog do
end
end
context "when validaiting fixed term tenancy" do
context "when validating fixed term tenancy" do
it "Must not be completed if Type of main tenancy is not responded with either Secure or Assured shorthold " do
expect {
described_class.create!(tenancy: "Other",
@ -406,59 +406,6 @@ RSpec.describe CaseLog do
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Must be completed and between 2 and 99 if type of tenancy is Assured shorthold" do
expect {
described_class.create!(tenancy: "Assured Shorthold",
tenancylength: 1,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
described_class.create!(tenancy: "Assured Shorthold",
tenancylength: nil,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
described_class.create!(tenancy: "Assured Shorthold",
tenancylength: 2,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.not_to raise_error
end
it "Must be empty or between 2 and 99 if type of tenancy is Secure" do
expect {
described_class.create!(tenancy: "Secure (including flexible)",
tenancylength: 1,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
described_class.create!(tenancy: "Secure (including flexible)",
tenancylength: 100,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
described_class.create!(tenancy: "Secure (including flexible)",
tenancylength: nil,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.not_to raise_error
expect {
described_class.create!(tenancy: "Secure (including flexible)",
tenancylength: 2,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.not_to raise_error
end
end
context "when validating armed forces is active" do

92
spec/models/validations/tenancy_validations_spec.rb

@ -0,0 +1,92 @@
require "rails_helper"
RSpec.describe Validations::TenancyValidations do
subject(:tenancy_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::TenancyValidations } }
let(:record) { FactoryBot.create(:case_log) }
describe "#validate_fixed_term_tenancy" do
context "when fixed term tenancy" do
context "when type of tenancy is not assured or assured shorthold" do
let(:expected_error) { I18n.t("validations.tenancy.length.fixed_term_not_required") }
it "tenancy length should not be present" do
record.tenancy = "Other"
record.tenancylength = 10
subject.validate_fixed_term_tenancy(record)
expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error))
end
end
context "when type of tenancy is assured shorthold" do
let(:expected_error) { I18n.t("validations.tenancy.length.shorthold") }
context "when tenancy length is greater than 1" do
it "adds an error" do
record.tenancy = "Assured Shorthold"
record.tenancylength = 1
subject.validate_fixed_term_tenancy(record)
expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error))
end
end
context "when tenancy length is less than 100" do
it "adds an error" do
record.tenancy = "Assured Shorthold"
record.tenancylength = 100
subject.validate_fixed_term_tenancy(record)
expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error))
end
end
context "when tenancy length is between 2-99" do
it "adds an error" do
record.tenancy = "Assured Shorthold"
record.tenancylength = 3
subject.validate_fixed_term_tenancy(record)
expect(record.errors["tenancylength"]).to be_empty
expect(record.errors["tenancy"]).to be_empty
end
end
end
context "when type of tenancy is secure" do
let(:expected_error) { I18n.t("validations.tenancy.length.secure") }
context "when tenancy length is greater than 1" do
it "adds an error" do
record.tenancy = "Secure (including flexible)"
record.tenancylength = 1
subject.validate_fixed_term_tenancy(record)
expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error))
end
end
context "when tenancy length is less than 100" do
it "adds an error" do
record.tenancy = "Secure (including flexible)"
record.tenancylength = 100
subject.validate_fixed_term_tenancy(record)
expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error))
end
end
context "when tenancy length is between 2-99" do
it "adds an error" do
record.tenancy = "Secure (including flexible)"
record.tenancylength = 3
subject.validate_fixed_term_tenancy(record)
expect(record.errors["tenancylength"]).to be_empty
expect(record.errors["tenancy"]).to be_empty
end
end
end
end
end
end
Loading…
Cancel
Save