forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
294 lines (223 loc) · 10.5 KB
/
test_cli.py
File metadata and controls
294 lines (223 loc) · 10.5 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from typer.testing import CliRunner
from openapi_python_client import Config
from openapi_python_client.parser.errors import GeneratorError, ParseError
runner = CliRunner()
def test_version(mocker):
generate = mocker.patch("openapi_python_client.cli.generate")
from openapi_python_client.cli import app
result = runner.invoke(app, ["--version", "generate"])
generate.assert_not_called()
assert result.exit_code == 0
assert "openapi-python-client version: " in result.stdout
@pytest.fixture
def _create_new_client(mocker) -> MagicMock:
return mocker.patch("openapi_python_client.create_new_client", return_value=[])
def test_config_arg(mocker, _create_new_client):
load_config = mocker.patch("openapi_python_client.config.Config.load_from_path")
from openapi_python_client.cli import MetaType, app
config_path = "config/path"
path = "cool/path"
file_encoding = "utf-8"
result = runner.invoke(
app,
["generate", f"--config={config_path}", f"--path={path}", f"--file-encoding={file_encoding}"],
catch_exceptions=False,
)
assert result.exit_code == 0
load_config.assert_called_once_with(path=Path(config_path))
_create_new_client.assert_called_once_with(
url=None,
path=Path(path),
custom_template_path=None,
meta=MetaType.POETRY,
file_encoding="utf-8",
config=load_config.return_value,
)
def test_bad_config(mocker, _create_new_client):
load_config = mocker.patch(
"openapi_python_client.config.Config.load_from_path", side_effect=ValueError("Bad Config")
)
from openapi_python_client.cli import app
config_path = "config/path"
path = "cool/path"
result = runner.invoke(app, ["generate", f"--config={config_path}", f"--path={path}"])
assert result.exit_code == 2
assert "Unable to parse config" in result.stdout
load_config.assert_called_once_with(path=Path(config_path))
_create_new_client.assert_not_called()
class TestGenerate:
def test_generate_no_params(self, _create_new_client):
from openapi_python_client.cli import app
result = runner.invoke(app, ["generate"])
assert result.exit_code == 1, result.output
_create_new_client.assert_not_called()
def test_generate_url_and_path(self, _create_new_client):
from openapi_python_client.cli import app
result = runner.invoke(app, ["generate", "--path=blah", "--url=otherblah"])
assert result.exit_code == 1
_create_new_client.assert_not_called()
def test_generate_url(self, _create_new_client):
url = "cool.url"
from openapi_python_client.cli import Config, MetaType, app
result = runner.invoke(app, ["generate", f"--url={url}"])
assert result.exit_code == 0
_create_new_client.assert_called_once_with(
url=url, path=None, custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8", config=Config()
)
def test_generate_path(self, _create_new_client):
path = "cool/path"
from openapi_python_client.cli import Config, MetaType, app
result = runner.invoke(app, ["generate", f"--path={path}"])
assert result.exit_code == 0
_create_new_client.assert_called_once_with(
url=None,
path=Path(path),
custom_template_path=None,
meta=MetaType.POETRY,
file_encoding="utf-8",
config=Config(),
)
def test_generate_meta(self, _create_new_client):
path = "cool/path"
from openapi_python_client.cli import Config, MetaType, app
result = runner.invoke(app, ["generate", f"--path={path}", "--meta=none"])
assert result.exit_code == 0
_create_new_client.assert_called_once_with(
url=None,
path=Path(path),
custom_template_path=None,
meta=MetaType.NONE,
file_encoding="utf-8",
config=Config(),
)
def test_generate_encoding(self, _create_new_client):
path = "cool/path"
file_encoding = "utf-8"
from openapi_python_client.cli import Config, MetaType, app
result = runner.invoke(app, ["generate", f"--path={path}", f"--file-encoding={file_encoding}"])
assert result.exit_code == 0
_create_new_client.assert_called_once_with(
url=None,
path=Path(path),
custom_template_path=None,
meta=MetaType.POETRY,
file_encoding="utf-8",
config=Config(),
)
def test_generate_encoding_errors(self, _create_new_client):
path = "cool/path"
file_encoding = "error-file-encoding"
from openapi_python_client.cli import MetaType, app
result = runner.invoke(app, ["generate", f"--path={path}", f"--file-encoding={file_encoding}"])
assert result.exit_code == 1
assert result.output == "Unknown encoding : {}\n".format(file_encoding)
def test_generate_handle_errors(self, _create_new_client):
_create_new_client.return_value = [GeneratorError(detail="this is a message")]
path = "cool/path"
from openapi_python_client.cli import app
result = runner.invoke(app, ["generate", f"--path={path}"])
assert result.exit_code == 1
assert result.output == (
"Error(s) encountered while generating, client was not created\n\n"
"Unable to generate the client\n\n"
"this is a message\n\n\n"
"If you believe this was a mistake or this tool is missing a feature you need, please open an issue at "
"https://github.com/triaxtec/openapi-python-client/issues/new/choose\n"
)
def test_generate_handle_multiple_warnings(self, _create_new_client):
error_1 = ParseError(data={"test": "data"}, detail="this is a message")
error_2 = ParseError(data={"other": "data"}, detail="this is another message", header="Custom Header")
_create_new_client.return_value = [error_1, error_2]
path = "cool/path"
from openapi_python_client.cli import app
result = runner.invoke(app, ["generate", f"--path={path}"])
assert result.exit_code == 0
assert result.output == (
"Warning(s) encountered while generating. Client was generated, but some pieces may be missing\n\n"
"Unable to parse this part of your OpenAPI document: \n\n"
"this is a message\n\n"
"{'test': 'data'}\n\n"
"Custom Header\n\n"
"this is another message\n\n"
"{'other': 'data'}\n\n"
"If you believe this was a mistake or this tool is missing a feature you need, please open an issue at "
"https://github.com/triaxtec/openapi-python-client/issues/new/choose\n"
)
def test_generate_fail_on_warning(self, _create_new_client):
error_1 = ParseError(data={"test": "data"}, detail="this is a message")
error_2 = ParseError(data={"other": "data"}, detail="this is another message", header="Custom Header")
_create_new_client.return_value = [error_1, error_2]
path = "cool/path"
from openapi_python_client.cli import app
result = runner.invoke(app, ["generate", f"--path={path}", "--fail-on-warning"])
assert result.exit_code == 1
assert result.output == (
"Warning(s) encountered while generating. Client was generated, but some pieces may be missing\n\n"
"Unable to parse this part of your OpenAPI document: \n\n"
"this is a message\n\n"
"{'test': 'data'}\n\n"
"Custom Header\n\n"
"this is another message\n\n"
"{'other': 'data'}\n\n"
"If you believe this was a mistake or this tool is missing a feature you need, please open an issue at "
"https://github.com/triaxtec/openapi-python-client/issues/new/choose\n"
)
@pytest.fixture
def _update_existing_client(mocker):
return mocker.patch("openapi_python_client.update_existing_client")
class TestUpdate:
def test_update_no_params(self, _update_existing_client):
from openapi_python_client.cli import app
result = runner.invoke(app, ["update"])
assert result.exit_code == 1
_update_existing_client.assert_not_called()
def test_update_url_and_path(self, _update_existing_client):
from openapi_python_client.cli import app
result = runner.invoke(app, ["update", "--path=blah", "--url=otherblah"])
assert result.exit_code == 1
_update_existing_client.assert_not_called()
def test_update_url(self, _update_existing_client):
url = "cool.url"
from openapi_python_client.cli import Config, MetaType, app
result = runner.invoke(app, ["update", f"--url={url}"])
assert result.exit_code == 0
_update_existing_client.assert_called_once_with(
url=url, path=None, custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8", config=Config()
)
def test_update_path(self, _update_existing_client):
path = "cool/path"
from openapi_python_client.cli import Config, MetaType, app
result = runner.invoke(app, ["update", f"--path={path}"])
assert result.exit_code == 0
_update_existing_client.assert_called_once_with(
url=None,
path=Path(path),
custom_template_path=None,
meta=MetaType.POETRY,
file_encoding="utf-8",
config=Config(),
)
def test_update_encoding(self, _update_existing_client):
path = "cool/path"
file_encoding = "utf-8"
from openapi_python_client.cli import Config, MetaType, app
result = runner.invoke(app, ["update", f"--path={path}", f"--file-encoding={file_encoding}"])
assert result.exit_code == 0
_update_existing_client.assert_called_once_with(
url=None,
path=Path(path),
custom_template_path=None,
meta=MetaType.POETRY,
file_encoding="utf-8",
config=Config(),
)
def test_update_encoding_errors(self, _update_existing_client):
path = "cool/path"
file_encoding = "error-file-encoding"
from openapi_python_client.cli import MetaType, app
result = runner.invoke(app, ["update", f"--path={path}", f"--file-encoding={file_encoding}"])
assert result.exit_code == 1
assert result.output == "Unknown encoding : {}\n".format(file_encoding)