from click.testing import CliRunner

import responses

from mapbox.scripts.cli import main_group


@responses.activate
def test_cli_geocode_fwd():

    responses.add(
        responses.GET,
        'http://api.mapbox.com/v4/geocode/mapbox.places/1600%20pennsylvania%20ave%20nw.json?access_token=bogus',
        match_querystring=True,
        body='{"query": ["1600", "pennsylvania", "ave", "nw"]}', status=200,
        content_type='application/json')

    runner = CliRunner()
    result = runner.invoke(
        main_group,
        ['geocode', '1600 pennsylvania ave nw', '--access-token', 'bogus'])
    assert result.exit_code == 0
    assert result.output == '{"query": ["1600", "pennsylvania", "ave", "nw"]}\n'


@responses.activate
def test_cli_geocode_fwd_env_token():

    responses.add(
        responses.GET,
        'http://api.mapbox.com/v4/geocode/mapbox.places/1600%20pennsylvania%20ave%20nw.json?access_token=bogus',
        match_querystring=True,
        body='{"query": ["1600", "pennsylvania", "ave", "nw"]}', status=200,
        content_type='application/json')

    runner = CliRunner()
    result = runner.invoke(
        main_group,
        ['geocode', '1600 pennsylvania ave nw'],
        env={'MapboxAccessToken': 'bogus'})
    assert result.exit_code == 0
    assert result.output == '{"query": ["1600", "pennsylvania", "ave", "nw"]}\n'


@responses.activate
def test_cli_geocode_unauthorized():

    responses.add(
        responses.GET,
        'http://api.mapbox.com/v4/geocode/mapbox.places/1600%20pennsylvania%20ave%20nw.json',
        body='{"message":"Not Authorized - Invalid Token"}', status=401,
        content_type='application/json')

    runner = CliRunner()
    result = runner.invoke(main_group, ['geocode', '1600 pennsylvania ave nw'])
    assert result.exit_code == 1
    assert result.output == 'Error: {"message":"Not Authorized - Invalid Token"}\n'

@@ -1,12 +1,24 @@
+import json%0A
 from click.t
@@ -41,17 +41,16 @@
 iRunner%0A
-%0A
 import r
@@ -58,17 +58,16 @@
 sponses%0A
-%0A
 from map
@@ -541,32 +541,45 @@
      %5B'geocode',
+ '--forward',
  '1600 pennsylva
@@ -1184,32 +1184,45 @@
      %5B'geocode',
+ '--forward',
  '1600 pennsylva
@@ -1304,32 +1304,32 @@
 .exit_code == 0%0A
-
     assert resul
@@ -1386,32 +1386,1391 @@
 ve%22, %22nw%22%5D%7D%5Cn'%0A%0A
+@responses.activate%0Adef test_cli_geocode_reverse():%0A%0A    coords = %5B-77.4371, 37.5227%5D%0A%0A    responses.add(%0A        responses.GET,%0A        'http://api.mapbox.com/v4/geocode/mapbox.places/%25s.json?access_token=pk.test' %25 ','.join(map(lambda x: str(x), coords)),%0A        match_querystring=True,%0A        body='%7B%22query%22: %25s%7D' %25 json.dumps(coords),%0A        status=200,%0A        content_type='application/json')%0A%0A    runner = CliRunner()%0A    result = runner.invoke(%0A        main_group,%0A        %5B'geocode', '--reverse', ','.join(%5Bstr(x) for x in coords%5D), '--access-token', 'bogus'%5D)%0A    assert result.exit_code == 0%0A    assert result.output == '%7B%22query%22: %25s%7D%5Cn' %25 json.dumps(coords)%0A%0A@responses.activate%0Adef test_cli_geocode_reverse_env_token():%0A%0A    coords = %5B-77.4371, 37.5227%5D%0A%0A    responses.add(%0A        responses.GET,%0A        'http://api.mapbox.com/v4/geocode/mapbox.places/1600%2520pennsylvania%2520ave%2520nw.json?access_token=bogus',%0A        match_querystring=True,%0A        body='%7B%22query%22: %25s%7D' %25 json.dumps(coords),%0A        status=200,%0A        content_type='application/json')%0A%0A    runner = CliRunner()%0A    result = runner.invoke(%0A        main_group,%0A        %5B'geocode', '--reverse', ','.join(%5Bstr(x) for x in coords%5D)%5D,%0A        env=%7B'MapboxAccessToken': 'bogus'%7D)%0A    assert result.exit_code == 0%0A    assert result.output == '%7B%22query%22: %25s%7D%5Cn' %25 json.dumps(coords)%0A%0A
 %0A@responses.acti

test reverse functionality