-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from mapbox/cligj-4-ldgj
LD-GeoJSON abstraction + mini-refactor
- Loading branch information
Showing
19 changed files
with
314 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.