1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
from dataclasses import dataclass
from typing import Optional, List, Union, Dict
import pytest
from dacite import from_dict, UnionMatchError
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")
def test_from_dict_with_union_of_data_classes():
@dataclass
class X:
i: int
@dataclass
class Y:
s: str
@dataclass
class Z:
x_or_y: Union[X, Y]
result = from_dict(Z, {"x_or_y": {"s": "test"}})
assert result == Z(x_or_y=Y(s="test"))
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) == f'can not match type "float" to any type of "i" union: {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
def test_from_dict_with_union_of_data_classes_and_wrong_data():
@dataclass
class X:
i: int
@dataclass
class Y:
s: str
@dataclass
class Z:
x_or_y: Union[X, Y]
with pytest.raises(UnionMatchError) as exception_info:
from_dict(Z, {"x_or_y": {"f": 2.0}})
assert exception_info.value.field_path == "x_or_y"
assert exception_info.value.field_type == Union[X, Y]
assert exception_info.value.value == {"f": 2.0}
def test_from_dict_with_union_of_generic_collecionts_of_data_classes():
@dataclass
class X:
i: int
@dataclass
class Y:
s: str
@dataclass
class Z:
x_or_y: Union[List[X], List[Y]]
result = from_dict(Z, {"x_or_y": [{"s": "test"}]})
assert result == Z(x_or_y=[Y(s="test")])
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")
def test_from_dict_with_union_and_optional_and_missing_value():
@dataclass
class X:
i: Union[int, Optional[str]]
result = from_dict(X, {})
assert result == X(i=None)
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_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_union_of_mixed_types_and_builtin_type_as_a_result():
@dataclass
class X:
i: int
@dataclass
class Y:
u: Union[X, List[X], str]
result = from_dict(Y, {"u": "test"})
assert result == Y(u="test")
def test_from_dict_with_union_of_mixed_types_and_data_class_as_a_result():
@dataclass
class X:
i: int
@dataclass
class Y:
u: Union[str, List[X], X]
result = from_dict(Y, {"u": {"i": 1}})
assert result == Y(u=X(i=1))
def test_from_dict_with_union_of_mixed_types_and_collection_of_data_classes_as_a_result():
@dataclass
class X:
i: int
@dataclass
class Y:
u: Union[str, X, List[X]]
result = from_dict(Y, {"u": [{"i": 1}]})
assert result == Y(u=[X(i=1)])
def test_from_dict_with_union_of_mixed_types_and_dict_of_data_classes_as_a_result():
@dataclass
class X:
i: int
@dataclass
class Y:
d: Union[int, List[X], Dict[str, X]]
result = from_dict(Y, {"d": {"x": {"i": 42}, "z": {"i": 37}}})
assert result == Y(d={"x": X(i=42), "z": X(i=37)})
|