Browse Source

Move specs to shared spec file

pull/1620/head
Jack S 3 years ago
parent
commit
c6ed316ecb
  1. 58
      spec/models/lettings_log_spec.rb
  2. 59
      spec/models/sales_log_spec.rb
  3. 58
      spec/shared/shared_log_examples.rb

58
spec/models/lettings_log_spec.rb

@ -3,7 +3,6 @@ require "shared/shared_examples_for_derived_fields"
require "shared/shared_log_examples"
# rubocop:disable RSpec/MessageChain
# rubocop:disable RSpec/AnyInstance
RSpec.describe LettingsLog do
let(:different_managing_organisation) { create(:organisation) }
let(:created_by_user) { create(:user) }
@ -2991,62 +2990,6 @@ RSpec.describe LettingsLog do
end
end
describe "#process_uprn_change!" do
context "when UPRN set to a value" do
let(:lettings_log) do
create(
:lettings_log,
uprn: "123456789",
uprn_confirmed: 1,
county: "county",
)
end
it "updates sales log fields" do
lettings_log.uprn = "1111111"
allow_any_instance_of(UprnClient).to receive(:call)
allow_any_instance_of(UprnClient).to receive(:result).and_return({
"UPRN" => "UPRN",
"UDPRN" => "UDPRN",
"ADDRESS" => "full address",
"SUB_BUILDING_NAME" => "0",
"BUILDING_NAME" => "building name",
"THOROUGHFARE_NAME" => "thoroughfare",
"POST_TOWN" => "posttown",
"POSTCODE" => "postcode",
})
expect { lettings_log.process_uprn_change! }.to change(lettings_log, :address_line1).from(nil).to("0, Building Name, Thoroughfare")
.and change(lettings_log, :town_or_city).from(nil).to("Posttown")
.and change(lettings_log, :postcode_full).from(nil).to("POSTCODE")
.and change(lettings_log, :uprn_confirmed).from(1).to(nil)
.and change(lettings_log, :county).from("county").to(nil)
.and change(lettings_log, :uprn_known).from(nil).to(1)
end
end
context "when UPRN nil" do
let(:lettings_log) { create(:lettings_log, uprn: nil) }
it "does not update sales log" do
expect { lettings_log.process_uprn_change! }.not_to change(lettings_log, :attributes)
end
end
context "when service errors" do
let(:lettings_log) { create(:lettings_log, uprn: "123456789", uprn_confirmed: 1) }
let(:error_message) { "error" }
it "adds error to sales log" do
allow_any_instance_of(UprnClient).to receive(:call)
allow_any_instance_of(UprnClient).to receive(:error).and_return(error_message)
expect { lettings_log.process_uprn_change! }.to change { lettings_log.errors[:uprn] }.from([]).to([error_message])
end
end
end
describe "#beds_for_la_rent_range" do
context "when beds nil" do
let(:lettings_log) { build(:lettings_log, beds: nil) }
@ -3122,5 +3065,4 @@ RSpec.describe LettingsLog do
end
end
end
# rubocop:enable RSpec/AnyInstance
# rubocop:enable RSpec/MessageChain

59
spec/models/sales_log_spec.rb

