|
| 1 | +import json |
| 2 | +import pytest |
| 3 | + |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +from click.testing import CliRunner |
| 7 | + |
| 8 | +from mapbox_tilesets.scripts.cli import list_activity |
| 9 | + |
| 10 | +DEFAULT_ENDPOINT = "https://api.mapbox.com/activity/v1/test/tilesets?access_token=pk.eyJ1IjoidGVzdC11c2VyIn0K&sortby=requests&orderby=desc&limit=100" |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.usefixtures("token_environ") |
| 14 | +@mock.patch("requests.Session.get") |
| 15 | +def test_cli_list_activity(mock_request_get, MockResponse): |
| 16 | + runner = CliRunner() |
| 17 | + |
| 18 | + message = [ |
| 19 | + { |
| 20 | + "id": "penny.map-one", |
| 21 | + "request_count": 500, |
| 22 | + "last_modified": "2023-06-14T20:18:54.809Z", |
| 23 | + }, |
| 24 | + { |
| 25 | + "id": "penny.map-two", |
| 26 | + "request_count": 400, |
| 27 | + "last_modified": "2023-06-14T20:18:54.809Z", |
| 28 | + }, |
| 29 | + { |
| 30 | + "id": "penny.map-three", |
| 31 | + "request_count": 300, |
| 32 | + "last_modified": "2023-06-14T20:18:54.809Z", |
| 33 | + }, |
| 34 | + ] |
| 35 | + # sends expected request |
| 36 | + mock_request_get.return_value = MockResponse(message) |
| 37 | + MockResponse.headers = {"Link": '<meow.com?start=foo>; rel="next"'} |
| 38 | + result = runner.invoke(list_activity, ["test"]) |
| 39 | + mock_request_get.assert_called_with(DEFAULT_ENDPOINT) |
| 40 | + assert json.loads(result.output) == {"data": message, "next": "foo"} |
| 41 | + assert result.exit_code == 0 |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.usefixtures("token_environ") |
| 45 | +@mock.patch("requests.Session.get") |
| 46 | +def test_cli_list_activity_bad_token(mock_request_get, MockResponse): |
| 47 | + runner = CliRunner() |
| 48 | + |
| 49 | + message = {"message": "Not Found"} |
| 50 | + # sends expected request |
| 51 | + mock_request_get.return_value = MockResponse(message, status_code=404) |
| 52 | + result = runner.invoke(list_activity, ["test"]) |
| 53 | + mock_request_get.assert_called_with(DEFAULT_ENDPOINT) |
| 54 | + assert result.exit_code == 1 |
| 55 | + assert result.exception |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.usefixtures("token_environ") |
| 59 | +@mock.patch("requests.Session.get") |
| 60 | +def test_cli_list_activity_arguments_valid(mock_request_get, MockResponse): |
| 61 | + runner = CliRunner() |
| 62 | + runner.invoke( |
| 63 | + list_activity, |
| 64 | + [ |
| 65 | + "test", |
| 66 | + "--sortby", |
| 67 | + "modified", |
| 68 | + "--orderby", |
| 69 | + "asc", |
| 70 | + "--limit", |
| 71 | + "5", |
| 72 | + "--start", |
| 73 | + "foo", |
| 74 | + ], |
| 75 | + ) |
| 76 | + # Arguments are passed into the query string |
| 77 | + mock_request_get.assert_called_with( |
| 78 | + "https://api.mapbox.com/activity/v1/test/tilesets?access_token=pk.eyJ1IjoidGVzdC11c2VyIn0K&sortby=modified&orderby=asc&limit=5&start=foo" |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.usefixtures("token_environ") |
| 83 | +@mock.patch("requests.Session.get") |
| 84 | +@pytest.mark.parametrize( |
| 85 | + "extra_args", |
| 86 | + [ |
| 87 | + (["--sortby", "likes"]), |
| 88 | + (["--orderby", "chaos"]), |
| 89 | + (["--limit", "0"]), |
| 90 | + (["--limit", "501"]), |
| 91 | + ], |
| 92 | +) |
| 93 | +def test_cli_list_activity_arguments_invalid( |
| 94 | + mock_request_get, MockResponse, extra_args |
| 95 | +): |
| 96 | + runner = CliRunner() |
| 97 | + result = runner.invoke(list_activity, ["test"] + extra_args) |
| 98 | + # Invalid argument values should error |
| 99 | + mock_request_get.assert_not_called() |
| 100 | + assert result.exit_code == 2 |
0 commit comments