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
|
from dataclasses import dataclass
from typing import Optional, Union, List, NewType
import pytest
from dacite import from_dict, MissingValueError, WrongTypeError
def test_from_dict_with_missing_optional_value():
@dataclass
class X:
s: Optional[str]
i: int
result = from_dict(X, {"i": 1})
assert result == X(s=None, i=1)
def test_from_dict_with_existing_optional_value():
@dataclass
class X:
s: Optional[str]
i: int
result = from_dict(X, {"s": "test", "i": 1})
assert result == X(s="test", i=1)
def test_from_dict_with_missing_optional_value_for_union():
@dataclass
class X:
i: Optional[Union[int, str]]
result = from_dict(X, {})
assert result == X(i=None)
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_none_as_optional_value():
@dataclass
class X:
s: Optional[str]
i: int
result = from_dict(X, {"s": None, "i": 1})
assert result == X(s=None, i=1)
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]
def test_from_dict_with_missing_optional_nested_data_class():
@dataclass
class X:
i: int
@dataclass
class Y:
x: Optional[X]
result = from_dict(Y, {})
assert result == Y(x=None)
def test_from_dict_with_optional_nested_data_class():
@dataclass
class X:
i: int
@dataclass
class Y:
x: Optional[X]
result = from_dict(Y, {"x": {"i": 1}})
assert result == Y(x=X(i=1))
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"
def test_from_dict_with_null_as_optional_value_for_nested_data_class():
@dataclass
class X:
i: int
@dataclass
class Y:
x: Optional[X]
result = from_dict(Y, {"x": None})
assert result == Y(x=None)
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_optional_generic_collection_of_data_classes():
@dataclass
class X:
i: int
@dataclass
class Y:
x_list: Optional[List[X]]
result = from_dict(Y, {"x_list": [{"i": 1}, {"i": 2}]})
assert result == Y(x_list=[X(i=1), X(i=2)])
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)
def test_from_dict_with_optional_new_type():
MyStr = NewType("MyStr", str)
@dataclass
class X:
s: Optional[MyStr]
result = from_dict(X, {"s": MyStr("test")})
assert result == X(s=MyStr("test"))
|