forked from bogdandm/json2python-models
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_script.py
More file actions
351 lines (273 loc) · 12.4 KB
/
test_script.py
File metadata and controls
351 lines (273 loc) · 12.4 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import json
import re
import subprocess
import sys
import tempfile
import types
import uuid
from pathlib import Path
from time import time
import pytest
tmp_dir = tempfile.TemporaryDirectory(f"-pytest-{time()}")
tmp_path = Path(tmp_dir.name)
test_data_path = (Path(__file__) / ".." / "data").resolve().absolute()
if not test_data_path.exists():
test_data_path = Path("./test/test_cli/data").resolve().absolute()
# Create fixture to auto cleanup tmp directory after tests
@pytest.fixture(scope="session", autouse=True)
def tmp_dir_cleanup():
yield tmp_dir
tmp_dir.cleanup()
# download GitHub Gist dataset into tmp folder
with (test_data_path / "gists.json").open("r") as f:
gists = json.load(f)
assert type(gists) is list and gists and type(gists[0]) is dict
for item in gists:
with (tmp_path / f"{item['id']}.gist").open("w") as f:
json.dump(item, f)
# detect script path
setuptools_script = subprocess.call(["json2models"], shell=True) == 0
if setuptools_script:
executable = "json2models"
else:
python_path = sys.executable.replace('\\', '/')
executable = f"{python_path} -m json_to_models"
def test_help():
c = f"{executable} -h"
proc = subprocess.Popen(
c, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
assert not stderr, stderr
assert stdout, stdout
assert proc.returncode == 0
print(stdout.decode())
test_commands = [
pytest.param(
f"""{executable} -m Photo items "{test_data_path / 'photos.json'}" """, id="list1"),
pytest.param(
f"""{executable} -l Photo items "{test_data_path / 'photos.json'}" """, id="list1_legacy"),
pytest.param(
f"""{executable} -m User "{test_data_path / 'users.json'}" """, id="list2"),
pytest.param(
f"""{executable} -l User - "{test_data_path / 'users.json'}" """, id="list2_legacy"),
pytest.param(
f"""{executable} -m Photos "{test_data_path / 'photos.json'}" """, id="model1"),
pytest.param(f"""{executable} -m Model items "{test_data_path / 'photos.json'}" \
-m Model - "{test_data_path / 'users.json'}" """, id="duplicate_name"),
pytest.param(f"""{executable} -m Photo items "{test_data_path / 'photos.json'}" \
-m Photos "{test_data_path / 'photos.json'}" """,
id="list1_model1"),
pytest.param(f"""{executable} -m Photo items "{test_data_path / 'photos.json'}" \
-m User "{test_data_path / 'users.json'}" """,
id="list1_list2"),
pytest.param(f"""{executable} -m Gist "{tmp_path / '*.gist'}" --dkf files""",
id="gists"),
pytest.param(f"""{executable} -m Gist "{tmp_path / '*.gist'}" --dkf files --datetime""",
id="gists_datetime"),
pytest.param(f"""{executable} -m Gist "{tmp_path / '*.gist'}" --dkf files --merge percent number_10""",
id="gists_merge_policy"),
pytest.param(f"""{executable} -m Gist "{tmp_path / '*.gist'}" --dkf files --merge exact""",
id="gists_no_merge"),
pytest.param(f"""{executable} -m Gist "{tmp_path / '*.gist'}" --dkf files --datetime --strings-converters""",
id="gists_strings_converters"),
pytest.param(f"""{executable} -m User "{test_data_path / 'users.json'}" --strings-converters""",
id="users_strings_converters"),
pytest.param(f"""{executable} -m SomeUnicode "{test_data_path / 'unicode.json'}" """,
id="convert_unicode"),
pytest.param(f"""{executable} -m SomeUnicode "{test_data_path / 'unicode.json'}" --no-unidecode""",
id="dont_convert_unicode"),
pytest.param(f"""{executable} -m SomeUnicode "{test_data_path / 'unicode.json'}" --disable-unicode-conversion""",
id="dont_convert_unicode_2"),
pytest.param(f"""{executable} -m YamlFile "{test_data_path / 'spotify-swagger.yaml'}" -i yaml""",
id="yaml_file"),
pytest.param(f"""{executable} -m IniFile "{test_data_path / 'file.ini'}" -i ini""",
id="ini_file"),
]
def load_model(code, module_name=''):
module_name = module_name or uuid.uuid4().hex
module = types.ModuleType(module_name)
sys.modules[module_name] = module
exec(code, module.__dict__)
return module
def execute_test(command, output_file: Path = None, output=None) -> str:
proc = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = map(bytes.decode, proc.communicate())
if output_file:
assert output is None
with output_file.open(encoding='utf-8') as f:
output = f.read()
if output:
stdout = output
assert not stderr, stderr
assert stdout, stdout
assert proc.returncode == 0
load_model(stdout)
print(stdout)
return stdout
@pytest.mark.parametrize("command", test_commands)
def test_script(command):
execute_test(command)
@pytest.mark.parametrize("command", test_commands)
def test_script_flat(command):
command += " -s flat"
execute_test(command)
@pytest.mark.parametrize("command", test_commands)
def test_script_attrs(command):
command += " -f attrs"
stdout = execute_test(command)
assert "@attr.s" in stdout
@pytest.mark.parametrize("command", test_commands)
def test_script_pydantic(command):
command += " -f pydantic"
# Pydantic has native (str) -> (builtin_type) converters
command = command.replace('--strings-converters', '')
stdout = execute_test(command)
assert "(BaseModel):" in stdout
@pytest.mark.parametrize("command", test_commands)
def test_script_pydantic_disable_literals(command):
command += " -f pydantic --code-generator-kwargs max_literals=0"
# Pydantic has native (str) -> (builtin_type) converters
command = command.replace('--strings-converters', '')
stdout = execute_test(command)
assert "(BaseModel):" in stdout
assert "Literal" not in stdout
@pytest.mark.parametrize("command", test_commands)
def test_script_dataclasses(command):
command += " -f dataclasses"
stdout = execute_test(command)
assert "@dataclass" in stdout
@pytest.mark.parametrize("command", test_commands)
def test_script_custom(command):
command += " -f custom --code-generator json_to_models.models.attr.AttrsModelCodeGenerator"
command += ' --code-generator-kwargs "meta=true"'
stdout = execute_test(command)
assert "@attr.s" in stdout
@pytest.mark.parametrize("command", test_commands)
def test_add_preamble(command):
PREAMBLE_TEXT = """
# this is some test code
# to be added to the file
# let's see if it works
"""
stdout = execute_test(command + ' --preamble "' + PREAMBLE_TEXT + '"')
assert "let's see if it works" in stdout
@pytest.mark.parametrize("command", test_commands)
def test_disable_some_string_types_smoke(command):
command += " --disable-str-serializable-types float int"
execute_test(command)
@pytest.mark.parametrize("command", test_commands)
def test_add_trim_preamble(command):
def trim_header(line_string):
"""remove the quoted command and everything from the first class declaration onwards"""
lines = line_string.splitlines()
start = 0
end = 0
line_no = 0
for l in lines:
if l.startswith('"""'):
start = line_no
if l.startswith('class '):
end = line_no
break
line_no += 1
return lines[start:end]
expected_result = execute_test(command)
BLANK_SPACE = """
"""
# ensure blank space does not get propagated
stdout = execute_test(command + ' --preamble "' + BLANK_SPACE + '"')
assert trim_header(expected_result) == trim_header(stdout)
wrong_arguments_commands = [
pytest.param(f"""{executable} -m Model items "{test_data_path / 'photos.json'}" --merge unknown""",
id="wrong_merge_policy"),
pytest.param(f"""{executable} -m Model items "{test_data_path / 'photos.json'}" --merge unknown_10""",
id="wrong_merge_policy"),
pytest.param(f"""{executable} -m Model items "{test_data_path / 'photos.json'}" -f custom""",
id="custom_model_generator_without_class_link"),
pytest.param(f"""{executable} -m Model items "{test_data_path / 'photos.json'}" --code-generator test""",
id="class_link_without_custom_model_generator_enabled"),
pytest.param(f"""{executable} -m Model items "{test_data_path / 'photos.json'}" another_arg --code-generator test""",
id="4_args_model"),
pytest.param(f"""{executable} -m Model total "{test_data_path / 'photos.json'}" --code-generator test""",
id="non_dict_or_list_data"),
]
@pytest.mark.xfail(strict=True)
@pytest.mark.parametrize("command", wrong_arguments_commands)
def test_wrong_arguments(command):
print("Command:", command)
execute_test(command)
@pytest.mark.parametrize("command", test_commands)
def test_script_output_file(command):
file = tmp_path / 'out.py'
command += f" -o {file}"
execute_test(command, output_file=file)
cmds = [
pytest.param(f"""{executable} -m User "{test_data_path / 'users.json'}" -f pydantic --disable-str-serializable-types float int""",
id="users")
]
@pytest.mark.parametrize("command", cmds)
def test_disable_some_string_types(command):
stdout = execute_test(command)
assert 'lat: str' in stdout
assert 'lng: str' in stdout
assert not any(re.match(r'\s+zipcode:.+int.+', line)
for line in stdout.split('\n')), "zipcode should not be parsed as int"
# -- allow-words tests -------------------------------------------------------
allow_words_commands = [
pytest.param(
f"""{executable} -m User "{test_data_path / 'users.json'}" --allow-words id""",
id="users_allow_id",
),
pytest.param(
f"""{executable} -m Photo items "{test_data_path / 'photos.json'}" --allow-words id""",
id="photos_allow_id",
),
]
@pytest.mark.parametrize("command", allow_words_commands)
def test_allow_words_prevents_underscore(command):
"""--allow-words should remove the trailing '_' from the listed field names."""
stdout = execute_test(command)
assert "id:" in stdout, "field 'id' should appear unmodified"
assert "id_:" not in stdout, "field 'id_' should not appear when 'id' is allowed"
@pytest.mark.parametrize("command", allow_words_commands)
def test_allow_words_pydantic(command):
"""--allow-words should work with the pydantic framework."""
command += " -f pydantic"
stdout = execute_test(command)
assert "(BaseModel):" in stdout
assert "id:" in stdout
assert "id_:" not in stdout
@pytest.mark.parametrize("command", allow_words_commands)
def test_allow_words_attrs(command):
"""--allow-words should work with the attrs framework."""
command += " -f attrs"
stdout = execute_test(command)
assert "@attr.s" in stdout
assert "id:" in stdout
assert "id_:" not in stdout
@pytest.mark.parametrize("command", allow_words_commands)
def test_allow_words_dataclasses(command):
"""--allow-words should work with the dataclasses framework."""
command += " -f dataclasses"
stdout = execute_test(command)
assert "@dataclass" in stdout
assert "id:" in stdout
assert "id_:" not in stdout
def test_default_behavior_appends_underscore():
"""Without --allow-words the blacklisted 'id' field should be renamed to 'id_'."""
command = f"""{executable} -m User "{test_data_path / 'users.json'}" """
stdout = execute_test(command)
assert "id_:" in stdout, "field 'id' should be renamed to 'id_' by default"
assert "\n id:" not in stdout, "field 'id' should not appear unmodified without --allow-words"
def test_allow_words_multiple():
"""Multiple words can be unblacklisted at once with --allow-words."""
# Create a temporary JSON file whose keys include several blacklisted names
tmp_json = tmp_path / "allow_words_multi.json"
tmp_json.write_text(json.dumps(
{"id": 1, "type": "user", "hash": "abc", "name": "John"}))
command = f"""{executable} -m Model "{tmp_json}" --allow-words id type hash"""
stdout = execute_test(command)
for word in ("id", "type", "hash"):
assert f"{word}:" in stdout, f"field '{word}' should appear unmodified"
assert f"{word}_:" not in stdout, f"field '{word}_' should not appear when allowed"