Browse Source

fixed tests

pull/769/head
JG 3 years ago
parent
commit
e4396bab2d
  1. 3
      app/models/case_log.rb
  2. 11
      app/models/location.rb
  3. 24
      app/services/postcode_service.rb
  4. 2
      spec/services/imports/case_logs_import_service_spec.rb

3
app/models/case_log.rb

@ -603,7 +603,8 @@ private
end end
def get_inferred_la(postcode) def get_inferred_la(postcode)
PIO.infer_la(postcode) result = PIO.lookup(postcode)
result[:location_code] if result
end end
def get_has_benefits def get_has_benefits

11
app/models/location.rb

@ -3,7 +3,7 @@ class Location < ApplicationRecord
validates :units, :type_of_unit, :mobility_type, presence: true validates :units, :type_of_unit, :mobility_type, presence: true
belongs_to :scheme belongs_to :scheme
before_save :infer_la!, if: :postcode_changed? before_save :lookup_postcode!, if: :postcode_changed?
attr_accessor :add_another_location attr_accessor :add_another_location
@ -48,8 +48,11 @@ private
end end
end end
def infer_la! def lookup_postcode!
self.location_code = PIO.infer_la(postcode) result = PIO.lookup(postcode)
self.location_admin_district = PIO.infer_admin_district(postcode) if result
self.location_code = result[:location_code]
self.location_admin_district = result[:location_admin_district]
end
end end
end end

24
app/services/postcode_service.rb

@ -3,7 +3,7 @@ class PostcodeService
@pio = Postcodes::IO.new @pio = Postcodes::IO.new
end end
def infer_la(postcode) def lookup(postcode)
# Avoid network calls when postcode is invalid # Avoid network calls when postcode is invalid
return unless postcode.match(POSTCODE_REGEXP) return unless postcode.match(POSTCODE_REGEXP)
@ -16,24 +16,10 @@ class PostcodeService
Rails.logger.warn("Postcodes.io lookup timed out") Rails.logger.warn("Postcodes.io lookup timed out")
end end
if postcode_lookup && postcode_lookup.info.present? if postcode_lookup && postcode_lookup.info.present?
postcode_lookup.codes["admin_district"] {
end location_code: postcode_lookup.codes["admin_district"],
end location_admin_district: postcode_lookup&.admin_district,
}
def infer_admin_district(postcode)
# Avoid network calls when postcode is invalid
return unless postcode.match(POSTCODE_REGEXP)
postcode_lookup = nil
begin
# URI encoding only supports ASCII characters
ascii_postcode = self.class.clean(postcode)
Timeout.timeout(5) { postcode_lookup = @pio.lookup(ascii_postcode) }
rescue Timeout::Error
Rails.logger.warn("Postcodes.io lookup timed out")
end
if postcode_lookup && postcode_lookup.info.present?
postcode_lookup.admin_district
end end
end end

2
spec/services/imports/case_logs_import_service_spec.rb

@ -20,7 +20,7 @@ RSpec.describe Imports::CaseLogsImportService do
before do before do
WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/LS166FT/) WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/LS166FT/)
.to_return(status: 200, body: '{"status":200,"result":{"codes":{"admin_district":"E08000035"}}}', headers: {}) .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Westminster","codes":{"admin_district":"E08000035"}}}', headers: {})
allow(Organisation).to receive(:find_by).and_return(nil) allow(Organisation).to receive(:find_by).and_return(nil)
allow(Organisation).to receive(:find_by).with(old_visible_id: organisation.old_visible_id.to_i).and_return(organisation) allow(Organisation).to receive(:find_by).with(old_visible_id: organisation.old_visible_id.to_i).and_return(organisation)

Loading…
Cancel
Save