Browse Source

CLDC-494 key dates validation (#115)

* Add major repairs dates validation

* Add more key dates validations

* Add remaining validations

Co-authored-by: Dushan <dushan-madetech@users.noreply.github.com>

* Move validation tests from feature to model spec

Co-authored-by: Dushan <dushan-madetech@users.noreply.github.com>

* Fix dates for validation in complete case log fixture

Co-authored-by: Dushan <dushan-madetech@users.noreply.github.com>

* schema fix

* fix all tests 🙌

* lint fixes 😎

* recactor out a conditional method

Co-authored-by: Kat <katrina@madetech.com>
Co-authored-by: Dushan <dushan-madetech@users.noreply.github.com>
pull/116/head
Dushan 3 years ago committed by GitHub
parent
commit
9f332bcecd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      app/validations/date_validations.rb
  2. 15
      db/migrate/20211125154916_change_property_void_date_type.rb
  3. 4
      db/schema.rb
  4. 14
      spec/fixtures/complete_case_log.json
  5. 137
      spec/models/case_log_spec.rb
  6. 3
      spec/requests/case_log_controller_spec.rb

31
app/validations/date_validations.rb

@ -1,6 +1,31 @@
module DateValidations
def validate_property_major_repairs(record)
date_valid?("mrcdate", record)
if record["startdate"].present? && record["mrcdate"].present? && record["startdate"] < record["mrcdate"]
record.errors.add :mrcdate, "Major repairs date must be before the tenancy start date"
end
if is_rsnvac_first_let?(record) && record["mrcdate"].present?
record.errors.add :mrcdate, "Major repairs date must not be completed if the tenancy is first let"
end
if record["mrcdate"].present? && record["startdate"].present? && record["startdate"].to_date - record["mrcdate"].to_date > 730
record.errors.add :mrcdate, "Major repairs cannot be more than 730 days before the tenancy start date"
end
end
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 > 3650
record.errors.add :property_void_date, "Void date cannot be more than 730 days before the tenancy start date"
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
def validate_startdate(record)
@ -18,4 +43,10 @@ private
record.errors.add question, "Please enter a valid date"
end
end
def is_rsnvac_first_let?(record)
record["rsnvac"] == "First let of newbuild property" ||
record["rsnvac"] == "First let of conversion/rehabilitation/acquired property" ||
record["rsnvac"] == "First let of leased property"
end
end

15
db/migrate/20211125154916_change_property_void_date_type.rb

@ -0,0 +1,15 @@
class ChangePropertyVoidDateType < ActiveRecord::Migration[6.1]
def up
change_table :case_logs, bulk: true do |t|
t.remove :property_void_date
t.column :property_void_date, :datetime
end
end
def down
change_table :case_logs, bulk: true do |t|
t.remove :property_void_date
t.column :property_void_date, :string
end
end
end

4
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_11_25_114400) do
ActiveRecord::Schema.define(version: 2021_11_25_154916) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -83,7 +83,6 @@ ActiveRecord::Schema.define(version: 2021_11_25_114400) do
t.integer "rsnvac"
t.integer "unittype_gn"
t.integer "beds"
t.string "property_void_date"
t.integer "offered"
t.integer "wchair"
t.integer "earnings"
@ -166,6 +165,7 @@ ActiveRecord::Schema.define(version: 2021_11_25_114400) do
t.string "why_dont_you_know_la"
t.integer "unitletas"
t.integer "builtype"
t.datetime "property_void_date"
t.index ["discarded_at"], name: "index_case_logs_on_discarded_at"
end

14
spec/fixtures/complete_case_log.json vendored

@ -50,7 +50,7 @@
"accessibility_requirements": "No",
"condition_effects": "dummy",
"tenancy_code": "BZ757",
"startdate": "12/03/2019",
"startdate": "12/12/2020",
"startertenancy": "No",
"tenancylength": "5",
"tenancy": "Secure (including flexible)",
@ -59,16 +59,16 @@
"la": "Barnet",
"property_postcode": "NW1 5TY",
"property_relet": "No",
"rsnvac": "First let of newbuild property",
"rsnvac": "Renewal of fixed-term tenancy",
"property_reference": "P9876",
"unittype_gn": "House",
"property_building_type": "dummy",
"beds": 3,
"property_void_date": "03/11/2019",
"majorrepairs": "No",
"mrcdate": "05/05/2020",
"mrcday": 5,
"mrcmonth": 5,
"property_void_date": "10/10/2020",
"majorrepairs": "Yes",
"mrcdate": "11/11/2020",
"mrcday": 11,
"mrcmonth": 11,
"mrcyear": 2020,
"offered": 2,
"wchair": "Yes",

137
spec/models/case_log_spec.rb

@ -410,6 +410,143 @@ RSpec.describe Form, type: :model do
end
end
end
context "major repairs date" do
it "cannot be later than the tenancy start date" do
expect {
CaseLog.create!(
mrcdate: Date.new(2020, 10, 10),
startdate: Date.new(2020, 10, 9),
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(
mrcdate: Date.new(2020, 10, 9),
startdate: Date.new(2020, 10, 10),
)
}.not_to raise_error
end
it "must not be completed if reason for vacancy is first let" do
expect {
CaseLog.create!(
mrcdate: Date.new(2020, 10, 10),
rsnvac: "First let of newbuild property",
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(
mrcdate: Date.new(2020, 10, 10),
rsnvac: "First let of conversion/rehabilitation/acquired property",
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(
mrcdate: Date.new(2020, 10, 10),
rsnvac: "First let of leased property",
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "must have less than two years between the tenancy start date and major repairs date" do
expect {
CaseLog.create!(
startdate: Date.new(2020, 10, 10),
mrcdate: Date.new(2017, 10, 10),
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
context "void date" do
it "must have less than 10 years between the tenancy start date and void" do
expect {
CaseLog.create!(
startdate: Date.new(2020, 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)
expect {
CaseLog.create!(
startdate: Date.new(2020, 10, 10),
property_void_date: Date.new(2019, 10, 10),
)
}.not_to raise_error
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
context "Validate pregnancy questions" do
it "Cannot answer yes if no female tenants" do
expect {
CaseLog.create!(preg_occ: "Yes",
sex1: "Male",
age1: 20)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Cannot answer yes if no female tenants within age range" do
expect {
CaseLog.create!(preg_occ: "Yes",
sex1: "Female",
age1: 51)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Cannot answer prefer not to say if no valid tenants" do
expect {
CaseLog.create!(preg_occ: "Prefer not to say",
sex1: "Male",
age1: 20)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Can answer yes if valid tenants" do
expect {
CaseLog.create!(preg_occ: "Yes",
sex1: "Female",
age1: 20)
}.not_to raise_error
end
it "Can answer yes if valid second tenant" do
expect {
CaseLog.create!(preg_occ: "Yes",
sex1: "Male", age1: 99,
sex2: "Female",
age2: 20)
}.not_to raise_error
end
end
end
describe "status" do

3
spec/requests/case_log_controller_spec.rb

@ -84,6 +84,7 @@ RSpec.describe CaseLogsController, type: :request do
it "marks the record as completed" do
json_response = JSON.parse(response.body)
expect(json_response).not_to have_key("errors")
expect(json_response["status"]).to eq(completed)
end
end
@ -318,7 +319,7 @@ RSpec.describe CaseLogsController, type: :request do
case_log: {
page: page_id,
age1: answer,
age2: 2000
age2: 2000,
},
}
end

Loading…
Cancel
Save