1
1
import pathlib
2
2
3
+ import httpcore
3
4
import jinja2
4
5
import pytest
6
+ import yaml
5
7
6
8
from openapi_python_client import GeneratorError
7
9
@@ -44,6 +46,23 @@ def test__get_project_for_url_or_path_generator_error(mocker):
44
46
assert project == error
45
47
46
48
49
+ def test__get_project_for_url_or_path_document_error (mocker ):
50
+ error = GeneratorError ()
51
+ _get_document = mocker .patch ("openapi_python_client._get_document" , return_value = error )
52
+
53
+ from_dict = mocker .patch ("openapi_python_client.parser.GeneratorData.from_dict" )
54
+ url = mocker .MagicMock ()
55
+ path = mocker .MagicMock ()
56
+
57
+ from openapi_python_client import _get_project_for_url_or_path
58
+
59
+ project = _get_project_for_url_or_path (url = url , path = path )
60
+
61
+ _get_document .assert_called_once_with (url = url , path = path )
62
+ from_dict .assert_not_called ()
63
+ assert project == error
64
+
65
+
47
66
def test_create_new_client (mocker ):
48
67
project = mocker .MagicMock ()
49
68
_get_project_for_url_or_path = mocker .patch (
@@ -118,9 +137,9 @@ def test__get_document_no_url_or_path(self, mocker):
118
137
119
138
from openapi_python_client import _get_document
120
139
121
- with pytest .raises (ValueError ):
122
- _get_document (url = None , path = None )
140
+ result = _get_document (url = None , path = None )
123
141
142
+ assert result == GeneratorError (header = "No URL or Path provided" )
124
143
get .assert_not_called ()
125
144
Path .assert_not_called ()
126
145
loads .assert_not_called ()
@@ -132,13 +151,28 @@ def test__get_document_url_and_path(self, mocker):
132
151
133
152
from openapi_python_client import _get_document
134
153
135
- with pytest .raises (ValueError ):
136
- _get_document (url = mocker .MagicMock (), path = mocker .MagicMock ())
154
+ result = _get_document (url = mocker .MagicMock (), path = mocker .MagicMock ())
137
155
156
+ assert result == GeneratorError (header = "Provide URL or Path, not both." )
138
157
get .assert_not_called ()
139
158
Path .assert_not_called ()
140
159
loads .assert_not_called ()
141
160
161
+ def test__get_document_bad_url (self , mocker ):
162
+ get = mocker .patch ("httpx.get" , side_effect = httpcore .NetworkError )
163
+ Path = mocker .patch ("openapi_python_client.Path" )
164
+ loads = mocker .patch ("yaml.safe_load" )
165
+
166
+ from openapi_python_client import _get_document
167
+
168
+ url = mocker .MagicMock ()
169
+ result = _get_document (url = url , path = None )
170
+
171
+ assert result == GeneratorError (header = "Could not get OpenAPI document from provided URL" )
172
+ get .assert_called_once_with (url )
173
+ Path .assert_not_called ()
174
+ loads .assert_not_called ()
175
+
142
176
def test__get_document_url_no_path (self , mocker ):
143
177
get = mocker .patch ("httpx.get" )
144
178
Path = mocker .patch ("openapi_python_client.Path" )
@@ -166,6 +200,20 @@ def test__get_document_path_no_url(self, mocker):
166
200
path .read_bytes .assert_called_once ()
167
201
loads .assert_called_once_with (path .read_bytes ())
168
202
203
+ def test__get_document_bad_yaml (self , mocker ):
204
+ get = mocker .patch ("httpx.get" )
205
+ loads = mocker .patch ("yaml.safe_load" , side_effect = yaml .YAMLError )
206
+
207
+ from openapi_python_client import _get_document
208
+
209
+ path = mocker .MagicMock ()
210
+ result = _get_document (url = None , path = path )
211
+
212
+ get .assert_not_called ()
213
+ path .read_bytes .assert_called_once ()
214
+ loads .assert_called_once_with (path .read_bytes ())
215
+ assert result == GeneratorError (header = "Invalid YAML from provided source" )
216
+
169
217
170
218
class TestProject :
171
219
def test___init__ (self , mocker ):
0 commit comments