From 54bb2f91d75e3654f2b22b29272cc307f09c3022 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 30 Nov 2021 18:01:56 +0000 Subject: [PATCH] Update inferred fields in the db --- app/models/case_log.rb | 81 +++++-------------- spec/controllers/case_logs_controller_spec.rb | 49 ----------- spec/models/case_log_spec.rb | 75 ++++++++++++++--- 3 files changed, 84 insertions(+), 121 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 5d3608274..e30c7fa71 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -145,65 +145,38 @@ class CaseLog < ApplicationRecord end end - def postcode - if property_postcode.present? - UKPostcode.parse(property_postcode).outcode - end - end - - def postcod2 - if property_postcode.present? - UKPostcode.parse(property_postcode).incode - end - end - - def ppostc1 - if previous_postcode.present? - UKPostcode.parse(previous_postcode).outcode - end - end - - def ppostc2 - if previous_postcode.present? - UKPostcode.parse(previous_postcode).incode - end - end - - def hhmemb - other_hhmemb.presence - end - def applicable_income_range return unless ecstat1 IncomeRange::ALLOWED[ecstat1.to_sym] end - def mrcday - if mrcdate.present? - mrcdate.day - end - end +private - def mrcmonth - if mrcdate.present? - mrcdate.month - end + def update_status! + self.status = if all_fields_completed? && errors.empty? + "completed" + elsif all_fields_nil? + "not_started" + else + "in_progress" + end + set_derived_fields end - def mrcyear + def set_derived_fields + self.postcode = UKPostcode.parse(property_postcode).outcode if property_postcode.present? + self.postcod2 = UKPostcode.parse(property_postcode).incode if property_postcode.present? + self.ppostc1 = UKPostcode.parse(previous_postcode).outcode if previous_postcode.present? + self.ppostc2 = UKPostcode.parse(previous_postcode).incode if previous_postcode.present? if mrcdate.present? - mrcdate.year + self.mrcday = mrcdate.day + self.mrcmonth = mrcdate.month + self.mrcyear = mrcdate.year end - end + self.incref = 1 if net_income_known == "Prefer not to say" + self.hhmemb = other_hhmemb + 1 if other_hhmemb.present? - def incref - if net_income_known == "Prefer not to say" - 1 - end - end - - def renttype rent_type_mapping = { "Social Rent" => "Social Rent", "Affordable Rent" => "Affordable Rent", @@ -212,19 +185,7 @@ class CaseLog < ApplicationRecord "London Living Rent" => "Intermediate Rent", "Other Intermediate Rent Product" => "Intermediate Rent", } - rent_type_mapping[rent_type] - end - -private - - def update_status! - self.status = if all_fields_completed? && errors.empty? - "completed" - elsif all_fields_nil? - "not_started" - else - "in_progress" - end + self.renttype = rent_type_mapping[rent_type] end def all_fields_completed? diff --git a/spec/controllers/case_logs_controller_spec.rb b/spec/controllers/case_logs_controller_spec.rb index 43458e1b8..1c784eaa3 100644 --- a/spec/controllers/case_logs_controller_spec.rb +++ b/spec/controllers/case_logs_controller_spec.rb @@ -168,54 +168,5 @@ RSpec.describe CaseLogsController, type: :controller do expect(response).to redirect_to("/case_logs/#{id}/conditional_question_no_page") end end - - context "partition postcode" do - let(:case_log_with_postcode) do - { - property_postcode: "M1 1AE", - previous_postcode: "M2 2AE", - page: "property_postcode", - } - end - it "saves full and partial postcodes" do - post :submit_form, params: { id: id, case_log: case_log_with_postcode } - case_log.reload - - expect(case_log.property_postcode).to eq("M1 1AE") - expect(case_log.postcode).to eq("M1") - expect(case_log.postcod2).to eq("1AE") - end - - it "saves full and partial previous postcodes" do - post :submit_form, params: { id: id, case_log: case_log_with_postcode } - case_log.reload - - expect(case_log.previous_postcode).to eq("M2 2AE") - expect(case_log.ppostc1).to eq("M2") - expect(case_log.ppostc2).to eq("2AE") - end - end - - context "partition date" do - let(:case_log_with_date) do - { - "mrcdate(1i)": "2021", - "mrcdate(2i)": "05", - "mrcdate(3i)": "04", - page: "property_major_repairs", - } - end - it "saves full and partial dates" do - post :submit_form, params: { id: id, case_log: case_log_with_date } - case_log.reload - - expect(case_log.mrcdate.day).to eq(4) - expect(case_log.mrcdate.month).to eq(5) - expect(case_log.mrcdate.year).to eq(2021) - expect(case_log.mrcday).to eq(4) - expect(case_log.mrcmonth).to eq(5) - expect(case_log.mrcyear).to eq(2021) - end - end end end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index b0a987127..740b26bd6 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -816,14 +816,6 @@ RSpec.describe Form, type: :model do end end - describe "incref" do - let(:case_log) { FactoryBot.build(:case_log, net_income_known: "Prefer not to say") } - - it "sets income refused to Yes" do - expect(case_log.incref).to eq(1) - end - end - describe "weekly_net_income" do let(:net_income) { 5000 } let(:case_log) { FactoryBot.build(:case_log, earnings: net_income) } @@ -844,11 +836,70 @@ RSpec.describe Form, type: :model do end end - describe "inferred fields" do - let!(:case_log) { FactoryBot.create(:case_log, rent_type: "London Affordable Rent") } + describe "derived variables" do + require "date" + let(:organisation) { FactoryBot.create(:organisation) } + let!(:case_log) do + CaseLog.create({ + managing_organisation: organisation, + owning_organisation: organisation, + property_postcode: "M1 1AE", + previous_postcode: "M2 2AE", + mrcdate: Time.zone.local(2021, 5, 4), + net_income_known: "Prefer not to say", + other_hhmemb: 6, + rent_type: "London Living Rent", + }) + end + + it "correctly derives and saves partial and full postcodes" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select postcode, postcod2 from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["postcode"]).to eq("M1") + expect(record_from_db["postcod2"]).to eq("1AE") + end + + it "correctly derives and saves partial and full previous postcodes" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select ppostc1, ppostc2 from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["ppostc1"]).to eq("M2") + expect(record_from_db["ppostc2"]).to eq("2AE") + end + + it "correctly derives and saves partial and full major repairs date" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select mrcday, mrcmonth, mrcyear, mrcdate from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["mrcdate"].day).to eq(4) + expect(record_from_db["mrcdate"].month).to eq(5) + expect(record_from_db["mrcdate"].year).to eq(2021) + expect(record_from_db["mrcday"]).to eq(4) + expect(record_from_db["mrcmonth"]).to eq(5) + expect(record_from_db["mrcyear"]).to eq(2021) + end + + it "correctly derives and saves incref" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select incref from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["incref"]).to eq(1) + end + + it "correctly derives and saves hhmemb" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select hhmemb from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["hhmemb"]).to eq(7) + end + + it "correctly derives and saves renttype" do + case_log.reload - it "sets renttype correctly" do - expect(case_log.renttype).to eq("Affordable Rent") + record_from_db = ActiveRecord::Base.connection.execute("select renttype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.renttype).to eq("Intermediate Rent") + expect(record_from_db["renttype"]).to eq(3) end end end