Skip to content

Commit a78230a

Browse files
committed
Rewrite tests to ensure that the typescript output is identical for both v1 and v2 of pydantic; if a project upgrades to pydantic v2, the typescript definitions should not change unnecessarily
1 parent 1b82de0 commit a78230a

File tree

30 files changed

+50
-143
lines changed

30 files changed

+50
-143
lines changed

tests/expected_results/excluding_models/v2/output.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/expected_results/excluding_models/v2/input.py renamed to tests/expected_results/excluding_models/v2_input.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from typing import List, Optional
2+
13
from pydantic import BaseModel
2-
from typing import Optional, List
34

45

56
class LoginCredentials(BaseModel):

tests/expected_results/extra_fields/v1/input.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/expected_results/extra_fields/v1/output.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
try:
2+
from pydantic.v1 import BaseConfig, BaseModel, Extra
3+
except ImportError:
4+
from pydantic import BaseConfig, BaseModel, Extra
5+
6+
7+
class ModelExtraAllow(BaseModel):
8+
a: str
9+
10+
class Config(BaseConfig):
11+
extra = Extra.allow
12+
13+
14+
class ModelExtraForbid(BaseModel):
15+
a: str
16+
17+
class Config(BaseConfig):
18+
extra = Extra.forbid
19+
20+
21+
class ModelExtraIgnore(BaseModel):
22+
a: str
23+
24+
class Config(BaseConfig):
25+
extra = Extra.ignore
26+
27+
28+
class ModelExtraNone(BaseModel):
29+
a: str

tests/expected_results/generics/v2/output.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

tests/expected_results/single_module/v2/output.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/expected_results/single_module/v2/input.py renamed to tests/expected_results/single_module/v2_input.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from typing import List, Optional
2+
13
from pydantic import BaseModel
2-
from typing import Optional, List
34

45

56
class LoginCredentials(BaseModel):

tests/expected_results/submodules/v1/input.py renamed to tests/expected_results/submodules/v1_input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
except ImportError:
66
from pydantic import BaseModel
77

8-
from .animals.cats import Cat
9-
from .animals.dogs import Dog
8+
from .v1_animals.cats import Cat
9+
from .v1_animals.dogs import Dog
1010

1111

1212
class AnimalShelter(BaseModel):

tests/expected_results/submodules/v2/output.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/expected_results/submodules/v2/animals/cats.py renamed to tests/expected_results/submodules/v2_animals/cats.py

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

3+
from pydantic import BaseModel
4+
55

66
class CatBreed(str, Enum):
77
domestic_shorthair = "domestic shorthair"

tests/expected_results/submodules/v2/animals/dogs.py renamed to tests/expected_results/submodules/v2_animals/dogs.py

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

3+
from pydantic import BaseModel
4+
55

66
class DogBreed(str, Enum):
77
mutt = "mutt"

tests/expected_results/submodules/v2/input.py renamed to tests/expected_results/submodules/v2_input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from pydantic import BaseModel
44

5-
from .animals.cats import Cat
6-
from .animals.dogs import Dog
5+
from .v2_animals.cats import Cat
6+
from .v2_animals.dogs import Dog
77

88

99
class AnimalShelter(BaseModel):

tests/test_script.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
)
1717

1818

19-
def _get_input_module(test_name: str, pydantic_version: int) -> str:
20-
return str(_RESULTS_DIRECTORY / test_name / f"v{pydantic_version}" / "input.py")
19+
def _python_module_path(test_name: str, pydantic_version: int) -> str:
20+
return str(_RESULTS_DIRECTORY / test_name / f"v{pydantic_version}_input.py")
2121

2222

23-
def _get_expected_output(test_name: str, pydantic_version: int) -> str:
24-
return (_RESULTS_DIRECTORY / test_name / f"v{pydantic_version}" / "output.ts").read_text()
23+
def _expected_typescript_code(test_name: str) -> str:
24+
return (_RESULTS_DIRECTORY / test_name / "output.ts").read_text()
2525

2626

2727
def _run_test(
@@ -37,7 +37,7 @@ def _run_test(
3737
Execute pydantic2ts logic for converting pydantic models into tyepscript definitions.
3838
Compare the output with the expected output, verifying it is identical.
3939
"""
40-
module_path = module_path or _get_input_module(test_name, pydantic_version)
40+
module_path = module_path or _python_module_path(test_name, pydantic_version)
4141
output_path = str(tmp_path / f"{test_name}_v{pydantic_version}.ts")
4242

4343
if call_from_python:
@@ -48,7 +48,7 @@ def _run_test(
4848
cmd += f" --exclude {model_to_exclude}"
4949
subprocess.run(cmd, shell=True, check=True)
5050

51-
assert Path(output_path).read_text() == _get_expected_output(test_name, pydantic_version)
51+
assert Path(output_path).read_text() == _expected_typescript_code(test_name)
5252

5353

5454
@pytest.mark.parametrize(
@@ -108,9 +108,8 @@ def test_extra_fields(tmp_path: Path, pydantic_version: int, call_from_python: b
108108
def test_relative_filepath(tmp_path: Path):
109109
test_name = "single_module"
110110
pydantic_version = _PYDANTIC_VERSIONS[0]
111-
relative_path = (
112-
Path(".") / "tests" / "expected_results" / test_name / f"v{pydantic_version}" / "input.py"
113-
)
111+
absolute_path = _python_module_path(test_name, pydantic_version)
112+
relative_path = Path(absolute_path).relative_to(Path.cwd())
114113
_run_test(
115114
tmp_path,
116115
test_name,
@@ -120,7 +119,7 @@ def test_relative_filepath(tmp_path: Path):
120119

121120

122121
def test_error_if_json2ts_not_installed(tmp_path: Path):
123-
module_path = _get_input_module("single_module", _PYDANTIC_VERSIONS[0])
122+
module_path = _python_module_path("single_module", _PYDANTIC_VERSIONS[0])
124123
output_path = str(tmp_path / "json2ts_test_output.ts")
125124

126125
# If the json2ts command has no spaces and the executable cannot be found,

0 commit comments

Comments
 (0)