-
-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pydantic version selection in tests (#2580)
* fix: pydantic version selection in tests The `pydantic_version` fixture returns strings of "v1" and "v2", however, we're using `if pydantic_version == "1"` as the branching condition in the tests. This PR ensures that we are comparing the correct literal value in the tests. * fix type error
- Loading branch information
1 parent
7443afb
commit 57a8411
Showing
5 changed files
with
38 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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,3 @@ | ||
from typing import Literal | ||
|
||
PydanticVersion = Literal["v1", "v2"] |
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -25,6 +25,8 @@ | |
get_serializer, | ||
) | ||
|
||
from . import PydanticVersion | ||
|
||
|
||
class CustomStr(str): | ||
pass | ||
|
@@ -118,13 +120,13 @@ class ModelV2(pydantic_v2.BaseModel): | |
|
||
|
||
@pytest.fixture() | ||
def model_type(pydantic_version: str) -> type[ModelV1 | ModelV2]: | ||
return ModelV1 if pydantic_version == "1" else ModelV2 | ||
def model_type(pydantic_version: PydanticVersion) -> type[ModelV1 | ModelV2]: | ||
return ModelV1 if pydantic_version == "v1" else ModelV2 | ||
|
||
|
||
@pytest.fixture() | ||
def model(pydantic_version: str) -> ModelV1 | ModelV2: | ||
if pydantic_version == "1": | ||
def model(pydantic_version: PydanticVersion) -> ModelV1 | ModelV2: | ||
if pydantic_version == "v1": | ||
return ModelV1( | ||
path=Path("example"), | ||
email_str=pydantic_v1.parse_obj_as(pydantic_v1.EmailStr, "[email protected]"), | ||
|
@@ -195,14 +197,16 @@ def test_serialization_of_model_instance(model: ModelV1 | ModelV2) -> None: | |
|
||
|
||
@pytest.mark.parametrize("prefer_alias", [False, True]) | ||
def test_pydantic_json_compatibility(model: ModelV1 | ModelV2, prefer_alias: bool, pydantic_version: str) -> None: | ||
def test_pydantic_json_compatibility( | ||
model: ModelV1 | ModelV2, prefer_alias: bool, pydantic_version: PydanticVersion | ||
) -> None: | ||
raw = _model_dump_json(model, by_alias=prefer_alias) | ||
encoded_json = encode_json(model, serializer=get_serializer(PydanticInitPlugin.encoders(prefer_alias=prefer_alias))) | ||
|
||
raw_result = json.loads(raw) | ||
encoded_result = json.loads(encoded_json) | ||
|
||
if pydantic_version == "1": | ||
if pydantic_version == "v1": | ||
# pydantic v1 dumps decimals into floats as json, we therefore regard this as an error | ||
assert raw_result.get("condecimal") == float(encoded_result.get("condecimal")) | ||
del raw_result["condecimal"] | ||
|