Exemple #1
0
def test_from_dict_with_strict():
    @dataclass
    class X:
        s: str

    with pytest.raises(UnexpectedDataError) as exception_info:
        from_dict(X, {"s": "test", "i": 1}, Config(strict=True))

    assert str(exception_info.value) == 'can not match "i" to any data class field'
Exemple #2
0
def test_from_dict_with_literal_and_wrong_value():
    from typing import Literal

    @dataclass
    class X:
        l: Literal["A", "B"]

    with pytest.raises(WrongTypeError) as exception_info:
        from_dict(X, {"l": "C"})
Exemple #3
0
def test_from_dict_with_wrong_type_for_type_parameter():
    @dataclass
    class X:
        l: Type[int]

    with pytest.raises(WrongTypeError) as exception_info:
        from_dict(X, {"l": float})

    assert exception_info.value.field_path == "l"
    assert exception_info.value.field_type == Type[int]
Exemple #4
0
def test_from_dict_with_value_instead_of_type():
    @dataclass
    class X:
        l: Type[int]

    with pytest.raises(WrongTypeError) as exception_info:
        from_dict(X, {"l": 1})

    assert exception_info.value.field_path == "l"
    assert exception_info.value.field_type == Type[int]
Exemple #5
0
def test_from_dict_with_wrong_type_of_collection_item():
    @dataclass
    class X:
        l: List[int]

    with pytest.raises(WrongTypeError) as exception_info:
        from_dict(X, {"l": ["1"]})

    assert exception_info.value.field_path == "l"
    assert exception_info.value.field_type == List[int]
Exemple #6
0
def test_from_dict_with_wrong_type_of_dict_value():
    @dataclass
    class X:
        d: Dict[str, int]

    with pytest.raises(WrongTypeError) as exception_info:
        from_dict(X, {"d": {"a": "test"}})

    assert exception_info.value.field_path == "d"
    assert exception_info.value.field_type == Dict[str, int]
Exemple #7
0
def test_from_dict_with_union_and_optional_and_wrong_value():
    @dataclass
    class X:
        i: Union[int, Optional[str]]

    with pytest.raises(UnionMatchError) as exception_info:
        from_dict(X, {"i": 1.0})

    assert exception_info.value.field_path == "i"
    assert exception_info.value.field_type == Union[int, str, None]
    assert exception_info.value.value == 1.0
def test_from_dict_with_missing_value():
    @dataclass
    class X:
        s: str
        i: int

    with pytest.raises(MissingValueError) as exception_info:
        from_dict(X, {"s": "test"})

    assert str(exception_info.value) == 'missing value for field "i"'
    assert exception_info.value.field_path == "i"
def test_from_dict_with_none_for_non_optional_field():
    @dataclass
    class X:
        s: str

    with pytest.raises(WrongTypeError) as exception_info:
        from_dict(X, {"s": None})

    assert exception_info.value.field_path == "s"
    assert exception_info.value.field_type == str
    assert exception_info.value.value is None
def test_from_dict_with_wrong_type_of_optional_value():
    @dataclass
    class X:
        s: Optional[str]
        i: int

    with pytest.raises(WrongTypeError) as exception_info:
        from_dict(X, {"s": 1, "i": 1})

    assert exception_info.value.field_path == "s"
    assert exception_info.value.field_type == Optional[str]
Exemple #11
0
def test_from_dict_with_missing_forward_reference():
    @dataclass
    class X:
        y: "Y"

    @dataclass
    class Y:
        s: str

    with pytest.raises(ForwardReferenceError) as exception_info:
        from_dict(X, {"y": {"s": "text"}})

    assert str(exception_info.value) == "can not resolve forward reference: name 'Y' is not defined"
def test_from_dict_with_missing_value_of_nested_data_class():
    @dataclass
    class X:
        i: int

    @dataclass
    class Y:
        x: X

    with pytest.raises(MissingValueError) as exception_info:
        from_dict(Y, {"x": {}})

    assert exception_info.value.field_path == "x.i"
def test_from_dict_with_optional_nested_data_class_and_missing_value():
    @dataclass
    class X:
        i: int
        j: int

    @dataclass
    class Y:
        x: Optional[X]

    with pytest.raises(MissingValueError) as exception_info:
        from_dict(Y, {"x": {"i": 1}})

    assert exception_info.value.field_path == "x.j"
