Browse Source

Add remaining validations

Co-authored-by: Dushan <dushan-madetech@users.noreply.github.com>
pull/115/head
Kat 4 years ago
parent
commit
d9aaab3af0
  1. 10
      app/validations/date_validations.rb
  2. 2
      spec/fixtures/complete_case_log.json
  3. 32
      spec/models/case_log_spec.rb

10
app/validations/date_validations.rb

@ -17,9 +17,17 @@ module DateValidations
end end
def validate_property_void_date(record) def validate_property_void_date(record)
if record["property_void_date"].present? && record["startdate"].present? && record["startdate"].to_date - record["property_void_date"].to_date > 730 if record["property_void_date"].present? && record["startdate"].present? && record["startdate"].to_date - record["property_void_date"].to_date > 3650
record.errors.add :property_void_date, "Void date cannot be more than 730 days before the tenancy start date" record.errors.add :property_void_date, "Void date cannot be more than 730 days before the tenancy start date"
end end
if record["property_void_date"].present? && record["startdate"].present? && record["startdate"].to_date < record["property_void_date"].to_date
record.errors.add :property_void_date, "Void date must be before the tenancy start date"
end
if record["property_void_date"].present? && record["mrcdate"].present? && record["mrcdate"].to_date < record["property_void_date"].to_date
record.errors.add :property_void_date, "Void date must be after the major repair date if a major repair date has been provided"
end
end end
def validate_startdate(record) def validate_startdate(record)

2
spec/fixtures/complete_case_log.json vendored

@ -50,7 +50,7 @@
"accessibility_requirements": "No", "accessibility_requirements": "No",
"condition_effects": "dummy", "condition_effects": "dummy",
"tenancy_code": "BZ757", "tenancy_code": "BZ757",
"startdate": "12/03/2019", "startdate": "12/03/2020",
"startertenancy": "No", "startertenancy": "No",
"tenancylength": "5", "tenancylength": "5",
"tenancy": "Secure (including flexible)", "tenancy": "Secure (including flexible)",

32
spec/models/case_log_spec.rb

@ -451,7 +451,7 @@ RSpec.describe Form, type: :model do
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "must have less than two years between the tenancy start date and major cprepairs date" do it "must have less than two years between the tenancy start date and major repairs date" do
expect { expect {
CaseLog.create!( CaseLog.create!(
startdate: Date.new(2020, 10, 10), startdate: Date.new(2020, 10, 10),
@ -462,11 +462,27 @@ RSpec.describe Form, type: :model do
end end
context "void date" do context "void date" do
it "must have less than two years between the tenancy start date and void" do it "must have less than 10 years between the tenancy start date and void" do
expect { expect {
CaseLog.create!( CaseLog.create!(
startdate: Date.new(2020, 10, 10), startdate: Date.new(2020, 10, 10),
property_void_date: Date.new(2017, 10, 10), property_void_date: Date.new(2009, 10, 10),
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(
startdate: Date.new(2020, 10, 10),
property_void_date: Date.new(2015, 10, 10),
)
}.not_to raise_error
end
it "must be before the tenancy start date" do
expect {
CaseLog.create!(
startdate: Date.new(2020, 10, 10),
property_void_date: Date.new(2021, 10, 10),
) )
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
@ -477,6 +493,16 @@ RSpec.describe Form, type: :model do
) )
}.not_to raise_error }.not_to raise_error
end end
it "must be before major repairs date if major repairs date provided" do
expect {
CaseLog.create!(
startdate: Date.new(2020, 10, 10),
mrcdate: Date.new(2019, 10, 10),
property_void_date: Date.new(2019, 11, 11),
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
end end
end end

Loading…
Cancel
Save