Skip to content

Commit

Permalink
Merge pull request #22 from mapbox/cligj-4-ldgj
Browse files Browse the repository at this point in the history
LD-GeoJSON abstraction + mini-refactor
  • Loading branch information
dnomadb authored Sep 30, 2019
2 parents 35c5b55 + 7a5cef5 commit 2fcb787
Show file tree
Hide file tree
Showing 19 changed files with 314 additions and 332 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.3.0 (2019-09-30)
- Feature input abstraction using `cligj`
- Logging refactor: default output is no compact JSON
- Informational printing (non-api responses) directed to stderr

## 0.2.1 (20190-09-16)
- Reformatting using `black`
- More robust tileset id checking
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
boto3==1.9.99
Click==7.0
cligj==0.5.0
requests==2.21.0
jsonschema==3.0.1
jsonseq==1.0.0
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def read(fname):

setup(
name="tilesets-cli",
version="0.2.0",
version="0.3.0",
description=u"CLI for interacting with and preparing data for the Tilesets API",
long_description=long_description,
classifiers=[],
Expand All @@ -38,6 +38,6 @@ def read(fname):
},
entry_points="""
[console_scripts]
tilesets=tilesets.cli:cli
tilesets=tilesets.scripts.cli:cli
""",
)
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
import pytest
import json


@pytest.fixture(scope="function")
def token_environ(monkeypatch):
monkeypatch.setenv("MAPBOX_ACCESS_TOKEN", "fake-token")
monkeypatch.setenv("MapboxAccessToken", "test-token")


class _MockResponse:
def __init__(self, mock_json, status_code=200):
self.text = json.dumps(mock_json)
self._json = mock_json
self.status_code = status_code

def MockResponse(self):
return self

def json(self):
return self._json


@pytest.fixture
def MockResponse():
return _MockResponse
40 changes: 19 additions & 21 deletions tests/test_cli_create.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import json

from unittest import mock

from click.testing import CliRunner
import pytest

from tilesets.cli import create


class MockResponse:
def __init__(self, mock_text):
self.text = mock_text

def MockResponse(self):
return self
from tilesets.scripts.cli import create


@pytest.mark.usefixtures("token_environ")
Expand All @@ -36,10 +30,12 @@ def test_cli_create_missing_name():

@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.post")
def test_cli_create_success(mock_request_post):
def test_cli_create_success(mock_request_post, MockResponse):
runner = CliRunner()
# sends request to proper endpoints
mock_request_post.return_value = MockResponse('{"message":"mock message"}')
message = {"message": "mock message"}

mock_request_post.return_value = MockResponse(message)
result = runner.invoke(
create,
["test.id", "--recipe", "tests/fixtures/recipe.json", "--name", "test name"],
Expand All @@ -53,17 +49,17 @@ def test_cli_create_success(mock_request_post):
"recipe": {"minzoom": 0, "maxzoom": 10, "layer_name": "test_layer"},
},
)
assert '{\n "message": "mock message"\n}\n' in result.output
assert json.loads(result.output) == message


@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.post")
def test_cli_create_success_description(mock_request_post):
def test_cli_create_success_description(mock_request_post, MockResponse):
runner = CliRunner()
# sends request with "description" included
mock_request_post.return_value = MockResponse(
'{"message":"mock message with description"}'
)

message = {"message": "mock message with description"}
mock_request_post.return_value = MockResponse(message)
result = runner.invoke(
create,
[
Expand All @@ -77,6 +73,7 @@ def test_cli_create_success_description(mock_request_post):
],
)
assert result.exit_code == 0

mock_request_post.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test.id?access_token=fake-token",
json={
Expand All @@ -85,12 +82,12 @@ def test_cli_create_success_description(mock_request_post):
"recipe": {"minzoom": 0, "maxzoom": 10, "layer_name": "test_layer"},
},
)
assert '{\n "message": "mock message with description"\n}\n' in result.output
assert json.loads(result.output) == {"message": "mock message with description"}


