3
3
import httpcore
4
4
import jinja2
5
5
import pytest
6
- import yaml
7
6
8
7
from openapi_python_client import Config , ErrorLevel , GeneratorError , Project
9
8
@@ -148,7 +147,7 @@ def test_update_existing_client_project_error(mocker):
148
147
class TestGetJson :
149
148
def test__get_document_no_url_or_path (self , mocker ):
150
149
get = mocker .patch ("httpx.get" )
151
- Path = mocker .patch ("openapi_python_client.Path" )
150
+ _Path = mocker .patch ("openapi_python_client.Path" )
152
151
loads = mocker .patch ("yaml.safe_load" )
153
152
154
153
from openapi_python_client import _get_document
@@ -157,12 +156,12 @@ def test__get_document_no_url_or_path(self, mocker):
157
156
158
157
assert result == GeneratorError (header = "No URL or Path provided" )
159
158
get .assert_not_called ()
160
- Path .assert_not_called ()
159
+ _Path .assert_not_called ()
161
160
loads .assert_not_called ()
162
161
163
162
def test__get_document_url_and_path (self , mocker ):
164
163
get = mocker .patch ("httpx.get" )
165
- Path = mocker .patch ("openapi_python_client.Path" )
164
+ _Path = mocker .patch ("openapi_python_client.Path" )
166
165
loads = mocker .patch ("yaml.safe_load" )
167
166
168
167
from openapi_python_client import _get_document
@@ -171,12 +170,12 @@ def test__get_document_url_and_path(self, mocker):
171
170
172
171
assert result == GeneratorError (header = "Provide URL or Path, not both." )
173
172
get .assert_not_called ()
174
- Path .assert_not_called ()
173
+ _Path .assert_not_called ()
175
174
loads .assert_not_called ()
176
175
177
176
def test__get_document_bad_url (self , mocker ):
178
177
get = mocker .patch ("httpx.get" , side_effect = httpcore .NetworkError )
179
- Path = mocker .patch ("openapi_python_client.Path" )
178
+ _Path = mocker .patch ("openapi_python_client.Path" )
180
179
loads = mocker .patch ("yaml.safe_load" )
181
180
182
181
from openapi_python_client import _get_document
@@ -186,49 +185,84 @@ def test__get_document_bad_url(self, mocker):
186
185
187
186
assert result == GeneratorError (header = "Could not get OpenAPI document from provided URL" )
188
187
get .assert_called_once_with (url )
189
- Path .assert_not_called ()
188
+ _Path .assert_not_called ()
190
189
loads .assert_not_called ()
191
190
192
191
def test__get_document_url_no_path (self , mocker ):
193
192
get = mocker .patch ("httpx.get" )
194
- Path = mocker .patch ("openapi_python_client.Path" )
193
+ _Path = mocker .patch ("openapi_python_client.Path" )
195
194
loads = mocker .patch ("yaml.safe_load" )
196
195
197
196
from openapi_python_client import _get_document
198
197
199
- url = mocker . MagicMock ()
198
+ url = "test"
200
199
_get_document (url = url , path = None )
201
200
202
201
get .assert_called_once_with (url )
203
- Path .assert_not_called ()
202
+ _Path .assert_not_called ()
204
203
loads .assert_called_once_with (get ().content )
205
204
206
- def test__get_document_path_no_url (self , mocker ):
205
+ def test__get_document_path_no_url (self , tmp_path , mocker ):
207
206
get = mocker .patch ("httpx.get" )
208
207
loads = mocker .patch ("yaml.safe_load" )
208
+ path = tmp_path / "test.yaml"
209
+ path .write_text ("some test data" )
209
210
210
211
from openapi_python_client import _get_document
211
212
212
- path = mocker .MagicMock ()
213
213
_get_document (url = None , path = path )
214
214
215
215
get .assert_not_called ()
216
- path .read_bytes .assert_called_once ()
217
- loads .assert_called_once_with (path .read_bytes ())
216
+ loads .assert_called_once_with (b"some test data" )
218
217
219
- def test__get_document_bad_yaml (self , mocker ):
218
+ def test__get_document_bad_yaml (self , mocker , tmp_path ):
220
219
get = mocker .patch ("httpx.get" )
221
- loads = mocker .patch ("yaml.safe_load" , side_effect = yaml .YAMLError )
222
-
223
220
from openapi_python_client import _get_document
224
221
225
- path = mocker .MagicMock ()
222
+ path = tmp_path / "test.yaml"
223
+ path .write_text ("'" )
226
224
result = _get_document (url = None , path = path )
227
225
228
226
get .assert_not_called ()
229
- path .read_bytes .assert_called_once ()
230
- loads .assert_called_once_with (path .read_bytes ())
231
- assert result == GeneratorError (header = "Invalid YAML from provided source" )
227
+ assert isinstance (result , GeneratorError )
228
+ assert "Invalid YAML" in result .header
229
+
230
+ def test__get_document_json (self , mocker ):
231
+ class FakeResponse :
232
+ content = b'{\n \t "foo": "bar"}'
233
+ headers = {"content-type" : "application/json; encoding=utf8" }
234
+
235
+ get = mocker .patch ("httpx.get" , return_value = FakeResponse ())
236
+ yaml_loads = mocker .patch ("yaml.safe_load" )
237
+ json_result = mocker .MagicMock ()
238
+ json_loads = mocker .patch ("json.loads" , return_value = json_result )
239
+
240
+ from openapi_python_client import _get_document
241
+
242
+ url = mocker .MagicMock ()
243
+ result = _get_document (url = url , path = None )
244
+
245
+ get .assert_called_once ()
246
+ json_loads .assert_called_once_with (FakeResponse .content .decode ())
247
+ yaml_loads .assert_not_called ()
248
+ assert result == json_result
249
+
250
+ def test__get_document_bad_json (self , mocker ):
251
+ class FakeResponse :
252
+ content = b'{"foo"}'
253
+ headers = {"content-type" : "application/json; encoding=utf8" }
254
+
255
+ get = mocker .patch ("httpx.get" , return_value = FakeResponse ())
256
+
257
+ from openapi_python_client import _get_document
258
+
259
+ url = mocker .MagicMock ()
260
+ result = _get_document (url = url , path = None )
261
+
262
+ get .assert_called_once ()
263
+ assert result == GeneratorError (
264
+ header = "Invalid JSON from provided source: " "Expecting ':' delimiter: line 1 column 7 (char 6)"
265
+ )
232
266
233
267
234
268
def make_project (** kwargs ):
0 commit comments