Skip to content

Commit

Permalink
fix(bulk): gracefully handle unfound bulk result
Browse files Browse the repository at this point in the history
  • Loading branch information
evansiroky committed Feb 9, 2018
1 parent 90cfdad commit 44c6092
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 32 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",
],
},
}
`;
90 changes: 60 additions & 30 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,59 @@ 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')

function prepareNock (
type: 'autocomplete' | 'bulk' | 'search' | 'reverse',
snapshotUri = true,
response?: Object
) {
if (type === 'bulk') {
// nock for auth token
nock('https://www.arcgis.com/')
.post(/sharing\/oauth2\/token/)
.reply(200, { access_token: 'test', expires_in: 86400 })
}

let n = nock('https://geocode.arcgis.com/')

switch (type) {
case 'autocomplete':
n = n.get(/arcgis\/rest\/services\/World\/GeocodeServer\/suggest/)
response = response || mockSuggestResult
break
case 'bulk':
n = n.post(/arcgis\/rest\/services\/World\/GeocodeServer\/geocodeAddresses/)
response = response || mockBulkResult
break
case 'reverse':
n = n.get(/arcgis\/rest\/services\/World\/GeocodeServer\/reverseGeocode/)
response = response || mockReverseResult
break
case 'search':
n = n.get(/arcgis\/rest\/services\/World\/GeocodeServer\/findAddressCandidates/)
response = response || mockSearchResult
break
default:
throw new Error('invalid type')
}

n.reply(200, (uri, requestBody) => {
if (snapshotUri) {
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
})
prepareNock('autocomplete')

const result = await autocomplete({
text: '123 main st'
Expand All @@ -28,18 +68,7 @@ describe('geocoder-arcgis-geojson', () => {

describe('bulk', () => {
it('should make basic bulk query', async () => {
// 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
})
prepareNock('bulk', false)

const result = await bulk({
addresses: ['123 main st'],
Expand All @@ -48,16 +77,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 () => {
prepareNock('bulk', false, 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
})
prepareNock('reverse')

const result = await reverse({
point: {
Expand All @@ -71,12 +106,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
})
prepareNock('search')

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 44c6092

Please sign in to comment.