Skip to content

Commit 5040dcc

Browse files
committed
test: Fix tests that were failing on Python 3.7
1 parent 0a106eb commit 5040dcc

File tree

2 files changed

+36
-57
lines changed

2 files changed

+36
-57
lines changed

tests/test___init__.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import httpcore
44
import jinja2
55
import pytest
6-
import yaml
76

87
from openapi_python_client import Config, ErrorLevel, GeneratorError, Project
98

@@ -148,7 +147,7 @@ def test_update_existing_client_project_error(mocker):
148147
class TestGetJson:
149148
def test__get_document_no_url_or_path(self, mocker):
150149
get = mocker.patch("httpx.get")
151-
Path = mocker.patch("openapi_python_client.Path")
150+
_Path = mocker.patch("openapi_python_client.Path")
152151
loads = mocker.patch("yaml.safe_load")
153152

154153
from openapi_python_client import _get_document
@@ -157,12 +156,12 @@ def test__get_document_no_url_or_path(self, mocker):
157156

158157
assert result == GeneratorError(header="No URL or Path provided")
159158
get.assert_not_called()
160-
Path.assert_not_called()
159+
_Path.assert_not_called()
161160
loads.assert_not_called()
162161

163162
def test__get_document_url_and_path(self, mocker):
164163
get = mocker.patch("httpx.get")
165-
Path = mocker.patch("openapi_python_client.Path")
164+
_Path = mocker.patch("openapi_python_client.Path")
166165
loads = mocker.patch("yaml.safe_load")
167166

168167
from openapi_python_client import _get_document
@@ -171,12 +170,12 @@ def test__get_document_url_and_path(self, mocker):
171170

172171
assert result == GeneratorError(header="Provide URL or Path, not both.")
173172
get.assert_not_called()
174-
Path.assert_not_called()
173+
_Path.assert_not_called()
175174
loads.assert_not_called()
176175

177176
def test__get_document_bad_url(self, mocker):
178177
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")
180179
loads = mocker.patch("yaml.safe_load")
181180

182181
from openapi_python_client import _get_document
@@ -186,49 +185,47 @@ def test__get_document_bad_url(self, mocker):
186185

187186
assert result == GeneratorError(header="Could not get OpenAPI document from provided URL")
188187
get.assert_called_once_with(url)
189-
Path.assert_not_called()
188+
_Path.assert_not_called()
190189
loads.assert_not_called()
191190

192191
def test__get_document_url_no_path(self, mocker):
193192
get = mocker.patch("httpx.get")
194-
Path = mocker.patch("openapi_python_client.Path")
193+
_Path = mocker.patch("openapi_python_client.Path")
195194
loads = mocker.patch("yaml.safe_load")
196195

197196
from openapi_python_client import _get_document
198197

199-
url = mocker.MagicMock()
198+
url = "test"
200199
_get_document(url=url, path=None)
201200

202201
get.assert_called_once_with(url)
203-
Path.assert_not_called()
202+
_Path.assert_not_called()
204203
loads.assert_called_once_with(get().content)
205204

206-
def test__get_document_path_no_url(self, mocker):
205+
def test__get_document_path_no_url(self, tmp_path, mocker):
207206
get = mocker.patch("httpx.get")
208207
loads = mocker.patch("yaml.safe_load")
208+
path = tmp_path / "test.yaml"
209+
path.write_text("some test data")
209210

210211
from openapi_python_client import _get_document
211212

212-
path = mocker.MagicMock()
213213
_get_document(url=None, path=path)
214214

215215
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")
218217

219-
def test__get_document_bad_yaml(self, mocker):
218+
def test__get_document_bad_yaml(self, mocker, tmp_path):
220219
get = mocker.patch("httpx.get")
221-
loads = mocker.patch("yaml.safe_load", side_effect=yaml.YAMLError("error line 2"))
222-
223220
from openapi_python_client import _get_document
224221

