Browse Source

Reset partial postcodes after setting the main postcode field and add tests

pull/170/head
Kat 4 years ago
parent
commit
fb5220382c
  1. 11
      app/models/case_log.rb
  2. 33
      spec/models/case_log_spec.rb

11
app/models/case_log.rb

@ -190,10 +190,6 @@ private
end
def set_derived_fields
if property_postcode.present?
self.postcode = UKPostcode.parse(property_postcode).outcode
self.postcod2 = UKPostcode.parse(property_postcode).incode
end
if previous_postcode.present?
self.ppostc1 = UKPostcode.parse(previous_postcode).outcode
self.ppostc2 = UKPostcode.parse(previous_postcode).incode
@ -218,7 +214,12 @@ private
else
self.la = get_la(property_postcode)
end
if property_postcode.present?
self.postcode = UKPostcode.parse(property_postcode).outcode
self.postcod2 = UKPostcode.parse(property_postcode).incode
else
self.postcode = self.postcod2 = nil
end
self.totchild = get_totchild
self.totelder = get_totelder
self.totadult = get_totadult

33
spec/models/case_log_spec.rb

@ -1044,14 +1044,18 @@ RSpec.describe Form, type: :model do
expect(record_from_db["la"]).to eq("E08000003")
end
it "correctly resets all fields" do
it "correctly resets all fields if property postcode is empty" do
address_case_log.update!({ property_postcode: "" })
address_case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select la from case_logs where id=#{address_case_log.id}").to_a[0]
expect(address_case_log.la).to eq("Manchester")
expect(record_from_db["la"]).to eq("E08000003")
record_from_db = ActiveRecord::Base.connection.execute("select la, property_postcode from case_logs where id=#{address_case_log.id}").to_a[0]
expect(record_from_db["property_postcode"]).to eq(nil)
expect(address_case_log.la).to eq(nil)
expect(record_from_db["la"]).to eq(nil)
end
address_case_log.update!({ property_postcode: "" })
it "correctly resets all fields if property postcode not known" do
address_case_log.update!({ postcode_known: "No" })
address_case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select la, property_postcode from case_logs where id=#{address_case_log.id}").to_a[0]
@ -1059,6 +1063,25 @@ RSpec.describe Form, type: :model do
expect(address_case_log.la).to eq(nil)
expect(record_from_db["la"]).to eq(nil)
end
it "keeps the LA if property postcode changes from not known to known and not provided" do
address_case_log.update!({ postcode_known: "No" })
address_case_log.update!({ la: "Westminster" })
address_case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select la, property_postcode from case_logs where id=#{address_case_log.id}").to_a[0]
expect(record_from_db["property_postcode"]).to eq(nil)
expect(address_case_log.la).to eq("Westminster")
expect(record_from_db["la"]).to eq("E09000033")
address_case_log.update!({ postcode_known: "Yes" })
address_case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select la, property_postcode from case_logs where id=#{address_case_log.id}").to_a[0]
expect(record_from_db["property_postcode"]).to eq(nil)
expect(address_case_log.la).to eq("Westminster")
expect(record_from_db["la"]).to eq("E09000033")
end
end
context "household members derived vars" do

Loading…
Cancel
Save