From 4a5faf4154070ff0a2ce4d1b7d13f71a0c5f02a4 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 23 Feb 2022 11:15:47 +0000 Subject: [PATCH] Add a spec --- spec/models/case_log_spec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index f76bb8da8..d784c2fda 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -531,6 +531,29 @@ RSpec.describe CaseLog do expect(case_log["referral"]).to eq("Internal transfer") end end + + context "when it is not a renewal" do + let!(:case_log) do + described_class.create({ + managing_organisation: organisation, + owning_organisation: organisation, + renewal: "No", + year: 2021, + }) + end + + it "correctly derives and saves reasonpref when changed to renewal" do + case_log.update!({ reasonpref: "Yes" }) + record_from_db = ActiveRecord::Base.connection.execute("select reasonpref from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["reasonpref"]).to eq(1) + expect(case_log["reasonpref"]).to eq("Yes") + + case_log.update!({ renewal: "Yes" }) + record_from_db = ActiveRecord::Base.connection.execute("select reasonpref from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["reasonpref"]).to eq(nil) + expect(case_log["reasonpref"]).to eq(nil) + end + end end describe "resetting invalidated fields" do