Skip to content

Commit 3bedaaa

Browse files
authored
test: make integration helpers explicit about output model (#3028)
1 parent b868fa8 commit 3bedaaa

3 files changed

Lines changed: 78 additions & 19 deletions

File tree

tests/main/conftest.py

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,40 @@ def _validate_extra_args(extra_args: Sequence[str] | None) -> None:
163163
pytest.fail(f"Invalid CLI options in extra_args: {invalid_args}. Valid options: {sorted(_VALID_CLI_OPTIONS)}")
164164

165165

166+
def _get_argument_value(arguments: Sequence[str] | None, argument_name: str) -> str | None:
167+
"""Extract argument value from arguments."""
168+
if arguments is None:
169+
return None
170+
argument_list = list(arguments)
171+
for index, argument in enumerate(argument_list):
172+
if argument == argument_name and index + 1 < len(argument_list):
173+
return argument_list[index + 1]
174+
return None
175+
176+
177+
def _normalize_extra_args(
178+
extra_args: Sequence[str] | None,
179+
*,
180+
default_output_model_type: str | None,
181+
) -> list[str] | None:
182+
"""Return a copy of extra_args with default output model injected when needed."""
183+
normalized_extra_args = list(extra_args) if extra_args is not None else []
184+
if (
185+
default_output_model_type is not None
186+
and _get_argument_value(normalized_extra_args, "--output-model-type") is None
187+
):
188+
normalized_extra_args.extend(["--output-model-type", default_output_model_type])
189+
return normalized_extra_args or None
190+
191+
166192
def _extend_args(
167193
args: list[str],
168194
*,
169195
input_path: Path | None = None,
170196
output_path: Path | None = None,
171197
input_file_type: InputFileTypeLiteral | None = None,
172198
extra_args: Sequence[str] | None = None,
199+
default_output_model_type: str | None = None,
173200
) -> None:
174201
"""Extend args with optional input_path, output_path, input_file_type and extra_args."""
175202
if input_path is not None:
@@ -178,6 +205,7 @@ def _extend_args(
178205
args.extend(["--output", str(output_path)])
179206
if input_file_type is not None:
180207
args.extend(["--input-file-type", input_file_type])
208+
extra_args = _normalize_extra_args(extra_args, default_output_model_type=default_output_model_type)
181209
_validate_extra_args(extra_args)
182210
if extra_args is not None:
183211
args.extend(extra_args)
@@ -190,12 +218,18 @@ def _run_main(
190218
*,
191219
extra_args: Sequence[str] | None = None,
192220
copy_files: CopyFilesMapping | None = None,
221+
default_output_model_type: str | None = None,
193222
) -> Exit:
194223
"""Execute main() with standard arguments (internal use)."""
195224
_copy_files(copy_files)
196225
args: list[str] = []
197226
_extend_args(
198-
args, input_path=input_path, output_path=output_path, input_file_type=input_file_type, extra_args=extra_args
227+
args,
228+
input_path=input_path,
229+
output_path=output_path,
230+
input_file_type=input_file_type,
231+
extra_args=extra_args,
232+
default_output_model_type=default_output_model_type,
199233
)
200234
return main(args)
201235

@@ -206,10 +240,17 @@ def _run_main_url(
206240
input_file_type: InputFileTypeLiteral | None = None,
207241
*,
208242
extra_args: Sequence[str] | None = None,
243+
default_output_model_type: str | None = None,
209244
) -> Exit:
210245
"""Execute main() with URL input (internal use)."""
211246
args = ["--url", url]
212-
_extend_args(args, output_path=output_path, input_file_type=input_file_type, extra_args=extra_args)
247+
_extend_args(
248+
args,
249+
output_path=output_path,
250+
input_file_type=input_file_type,
251+
extra_args=extra_args,
252+
default_output_model_type=default_output_model_type,
253+
)
213254
return main(args)
214255

215256

@@ -248,6 +289,7 @@ def run_main_and_assert( # noqa: PLR0912
248289
output_path: Path | None = None,
249290
input_file_type: InputFileTypeLiteral | None = None,
250291
extra_args: Sequence[str] | None = None,
292+
default_output_model_type: str | None = DataModelType.PydanticBaseModel.value,
251293
expected_exit: Exit = Exit.OK,
252294
# Output verification options (use one)
253295
assert_func: AssertFileContent | None = None,
@@ -289,6 +331,9 @@ def run_main_and_assert( # noqa: PLR0912
289331
Common options:
290332
input_file_type: Type of input file (openapi, jsonschema, graphql, etc.)
291333
extra_args: Additional CLI arguments
334+
default_output_model_type: Output model type injected when extra_args omits
335+
--output-model-type. Set to None for tests that intentionally validate
336+
the real CLI default.
292337
expected_exit: Expected exit code (default: Exit.OK)
293338
copy_files: Files to copy before running
294339
@@ -325,20 +370,39 @@ def run_main_and_assert( # noqa: PLR0912
325370
pytest.fail("monkeypatch is required when using stdin_path")
326371
monkeypatch.setattr("sys.stdin", stdin_path.open(encoding="utf-8"))
327372
args: list[str] = []
328-
_extend_args(args, output_path=output_path, input_file_type=input_file_type, extra_args=extra_args)
373+
_extend_args(
374+
args,
375+
output_path=output_path,
376+
input_file_type=input_file_type,
377+
extra_args=extra_args,
378+
default_output_model_type=default_output_model_type,
379+
)
329380
return_code = main(args)
330381
# Handle stdout-only output (no output_path)
331382
elif output_path is None:
332383
if input_path is None: # pragma: no cover
333384
pytest.fail("input_path is required when output_path is None")
334385
args = []
335-
_extend_args(args, input_path=input_path, input_file_type=input_file_type, extra_args=extra_args)
386+
_extend_args(
387+
args,
388+
input_path=input_path,
389+
input_file_type=input_file_type,
390+
extra_args=extra_args,
391+
default_output_model_type=default_output_model_type,
392+
)
336393
return_code = main(args)
337394
# Standard file input
338395
else:
339396
if input_path is None: # pragma: no cover
340397
pytest.fail("input_path is required")
341-
return_code = _run_main(input_path, output_path, input_file_type, extra_args=extra_args, copy_files=copy_files)
398+
return_code = _run_main(
399+
input_path,
400+
output_path,
401+
input_file_type,
402+
extra_args=extra_args,
403+
copy_files=copy_files,
404+
default_output_model_type=default_output_model_type,
405+
)
342406

343407
_assert_exit_code(return_code, expected_exit, f"Input: {input_path}")
344408

@@ -409,17 +473,6 @@ def run_main_and_assert( # noqa: PLR0912
409473
_validate_output_files(output_path, extra_args, force_exec_validation=force_exec_validation)
410474

411475

412-
def _get_argument_value(arguments: Sequence[str] | None, argument_name: str) -> str | None:
413-
"""Extract argument value from arguments."""
414-
if arguments is None:
415-
return None
416-
argument_list = list(arguments)
417-
for index, argument in enumerate(argument_list):
418-
if argument == argument_name and index + 1 < len(argument_list):
419-
return argument_list[index + 1]
420-
return None
421-
422-
423476
def _parse_target_version(extra_arguments: Sequence[str] | None) -> tuple[int, int] | None:
424477
"""Parse target Python version from arguments."""
425478
if (target_version := _get_argument_value(extra_arguments, "--target-python-version")) is None:

tests/main/test_main_general.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def test_space_and_special_characters_dict(output_file: Path) -> None:
112112
output_path=output_file,
113113
input_file_type="dict",
114114
assert_func=assert_file_content,
115+
default_output_model_type=None,
115116
)
116117

117118

tests/test_input_model.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import pydantic
1111
import pytest
1212

13+
from datamodel_code_generator import DataModelType, arguments
1314
from datamodel_code_generator import __main__ as main_module
14-
from datamodel_code_generator import arguments
1515
from datamodel_code_generator.__main__ import Exit, main
1616
from tests.conftest import assert_output, freeze_time
1717

@@ -54,12 +54,16 @@ def run_input_model_and_assert(
5454
output_path: Path,
5555
expected_file: Path,
5656
extra_args: Sequence[str] | None = None,
57+
default_output_model_type: str | None = DataModelType.PydanticBaseModel.value,
5758
) -> None:
5859
"""Run main with --input-model and assert results."""
5960
__tracebackhide__ = True
6061
args = ["--input-model", input_model, "--output", str(output_path)]
61-
if extra_args:
62-
args.extend(extra_args)
62+
normalized_extra_args = list(extra_args) if extra_args else []
63+
if default_output_model_type is not None and "--output-model-type" not in normalized_extra_args:
64+
normalized_extra_args.extend(["--output-model-type", default_output_model_type])
65+
if normalized_extra_args:
66+
args.extend(normalized_extra_args)
6367

6468
with freeze_time(TIMESTAMP):
6569
return_code = main(args)
@@ -111,6 +115,7 @@ def test_input_model_pydantic_basemodel(tmp_path: Path) -> None:
111115
input_model="tests.data.python.input_model.pydantic_models:User",
112116
output_path=tmp_path / "output.py",
113117
expected_file=EXPECTED_INPUT_MODEL_PATH / "pydantic_basemodel.py",
118+
default_output_model_type=None,
114119
)
115120

116121

0 commit comments

Comments
 (0)