Browse Source

CLDC-3426: Refactor tests

pull/2399/head
Rachael Booth 2 years ago
parent
commit
0d45fb633e
  1. 4
      app/models/derived_variables/sales_log_variables.rb
  2. 5
      spec/factories/sales_log.rb
  3. 164
      spec/models/sales_log_derived_fields_spec.rb
  4. 170
      spec/models/sales_log_spec.rb

4
app/models/derived_variables/sales_log_variables.rb

@ -23,8 +23,8 @@ module DerivedVariables::SalesLogVariables
self.deposit = value self.deposit = value
elsif mortgage_use_unknown? elsif mortgage_use_unknown?
self.deposit = nil self.deposit = nil
elsif mortgageused_changed?(to: 1) elsif mortgageused_changed?(from: 2, to: 1)
# Clear when switching mortgage used to yes # Clear when switching mortgage used from no to yes
self.deposit = nil self.deposit = nil
end end
end end

5
spec/factories/sales_log.rb

@ -27,6 +27,8 @@ FactoryBot.define do
ownershipsch { 1 } ownershipsch { 1 }
type { 30 } type { 30 }
jointpur { 2 } jointpur { 2 }
noint { 2 }
privacynotice { 1 }
end end
trait :outright_sale_setup_complete do trait :outright_sale_setup_complete do
saledate_today saledate_today
@ -35,6 +37,9 @@ FactoryBot.define do
companybuy { 2 } companybuy { 2 }
buylivein { 1 } buylivein { 1 }
jointpur { 2 } jointpur { 2 }
noint { 2 }
privacynotice { 1 }
purchid { rand(999_999_999).to_s }
end end
trait :duplicate do trait :duplicate do
purchid { "PC123" } purchid { "PC123" }

164
spec/models/sales_log_derived_fields_spec.rb

