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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
from dataclasses import dataclass
from enum import Enum
from typing import Optional, List, Union
import pytest
from dacite import (
from_dict,
Config,
ForwardReferenceError,
UnexpectedDataError,
StrictUnionMatchError,
)
def test_from_dict_with_type_hooks():
@dataclass
class X:
s: str
result = from_dict(X, {"s": "TEST"}, Config(type_hooks={str: str.lower}))
assert result == X(s="test")
def test_from_dict_with_type_hooks_and_optional():
@dataclass
class X:
s: Optional[str]
result = from_dict(X, {"s": "TEST"}, Config(type_hooks={str: str.lower}))
assert result == X(s="test")
def test_from_dict_with_type_hooks_and_optional_null_value():
@dataclass
class X:
s: Optional[str]
result = from_dict(X, {"s": None}, Config(type_hooks={str: str.lower}))
assert result == X(s=None)
def test_from_dict_with_type_hooks_and_union():
@dataclass
class X:
s: Union[str, int]
result = from_dict(X, {"s": "TEST"}, Config(type_hooks={str: str.lower}))
assert result == X(s="test")
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")
def test_from_dict_with_base_class_cast():
class E(Enum):
A = "a"
@dataclass
class X:
e: E
result = from_dict(X, {"e": "a"}, Config(cast=[Enum]))
assert result == X(e=E.A)
def test_from_dict_with_base_class_cast_and_optional():
class E(Enum):
A = "a"
@dataclass
class X:
e: Optional[E]
result = from_dict(X, {"e": "a"}, Config(cast=[Enum]))
assert result == X(e=E.A)
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])
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_type_hook_exception():
@dataclass
class X:
i: int
def raise_error(_):
raise KeyError()
with pytest.raises(KeyError):
from_dict(X, {"i": 1}, config=Config(type_hooks={int: raise_error}))
def test_from_dict_with_forward_reference():
@dataclass
class X:
y: "Y"
@dataclass
class Y:
s: str
data = from_dict(X, {"y": {"s": "text"}}, Config(forward_references={"Y": Y}))
assert data == X(Y("text"))
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"
assert exception_info._excinfo[1].__suppress_context__
def test_form_dict_with_disabled_type_checking():
@dataclass
class X:
i: int
result = from_dict(X, {"i": "test"}, config=Config(check_types=False))
# noinspection PyTypeChecker
assert result == X(i="test")
def test_form_dict_with_disabled_type_checking_and_union():
@dataclass
class X:
i: Union[int, float]
result = from_dict(X, {"i": "test"}, config=Config(check_types=False))
# noinspection PyTypeChecker
assert result == X(i="test")
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'
def test_from_dict_with_strict_unions_match_and_ambiguous_match():
@dataclass
class X:
i: int
@dataclass
class Y:
i: int
@dataclass
class Z:
u: Union[X, Y]
data = {
"u": {"i": 1},
}
with pytest.raises(StrictUnionMatchError) as exception_info:
from_dict(Z, data, Config(strict_unions_match=True))
assert str(exception_info.value) == 'can not choose between possible Union matches for field "u": X, Y'
def test_from_dict_with_strict_unions_match_and_single_match():
@dataclass
class X:
f: str
@dataclass
class Y:
f: int
@dataclass
class Z:
u: Union[X, Y]
data = {
"u": {"f": 1},
}
result = from_dict(Z, data, Config(strict_unions_match=True))
assert result == Z(u=Y(f=1))
|