Browse Source

Cldc 1388 Show local authority for location (#769)

* failing test to display local authority

* passing tests - sadly with not allowed net connections

* added relevant tests and tweaked the webmock

* shorter output from api

* location_admin_district for location

* location_admin_district for location

* fixed tests
pull/776/head
J G 2 years ago committed by GitHub
parent
commit
3a6dfaa30d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      app/models/case_log.rb
  2. 10
      app/models/location.rb
  3. 7
      app/services/postcode_service.rb
  4. 4
      app/views/locations/index.html.erb
  5. 4
      app/views/schemes/check_answers.html.erb
  6. 5
      db/migrate/20220722083835_add_location_admin_district_to_locations.rb
  7. 3
      db/schema.rb
  8. 2
      spec/features/schemes_helpers.rb
  9. 7
      spec/features/schemes_spec.rb
  10. 4
      spec/request_helper.rb
  11. 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

10
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
@ -52,7 +52,11 @@ private
end end
end end
def infer_la! def lookup_postcode!
self.location_code = PIO.infer_la(postcode) result = PIO.lookup(postcode)
if result
self.location_code = result[:location_code]
self.location_admin_district = result[:location_admin_district]
end
end end
end end

7
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,7 +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"] {
location_code: postcode_lookup.codes["admin_district"],
location_admin_district: postcode_lookup&.admin_district,
}
end end
end end

4
app/views/locations/index.html.erb

@ -38,6 +38,9 @@
<% row.cell(header: true, text: "Mobility type", html_attributes: { <% row.cell(header: true, text: "Mobility type", html_attributes: {
scope: "col", scope: "col",
}) %> }) %>
<% row.cell(header: true, text: "Local authority", html_attributes: {
scope: "col",
}) %>
<% end %> <% end %>
<% end %> <% end %>
<% @locations.each do |location| %> <% @locations.each do |location| %>
@ -48,6 +51,7 @@
<% row.cell(text: location.units) %> <% row.cell(text: location.units) %>
<% row.cell(text: simple_format("<span>#{location.type_of_unit}</span>")) %> <% row.cell(text: simple_format("<span>#{location.type_of_unit}</span>")) %>
<% row.cell(text: location.mobility_type) %> <% row.cell(text: location.mobility_type) %>
<% row.cell(text: location.location_admin_district) %>
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>

4
app/views/schemes/check_answers.html.erb

@ -105,6 +105,9 @@
<% row.cell(header: true, text: "Mobility type", html_attributes: { <% row.cell(header: true, text: "Mobility type", html_attributes: {
scope: "col", scope: "col",
}) %> }) %>
<% row.cell(header: true, text: "Local authority", html_attributes: {
scope: "col",
}) %>
<% end %> <% end %>
<% end %> <% end %>
<% @scheme.locations.each do |location| %> <% @scheme.locations.each do |location| %>
@ -115,6 +118,7 @@
<% row.cell(text: location.units) %> <% row.cell(text: location.units) %>
<% row.cell(text: simple_format("<span>#{location.type_of_unit}</span>")) %> <% row.cell(text: simple_format("<span>#{location.type_of_unit}</span>")) %>
<% row.cell(text: location.mobility_type) %> <% row.cell(text: location.mobility_type) %>
<% row.cell(text: location.location_admin_district) %>
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>

5
db/migrate/20220722083835_add_location_admin_district_to_locations.rb

@ -0,0 +1,5 @@
class AddLocationAdminDistrictToLocations < ActiveRecord::Migration[7.0]
def change
add_column :locations, :location_admin_district, :string
end
end

3
db/schema.rb

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_07_20_111635) do ActiveRecord::Schema[7.0].define(version: 2022_07_22_083835) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -250,6 +250,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_20_111635) do
t.integer "old_visible_id" t.integer "old_visible_id"
t.string "mobility_type" t.string "mobility_type"
t.datetime "startdate", precision: nil t.datetime "startdate", precision: nil
t.string "location_admin_district"
t.index ["old_id"], name: "index_locations_on_old_id", unique: true t.index ["old_id"], name: "index_locations_on_old_id", unique: true
t.index ["scheme_id"], name: "index_locations_on_scheme_id" t.index ["scheme_id"], name: "index_locations_on_scheme_id"
end end

2
spec/features/schemes_helpers.rb

@ -55,7 +55,7 @@ module SchemesHelpers
end end
def fill_in_and_save_location def fill_in_and_save_location
fill_in "Postcode", with: "SW1P 4DF" fill_in "Postcode", with: "AA1 1AA"
fill_in "Location name (optional)", with: "Some name" fill_in "Location name (optional)", with: "Some name"
fill_in "Total number of units at this location", with: 1 fill_in "Total number of units at this location", with: 1
choose "Self-contained house" choose "Self-contained house"

7
spec/features/schemes_spec.rb

@ -175,7 +175,7 @@ RSpec.describe "Schemes scheme Features" do
context "when there are locations that belong to the selected scheme" do context "when there are locations that belong to the selected scheme" do
let!(:schemes) { FactoryBot.create_list(:scheme, 5) } let!(:schemes) { FactoryBot.create_list(:scheme, 5) }
let(:scheme) { schemes.first } let(:scheme) { schemes.first }
let!(:locations) { FactoryBot.create_list(:location, 3, scheme:) } let!(:locations) { FactoryBot.create_list(:location, 3, scheme:, postcode: "AA11AA") }
before do before do
visit("schemes") visit("schemes")
@ -199,6 +199,7 @@ RSpec.describe "Schemes scheme Features" do
expect(page).to have_content(location.units) expect(page).to have_content(location.units)
expect(page).to have_content(location.type_of_unit) expect(page).to have_content(location.type_of_unit)
expect(page).to have_content(location.mobility_type) expect(page).to have_content(location.mobility_type)
expect(page).to have_content(location.location_admin_district)
end end
end end
end end
@ -468,10 +469,12 @@ RSpec.describe "Schemes scheme Features" do
end end
it "displays information about the first created location" do it "displays information about the first created location" do
expect(page).to have_content "SW1P4DF" expect(page).to have_content "AA11AA"
expect(page).to have_content "Some name" expect(page).to have_content "Some name"
expect(page).to have_content "Self-contained house" expect(page).to have_content "Self-contained house"
expect(page).to have_content "None" expect(page).to have_content "None"
expect(page).to have_content "Local authority"
expect(page).to have_content "Westminster"
end end
it "displays information about another location" do it "displays information about another location" do

4
spec/request_helper.rb

@ -5,6 +5,10 @@ module RequestHelper
WebMock.disable_net_connect!(allow_localhost: true) WebMock.disable_net_connect!(allow_localhost: true)
WebMock.stub_request(:get, /api.postcodes.io/) WebMock.stub_request(:get, /api.postcodes.io/)
.to_return(status: 200, body: "{\"status\":404,\"error\":\"Postcode not found\"}", headers: {}) .to_return(status: 200, body: "{\"status\":404,\"error\":\"Postcode not found\"}", headers: {})
WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/AA11AA")
.to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 1AA\",\"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {})
WebMock.stub_request(:post, /api.notifications.service.gov.uk\/v2\/notifications\/email/) WebMock.stub_request(:post, /api.notifications.service.gov.uk\/v2\/notifications\/email/)
.to_return(status: 200, body: "", headers: {}) .to_return(status: 200, body: "", headers: {})
WebMock.stub_request(:post, /api.notifications.service.gov.uk\/v2\/notifications\/sms/) WebMock.stub_request(:post, /api.notifications.service.gov.uk\/v2\/notifications\/sms/)

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