@ -0,0 +1,164 @@
require "rails_helper"
require "shared/shared_examples_for_derived_fields"
RSpec.describe SalesLog, type: :model do
include_examples "shared examples for derived fields", :sales_log
describe "set_derived_fields!" do
it "correctly derives and saves exday, exmonth and exyear" do
log = build(:sales_log, exdate: Time.gm(2023, 5, 4))
expect { log.set_derived_fields! }.to change(log, :exday).from(nil).to(4)
.and change(log, :exmonth).from(nil).to(5)
.and change(log, :exyear).from(nil).to(2023)
end
it "correctly derives and saves pcode1 and pcode1 and pcode2" do
log = build(:sales_log, postcode_full: "W6 0SP")
expect { log.set_derived_fields! }.to change(log, :pcode1).from(nil).to("W6")
.and change(log, :pcode2).from(nil).to("0SP")
end
it "sets pregblank field when no buyer organisation is selected" do
log = build(:sales_log, pregyrha: 0, pregla: 0, pregghb: 0, pregother: 0)
expect { log.set_derived_fields! }.to change(log, :pregblank).from(nil).to(1)
end
%i[pregyrha pregla pregghb pregother].each do |field|
it "does not set pregblank field when #{field} is selected" do
log = build(:sales_log, pregyrha: 0, pregla: 0, pregghb: 0, pregother: 0)
log[field] = 1
expect { log.set_derived_fields! }.to not_change(log, :pregblank)
end
end
it "correctly derives nationality_all/nationality_all_buyer2 when _group is UK" do
log = build(:sales_log, nationality_all_group: 826, nationality_all_buyer2_group: 826)
expect { log.set_derived_fields! }.to change(log, :nationality_all).from(nil).to(826)
.and change(log, :nationality_all_buyer2).from(nil).to(826)
end
it "correctly derives nationality_all/nationality_all_buyer2 when buyer prefers not to say" do
log = build(:sales_log, nationality_all_group: 0, nationality_all_buyer2_group: 0)
expect { log.set_derived_fields! }.to change(log, :nationality_all).from(nil).to(0)
.and change(log, :nationality_all_buyer2).from(nil).to(0)
end
it "does not derive nationality_all/nationality_all_buyer2 when it is other" do
log = build(:sales_log, nationality_all_group: 12, nationality_all_buyer2_group: 12)
expect { log.set_derived_fields! }.to not_change(log, :nationality_all)
.and not_change(log, :nationality_all_buyer2)
end
it "does not derive nationality_all/nationality_all_buyer2 when it is not given" do
log = build(:sales_log, nationality_all_group: nil, nationality_all_buyer2_group: nil)
expect { log.set_derived_fields! }.to not_change(log, :nationality_all)
.and not_change(log, :nationality_all_buyer2)
end
it "derives a mortgage value of 0 when mortgage is not used" do
log = build(:sales_log, mortgage: 100_000, mortgageused: 2)
expect { log.set_derived_fields! }.to change(log, :mortgage).from(100_000).to(0)
end
it "clears mortgage value if mortgage used is changed from no to yes" do
log = create(:sales_log, :completed, mortgageused: 2, grant: nil)
log.mortgageused = 1
expect { log.set_derived_fields! }.to change(log, :mortgage).from(0).to(nil)
end
it "clears mortgage value if mortgage used is changed from no to don't know" do
log = create(:sales_log, :outright_sale_setup_complete, mortgage: 0, mortgageused: 2)
log.mortgageused = 3
expect { log.set_derived_fields! }.to change(log, :mortgage).from(0).to(nil)
end
it "clears mortgage value if mortgage used is changed from yes to don't know" do
log = create(:sales_log, :outright_sale_setup_complete, mortgage: 50_000, mortgageused: 1)
log.mortgageused = 3
expect { log.set_derived_fields! }.to change(log, :mortgage).from(50_000).to(nil)
end
context "with a log that is not outright sales" do
it "does not derive deposit when mortgage used is no" do
log = build(:sales_log, :shared_ownership_setup_complete, value: 123_400, deposit: nil, mortgageused: 2)
expect { log.set_derived_fields! }.to not_change(log, :deposit)
end
end
context "with an outright sales log" do
it "derives deposit as the value when mortgage used is no" do
log = build(:sales_log, :outright_sale_setup_complete, value: 123_400, deposit: nil, mortgageused: 2)
expect { log.set_derived_fields! }.to change(log, :deposit).from(nil).to(123_400)
end
it "does not derive deposit when mortgage used is yes" do
log = build(:sales_log, :outright_sale_setup_complete, value: 123_400, deposit: nil, mortgageused: 1)
expect { log.set_derived_fields! }.to not_change(log, :deposit)
end
it "sets deposit to nil when mortgage used is don't know" do
log = build(:sales_log, :outright_sale_setup_complete, value: 123_400, deposit: 0, mortgageused: 3)
expect { log.set_derived_fields! }.to change(log, :deposit).from(0).to(nil)
end
it "clears derived deposit when setting mortgage used to yes" do
log = create(:sales_log, :outright_sale_setup_complete, value: 123_400, deposit: 123_400, mortgageused: 2)
log.mortgageused = 1
expect { log.set_derived_fields! }.to change(log, :deposit).from(123_400).to(nil)
end
context "when buyers will live in the property" do
context "and the sale is not a joint purchase" do
it "derives that buyer 1 will live in the property" do
log = build(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2)
expect { log.set_derived_fields! }.to change(log, :buy1livein).from(nil).to(1)
end
it "does not derive a value for whether buyer 2 will live in the property" do
log = build(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2)
log.set_derived_fields!
expect(log.buy2livein).to be_nil
end
it "clears that buyer 1 will live in the property if joint purchase is updated" do
log = create(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2)
log.jointpur = 1
expect { log.set_derived_fields! }.to change(log, :buy1livein).from(1).to(nil)
end
end
context "and the sale is a joint purchase" do
it "does not derive values for whether buyer 1 or buyer 2 will live in the property" do
log = build(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 1)
log.set_derived_fields!
expect(log.buy1livein).to be_nil
expect(log.buy2livein).to be_nil
end
end
end
context "when buyers will not live in the property" do
context "and the sale is not a joint purchase" do
it "derives that buyer 1 will not live in the property" do
log = build(:sales_log, :outright_sale_setup_complete, buylivein: 2, jointpur: 2)
expect { log.set_derived_fields! }.to change(log, :buy1livein).from(nil).to(2)
end
it "does not derive a value for whether buyer 2 will live in the property" do
log = build(:sales_log, :outright_sale_setup_complete, buylivein: 2, jointpur: 2)
log.set_derived_fields!
expect(log.buy2livein).to be_nil
end
end
context "and the sale is a joint purchase" do
it "derives that neither buyer 1 nor buyer 2 will live in the property" do
log = build(:sales_log, :outright_sale_setup_complete, buylivein: 2, jointpur: 1)
expect { log.set_derived_fields! }.to change(log, :buy1livein).from(nil).to(2)
.and change(log, :buy2livein).from(nil).to(2)
end
end
end
end
end
end