@ -3,7 +3,6 @@ require "shared/shared_examples_for_derived_fields"
require "shared/shared_log_examples"
# rubocop:disable RSpec/MessageChain
# rubocop:disable RSpec/AnyInstance
RSpec.describe SalesLog, type: :model do
let(:owning_organisation) { create(:organisation) }
let(:created_by_user) { create(:user) }
@ -575,63 +574,6 @@ RSpec.describe SalesLog, type: :model do
end
end
describe "#process_uprn_change!" do
context "when UPRN set to a value" do
let(:sales_log) do
create(
:sales_log,
uprn: "123456789",
uprn_confirmed: 1,
county: "county",
)
end
it "updates sales log fields" do
sales_log.uprn = "1111111"
sales_log.uprn_confirmed = 1
allow_any_instance_of(UprnClient).to receive(:call)
allow_any_instance_of(UprnClient).to receive(:result).and_return({
"UPRN" => "UPRN",
"UDPRN" => "UDPRN",
"ADDRESS" => "full address",
"SUB_BUILDING_NAME" => "0",
"BUILDING_NAME" => "building name",
"THOROUGHFARE_NAME" => "thoroughfare",
"POST_TOWN" => "posttown",
"POSTCODE" => "postcode",
})
expect { sales_log.process_uprn_change! }.to change(sales_log, :address_line1).from(nil).to("0, Building Name, Thoroughfare")
.and change(sales_log, :town_or_city).from(nil).to("Posttown")
.and change(sales_log, :postcode_full).from(nil).to("POSTCODE")
.and change(sales_log, :uprn_confirmed).from(1).to(nil)
.and change(sales_log, :county).from("county").to(nil)
.and change(sales_log, :uprn_known).from(nil).to(1)
end
end
context "when UPRN nil" do
let(:sales_log) { create(:sales_log, uprn: nil) }
it "does not update sales log" do
expect { sales_log.process_uprn_change! }.not_to change(sales_log, :attributes)
end
end
context "when the API returns an error" do
let(:sales_log) { build(:sales_log, :outright_sale_setup_complete, uprn_known: 1, uprn: "123456789", uprn_confirmed: 1) }
let(:error_message) { "error" }
it "adds error to sales log" do
allow_any_instance_of(UprnClient).to receive(:call)
allow_any_instance_of(UprnClient).to receive(:error).and_return(error_message)
expect { sales_log.process_uprn_change! }.to change { sales_log.errors[:uprn] }.from([]).to([error_message])
end
end
end
describe "#beds_for_la_sale_range" do
context "when beds nil" do
let(:sales_log) { build(:sales_log, beds: nil) }
@ -707,5 +649,4 @@ RSpec.describe SalesLog, type: :model do
end
end
end
# rubocop:enable RSpec/AnyInstance
# rubocop:enable RSpec/MessageChain

58
spec/shared/shared_log_examples.rb

@ -1,5 +1,6 @@
require "rails_helper"
# rubocop:disable RSpec/AnyInstance
RSpec.shared_examples "shared log examples" do |log_type|
describe "status" do
let(:empty_log) { create(log_type) }
@ -46,4 +47,61 @@ RSpec.shared_examples "shared log examples" do |log_type|
expect { log.discard! }.to change { log.reload.status }.to("deleted")
end
end
describe "#process_uprn_change!" do
context "when UPRN set to a value" do
let(:log) do
create(
log_type,
uprn: "123456789",
uprn_confirmed: 1,
county: "county",
)
end
it "updates log fields" do
log.uprn = "1111111"
allow_any_instance_of(UprnClient).to receive(:call)
allow_any_instance_of(UprnClient).to receive(:result).and_return({
"UPRN" => "UPRN",
"UDPRN" => "UDPRN",
"ADDRESS" => "full address",
"SUB_BUILDING_NAME" => "0",
"BUILDING_NAME" => "building name",
"THOROUGHFARE_NAME" => "thoroughfare",
"POST_TOWN" => "posttown",
"POSTCODE" => "postcode",
})
expect { log.process_uprn_change! }.to change(log, :address_line1).from(nil).to("0, Building Name, Thoroughfare")
.and change(log, :town_or_city).from(nil).to("Posttown")
.and change(log, :postcode_full).from(nil).to("POSTCODE")
.and change(log, :uprn_confirmed).from(1).to(nil)
.and change(log, :county).from("county").to(nil)
.and change(log, :uprn_known).from(nil).to(1)
end
end
context "when UPRN nil" do
let(:log) { create(log_type, uprn: nil) }
it "does not update log" do
expect { log.process_uprn_change! }.not_to change(log, :attributes)
end
end
context "when service errors" do
let(:log) { create(log_type, uprn: "123456789", uprn_confirmed: 1) }
let(:error_message) { "error" }
it "adds error to log" do
allow_any_instance_of(UprnClient).to receive(:call)
allow_any_instance_of(UprnClient).to receive(:error).and_return(error_message)
expect { log.process_uprn_change! }.to change { log.errors[:uprn] }.from([]).to([error_message])
end
end
end
end
# rubocop:enable RSpec/AnyInstance

Loading…
Cancel
Save