Skip to content

Commit

Permalink
Merge pull request #2 from conveyal/unfound-bulk
Browse files Browse the repository at this point in the history
Gracefully handle unfound bulk result
  • Loading branch information
evansiroky authored Feb 15, 2018
2 parents 90cfdad + b00a611 commit e6ce728
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 28 deletions.
36 changes: 34 additions & 2 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ Object {
}
`;

exports[`basic bulk request uri 1`] = `"/arcgis/rest/services/World/GeocodeServer/geocodeAddresses"`;

exports[`basic reverse g-a-g response 1`] = `
Object {
"features": Array [
Expand Down Expand Up @@ -251,3 +249,37 @@ Object {
`;

exports[`basic search request uri 1`] = `"/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?outFields=*&maxLocations=10&SingleLine=123%20main%20st&f=json"`;

exports[`bulk g-a-g response with no address found 1`] = `
Object {
"features": Array [
Object {
"geometry": Object {
"coordinates": Array [
0,
0,
],
"type": "point",
},
"properties": Object {
"confidence": 0,
"country": "",
"country_a": "",
"county": "",
"label": "Address not found",
"locality": "",
"name": "",
"neighbourhood": "",
"region": "",
"resultId": 0,
},
"type": "feature",
},
],
"query": Object {
"addresses": Array [
"aefgjil",
],
},
}
`;
66 changes: 40 additions & 26 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,36 @@ import nock from 'nock'
import {autocomplete, bulk, reverse, search} from '../index'

const mockBulkResult = require('./mock-bulk-result.json')
const mockBulkResultWithBadAddress = require('./mock-bulk-result-with-bad-address.json')
const mockReverseResult = require('./mock-reverse-result.json')
const mockSearchResult = require('./mock-search-result.json')
const mockSuggestResult = require('./mock-suggest-result.json')

const ARCGIS_GEOCODE_URL = 'https://geocode.arcgis.com/'
const BULK_URL = /arcgis\/rest\/services\/World\/GeocodeServer\/geocodeAddresses/
const REVERSE_URL = /arcgis\/rest\/services\/World\/GeocodeServer\/reverseGeocode/
const SEARCH_URL = /arcgis\/rest\/services\/World\/GeocodeServer\/findAddressCandidates/
const SUGGEST_URL = /arcgis\/rest\/services\/World\/GeocodeServer\/suggest/

function nockArcGet (url: RegExp) {
return nock(ARCGIS_GEOCODE_URL).get(url)
}

function nockArcPost (url: RegExp) {
return nock(ARCGIS_GEOCODE_URL).post(url)
}

function snapshotUri (type: string, response: Object) {
return (uri, requestBody) => {
expect(uri).toMatchSnapshot(`basic ${type} request uri`)
return response
}
}

describe('geocoder-arcgis-geojson', () => {
describe('autocomplete', () => {
it('should make basic autocomplete query', async () => {
nock('https://geocode.arcgis.com/')
.get(/arcgis\/rest\/services\/World\/GeocodeServer\/suggest/)
.reply(200, (uri, requestBody) => {
expect(uri).toMatchSnapshot('basic autocomplete request uri')
return mockSuggestResult
})
nockArcGet(SUGGEST_URL).reply(200, snapshotUri('autocomplete', mockSuggestResult))

const result = await autocomplete({
text: '123 main st'
Expand All @@ -27,19 +44,15 @@ describe('geocoder-arcgis-geojson', () => {
})

describe('bulk', () => {
it('should make basic bulk query', async () => {
beforeEach(() => {
// nock for auth token
nock('https://www.arcgis.com/')
.post(/sharing\/oauth2\/token/)
.reply(200, { access_token: 'test', expires_in: 86400 })
})

// nock for bulk geocode reqeust
nock('https://geocode.arcgis.com/')
.post(/arcgis\/rest\/services\/World\/GeocodeServer\/geocodeAddresses/)
.reply(200, (uri, requestBody) => {
expect(uri).toMatchSnapshot('basic bulk request uri')
return mockBulkResult
})
it('should make basic bulk query', async () => {
nockArcPost(BULK_URL).reply(200, mockBulkResult)

const result = await bulk({
addresses: ['123 main st'],
Expand All @@ -48,16 +61,22 @@ describe('geocoder-arcgis-geojson', () => {
})
expect(result).toMatchSnapshot('basic bulk g-a-g response')
})

it('should handle bulk query resulting in no address found', async () => {
nockArcPost(BULK_URL).reply(200, mockBulkResultWithBadAddress)

const result = await bulk({
addresses: ['aefgjil'],
clientId: 'test',
clientSecret: 'test'
})
expect(result).toMatchSnapshot('bulk g-a-g response with no address found')
})
})

describe('reverse', () => {
it('should make basic reverse query', async () => {
nock('https://geocode.arcgis.com/')
.get(/arcgis\/rest\/services\/World\/GeocodeServer\/reverseGeocode/)
.reply(200, (uri, requestBody) => {
expect(uri).toMatchSnapshot('basic reverse request uri')
return mockReverseResult
})
nockArcGet(REVERSE_URL).reply(200, snapshotUri('reverse', mockReverseResult))

const result = await reverse({
point: {
Expand All @@ -71,12 +90,7 @@ describe('geocoder-arcgis-geojson', () => {

describe('search', () => {
it('should make basic search query', async () => {
nock('https://geocode.arcgis.com/')
.get(/arcgis\/rest\/services\/World\/GeocodeServer\/findAddressCandidates/)
.reply(200, (uri, requestBody) => {
expect(uri).toMatchSnapshot('basic search request uri')
return mockSearchResult
})
nockArcGet(SEARCH_URL).reply(200, snapshotUri('search', mockSearchResult))

const result = await search({
text: '123 main st'
Expand Down
1 change: 1 addition & 0 deletions __tests__/mock-bulk-result-with-bad-address.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"spatialReference":{"wkid":4326,"latestWkid":4326},"locations":[{"address":"","score":0,"attributes":{"ResultID":0,"Loc_name":"","Status":"U","Score":0,"Match_addr":"","LongLabel":"","ShortLabel":"","Addr_type":"","Type":"","PlaceName":"","Place_addr":"","Phone":"","URL":"","Rank":0,"AddBldg":"","AddNum":"","AddNumFrom":"","AddNumTo":"","AddRange":"","Side":"","StPreDir":"","StPreType":"","StName":"","StType":"","StDir":"","BldgType":"","BldgName":"","LevelType":"","LevelName":"","UnitType":"","UnitName":"","SubAddr":"","StAddr":"","Block":"","Sector":"","Nbrhd":"","District":"","City":"","MetroArea":"","Subregion":"","Region":"","RegionAbbr":"","Territory":"","Zone":"","Postal":"","PostalExt":"","Country":"","LangCode":"","Distance":0,"X":0,"Y":0,"DisplayX":0,"DisplayY":0,"Xmin":0,"Xmax":0,"Ymin":0,"Ymax":0,"ExInfo":""}}]}
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ function getGeocoder (clientId: ?string, clientSecret: ?string, endpoint: ?strin
* Translate arcgis candidate json to geojson
*/
function candidateToGeojson (candidate) {
if (!candidate.location) {
// no result found for this candidate
return {
geometry: {
coordinates: [0, 0],
type: 'point'
},
properties: {
confidence: 0,
country: '',
country_a: '',
county: '',
label: 'Address not found',
locality: '',
name: '',
neighbourhood: '',
region: '',
resultId: candidate.attributes.ResultID // this only appears in bulk geocode results
},
type: 'feature'
}
}
return {
geometry: {
coordinates: lonlat.toCoordinates(candidate.location),
Expand Down

0 comments on commit e6ce728

Please sign in to comment.