@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.post")
def test_cli_create_private_invalid(mock_request_post):
def test_cli_create_private_invalid(mock_request_post, MockResponse):
runner = CliRunner()
# sends request with "description" included
mock_request_post.return_value = MockResponse(
Expand All @@ -117,9 +114,10 @@ def test_cli_create_private_invalid(mock_request_post):

@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.post")
def test_cli_use_token_flag(mock_request_post):
def test_cli_use_token_flag(mock_request_post, MockResponse):
runner = CliRunner()
mock_request_post.return_value = MockResponse('{"message":"mock message"}')
message = {"message": "mock message"}
mock_request_post.return_value = MockResponse(message)
# Provides the flag --token
result = runner.invoke(
create,
Expand All @@ -142,4 +140,4 @@ def test_cli_use_token_flag(mock_request_post):
"recipe": {"minzoom": 0, "maxzoom": 10, "layer_name": "test_layer"},
},
)
assert '{\n "message": "mock message"\n}\n' in result.output
assert json.loads(result.output) == message
56 changes: 21 additions & 35 deletions tests/test_cli_jobs.py
Original file line number Diff line number Diff line change
@@ -1,88 +1,74 @@
from unittest import mock

from click.testing import CliRunner
import json
import pytest

from tilesets.cli import jobs, job


class MockResponse:
def __init__(self, mock_text):
self.text = mock_text
self.status_code = 200

def MockResponse(self):
return self


class MockResponseError:
def __init__(self, mock_text):
self.text = mock_text
self.status_code = 404
from unittest import mock
from click.testing import CliRunner

def MockResponse(self):
return self
from tilesets.scripts.cli import jobs, job


@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.get")
def test_cli_job(mock_request_get):
def test_cli_job(mock_request_get, MockResponse):
runner = CliRunner()

# sends expected request
mock_request_get.return_value = MockResponse('{"message":"mock message"}')
message = {"message": "mock message"}
mock_request_get.return_value = MockResponse(message)
result = runner.invoke(jobs, ["test.id"])
mock_request_get.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test.id/jobs?access_token=fake-token"
)
assert result.exit_code == 0
assert '{\n "message": "mock message"\n}\n' in result.output
assert json.loads(result.output) == message


# noting for future that this test really is a copy of above
@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.get")
def test_cli_job_error(mock_request_get):
def test_cli_job_error(mock_request_get, MockResponse):
runner = CliRunner()

# sends expected request
mock_request_get.return_value = MockResponseError(
'{"message":"mock error message"}'
)
message = {"message": "mock error message"}
mock_request_get.return_value = MockResponse(message, status_code=404)
result = runner.invoke(jobs, ["test.id"])
mock_request_get.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test.id/jobs?access_token=fake-token"
)
assert result.exit_code == 0
assert '{\n "message": "mock error message"\n}\n' in result.output
assert json.loads(result.output) == message


@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.get")
def test_cli_jobs_and_stage(mock_request_get):
def test_cli_jobs_and_stage(mock_request_get, MockResponse):
"""test jobs + stage endpoint"""
runner = CliRunner()

# sends expected request
mock_request_get.return_value = MockResponse('{"message":"mock message"}')
message = {"message": "mock message"}
mock_request_get.return_value = MockResponse(message)
result = runner.invoke(jobs, ["test.id", "--stage", "complete"])
mock_request_get.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test.id/jobs?stage=complete&access_token=fake-token"
)
assert result.exit_code == 0
assert '{\n "message": "mock message"\n}\n' in result.output
assert json.loads(result.output) == message


@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.get")
def test_cli_single_job(mock_request_get):
def test_cli_single_job(mock_request_get, MockResponse):
"""test job endpoint"""
runner = CliRunner()

