attrs/tests/test_funcs.py

221 lines
5.8 KiB
Python
Raw Normal View History

2015-01-29 11:20:17 +00:00
"""
Tests for `attr._funcs`.
"""
2015-01-27 16:53:17 +00:00
from __future__ import absolute_import, division, print_function
2016-05-15 00:00:36 +00:00
from collections import OrderedDict, Sequence, Mapping
2015-01-27 16:53:17 +00:00
import pytest
from hypothesis import given, strategies as st
from . import simple_classes, nested_classes
2015-01-29 19:50:42 +00:00
from attr._funcs import (
asdict,
2015-01-29 19:50:42 +00:00
assoc,
2016-05-15 00:00:36 +00:00
has,
2015-01-29 19:50:42 +00:00
)
2015-01-27 16:53:17 +00:00
from attr._make import (
attr,
attributes,
2016-08-08 06:44:30 +00:00
fields,
2015-01-27 16:53:17 +00:00
)
2016-05-15 00:00:36 +00:00
MAPPING_TYPES = (dict, OrderedDict)
SEQUENCE_TYPES = (list, tuple)
2015-01-27 16:53:17 +00:00
class TestAsDict(object):
2015-01-28 10:15:40 +00:00
"""
Tests for `asdict`.
2015-01-28 10:15:40 +00:00
"""
2016-05-15 00:00:36 +00:00
@given(st.sampled_from(MAPPING_TYPES))
def test_shallow(self, C, dict_factory):
2015-01-27 16:53:17 +00:00
"""
Shallow asdict returns correct dict.
2015-01-27 16:53:17 +00:00
"""
assert {
"x": 1,
"y": 2,
} == asdict(C(x=1, y=2), False, dict_factory=dict_factory)
2015-01-27 16:53:17 +00:00
2016-05-15 00:00:36 +00:00
@given(st.sampled_from(MAPPING_TYPES))
def test_recurse(self, C, dict_class):
2015-01-27 16:53:17 +00:00
"""
Deep asdict returns correct dict.
2015-01-27 16:53:17 +00:00
"""
assert {
"x": {"x": 1, "y": 2},
"y": {"x": 3, "y": 4},
} == asdict(C(
2015-01-27 16:53:17 +00:00
C(1, 2),
C(3, 4),
), dict_factory=dict_class)
@given(nested_classes, st.sampled_from(MAPPING_TYPES))
def test_recurse_property(self, cls, dict_class):
"""
Property tests for recursive asdict.
"""
obj = cls()
obj_dict = asdict(obj, dict_factory=dict_class)
def assert_proper_dict_class(obj, obj_dict):
assert isinstance(obj_dict, dict_class)
for field in fields(obj.__class__):
field_val = getattr(obj, field.name)
if has(field_val.__class__):
# This field holds a class, recurse the assertions.
assert_proper_dict_class(field_val, obj_dict[field.name])
elif isinstance(field_val, Sequence):
dict_val = obj_dict[field.name]
for item, item_dict in zip(field_val, dict_val):
if has(item.__class__):
assert_proper_dict_class(item, item_dict)
elif isinstance(field_val, Mapping):
# This field holds a dictionary.
assert isinstance(obj_dict[field.name], dict_class)
for key, val in field_val.items():
if has(val.__class__):
assert_proper_dict_class(val, obj_dict[key])
assert_proper_dict_class(obj, obj_dict)
2015-01-28 10:15:40 +00:00
2016-05-15 00:00:36 +00:00
@given(st.sampled_from(MAPPING_TYPES))
def test_filter(self, C, dict_factory):
"""
Attributes that are supposed to be skipped are skipped.
"""
assert {
"x": {"x": 1},
} == asdict(C(
C(1, 2),
C(3, 4),
), filter=lambda a, v: a.name != "y", dict_factory=dict_factory)
2016-05-15 00:00:36 +00:00
@given(container=st.sampled_from(SEQUENCE_TYPES))
def test_lists_tuples(self, container, C):
"""
If recurse is True, also recurse into lists.
"""
assert {
"x": 1,
"y": [{"x": 2, "y": 3}, {"x": 4, "y": 5}, "a"],
} == asdict(C(1, container([C(2, 3), C(4, 5), "a"])))
2016-05-15 00:00:36 +00:00
@given(st.sampled_from(MAPPING_TYPES))
def test_dicts(self, C, dict_factory):
"""
If recurse is True, also recurse into dicts.
"""
res = asdict(C(1, {"a": C(4, 5)}), dict_factory=dict_factory)
assert {
"x": 1,
"y": {"a": {"x": 4, "y": 5}},
} == res
assert isinstance(res, dict_factory)
2016-05-15 00:00:36 +00:00
@given(simple_classes, st.sampled_from(MAPPING_TYPES))
def test_roundtrip(self, cls, dict_class):
2016-05-08 21:50:53 +00:00
"""
2016-05-15 00:00:36 +00:00
Test dumping to dicts and back for Hypothesis-generated classes.
2016-05-08 21:50:53 +00:00
"""
instance = cls()
dict_instance = asdict(instance, dict_factory=dict_class)
assert isinstance(dict_instance, dict_class)
roundtrip_instance = cls(**dict_instance)
assert instance == roundtrip_instance
@given(simple_classes)
def test_asdict_preserve_order(self, cls):
2016-05-08 21:50:53 +00:00
"""
Field order should be preserved when dumping to OrderedDicts.
"""
instance = cls()
dict_instance = asdict(instance, dict_factory=OrderedDict)
assert [a.name for a in fields(cls)] == list(dict_instance.keys())
2015-01-28 10:15:40 +00:00
class TestHas(object):
"""
Tests for `has`.
"""
def test_positive(self, C):
2015-01-28 10:15:40 +00:00
"""
Returns `True` on decorated classes.
"""
assert has(C)
def test_positive_empty(self):
"""
Returns `True` on decorated classes even if there are no attributes.
"""
@attributes
2015-01-28 10:15:40 +00:00
class D(object):
pass
assert has(D)
def test_negative(self):
"""
Returns `False` on non-decorated classes.
"""
assert not has(object)
2015-01-29 19:50:42 +00:00
class TestAssoc(object):
"""
Tests for `assoc`.
"""
def test_empty(self):
"""
Empty classes without changes get copied.
"""
@attributes
class C(object):
pass
i1 = C()
i2 = assoc(i1)
assert i1 is not i2
assert i1 == i2
def test_no_changes(self, C):
2015-01-29 19:50:42 +00:00
"""
No changes means a verbatim copy.
"""
i1 = C(1, 2)
i2 = assoc(i1)
assert i1 is not i2
assert i1 == i2
def test_change(self, C):
2015-01-29 19:50:42 +00:00
"""
Changes work.
"""
i = assoc(C(1, 2), x=42)
assert C(42, 2) == i
def test_unknown(self, C):
2015-01-29 19:50:42 +00:00
"""
Wanting to change an unknown attribute raises a ValueError.
"""
@attributes
class C(object):
x = attr()
y = 42
with pytest.raises(ValueError) as e:
assoc(C(1), y=2)
assert (
"y is not an attrs attribute on {cl!r}.".format(cl=C),
) == e.value.args