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 229 230 231 232 233 234 235 236 237 238 239
|
from collections import OrderedDict
from copy import deepcopy
from typing import Callable
from attr import define
from hypothesis import given
from hypothesis.strategies import just, one_of
from pytest import raises
from cattrs import BaseConverter, Converter, UnstructureStrategy
from cattrs.errors import ClassValidationError
from . import unstructure_strats
@define
class Simple:
a: int
@given(strat=unstructure_strats, detailed_validation=..., prefer_attrib=...)
def test_deepcopy(
converter_cls: type[BaseConverter],
strat: UnstructureStrategy,
prefer_attrib: bool,
detailed_validation: bool,
):
c = converter_cls(
unstruct_strat=strat,
prefer_attrib_converters=prefer_attrib,
detailed_validation=detailed_validation,
)
copy = deepcopy(c)
assert c is not copy
assert c.unstructure(Simple(1)) == copy.unstructure(Simple(1))
assert c.detailed_validation == copy.detailed_validation
assert c._prefer_attrib_converters == copy._prefer_attrib_converters
@given(
strat=unstructure_strats,
detailed_validation=...,
prefer_attrib=...,
dict_factory=one_of(just(dict), just(OrderedDict)),
)
def test_copy(
converter_cls: type[BaseConverter],
strat: UnstructureStrategy,
prefer_attrib: bool,
detailed_validation: bool,
dict_factory: Callable,
):
c = converter_cls(
unstruct_strat=strat,
prefer_attrib_converters=prefer_attrib,
detailed_validation=detailed_validation,
dict_factory=dict_factory,
)
copy = c.copy()
assert c is not copy
assert c.unstructure(Simple(1)) == copy.unstructure(Simple(1))
assert c.detailed_validation == copy.detailed_validation
assert c._prefer_attrib_converters == copy._prefer_attrib_converters
assert c._dict_factory == copy._dict_factory
@given(
strat=unstructure_strats,
detailed_validation=...,
prefer_attrib=...,
dict_factory=one_of(just(dict), just(OrderedDict)),
omit_if_default=...,
)
def test_copy_converter(
strat: UnstructureStrategy,
prefer_attrib: bool,
detailed_validation: bool,
dict_factory: Callable,
omit_if_default: bool,
):
"""cattrs.Converter can be copied, and keeps its attributes."""
c = Converter(
unstruct_strat=strat,
prefer_attrib_converters=prefer_attrib,
detailed_validation=detailed_validation,
dict_factory=dict_factory,
omit_if_default=omit_if_default,
)
copy = c.copy()
assert c is not copy
assert c.unstructure(Simple(1)) == copy.unstructure(Simple(1))
assert c.detailed_validation == copy.detailed_validation
assert c._prefer_attrib_converters == copy._prefer_attrib_converters
assert c._dict_factory == copy._dict_factory
assert c.omit_if_default == copy.omit_if_default
another_copy = c.copy(omit_if_default=not omit_if_default)
assert c.omit_if_default != another_copy.omit_if_default
@given(
strat=unstructure_strats,
detailed_validation=...,
prefer_attrib=...,
dict_factory=one_of(just(dict), just(OrderedDict)),
)
def test_copy_hooks(
converter_cls: type[BaseConverter],
strat: UnstructureStrategy,
prefer_attrib: bool,
detailed_validation: bool,
dict_factory: Callable,
):
"""Un/structure hooks are copied over."""
c = converter_cls(
unstruct_strat=strat,
prefer_attrib_converters=prefer_attrib,
detailed_validation=detailed_validation,
dict_factory=dict_factory,
)
c.register_unstructure_hook(Simple, lambda s: s.a)
c.register_structure_hook(Simple, lambda v, _: Simple(v))
assert c.unstructure(Simple(1)) == 1
assert c.structure(1, Simple) == Simple(1)
copy = c.copy()
assert c is not copy
assert c.unstructure(Simple(1)) == copy.unstructure(Simple(1))
assert copy.structure(copy.unstructure(Simple(1)), Simple) == Simple(1)
assert c.detailed_validation == copy.detailed_validation
assert c._prefer_attrib_converters == copy._prefer_attrib_converters
assert c._dict_factory == copy._dict_factory
@given(
strat=unstructure_strats,
detailed_validation=...,
prefer_attrib=...,
dict_factory=one_of(just(dict), just(OrderedDict)),
)
def test_copy_func_hooks(
converter_cls: type[BaseConverter],
strat: UnstructureStrategy,
prefer_attrib: bool,
detailed_validation: bool,
dict_factory: Callable,
):
"""Un/structure function hooks are copied over."""
c = converter_cls(
unstruct_strat=strat,
prefer_attrib_converters=prefer_attrib,
detailed_validation=detailed_validation,
dict_factory=dict_factory,
)
c.register_unstructure_hook_func(lambda t: t is Simple, lambda s: s.a)
c.register_structure_hook_func(lambda t: t is Simple, lambda v, _: Simple(v))
assert c.unstructure(Simple(1)) == 1
assert c.structure(1, Simple) == Simple(1)
copy = c.copy()
assert c is not copy
assert copy.unstructure(Simple(1)) == 1
assert copy.structure(copy.unstructure(Simple(1)), Simple) == Simple(1)
assert c.detailed_validation == copy.detailed_validation
assert c._prefer_attrib_converters == copy._prefer_attrib_converters
assert c._dict_factory == copy._dict_factory
@given(prefer_attrib=..., dict_factory=one_of(just(dict), just(OrderedDict)))
def test_detailed_validation(prefer_attrib: bool, dict_factory: Callable):
"""Copies with different detailed validation work correctly."""
c = Converter(
prefer_attrib_converters=prefer_attrib,
detailed_validation=True,
dict_factory=dict_factory,
)
# So the converter gets generated.
c.structure({"a": 1}, Simple)
copy = c.copy(detailed_validation=False)
assert c is not copy
assert copy.detailed_validation is False
with raises(ClassValidationError):
c.structure({}, Simple)
with raises(KeyError):
copy.structure({}, Simple)
@given(
prefer_attrib=...,
dict_factory=one_of(just(dict), just(OrderedDict)),
detailed_validation=...,
)
def test_col_overrides(
prefer_attrib: bool, dict_factory: Callable, detailed_validation: bool
):
"""Copies with different sequence overrides work correctly."""
c = Converter(
prefer_attrib_converters=prefer_attrib,
detailed_validation=detailed_validation,
dict_factory=dict_factory,
unstruct_collection_overrides={list: tuple},
)
# So the converter gets generated.
assert c.unstructure([1, 2, 3]) == (1, 2, 3)
# We also stick a manual hook on there so it gets copied too.
c.register_unstructure_hook(Simple, lambda s: s.a)
copy = c.copy(unstruct_collection_overrides={})
assert c is not copy
assert c.unstructure([1, 2, 3]) == (1, 2, 3)
assert copy.unstructure([1, 2, 3]) == [1, 2, 3]
assert c.unstructure(Simple(1)) == 1
assert copy.unstructure(Simple(1)) == 1
|