Skip to content

Commit

Permalink
apply limits and feature sequences to reverse geocoder
Browse files Browse the repository at this point in the history
  • Loading branch information
perrygeo committed Oct 12, 2016
1 parent 2075d72 commit 5be2817
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
13 changes: 9 additions & 4 deletions mapboxcli/scripts/geocoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def echo_headers(headers, file=None):
@click.option('--features', is_flag=True, default=False,
help="Return results as line-delimited GeoJSON Feature sequence, "
"not a FeatureCollection")
@click.option('--limit', type=int,
@click.option('--limit', type=int, default=None,
help="Limit the number of returned features")
@click.pass_context
def geocoding(ctx, query, forward, include_headers, lat, lon,
Expand Down Expand Up @@ -122,8 +122,8 @@ def geocoding(ctx, query, forward, include_headers, lat, lon,
if resp.status_code == 200:
if features:
collection = json.loads(resp.text)
for feature in collection['features']:
click.echo(json.dumps(feature), file=stdout)
for feat in collection['features']:
click.echo(json.dumps(feat), file=stdout)
else:
click.echo(resp.text, file=stdout)
else:
Expand All @@ -138,6 +138,11 @@ def geocoding(ctx, query, forward, include_headers, lat, lon,
if include_headers:
echo_headers(resp.headers, file=stdout)
if resp.status_code == 200:
click.echo(resp.text, file=stdout)
if features:
collection = json.loads(resp.text)
for feat in collection['features']:
click.echo(json.dumps(feat), file=stdout)
else:
click.echo(resp.text, file=stdout)
else:
raise MapboxCLIException(resp.text.strip())
36 changes: 35 additions & 1 deletion tests/test_geocoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,41 @@ def test_cli_geocode_fwd_limit():
'--forward', '1600 pennsylvania ave nw'],
catch_exceptions=False)
assert result.exit_code == 0
assert result.output == '{"features": [{"name": "first"}, {"name": "second"}]}'
assert result.output == '{"features": [{"name": "first"}, {"name": "second"}]}\n'


@responses.activate
def test_cli_geocode_reverse_limit_features():

lon, lat = -77.4371, 37.5227
res = {"query": [lon, lat],
"features": [{"name": "first"}, {"name": "second"}]}
body = json.dumps(res)

responses.add(
responses.GET,
'https://api.mapbox.com/geocoding/v5/mapbox.places/{0},{1}.json?access_token=pk.test'.format(lon, lat),
match_querystring=True,
body=body,
status=200,
content_type='application/json')

runner = CliRunner()
result = runner.invoke(
main_group,
['--access-token', 'pk.test', 'geocoding',
'--reverse', '--limit', '2'],
input='{0},{1}'.format(lon, lat))
assert result.exit_code == 0
assert result.output.strip() == body

result = runner.invoke(
main_group,
['--access-token', 'pk.test', 'geocoding',
'--reverse', '--limit', '2', '--features'],
input='{0},{1}'.format(lon, lat))
assert result.exit_code == 0
assert result.output == '{"name": "first"}\n{"name": "second"}\n'


@responses.activate
Expand Down

0 comments on commit 5be2817

Please sign in to comment.