Browse Source

Search both datasets for UPRN

pull/2479/head
Kat 2 years ago
parent
commit
97e09b8615
  1. 3
      app/services/uprn_client.rb
  2. 2
      spec/services/exports/lettings_log_export_service_spec.rb
  3. 26
      spec/services/uprn_client_spec.rb

3
app/services/uprn_client.rb

@ -20,7 +20,7 @@ class UprnClient
end end
def result def result
@result ||= JSON.parse(response.body).dig("results", 0, "DPA") @result ||= JSON.parse(response.body).dig("results", 0, "DPA") || JSON.parse(response.body).dig("results", 0, "LPI")
end end
private private
@ -39,6 +39,7 @@ private
params = { params = {
uprn:, uprn:,
key: ENV["OS_DATA_KEY"], key: ENV["OS_DATA_KEY"],
dataset: %w[DPA LPI].join(","),
} }
uri.query = URI.encode_www_form(params) uri.query = URI.encode_www_form(params)
uri.to_s uri.to_s

2
spec/services/exports/lettings_log_export_service_spec.rb

@ -181,7 +181,7 @@ RSpec.describe Exports::LettingsLogExportService do
before do before do
Timecop.freeze(Time.zone.local(2023, 4, 3)) Timecop.freeze(Time.zone.local(2023, 4, 3))
Singleton.__init__(FormHandler) Singleton.__init__(FormHandler)
stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=100023336956") stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=100023336956")
.to_return(status: 200, body: '{"status":200,"results":[{"DPA":{ .to_return(status: 200, body: '{"status":200,"results":[{"DPA":{
"PO_BOX_NUMBER": "fake", "PO_BOX_NUMBER": "fake",
"ORGANISATION_NAME": "org", "ORGANISATION_NAME": "org",

26
spec/services/uprn_client_spec.rb

@ -8,7 +8,7 @@ describe UprnClient do
end end
def stub_api_request(body:, status: 200) def stub_api_request(body:, status: 200)
stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=123") stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=123")
.to_return(status:, body:, headers: {}) .to_return(status:, body:, headers: {})
end end
@ -37,7 +37,28 @@ describe UprnClient do
end end
end end
context "when results empty" do context "when DPA results empty" do
context "and LPI result is present" do
let(:valid_lpi_response) do
{ results: [{ LPI: { postcode: "LPI postcode" } }] }.to_json
end
before do
stub_api_request(body: valid_lpi_response, status: 200)
client.call
end
it "returns result" do
expect(client.result).to eq({ "postcode" => "LPI postcode" })
end
it "returns no error" do
expect(client.error).to be_nil
end
end
context "and LPI results empty" do
before do before do
stub_api_request(body: {}.to_json) stub_api_request(body: {}.to_json)
@ -48,6 +69,7 @@ describe UprnClient do
expect(client.error).to eq("UPRN is not recognised. Check the number, or enter the address") expect(client.error).to eq("UPRN is not recognised. Check the number, or enter the address")
end end
end end
end
context "with results" do context "with results" do
before do before do

Loading…
Cancel
Save