forked from MarcoMuellner/openapi-python-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_generate_data.py
184 lines (152 loc) · 7.39 KB
/
test_generate_data.py
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
from pathlib import Path
import shutil
import subprocess
import pytest
import yaml
from httpx import ConnectError
from orjson import orjson
from pydantic import ValidationError
from openapi_python_generator.common import FormatOptions, Formatter, HTTPLibrary
from openapi_python_generator.common import library_config_dict
from openapi_python_generator.generate_data import generate_data
from openapi_python_generator.generate_data import get_open_api
from openapi_python_generator.generate_data import write_data
from openapi_python_generator.language_converters.python.generator import generator
from tests.conftest import test_data_folder
from tests.conftest import test_data_path
from tests.conftest import test_result_path
def test_get_open_api(model_data):
# Test JSON file
assert get_open_api(test_data_path) == model_data
# Create YAML version of the test file
yaml_path = test_data_path.with_suffix('.yaml')
with open(test_data_path) as f:
json_content = orjson.loads(f.read())
with open(yaml_path, 'w') as f:
yaml.dump(json_content, f)
# Test YAML file
assert get_open_api(yaml_path) == model_data
# Cleanup YAML file
yaml_path.unlink()
# Test remote file failure
with pytest.raises(ConnectError):
assert get_open_api("http://localhost:8080/api/openapi.json")
# Test invalid OpenAPI spec
with pytest.raises(ValidationError):
assert get_open_api(test_data_folder / "failing_api.json")
# Test non-existent file
with pytest.raises(FileNotFoundError):
assert get_open_api(test_data_folder / "file_does_not_exist.json")
def test_generate_data(model_data_with_cleanup):
generate_data(test_data_path, test_result_path)
assert test_result_path.exists()
assert test_result_path.is_dir()
assert (test_result_path / "api_config.py").exists()
assert (test_result_path / "models").exists()
assert (test_result_path / "models").is_dir()
assert (test_result_path / "services").exists()
assert (test_result_path / "services").is_dir()
assert (test_result_path / "models" / "__init__.py").exists()
assert (test_result_path / "services" / "__init__.py").exists()
assert (test_result_path / "services" / "__init__.py").is_file()
assert (test_result_path / "models" / "__init__.py").is_file()
assert (test_result_path / "__init__.py").exists()
assert (test_result_path / "__init__.py").is_file()
def test_write_data(model_data_with_cleanup):
result = generator(model_data_with_cleanup, library_config_dict[HTTPLibrary.httpx])
write_data(result, test_result_path, Formatter.BLACK)
assert test_result_path.exists()
assert test_result_path.is_dir()
assert (test_result_path / "api_config.py").exists()
assert (test_result_path / "models").exists()
assert (test_result_path / "models").is_dir()
assert (test_result_path / "services").exists()
assert (test_result_path / "services").is_dir()
assert (test_result_path / "models" / "__init__.py").exists()
assert (test_result_path / "services" / "__init__.py").exists()
assert (test_result_path / "services" / "__init__.py").is_file()
assert (test_result_path / "models" / "__init__.py").is_file()
assert (test_result_path / "__init__.py").exists()
assert (test_result_path / "__init__.py").is_file()
# delete test_result_path folder
shutil.rmtree(test_result_path)
model_data_copy = model_data_with_cleanup.copy()
model_data_copy.components = None
model_data_copy.paths = None
result = generator(model_data_copy, library_config_dict[HTTPLibrary.httpx])
write_data(result, test_result_path, Formatter.BLACK)
assert test_result_path.exists()
assert test_result_path.is_dir()
assert (test_result_path / "api_config.py").exists()
assert (test_result_path / "models").exists()
assert (test_result_path / "models").is_dir()
assert (test_result_path / "services").exists()
assert (test_result_path / "services").is_dir()
assert (test_result_path / "models" / "__init__.py").exists()
assert (test_result_path / "services" / "__init__.py").exists()
assert (test_result_path / "services" / "__init__.py").is_file()
assert (test_result_path / "models" / "__init__.py").is_file()
assert (test_result_path / "__init__.py").exists()
assert (test_result_path / "__init__.py").is_file()
def test_write_formatted_data(model_data_with_cleanup):
result = generator(model_data_with_cleanup, library_config_dict[HTTPLibrary.httpx])
# First write code without formatter
write_data(result, test_result_path, Formatter.NONE)
assert test_result_path.exists()
assert test_result_path.is_dir()
assert (test_result_path / "api_config.py").exists()
assert (test_result_path / "models").exists()
assert (test_result_path / "models").is_dir()
assert (test_result_path / "services").exists()
assert (test_result_path / "services").is_dir()
assert (test_result_path / "models" / "__init__.py").exists()
assert (test_result_path / "services" / "__init__.py").exists()
assert (test_result_path / "services" / "__init__.py").is_file()
assert (test_result_path / "models" / "__init__.py").is_file()
assert (test_result_path / "__init__.py").exists()
assert (test_result_path / "__init__.py").is_file()
assert not files_are_black_formatted(test_result_path)
# delete test_result_path folder
shutil.rmtree(test_result_path)
model_data_copy = model_data_with_cleanup.copy()
model_data_copy.components = None
model_data_copy.paths = None
result = generator(model_data_copy, library_config_dict[HTTPLibrary.httpx])
write_data(result, test_result_path, Formatter.BLACK)
assert test_result_path.exists()
assert test_result_path.is_dir()
assert (test_result_path / "api_config.py").exists()
assert (test_result_path / "models").exists()
assert (test_result_path / "models").is_dir()
assert (test_result_path / "services").exists()
assert (test_result_path / "services").is_dir()
assert (test_result_path / "models" / "__init__.py").exists()
assert (test_result_path / "services" / "__init__.py").exists()
assert (test_result_path / "services" / "__init__.py").is_file()
assert (test_result_path / "models" / "__init__.py").is_file()
assert (test_result_path / "__init__.py").exists()
assert (test_result_path / "__init__.py").is_file()
assert files_are_black_formatted(test_result_path)
def files_are_black_formatted(test_result_path: Path) -> bool:
# Run the `black --check` command on all files. This does not write any file.
result = subprocess.run([
"black",
"--check",
# Overwrite any exclusion due to a .gitignore.
"--exclude", "''",
# Settings also used when formatting the code when writing it
"--fast" if FormatOptions.skip_validation else "--safe",
"--line-length", str(FormatOptions.line_length),
# The source directory
str(test_result_path.absolute())
],
capture_output=True,
text=True
)
# With `--check` the return status has the following meaning:
# - Return code 0 means nothing would change.
# - Return code 1 means some files would be reformatted.
# - Return code 123 means there was an internal error.
if result.returncode == 123:
result.check_returncode # raise the error
return result.returncode == 0