Skip to content

Commit

Permalink
added a test to run all types through the builder with `allow_deseria…
Browse files Browse the repository at this point in the history
…lization_not_by_alias` enabled using the field name or alias in the load.
  • Loading branch information
matthew-chambers-pushly committed Nov 7, 2023
1 parent 77fc378 commit ad3bbd8
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion tests/test_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from typing_extensions import Final, LiteralString

from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig
from mashumaro.config import BaseConfig, TO_DICT_ADD_BY_ALIAS_FLAG
from mashumaro.core.const import PEP_585_COMPATIBLE, PY_39_MIN
from mashumaro.exceptions import (
InvalidFieldValue,
Expand Down Expand Up @@ -517,6 +517,29 @@ class DataClass(DataClassDictMixin):
assert same_types(instance_loaded.x, x_value)


@pytest.mark.parametrize("value_info", inner_values)
@pytest.mark.parametrize("use_alias", [False, True])
def test_level_one_with_aliased_unaliased_fields(value_info, use_alias):
x_type, x_value, x_value_dumped = value_info

@dataclass
class DataClass(DataClassDictMixin):
x: x_type = field(metadata={"alias": "alias_x"})

class Config(BaseConfig):
allow_deserialization_not_by_alias = True
code_generation_options = [TO_DICT_ADD_BY_ALIAS_FLAG]

instance = DataClass(x_value)
dumped = {"alias_x" if use_alias else "x": x_value_dumped}
instance_dumped = instance.to_dict(by_alias=use_alias)
instance_loaded = DataClass.from_dict(dumped)
assert instance_dumped == dumped
assert instance_loaded == instance
assert same_types(instance_dumped, dumped)
assert same_types(instance_loaded.x, x_value)


@pytest.mark.parametrize("value_info", inner_values)
def test_with_generic_list(value_info):
check_collection_generic(List, value_info)
Expand Down

0 comments on commit ad3bbd8

Please sign in to comment.