|
5 | 5 | from argparse import ArgumentTypeError, Namespace |
6 | 6 | from typing import TYPE_CHECKING |
7 | 7 |
|
| 8 | +import black |
8 | 9 | import pytest |
9 | 10 |
|
10 | 11 | from datamodel_code_generator import ( |
@@ -864,3 +865,88 @@ def test_check_respects_pyproject_toml_settings(tmp_path: Path) -> None: |
864 | 865 | extra_args=["--disable-timestamp", "--check"], |
865 | 866 | expected_exit=Exit.OK, |
866 | 867 | ) |
| 868 | + |
| 869 | + |
| 870 | +def test_use_specialized_enum_requires_python_311( |
| 871 | + tmp_path: Path, |
| 872 | + capsys: pytest.CaptureFixture[str], |
| 873 | +) -> None: |
| 874 | + """Test --use-specialized-enum requires --target-python-version 3.11+.""" |
| 875 | + input_json = tmp_path / "input.json" |
| 876 | + input_json.write_text( |
| 877 | + '{"type": "string", "enum": ["A", "B"]}', |
| 878 | + encoding="utf-8", |
| 879 | + ) |
| 880 | + |
| 881 | + run_main_and_assert( |
| 882 | + input_path=input_json, |
| 883 | + output_path=tmp_path / "output.py", |
| 884 | + input_file_type="jsonschema", |
| 885 | + extra_args=["--use-specialized-enum"], |
| 886 | + expected_exit=Exit.ERROR, |
| 887 | + capsys=capsys, |
| 888 | + expected_stderr_contains="--use-specialized-enum requires --target-python-version 3.11 or later", |
| 889 | + ) |
| 890 | + |
| 891 | + |
| 892 | +@pytest.mark.skipif( |
| 893 | + black.__version__.split(".")[0] == "22", |
| 894 | + reason="Installed black doesn't support StrEnum formatting", |
| 895 | +) |
| 896 | +def test_use_specialized_enum_with_python_311_ok(output_file: Path) -> None: |
| 897 | + """Test --use-specialized-enum works with --target-python-version 3.11.""" |
| 898 | + run_main_and_assert( |
| 899 | + input_path=JSON_SCHEMA_DATA_PATH / "string_enum.json", |
| 900 | + output_path=output_file, |
| 901 | + input_file_type="jsonschema", |
| 902 | + extra_args=["--use-specialized-enum", "--target-python-version", "3.11"], |
| 903 | + assert_func=assert_file_content, |
| 904 | + expected_file="use_specialized_enum_py311.py", |
| 905 | + ) |
| 906 | + |
| 907 | + |
| 908 | +def test_use_specialized_enum_pyproject_requires_python_311( |
| 909 | + tmp_path: Path, |
| 910 | + capsys: pytest.CaptureFixture[str], |
| 911 | +) -> None: |
| 912 | + """Test use_specialized_enum in pyproject.toml requires target_python_version 3.11+.""" |
| 913 | + pyproject_toml = tmp_path / "pyproject.toml" |
| 914 | + pyproject_toml.write_text( |
| 915 | + "[tool.datamodel-codegen]\nuse_specialized_enum = true\n", |
| 916 | + encoding="utf-8", |
| 917 | + ) |
| 918 | + |
| 919 | + input_json = tmp_path / "input.json" |
| 920 | + input_json.write_text( |
| 921 | + '{"type": "string", "enum": ["A", "B"]}', |
| 922 | + encoding="utf-8", |
| 923 | + ) |
| 924 | + |
| 925 | + with chdir(tmp_path): |
| 926 | + run_main_and_assert( |
| 927 | + input_path=input_json, |
| 928 | + output_path=tmp_path / "output.py", |
| 929 | + input_file_type="jsonschema", |
| 930 | + expected_exit=Exit.ERROR, |
| 931 | + capsys=capsys, |
| 932 | + expected_stderr_contains="--use-specialized-enum requires --target-python-version 3.11 or later", |
| 933 | + ) |
| 934 | + |
| 935 | + |
| 936 | +def test_use_specialized_enum_pyproject_override_with_cli(output_file: Path, tmp_path: Path) -> None: |
| 937 | + """Test --no-use-specialized-enum CLI can override pyproject.toml use_specialized_enum=true.""" |
| 938 | + pyproject_toml = tmp_path / "pyproject.toml" |
| 939 | + pyproject_toml.write_text( |
| 940 | + "[tool.datamodel-codegen]\nuse_specialized_enum = true\n", |
| 941 | + encoding="utf-8", |
| 942 | + ) |
| 943 | + |
| 944 | + with chdir(tmp_path): |
| 945 | + run_main_and_assert( |
| 946 | + input_path=JSON_SCHEMA_DATA_PATH / "string_enum.json", |
| 947 | + output_path=output_file, |
| 948 | + input_file_type="jsonschema", |
| 949 | + extra_args=["--no-use-specialized-enum"], |
| 950 | + assert_func=assert_file_content, |
| 951 | + expected_file="no_use_specialized_enum.py", |
| 952 | + ) |
0 commit comments