170
spec/models/sales_log_spec.rb

@ -1,5 +1,4 @@
require "rails_helper" require "rails_helper"
require "shared/shared_examples_for_derived_fields"
require "shared/shared_log_examples" require "shared/shared_log_examples"
# rubocop:disable RSpec/MessageChain # rubocop:disable RSpec/MessageChain
@ -17,7 +16,6 @@ RSpec.describe SalesLog, type: :model do
Singleton.__init__(FormHandler) Singleton.__init__(FormHandler)
end end
include_examples "shared examples for derived fields", :sales_log
include_examples "shared log examples", :sales_log include_examples "shared log examples", :sales_log
it "inherits from log" do it "inherits from log" do
@ -535,174 +533,6 @@ RSpec.describe SalesLog, type: :model do
end end
end end
describe "derived variables" do
let(:sales_log) { create(:sales_log, :completed) }
before do
Timecop.return
Singleton.__init__(FormHandler)
end
it "correctly derives and saves exday, exmonth and exyear" do
sales_log.update!(exdate: Time.gm(2023, 5, 4), saledate: Time.gm(2023, 7, 4), ownershipsch: 1, type: 18, staircase: 2, resale: 2, proplen: 0)
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["exday"]).to eq(4)
expect(record_from_db["exmonth"]).to eq(5)
expect(record_from_db["exyear"]).to eq(2023)
end
it "correctly derives and saves deposit for outright sales when no mortgage is used" do
sales_log.update!(value: 123_400, deposit: nil, mortgageused: 2, ownershipsch: 3, type: 10, companybuy: 1, jointpur: 1, jointmore: 1)
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["deposit"]).to eq(123_400)
end
it "does not derive deposit if the sale isn't outright" do
sales_log.update!(value: 123_400, deposit: nil, mortgageused: 2, ownershipsch: 2)
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["deposit"]).to eq(nil)
end
it "does not derive deposit if the mortgage is used" do
sales_log.update!(value: 123_400, deposit: nil, mortgageused: 1, ownershipsch: 3, type: 10, companybuy: 1, jointpur: 1, jointmore: 1)
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["deposit"]).to eq(nil)
end
it "derives deposit as nil if the mortgage use is unknown" do
sales_log.update!(value: 123_400, deposit: 0, saledate: Time.zone.local(2024, 5, 2), mortgageused: 3, ownershipsch: 3, type: 10, companybuy: 1, jointpur: 1, jointmore: 1)
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["deposit"]).to eq(nil)
end
it "clears deposit when setting mortgage used to yes for outright sales" do
sales_log.update!(value: 123_400, deposit: 123_400, mortgageused: 2, ownershipsch: 3, type: 10, companybuy: 1, jointpur: 1, jointmore: 1)
sales_log.update!(mortgageused: 1)
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["deposit"]).to eq(nil)
end
it "does not clear deposit when mortgage used is not changed" do
sales_log.update!(value: 125_000, deposit: 25_000, mortgageused: 1, mortgage: 100_000, ownershipsch: 3, type: 10, companybuy: 1, jointpur: 1, jointmore: 1)
sales_log.update!(mortgageused: 1)
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["deposit"]).to eq(25_000)
end
it "correctly derives and saves pcode1 and pcode1 and pcode2" do
sales_log.update!(postcode_full: "W6 0SP")
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["pcode1"]).to eq("W6")
expect(record_from_db["pcode2"]).to eq("0SP")
end
it "derives a mortgage value of 0 when mortgage is not used" do
# to avoid log failing validations when mortgage value is removed:
sales_log.update!(mortgage: 100_000, grant: nil, deposit: nil)
sales_log.update!(mortgageused: 2)
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["mortgage"]).to eq(0.0)
end
it "clears mortgage value if mortgage used is changed from no to yes" do
sales_log.update!(mortgageused: 2, grant: nil)
sales_log.update!(mortgageused: 1)
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["mortgage"]).to eq(nil)
end
context "when outright sale and buyers will live in the property" do
let(:sales_log) { create(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur:) }
context "and the sale is not a joint purchase" do
let(:jointpur) { 2 }
it "derives that buyer 1 will live in the property" do
expect(sales_log.buy1livein).to be 1
end
it "does not derive a value for whether buyer 2 will live in the property" do
expect(sales_log.buy2livein).to be nil
end
it "clears that buyer 1 will live in the property if joint purchase is updated" do
sales_log.update!(jointpur: 1)
expect(sales_log.buy1livein).to be nil
end
end
context "and the sale is a joint purchase" do
let(:jointpur) { 1 }
it "does not derive values for whether buyer 1 or buyer 2 will live in the property" do
expect(sales_log.buy1livein).to be nil
expect(sales_log.buy2livein).to be nil
end
end
end
context "when outright sale and buyers will not live in the property" do
let(:sales_log) { create(:sales_log, :outright_sale_setup_complete, buylivein: 2, jointpur:) }
context "and the sale is not a joint purchase" do
let(:jointpur) { 2 }
it "derives that buyer 1 will not live in the property" do
expect(sales_log.buy1livein).to be 2
end
it "does not derive a value for whether buyer 2 will live in the property" do
expect(sales_log.buy2livein).to be nil
end
end
context "and the sale is a joint purchase" do
let(:jointpur) { 1 }
it "derives that neither buyer 1 nor buyer 2 will live in the property" do
expect(sales_log.buy1livein).to be 2
expect(sales_log.buy2livein).to be 2
end
end
end
context "when deriving nationality variables" do
it "correctly derives nationality_all/nationality_all_buyer2 when it's UK" do
expect { sales_log.update!(nationality_all_group: 826) }.to change(sales_log, :nationality_all).to 826
expect { sales_log.update!(nationality_all_buyer2_group: 826) }.to change(sales_log, :nationality_all_buyer2).to 826
end
it "correctly derives nationality_all/nationality_all_buyer2 when buyer prefers not to say" do
expect { sales_log.update!(nationality_all_group: 0) }.to change(sales_log, :nationality_all).to 0
expect { sales_log.update!(nationality_all_buyer2_group: 0) }.to change(sales_log, :nationality_all_buyer2).to 0
end
it "does not derive nationality_all/nationality_all_buyer2 when it is other or not given" do
expect { sales_log.update!(nationality_all_group: 12) }.not_to change(sales_log, :nationality_all)
expect { sales_log.update!(nationality_all_buyer2_group: 12) }.not_to change(sales_log, :nationality_all_buyer2)
expect { sales_log.update!(nationality_all_group: nil) }.not_to change(sales_log, :nationality_all)
expect { sales_log.update!(nationality_all_buyer2_group: nil) }.not_to change(sales_log, :nationality_all_buyer2)
end
end
it "sets pregblank field when no buyer organisation is selected" do
expect {
sales_log.update!(pregyrha: 0,
pregla: 0,
pregghb: 0,
pregother: 0)
}.to change(sales_log, :pregblank).to 1
end
%i[pregyrha pregla pregghb pregother].each do |field|
it "does not set pregblank field when #{field} is selected" do
expect {
sales_log.update!({ field => 1 })
}.not_to change(sales_log, :pregblank)
end
end
end
context "when saving addresses" do context "when saving addresses" do
before do before do
stub_request(:get, /api.postcodes.io/) stub_request(:get, /api.postcodes.io/)

Loading…
Cancel
Save