-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest_cli_update.py
81 lines (63 loc) · 2.19 KB
/
test_cli_update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import json
import pytest
from click.testing import CliRunner
from unittest import mock
from mapbox_tilesets.scripts.cli import update
class MockResponse:
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.Session.patch")
def test_cli_patch(mock_request_patch):
runner = CliRunner()
# sends expected request
mock_request_patch.return_value = MockResponse("", 204)
result = runner.invoke(
update,
[
"test.id",
"--name=hola",
"--description=hello world",
"--privacy=private",
'--attribution=[{"text":"natural earth data","link":"https://naturalearthdata.com"}]',
],
)
mock_request_patch.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test.id?access_token=pk.eyJ1IjoidGVzdC11c2VyIn0K",
json={
"name": "hola",
"description": "hello world",
"private": True,
"attribution": [
{"text": "natural earth data", "link": "https://naturalearthdata.com"}
],
},
)
assert result.exit_code == 0
assert result.output == ""
@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.Session.patch")
def test_cli_patch_no_options(mock_request_patch):
runner = CliRunner()
# sends expected request
mock_request_patch.return_value = MockResponse("", 204)
result = runner.invoke(update, ["test.id"])
mock_request_patch.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test.id?access_token=pk.eyJ1IjoidGVzdC11c2VyIn0K",
json={},
)
assert result.exit_code == 0
assert result.output == ""
@pytest.mark.usefixtures("token_environ")
def test_cli_patch_invalid_json():
runner = CliRunner()
# send invalid json request
result = runner.invoke(update, ["test.id", "--attribution=invalid json"])
assert result.exit_code == 1
assert "Unable to parse attribution JSON" in result.output