Skip to content

Commit 6c9d5a3

Browse files
committed
Update test_script, update v1 tests to work with both pydantic<2 and pydantic>=2
1 parent 5a839c7 commit 6c9d5a3

File tree

8 files changed

+54
-41
lines changed

8 files changed

+54
-41
lines changed

tests/expected_results/excluding_models/v1/input.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
from pydantic import BaseModel
2-
from typing import Optional, List
1+
from typing import List, Optional
2+
3+
try:
4+
from pydantic.v1 import BaseModel
5+
except ImportError:
6+
from pydantic import BaseModel
37

48

59
class LoginCredentials(BaseModel):

tests/expected_results/extra_fields/v1/input.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
from pydantic import BaseModel, Extra
1+
try:
2+
from pydantic.v1 import BaseModel, Extra
3+
except ImportError:
4+
from pydantic import BaseModel, Extra
25

36

47
class ModelAllow(BaseModel, extra=Extra.allow):
58
a: str
69

10+
711
class ModelDefault(BaseModel):
812
a: str
9-

tests/expected_results/generics/v1/input.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
from datetime import datetime
2-
from typing import Generic, List, Optional, Type, TypeVar, cast
2+
from typing import Generic, List, Optional, Type, TypeVar
33

4-
from pydantic import BaseModel
5-
from pydantic.generics import GenericModel
4+
try:
5+
from pydantic.v1 import BaseModel
6+
from pydantic.v1.generics import GenericModel
7+
except ImportError:
8+
from pydantic import BaseModel
9+
from pydantic.generics import GenericModel
610

711
T = TypeVar("T")
812

@@ -26,7 +30,7 @@ def create_response_type(data_type: T, name: str) -> "Type[ApiResponse[T]]":
2630
t = ApiResponse[data_type]
2731
t.__name__ = name
2832
t.__qualname__ = name
29-
return cast(Type[ApiResponse[T]], t)
33+
return t
3034

3135

3236
class User(BaseModel):

tests/expected_results/single_module/v1/input.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
from pydantic import BaseModel
2-
from typing import Optional, List
1+
from typing import List, Optional
2+
3+
try:
4+
from pydantic.v1 import BaseModel
5+
except ImportError:
6+
from pydantic import BaseModel
37

48

59
class LoginCredentials(BaseModel):

tests/expected_results/submodules/v1/animals/cats.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
from pydantic import BaseModel
2-
from typing import Optional, Literal
31
from enum import Enum
42

3+
try:
4+
from pydantic.v1 import BaseModel
5+
except ImportError:
6+
from pydantic import BaseModel
7+
58

69
class CatBreed(str, Enum):
710
domestic_shorthair = "domestic shorthair"

tests/expected_results/submodules/v1/animals/dogs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
from pydantic import BaseModel
2-
from typing import Optional
31
from enum import Enum
42

3+
try:
4+
from pydantic.v1 import BaseModel
5+
except ImportError:
6+
from pydantic import BaseModel
7+
58

69
class DogBreed(str, Enum):
710
mutt = "mutt"

tests/expected_results/submodules/v1/input.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from typing import List
22

3-
from pydantic import BaseModel
3+
try:
4+
from pydantic.v1 import BaseModel
5+
except ImportError:
6+
from pydantic import BaseModel
47

58
from .animals.cats import Cat
69
from .animals.dogs import Dog

tests/test_script.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
44
from pathlib import Path
55

66
import pytest
7-
from pydantic import VERSION as _PYDANTIC_VERSION
87

98
from pydantic2ts import generate_typescript_defs
109
from pydantic2ts.cli.script import parse_cli_args
1110

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",)
1519

1620
_RESULTS_DIRECTORY = Path(
1721
os.path.join(os.path.dirname(os.path.realpath(__file__)), "expected_results")
1822
)
1923

2024

21-
def _get_input_module(test_name: str, pydantic_version: str) -> str:
25+
def _get_input_module(test_name: str, pydantic_version: str) -> Path:
2226
return _RESULTS_DIRECTORY / test_name / pydantic_version / "input.py"
2327

2428

@@ -50,19 +54,15 @@ def _run_test(
5054
cmd += f" --exclude {model_to_exclude}"
5155
subprocess.run(cmd, shell=True, check=True)
5256

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)
5658

5759

5860
@pytest.mark.parametrize(
5961
"pydantic_version, call_from_python",
6062
product(_PYDANTIC_VERSIONS, [False, True]),
6163
)
6264
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)
6666

6767

6868
@pytest.mark.parametrize(
@@ -102,31 +102,22 @@ def test_excluding_models(tmpdir, pydantic_version, call_from_python):
102102
def test_computed_fields(tmpdir, pydantic_version, call_from_python):
103103
if pydantic_version == "v1":
104104
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)
108106

109107

110108
@pytest.mark.parametrize(
111109
"pydantic_version, call_from_python",
112110
product(_PYDANTIC_VERSIONS, [False, True]),
113111
)
114112
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)
118114

119115

120116
def test_relative_filepath(tmpdir):
121117
test_name = "single_module"
122118
pydantic_version = _PYDANTIC_VERSIONS[0]
123119
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"
130121
)
131122
_run_test(
132123
tmpdir,
@@ -170,9 +161,7 @@ def test_error_if_json2ts_not_installed(tmpdir):
170161

171162
def test_error_if_invalid_module_path(tmpdir):
172163
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)
176165

177166

178167
def test_parse_cli_args():

0 commit comments

Comments
 (0)