Exemple #14
0
def test_from_dict_with_union_and_wrong_data():
    @dataclass
    class X:
        i: Union[int, str]

    with pytest.raises(UnionMatchError) as exception_info:
        from_dict(X, {"i": 1.0})

    assert (
        str(exception_info.value) ==
        'can not match type "float" to any type of "i" union: typing.Union[int, str]'
    )
    assert exception_info.value.field_path == "i"
    assert exception_info.value.field_type == Union[int, str]
    assert exception_info.value.value == 1.0
Exemple #15
0
def test_from_dict_with_union_and_optional_and_none_value():
    @dataclass
    class X:
        i: Union[int, Optional[str]]

    result = from_dict(X, {"i": None})
    assert result == X(i=None)
def test_from_dict_with_wrong_type():
    @dataclass
    class X:
        s: str
        i: int

    with pytest.raises(WrongTypeError) as exception_info:
        from_dict(X, {"s": "test", "i": "wrong"})

    assert (
        str(exception_info.value) ==
        'wrong value type for field "i" - should be "int" instead of value "wrong" of type "str"'
    )
    assert exception_info.value.field_path == "i"
    assert exception_info.value.field_type == int
    assert exception_info.value.value == "wrong"
Exemple #17
0
def test_from_dict_with_cast():
    @dataclass
    class X:
        s: str

    result = from_dict(X, {"s": 1}, Config(cast=[str]))

    assert result == X(s="1")
Exemple #18
0
def test_from_dict_with_cast_and_generic_collection():
    @dataclass
    class X:
        s: List[int]

    result = from_dict(X, {"s": (1,)}, Config(cast=[List]))

    assert result == X(s=[1])
Exemple #19
0
def test_from_dict_with_type_hooks_and_generic_sequence():
    @dataclass
    class X:
        c: List[str]

    result = from_dict(X, {"c": ["TEST"]}, config=Config(type_hooks={str: str.lower}))

    assert result == X(c=["test"])
def test_from_dict_with_none_optional_value_for_union():
    @dataclass
    class X:
        i: Optional[Union[int, str]]

    result = from_dict(X, {"i": None})

    assert result == X(i=None)
def test_from_dict_with_optional_field_and_default_value():
    @dataclass
    class X:
        i: Optional[int] = 1

    result = from_dict(X, {})

    assert result == X(i=1)
Exemple #22
0
def test_from_dict_with_union_and_optional():
    @dataclass
    class X:
        i: Union[int, Optional[str]]

    result = from_dict(X, {"i": "s"})

    assert result == X(i="s")
Exemple #23
0
def test_from_dict_with_generic_collection():
    @dataclass
    class X:
        l: List[int]

    result = from_dict(X, {"l": [1]})

    assert result == X(l=[1])
def test_from_dict_with_any():
    @dataclass
    class X:
        i: Any

    result = from_dict(X, {"i": 1})

    assert result == X(i=1)
def test_from_dict_with_additional_values():
    @dataclass
    class X:
        i: int

    result = from_dict(X, {"i": 1, "s": "extra"})

    assert result == X(i=1)
Exemple #26
0
def test_from_dict_with_set():
    @dataclass
    class X:
        i_set: Set[int]

    result = from_dict(X, {"i_set": {1, 2}})

    assert result == X(i_set={1, 2})
Exemple #27
0
def test_from_dict_with_dict():
    @dataclass
    class X:
        d: Dict[str, int]

    result = from_dict(X, {"d": {"a": 1, "b": 2}})

    assert result == X(d={"a": 1, "b": 2})
Exemple #28
0
def test_from_dict_with_type_parameter():
    @dataclass
    class X:
        l: Type[int]

    result = from_dict(X, {"l": bool})  # bool is a subclass of int in Python

    assert result == X(l=bool)
def test_from_dict_with_post_init_missing_value():
    @dataclass
    class X:
        s: str = field(init=False)

    result = from_dict(X, {})

    assert not hasattr(result, "s")
Exemple #30
0
def test_from_dict_with_union_of_builtin_types():
    @dataclass
    class X:
        i: Union[int, str]

    result = from_dict(X, {"i": "s"})

    assert result == X(i="s")