225-
path = mocker.MagicMock()
222+
path = tmp_path / "test.yaml"
223+
path.write_text("'")
226224
result = _get_document(url=None, path=path)
227225

228226
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: error line 2")
227+
assert isinstance(result, GeneratorError)
228+
assert "Invalid YAML" in result.header
232229

233230
def test__get_document_json(self, mocker):
234231
class FakeResponse:
@@ -247,6 +244,7 @@ class FakeResponse:
247244

248245
get.assert_called_once()
249246
json_loads.assert_called_once_with(FakeResponse.content.decode())
247+
yaml_loads.assert_not_called()
250248
assert result == json_result
251249

252250
def test__get_document_bad_json(self, mocker):
@@ -255,7 +253,6 @@ class FakeResponse:
255253
headers = {"content-type": "application/json; encoding=utf8"}
256254

257255
get = mocker.patch("httpx.get", return_value=FakeResponse())
258-
json_result = mocker.MagicMock()
259256

260257
from openapi_python_client import _get_document
261258

tests/test_config.py

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,11 @@
11
import json
2-
import pathlib
32

43
import pytest
54
import yaml
65

76
from openapi_python_client.config import Config
87

98

10-
def test_load_from_path(mocker):
11-
from openapi_python_client import utils
12-
13-
override1 = {"class_name": "ExampleClass", "module_name": "example_module"}
14-
override2 = {"class_name": "DifferentClass", "module_name": "different_module"}
15-
safe_load = mocker.patch(
16-
"yaml.safe_load",
17-
return_value={
18-
"field_prefix": "blah",
19-
"class_overrides": {"Class1": override1, "Class2": override2},
20-
"project_name_override": "project-name",
21-
"package_name_override": "package_name",
22-
"package_version_override": "package_version",
23-
},
24-
)
25-
fake_path = mocker.MagicMock(autospec=pathlib.Path)
26-
27-
config = Config.load_from_path(fake_path)
28-
safe_load.assert_called()
29-
assert config.field_prefix == "blah"
30-
assert config.class_overrides["Class1"] == override1
31-
assert config.class_overrides["Class2"] == override2
32-
assert config.project_name_override == "project-name"
33-
assert config.package_name_override == "package_name"
34-
assert config.package_version_override == "package_version"
35-
36-
37-
DATA = {"class_overrides": {"Class1": {"class_name": "ExampleClass", "module_name": "example_module"}}}
38-
39-
409
def json_with_tabs(d):
4110
return json.dumps(d, indent=4).replace(" ", "\t")
4211

@@ -50,10 +19,23 @@ def json_with_tabs(d):
5019
("example.json", json_with_tabs),
5120
],
5221
)
53-
def test_load_filenames(tmp_path, filename, dump):
22+
def test_load_from_path(tmp_path, filename, dump):
5423
yml_file = tmp_path.joinpath(filename)
55-
with open(yml_file, "w") as f:
56-
f.write(dump(DATA))
24+
override1 = {"class_name": "ExampleClass", "module_name": "example_module"}
25+
override2 = {"class_name": "DifferentClass", "module_name": "different_module"}
26+
data = {
27+
"field_prefix": "blah",
28+
"class_overrides": {"Class1": override1, "Class2": override2},
29+
"project_name_override": "project-name",
30+
"package_name_override": "package_name",
31+
"package_version_override": "package_version",
32+
}
33+
yml_file.write_text(dump(data))
5734

5835
config = Config.load_from_path(yml_file)
59-
assert config.class_overrides == DATA["class_overrides"]
36+
assert config.field_prefix == "blah"
37+
assert config.class_overrides["Class1"] == override1
38+
assert config.class_overrides["Class2"] == override2
39+
assert config.project_name_override == "project-name"
40+
assert config.package_name_override == "package_name"
41+
assert config.package_version_override == "package_version"

0 commit comments

Comments
 (0)