11import pathlib
22
3+ import httpcore
34import jinja2
45import pytest
6+ import yaml
57
68from openapi_python_client import GeneratorError
79
@@ -44,6 +46,23 @@ def test__get_project_for_url_or_path_generator_error(mocker):
4446 assert project == error
4547
4648
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+
4766def test_create_new_client (mocker ):
4867 project = mocker .MagicMock ()
4968 _get_project_for_url_or_path = mocker .patch (
@@ -118,9 +137,9 @@ def test__get_document_no_url_or_path(self, mocker):
118137
119138 from openapi_python_client import _get_document
120139
121- with pytest .raises (ValueError ):
122- _get_document (url = None , path = None )
140+ result = _get_document (url = None , path = None )
123141
142+ assert result == GeneratorError (header = "No URL or Path provided" )
124143 get .assert_not_called ()
125144 Path .assert_not_called ()
126145 loads .assert_not_called ()
@@ -132,13 +151,28 @@ def test__get_document_url_and_path(self, mocker):
132151
133152 from openapi_python_client import _get_document
134153
135- with pytest .raises (ValueError ):
136- _get_document (url = mocker .MagicMock (), path = mocker .MagicMock ())
154+ result = _get_document (url = mocker .MagicMock (), path = mocker .MagicMock ())
137155
156+ assert result == GeneratorError (header = "Provide URL or Path, not both." )
138157 get .assert_not_called ()
139158 Path .assert_not_called ()
140159 loads .assert_not_called ()
141160
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+
142176 def test__get_document_url_no_path (self , mocker ):
143177 get = mocker .patch ("httpx.get" )
144178 Path = mocker .patch ("openapi_python_client.Path" )
@@ -166,6 +200,20 @@ def test__get_document_path_no_url(self, mocker):
166200 path .read_bytes .assert_called_once ()
167201 loads .assert_called_once_with (path .read_bytes ())
168202
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+
169217
170218class TestProject :
171219 def test___init__ (self , mocker ):
0 commit comments