Skip to content

Commit bbe91f9

Browse files
committed
Cover parsed source cache parity
1 parent 1870d87 commit bbe91f9

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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:
23+
return "openapi"
24+
case _:
25+
pytest.fail(f"Unsupported parsed source cache parity input type: {input_file_type}")
26+
27+
28+
def _build_generate_args(
29+
input_path: Path,
30+
output_path: Path,
31+
input_file_type: InputFileType,
32+
extra_args: Sequence[str] | None,
33+
) -> list[str]:
34+
args = [
35+
"--input",
36+
str(input_path),
37+
"--output",
38+
str(output_path),
39+
"--input-file-type",
40+
_input_file_type_option(input_file_type),
41+
"--disable-timestamp",
42+
"--formatters",
43+
"builtin",
44+
]
45+
if extra := list(extra_args or ()):
46+
return [*args, *extra]
47+
return args
48+
49+
50+
def _run_generate_with_parsed_source_cache(
51+
input_path: Path,
52+
output_path: Path,
53+
input_file_type: InputFileType,
54+
*,
55+
use_cache: bool,
56+
extra_args: Sequence[str] | None,
57+
) -> None:
58+
_clear_parser_source_data_cache()
59+
run_main_with_args(
60+
_build_generate_args(input_path, output_path, input_file_type, extra_args),
61+
use_parsed_source_cache=use_cache,
62+
use_builtin_default_formatter=False,
63+
)
64+
65+
66+
@pytest.mark.parametrize(
67+
("input_path", "input_file_type", "extra_args"),
68+
[
69+
pytest.param(
70+
JSON_SCHEMA_DATA_PATH / "external_definitions_root.json",
71+
InputFileType.JsonSchema,
72+
None,
73+
id="jsonschema-external-definitions",
74+
),
75+
pytest.param(
76+
JSON_SCHEMA_DATA_PATH / "all_of_ref" / "test.json",
77+
InputFileType.JsonSchema,
78+
["--class-name", "Test"],
79+
id="jsonschema-relative-all-of",
80+
),
81+
pytest.param(
82+
OPEN_API_DATA_PATH / "paths_external_ref" / "openapi.yaml",
83+
InputFileType.OpenAPI,
84+
["--openapi-scopes", "paths"],
85+
id="openapi-paths-external-ref",
86+
),
87+
pytest.param(
88+
OPEN_API_DATA_PATH / "external_ref_mapping" / "api.yaml",
89+
InputFileType.OpenAPI,
90+
None,
91+
id="openapi-external-ref-mapping",
92+
),
93+
],
94+
)
95+
def test_generate_output_matches_with_and_without_parsed_source_cache(
96+
input_path: Path,
97+
input_file_type: InputFileType,
98+
extra_args: Sequence[str] | None,
99+
tmp_path: Path,
100+
) -> None:
101+
"""Keep representative ref-heavy generation output stable across cache states."""
102+
cached_output = tmp_path / "cached.py"
103+
uncached_output = tmp_path / "uncached.py"
104+
105+
_run_generate_with_parsed_source_cache(
106+
input_path,
107+
cached_output,
108+
input_file_type,
109+
use_cache=True,
110+
extra_args=extra_args,
111+
)
112+
_run_generate_with_parsed_source_cache(
113+
input_path,
114+
uncached_output,
115+
input_file_type,
116+
use_cache=False,
117+
extra_args=extra_args,
118+
)
119+
120+
assert_output(uncached_output.read_text(encoding="utf-8"), cached_output)

0 commit comments

Comments
 (0)