Skip to content

Commit 6dac609

Browse files
committed
Cover parsed source cache parity
1 parent cab75a2 commit 6dac609

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
"""Parity tests for the process-local parsed source cache."""
2+
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
6+
7+
import pytest
8+
9+
from datamodel_code_generator import InputFileType, _clear_parser_source_data_cache
10+
from tests.conftest import assert_output
11+
from tests.main.conftest import JSON_SCHEMA_DATA_PATH, OPEN_API_DATA_PATH, run_main_with_args
12+
13+
if TYPE_CHECKING:
14+
from collections.abc import Sequence
15+
from pathlib import Path
16+
17+
18+
def _input_file_type_option(input_file_type: InputFileType) -> str:
19+
match input_file_type:
20+
case InputFileType.JsonSchema:
21+
return "jsonschema"
22+
case InputFileType.OpenAPI: # pragma: no branch
23+
return "openapi"
24+
25+
26+
def _build_generate_args(
27+
input_path: Path,
28+
output_path: Path,
29+
input_file_type: InputFileType,
30+
extra_args: Sequence[str] | None,
31+
) -> list[str]:
32+
args = [
33+
"--input",
34+
str(input_path),
35+
"--output",
36+
str(output_path),
37+
"--input-file-type",
38+
_input_file_type_option(input_file_type),
39+
"--disable-timestamp",
40+
"--formatters",
41+
"builtin",
42+
]
43+
if extra := list(extra_args or ()):
44+
return [*args, *extra]
45+
return args
46+
47+
48+
def _run_generate_with_parsed_source_cache(
49+
input_path: Path,
50+
output_path: Path,
51+
input_file_type: InputFileType,
52+
*,
53+
use_cache: bool,
54+
extra_args: Sequence[str] | None,
55+
) -> None:
56+
_clear_parser_source_data_cache()
57+
run_main_with_args(
58+
_build_generate_args(input_path, output_path, input_file_type, extra_args),
59+
use_parsed_source_cache=use_cache,
60+
use_builtin_default_formatter=False,
61+
)
62+
63+
64+
@pytest.mark.parametrize(
65+
("input_path", "input_file_type", "extra_args"),
66+
[
67+
pytest.param(
68+
JSON_SCHEMA_DATA_PATH / "external_definitions_root.json",
69+
InputFileType.JsonSchema,
70+
None,
71+
id="jsonschema-external-definitions",
72+
),
73+
pytest.param(
74+
JSON_SCHEMA_DATA_PATH / "all_of_ref" / "test.json",
75+
InputFileType.JsonSchema,
76+
["--class-name", "Test"],
77+
id="jsonschema-relative-all-of",
78+
),
79+
pytest.param(
80+
OPEN_API_DATA_PATH / "paths_external_ref" / "openapi.yaml",
81+
InputFileType.OpenAPI,
82+
["--openapi-scopes", "paths"],
83+
id="openapi-paths-external-ref",
84+
),
85+
pytest.param(
86+
OPEN_API_DATA_PATH / "external_ref_mapping" / "api.yaml",
87+
InputFileType.OpenAPI,
88+
None,
89+
id="openapi-external-ref-mapping",
90+
),
91+
],
92+
)
93+
def test_generate_output_matches_with_and_without_parsed_source_cache(
94+
input_path: Path,
95+
input_file_type: InputFileType,
96+
extra_args: Sequence[str] | None,
97+
tmp_path: Path,
98+
) -> None:
99+
"""Keep representative ref-heavy generation output stable across cache states."""
100+
cached_output = tmp_path / "cached.py"
101+
uncached_output = tmp_path / "uncached.py"
102+
103+
_run_generate_with_parsed_source_cache(
104+
input_path,
105+
cached_output,
106+
input_file_type,
107+
use_cache=True,
108+
extra_args=extra_args,
109+
)
110+
_run_generate_with_parsed_source_cache(
111+
input_path,
112+
uncached_output,
113+
input_file_type,
114+
use_cache=False,
115+
extra_args=extra_args,
116+
)
117+
118+
assert_output(uncached_output.read_text(encoding="utf-8"), cached_output)

0 commit comments

Comments
 (0)