# sends expected request
mock_request_get.return_value = MockResponse('{"message":"mock message"}')
message = {"message": "mock message"}
mock_request_get.return_value = MockResponse(message)
result = runner.invoke(job, ["test.id", "job_id"])
mock_request_get.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test.id/jobs/job_id?access_token=fake-token"
)
assert result.exit_code == 0
assert '{\n "message": "mock message"\n}\n' in result.output
assert json.loads(result.output) == message
50 changes: 19 additions & 31 deletions tests/test_cli_list.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,47 @@
from unittest import mock

from click.testing import CliRunner
import json
import pytest

from tilesets.cli import list


class MockResponse:
def __init__(self, mock_text):
self.text = mock_text
self.status_code = 200

def MockResponse(self):
return self

from unittest import mock

class MockResponseError:
def __init__(self, mock_text):
self.text = mock_text
self.status_code = 404
from click.testing import CliRunner

def MockResponse(self):
return self
from tilesets.scripts.cli import list


@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.get")
def test_cli_list(mock_request_get):
def test_cli_list(mock_request_get, MockResponse):
runner = CliRunner()

message = [
{"id": "test.tileset-1", "something": "beep"},
{"id": "test.tileset-2", "something": "boop"},
]
# sends expected request
mock_request_get.return_value = MockResponse(
'[{"id":"test.tileset-1","something":"beep"},{"id":"test.tileset-2","something":"boop"}]'
)
mock_request_get.return_value = MockResponse(message)
result = runner.invoke(list, ["test"])
mock_request_get.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test?access_token=fake-token"
)
assert result.exit_code == 0
assert "test.tileset-1\ntest.tileset-2\n" in result.output
assert result.output == """test.tileset-1\ntest.tileset-2\n"""


@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.get")
def test_cli_list_verbose(mock_request_get):
def test_cli_list_verbose(mock_request_get, MockResponse):
runner = CliRunner()

message = [
{"id": "test.tileset-1", "something": "beep"},
{"id": "test.tileset-2", "something": "boop"},
]
# sends expected request
mock_request_get.return_value = MockResponse(
'[{"id":"test.tileset-1","something":"beep"},{"id":"test.tileset-2","something":"boop"}]'
)
mock_request_get.return_value = MockResponse(message)
result = runner.invoke(list, ["test", "--verbose"])
mock_request_get.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test?access_token=fake-token"
)
assert result.exit_code == 0
assert '"something": "beep"\n' in result.output
assert '"something": "boop"\n' in result.output

assert [json.loads(l.strip()) for l in result.output.split("\n") if l] == message
19 changes: 12 additions & 7 deletions tests/test_cli_publish.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import json
import pytest

from click.testing import CliRunner
from unittest import mock

import pytest

from tilesets.cli import publish
from tilesets.scripts.cli import publish


class MockResponse:
def __init__(self, mock_text, status_code):
self.text = mock_text
def __init__(self, mock_json, status_code):
self.text = json.dumps(mock_json)
self._json = mock_json
self.status_code = status_code

def MockResponse(self):
return self

def json(self):
return self._json


@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.post")
def test_cli_publish(mock_request_post):
runner = CliRunner()

# sends expected request
mock_request_post.return_value = MockResponse('{"message":"mock message"}', 200)
mock_request_post.return_value = MockResponse({"message": "mock message"}, 200)
result = runner.invoke(publish, ["test.id"])
mock_request_post.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test.id/publish?access_token=fake-token"
Expand All @@ -37,7 +42,7 @@ def test_cli_publish(mock_request_post):
@mock.patch("requests.post")
def test_cli_publish_use_token_flag(mock_request_post):
runner = CliRunner()
mock_request_post.return_value = MockResponse('{"message":"mock message"}', 200)
mock_request_post.return_value = MockResponse({"message": "mock message"}, 200)
# Provides the flag --token
result = runner.invoke(publish, ["test.id", "--token", "flag-token"])
mock_request_post.assert_called_with(
Expand Down
Loading

0 comments on commit 2fcb787

Please sign in to comment.