-
-
Notifications
You must be signed in to change notification settings - Fork 449
Cover parsed source cache parity #3535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+121
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| """Parity tests for the process-local parsed source cache.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
|
|
||
| from datamodel_code_generator import InputFileType, _clear_parser_source_data_cache | ||
| from tests.conftest import assert_output | ||
| from tests.main.conftest import JSON_SCHEMA_DATA_PATH, OPEN_API_DATA_PATH, run_main_with_args | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Sequence | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def _input_file_type_option(input_file_type: InputFileType) -> str: | ||
Check noticeCode scanning / CodeQL Explicit returns mixed with implicit (fall through) returns Note test
Mixing implicit and explicit returns may indicate an error, as implicit returns always return None.
|
||
| match input_file_type: | ||
| case InputFileType.JsonSchema: | ||
| return "jsonschema" | ||
| case InputFileType.OpenAPI: | ||
| return "openapi" | ||
| case _: # pragma: no cover | ||
| msg = f"Unsupported parsed source cache parity input type: {input_file_type}" | ||
| raise AssertionError(msg) | ||
|
|
||
|
|
||
| def _build_generate_args( | ||
| input_path: Path, | ||
| output_path: Path, | ||
| input_file_type: InputFileType, | ||
| extra_args: Sequence[str] | None, | ||
| ) -> list[str]: | ||
| args = [ | ||
| "--input", | ||
| str(input_path), | ||
| "--output", | ||
| str(output_path), | ||
| "--input-file-type", | ||
| _input_file_type_option(input_file_type), | ||
| "--disable-timestamp", | ||
| "--formatters", | ||
| "builtin", | ||
| ] | ||
| if extra := list(extra_args or ()): | ||
| return [*args, *extra] | ||
| return args | ||
|
|
||
|
|
||
| def _run_generate_with_parsed_source_cache( | ||
| input_path: Path, | ||
| output_path: Path, | ||
| input_file_type: InputFileType, | ||
| *, | ||
| use_cache: bool, | ||
| extra_args: Sequence[str] | None, | ||
| ) -> None: | ||
| _clear_parser_source_data_cache() | ||
| run_main_with_args( | ||
| _build_generate_args(input_path, output_path, input_file_type, extra_args), | ||
| use_parsed_source_cache=use_cache, | ||
| use_builtin_default_formatter=False, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("input_path", "input_file_type", "extra_args"), | ||
| [ | ||
| pytest.param( | ||
| JSON_SCHEMA_DATA_PATH / "external_definitions_root.json", | ||
| InputFileType.JsonSchema, | ||
| None, | ||
| id="jsonschema-external-definitions", | ||
| ), | ||
| pytest.param( | ||
| JSON_SCHEMA_DATA_PATH / "all_of_ref" / "test.json", | ||
| InputFileType.JsonSchema, | ||
| ["--class-name", "Test"], | ||
| id="jsonschema-relative-all-of", | ||
| ), | ||
| pytest.param( | ||
| OPEN_API_DATA_PATH / "paths_external_ref" / "openapi.yaml", | ||
| InputFileType.OpenAPI, | ||
| ["--openapi-scopes", "paths"], | ||
| id="openapi-paths-external-ref", | ||
| ), | ||
| pytest.param( | ||
| OPEN_API_DATA_PATH / "external_ref_mapping" / "api.yaml", | ||
| InputFileType.OpenAPI, | ||
| None, | ||
| id="openapi-external-ref-mapping", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_generate_output_matches_with_and_without_parsed_source_cache( | ||
| input_path: Path, | ||
| input_file_type: InputFileType, | ||
| extra_args: Sequence[str] | None, | ||
| tmp_path: Path, | ||
| ) -> None: | ||
| """Keep representative ref-heavy generation output stable across cache states.""" | ||
| cached_output = tmp_path / "cached.py" | ||
| uncached_output = tmp_path / "uncached.py" | ||
|
|
||
| _run_generate_with_parsed_source_cache( | ||
| input_path, | ||
| cached_output, | ||
| input_file_type, | ||
| use_cache=True, | ||
| extra_args=extra_args, | ||
| ) | ||
| _run_generate_with_parsed_source_cache( | ||
| input_path, | ||
| uncached_output, | ||
| input_file_type, | ||
| use_cache=False, | ||
| extra_args=extra_args, | ||
| ) | ||
|
|
||
| assert_output(uncached_output.read_text(encoding="utf-8"), cached_output) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.