From fbad7d837f4da0e6421013998fb7a8afc7bce1d2 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 25 Feb 2022 17:59:51 +0000 Subject: [PATCH] More tests --- app/models/case_log.rb | 19 ++--- spec/models/case_log_spec.rb | 118 +++++++++++++++++++++++++++++- spec/models/form/question_spec.rb | 12 +++ 3 files changed, 136 insertions(+), 13 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 5905be31c..6397a0e6b 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -123,6 +123,10 @@ class CaseLog < ApplicationRecord needstype == 1 end + def is_supported_housing? + !!(needstype && needstype.zero?) + end + def has_hbrentshortfall? !!(hbrentshortfall && hbrentshortfall.zero?) end @@ -378,24 +382,21 @@ private case RENT_TYPE_MAPPING_LABELS[renttype] when "Social Rent" - case needstype - when 0 + if is_supported_housing? owning_organisation[:provider_type] == "PRP" ? 2 : 4 - when 1 + elsif is_general_needs? owning_organisation[:provider_type] == "PRP" ? 1 : 3 end when "Affordable Rent" - case needstype - when 0 + if is_supported_housing? owning_organisation[:provider_type] == "PRP" ? 6 : 8 - when 1 + elsif is_general_needs? owning_organisation[:provider_type] == "PRP" ? 5 : 7 end when "Intermediate Rent" - case needstype - when 0 + if is_supported_housing? owning_organisation[:provider_type] == "PRP" ? 10 : 12 - when 1 + elsif is_general_needs? owning_organisation[:provider_type] == "PRP" ? 9 : 11 end end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 3c76b3365..ad7b255d7 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -273,10 +273,120 @@ RSpec.describe CaseLog do expect(record_from_db["renttype"]).to eq(3) end - it "correctly derives and saves lettype" do - record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] - expect(case_log.lettype).to eq(9) - expect(record_from_db["lettype"]).to eq(9) + context "when deriving lettype" do + context "when the owning organisation is a PRP" do + context "when the rent type is intermediate rent and supported housing" do + it "correctly derives and saves lettype" do + case_log.update!(rent_type: 4, needstype: 0) + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq(10) + expect(record_from_db["lettype"]).to eq(10) + end + end + + context "when the rent type is intermediate rent and general needs housing" do + it "correctly derives and saves lettype" do + case_log.update!(rent_type: 4, needstype: 1) + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq(9) + expect(record_from_db["lettype"]).to eq(9) + end + end + + context "when the rent type is affordable rent and supported housing" do + it "correctly derives and saves lettype" do + case_log.update!(rent_type: 2, needstype: 0) + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq(6) + expect(record_from_db["lettype"]).to eq(6) + end + end + + context "when the rent type is affordable rent and general needs housing" do + it "correctly derives and saves lettype" do + case_log.update!(rent_type: 2, needstype: 1) + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq(5) + expect(record_from_db["lettype"]).to eq(5) + end + end + + context "when the rent type is social rent and supported housing" do + it "correctly derives and saves lettype" do + case_log.update!(rent_type: 0, needstype: 0) + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq(2) + expect(record_from_db["lettype"]).to eq(2) + end + end + + context "when the rent type is social rent and general needs housing" do + it "correctly derives and saves lettype" do + case_log.update!(rent_type: 0, needstype: 1) + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq(1) + expect(record_from_db["lettype"]).to eq(1) + end + end + end + + context "when the owning organisation is an LA" do + let(:organisation) { FactoryBot.create(:organisation, provider_type: "LA") } + + context "when the rent type is intermediate rent and supported housing" do + it "correctly derives and saves lettype" do + case_log.update!(rent_type: 4, needstype: 0) + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq(12) + expect(record_from_db["lettype"]).to eq(12) + end + end + + context "when the rent type is intermediate rent and general needs housing" do + it "correctly derives and saves lettype" do + case_log.update!(rent_type: 4, needstype: 1) + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq(11) + expect(record_from_db["lettype"]).to eq(11) + end + end + + context "when the rent type is affordable rent and supported housing" do + it "correctly derives and saves lettype" do + case_log.update!(rent_type: 2, needstype: 0) + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq(8) + expect(record_from_db["lettype"]).to eq(8) + end + end + + context "when the rent type is affordable rent and general needs housing" do + it "correctly derives and saves lettype" do + case_log.update!(rent_type: 2, needstype: 1) + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq(7) + expect(record_from_db["lettype"]).to eq(7) + end + end + + context "when the rent type is social rent and supported housing" do + it "correctly derives and saves lettype" do + case_log.update!(rent_type: 0, needstype: 0) + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq(4) + expect(record_from_db["lettype"]).to eq(4) + end + end + + context "when the rent type is social rent and general needs housing" do + it "correctly derives and saves lettype" do + case_log.update!(rent_type: 0, needstype: 1) + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq(3) + expect(record_from_db["lettype"]).to eq(3) + end + end + end end it "correctly derives and saves day, month, year from start date" do diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb index d82293907..83fe9cc88 100644 --- a/spec/models/form/question_spec.rb +++ b/spec/models/form/question_spec.rb @@ -238,6 +238,18 @@ RSpec.describe Form::Question, type: :model do case_log.hb = "Housing benefit" expect(question.enabled?(case_log)).to be true end + + context "when the condition type hasn't been implemented yet" do + let(:unimplemented_question) { OpenStruct.new(id: "hb", type: "unkown") } + + before do + allow(page).to receive(:questions).and_return([unimplemented_question]) + end + + it "raises an exception" do + expect { question.enabled?(case_log) }.to raise_error("Not implemented yet") + end + end end context "when answers have a suffix dependent on another answer" do