From 07dbab2c71f1a19411514063f7b32144522c8c69 Mon Sep 17 00:00:00 2001 From: dnomadb Date: Fri, 11 Oct 2019 12:27:29 -0700 Subject: [PATCH] adding better url construction and dryrun mode --- tests/test_cli_sources.py | 20 ++++++++++++++++++++ tilesets/scripts/cli.py | 28 +++++++++++++++++++--------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/tests/test_cli_sources.py b/tests/test_cli_sources.py index 0828771..f3e0801 100644 --- a/tests/test_cli_sources.py +++ b/tests/test_cli_sources.py @@ -29,6 +29,26 @@ def test_cli_add_source(mock_request_post, MockResponse): ) +@pytest.mark.usefixtures("token_environ") +@mock.patch("requests.post") +def test_cli_add_source_dryrun(mock_request_post, MockResponse): + message = {"id": "mapbox://tileset-source/test-user/hello-world"} + mock_request_post.return_value = MockResponse(message, status_code=200) + runner = CliRunner() + result = runner.invoke( + add_source, + ["--dryrun", "test-user", "hello-world", "tests/fixtures/valid.ldgeojson"], + ) + assert not mock_request_post.called + + assert result.exit_code == 0 + + assert ( + result.output + == """[DRYRUN] Adding 1 features to https://api.mapbox.com/tilesets/v1/sources/test-user/hello-world\n""" + ) + + @pytest.mark.usefixtures("token_environ") @mock.patch("requests.get") def test_cli_view_source(mock_request_get, MockResponse): diff --git a/tilesets/scripts/cli.py b/tilesets/scripts/cli.py index 7d444e9..1aae8ff 100644 --- a/tilesets/scripts/cli.py +++ b/tilesets/scripts/cli.py @@ -305,20 +305,24 @@ def validate_source(features): @click.argument("id", required=True, type=str) @cligj.features_in_arg @click.option("--no-validation", is_flag=True, help="Bypass source file validation") +@click.option("--dryrun", is_flag=True, default=False, help="Don't upload") @click.option("--token", "-t", required=False, type=str, help="Mapbox access token") @click.option("--indent", type=int, default=None, help="Indent for JSON output") @click.pass_context -def add_source(ctx, username, id, features, no_validation, token=None, indent=None): +def add_source( + ctx, username, id, features, no_validation, dryrun, token=None, indent=None +): """Create/add a tileset source tilesets add-source """ mapbox_api = _get_api() mapbox_token = _get_token(token) - + url = ( + f"{mapbox_api}/tilesets/v1/sources/{username}/{id}?access_token={mapbox_token}" + ) with BytesIO() as io: - for feature in features: - url = f"{mapbox_api}/tilesets/v1/sources/{username}/{id}?access_token={mapbox_token}" + for count, feature in enumerate(features, 1): if not no_validation: utils.validate_geojson(feature) @@ -326,12 +330,18 @@ def add_source(ctx, username, id, features, no_validation, token=None, indent=No io.seek(0) - r = requests.post(url, files={"file": ("tileset-source", io)}) + if not dryrun: + r = requests.post(url, files={"file": ("tileset-source", io)}) - if r.status_code == 200: - click.echo(json.dumps(r.json(), indent=indent)) - else: - raise errors.TilesetsError(r.text) + if r.status_code == 200: + click.echo(json.dumps(r.json(), indent=indent)) + else: + raise errors.TilesetsError(r.text) + else: + click.echo( + f"[DRYRUN] Adding {count} features to {mapbox_api}/tilesets/v1/sources/{username}/{id}", + err=True, + ) @cli.command("view-source")