|
4 | 4 | from pathlib import Path |
5 | 5 |
|
6 | 6 | import pytest |
7 | | -from pydantic import VERSION as _PYDANTIC_VERSION |
8 | 7 |
|
9 | 8 | from pydantic2ts import generate_typescript_defs |
10 | 9 | from pydantic2ts.cli.script import parse_cli_args |
11 | 10 |
|
12 | | -_PYDANTIC_VERSIONS = ( |
13 | | - ("v1",) if int(_PYDANTIC_VERSION.split(".")[0]) < 2 else ("v1", "v2") |
14 | | -) |
| 11 | +try: |
| 12 | + from pydantic import BaseModel |
| 13 | + from pydantic.v1 import BaseModel as BaseModelV1 |
| 14 | + |
| 15 | + assert BaseModel is not BaseModelV1 |
| 16 | + _PYDANTIC_VERSIONS = ("v1", "v2") |
| 17 | +except (ImportError, AttributeError): |
| 18 | + _PYDANTIC_VERSIONS = ("v1",) |
15 | 19 |
|
16 | 20 | _RESULTS_DIRECTORY = Path( |
17 | 21 | os.path.join(os.path.dirname(os.path.realpath(__file__)), "expected_results") |
18 | 22 | ) |
19 | 23 |
|
20 | 24 |
|
21 | | -def _get_input_module(test_name: str, pydantic_version: str) -> str: |
| 25 | +def _get_input_module(test_name: str, pydantic_version: str) -> Path: |
22 | 26 | return _RESULTS_DIRECTORY / test_name / pydantic_version / "input.py" |
23 | 27 |
|
24 | 28 |
|
@@ -50,19 +54,15 @@ def _run_test( |
50 | 54 | cmd += f" --exclude {model_to_exclude}" |
51 | 55 | subprocess.run(cmd, shell=True, check=True) |
52 | 56 |
|
53 | | - assert Path(output_path).read_text() == _get_expected_output( |
54 | | - test_name, pydantic_version |
55 | | - ) |
| 57 | + assert Path(output_path).read_text() == _get_expected_output(test_name, pydantic_version) |
56 | 58 |
|
57 | 59 |
|
58 | 60 | @pytest.mark.parametrize( |
59 | 61 | "pydantic_version, call_from_python", |
60 | 62 | product(_PYDANTIC_VERSIONS, [False, True]), |
61 | 63 | ) |
62 | 64 | def test_single_module(tmpdir, pydantic_version, call_from_python): |
63 | | - _run_test( |
64 | | - tmpdir, "single_module", pydantic_version, call_from_python=call_from_python |
65 | | - ) |
| 65 | + _run_test(tmpdir, "single_module", pydantic_version, call_from_python=call_from_python) |
66 | 66 |
|
67 | 67 |
|
68 | 68 | @pytest.mark.parametrize( |
@@ -102,31 +102,22 @@ def test_excluding_models(tmpdir, pydantic_version, call_from_python): |
102 | 102 | def test_computed_fields(tmpdir, pydantic_version, call_from_python): |
103 | 103 | if pydantic_version == "v1": |
104 | 104 | pytest.skip("Computed fields are a pydantic v2 feature") |
105 | | - _run_test( |
106 | | - tmpdir, "computed_fields", pydantic_version, call_from_python=call_from_python |
107 | | - ) |
| 105 | + _run_test(tmpdir, "computed_fields", pydantic_version, call_from_python=call_from_python) |
108 | 106 |
|
109 | 107 |
|
110 | 108 | @pytest.mark.parametrize( |
111 | 109 | "pydantic_version, call_from_python", |
112 | 110 | product(_PYDANTIC_VERSIONS, [False, True]), |
113 | 111 | ) |
114 | 112 | def test_extra_fields(tmpdir, pydantic_version, call_from_python): |
115 | | - _run_test( |
116 | | - tmpdir, "extra_fields", pydantic_version, call_from_python=call_from_python |
117 | | - ) |
| 113 | + _run_test(tmpdir, "extra_fields", pydantic_version, call_from_python=call_from_python) |
118 | 114 |
|
119 | 115 |
|
120 | 116 | def test_relative_filepath(tmpdir): |
121 | 117 | test_name = "single_module" |
122 | 118 | pydantic_version = _PYDANTIC_VERSIONS[0] |
123 | 119 | relative_path = ( |
124 | | - Path(".") |
125 | | - / "tests" |
126 | | - / "expected_results" |
127 | | - / test_name |
128 | | - / pydantic_version |
129 | | - / "input.py" |
| 120 | + Path(".") / "tests" / "expected_results" / test_name / pydantic_version / "input.py" |
130 | 121 | ) |
131 | 122 | _run_test( |
132 | 123 | tmpdir, |
@@ -170,9 +161,7 @@ def test_error_if_json2ts_not_installed(tmpdir): |
170 | 161 |
|
171 | 162 | def test_error_if_invalid_module_path(tmpdir): |
172 | 163 | with pytest.raises(ModuleNotFoundError): |
173 | | - generate_typescript_defs( |
174 | | - "fake_module", tmpdir.join("fake_module_output.ts").strpath |
175 | | - ) |
| 164 | + generate_typescript_defs("fake_module", tmpdir.join("fake_module_output.ts").strpath) |
176 | 165 |
|
177 | 166 |
|
178 | 167 | def test_parse_cli_args(): |
|
0 commit comments