Browse Source

Update address search results (#2981)

* Update address search results

* Add search controller tests

* Check if the query is nil when using the address-search api

* Refactor

---------

Co-authored-by: Manny Dinssa <44172848+Dinssa@users.noreply.github.com>
CLDC-3788-export-sales-logs^2 v0.5.1
kosiakkatrina 4 days ago committed by GitHub
parent
commit
11d9705fdd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      app/controllers/address_search_controller.rb
  2. 40
      spec/requests/address_search_controller_spec.rb

6
app/controllers/address_search_controller.rb

@ -5,7 +5,9 @@ class AddressSearchController < ApplicationController
def index def index
query = params[:query] query = params[:query]
if query.match?(/\A\d+\z/) && query.length > 5 if query.nil?
render json: { error: "Query cannot be blank." }, status: :bad_request
elsif query.match?(/\A\d+\z/) && query.length > 5
# Query is all numbers and greater than 5 digits, assume it's a UPRN # Query is all numbers and greater than 5 digits, assume it's a UPRN
service = UprnClient.new(query) service = UprnClient.new(query)
service.call service.call
@ -38,7 +40,7 @@ class AddressSearchController < ApplicationController
address_service.call address_service.call
uprn_service.call uprn_service.call
results = ([uprn_service.result] || []) + (address_service.result || []) results = [uprn_service.result, *address_service.result].compact
if address_service.error.present? && uprn_service.error.present? if address_service.error.present? && uprn_service.error.present?
render json: { error: "Address and UPRN are not recognised." }, status: :not_found render json: { error: "Address and UPRN are not recognised." }, status: :not_found

40
spec/requests/address_search_controller_spec.rb

@ -144,5 +144,45 @@ RSpec.describe AddressSearchController, type: :request do
expect(sales_log.la).to eq(nil) expect(sales_log.la).to eq(nil)
end end
end end
context "when searching by address and UPRN" do
let(:sales_log) { create(:sales_log, :completed, manual_address_entry_selected: false, assigned_to: user) }
context "and theres no uprn returned" do
before do
body = { results: [{ DPA: { "ADDRESS": "1, Test Street", "UPRN": "123" } }] }.to_json
uprn_body = { results: [{ DPA: nil }] }.to_json
WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/find?key=OS_DATA_KEY&maxresults=10&minmatch=0.2&query=100")
.to_return(status: 200, body:, headers: {})
WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=100")
.to_return(status: 200, body: uprn_body, headers: {})
end
it "returns the address results" do
get "/address-search?query=100"
expect(response).to have_http_status(:ok)
expect(response.body).to eq([{ text: "1, Test Street", value: "123" }].to_json)
end
end
context "and theres no address returned" do
before do
body = { results: [{ DPA: nil }] }.to_json
uprn_body = { results: [{ DPA: { "ADDRESS": "2, Test Street", UPRN: "321" } }] }.to_json
WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/find?key=OS_DATA_KEY&maxresults=10&minmatch=0.2&query=100")
.to_return(status: 200, body:, headers: {})
WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=100")
.to_return(status: 200, body: uprn_body, headers: {})
end
it "returns the address results" do
get "/address-search?query=100"
expect(response).to have_http_status(:ok)
expect(response.body).to eq([{ text: "2, Test Street", value: "321" }].to_json)
end
end
end
end end
end end

Loading…
Cancel
Save