-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_immutablemap.py
More file actions
138 lines (110 loc) · 4.51 KB
/
test_immutablemap.py
File metadata and controls
138 lines (110 loc) · 4.51 KB
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
"""Test :mod:`xmmutablemap`."""
from collections import OrderedDict
from types import MappingProxyType
from typing import Any
import pytest
from xmmutablemap import ImmutableMap
class TestImmutableMap:
"""Test :class:`ImmutableMap`."""
@pytest.fixture(scope="class")
def d(self) -> ImmutableMap[str, Any]:
"""Example immutable map."""
return ImmutableMap(a=1, b=2)
# ===============================================================
@pytest.mark.parametrize(
("arg", "kwargs"),
[
((), {}),
({"a": 1, "b": 2}, {}),
([("a", 1), ("b", 2)], {}),
((("a", 1), ("b", 2)), {}),
],
)
def test_init(
self,
arg: tuple[str, Any] | dict[str, Any] | list[tuple[str, Any]],
kwargs: dict[str, Any],
) -> None:
"""Test initialization.
Should be able to initialize with all the same input types as a regular
dictionary.
"""
d = ImmutableMap(arg, **kwargs)
assert isinstance(d, ImmutableMap)
assert d._data == dict(arg, **kwargs)
def test_getitem(self, d: ImmutableMap[str, Any]) -> None:
"""Test `__getitem__`."""
assert d["a"] == 1
assert d["b"] == 2
def test_iter(self, d: ImmutableMap[str, Any]) -> None:
"""Test `__iter__`."""
assert list(d) == ["a", "b"]
def test_len(self, d: ImmutableMap[str, Any]) -> None:
"""Test `__len__`."""
assert len(d) == 2
def test_hash(self, d: ImmutableMap[str, Any]) -> None:
"""Test `__hash__`."""
assert hash(d) == hash(frozenset(d.items()))
# Not hashable if values aren't hashable.
d = ImmutableMap(a=1, b={"c"})
with pytest.raises(TypeError, match="unhashable type: 'set'"):
hash(d)
def test_eq_with_other_mappings(self) -> None:
"""Test mapping interoperability for `__eq__`."""
d = ImmutableMap(a=1, b=2)
other_dict = {"a": 1, "b": 2}
other_ordered_dict = OrderedDict([("a", 1), ("b", 2)])
other_proxy = MappingProxyType({"a": 1, "b": 2})
assert d == {"a": 1, "b": 2}
assert d == OrderedDict([("a", 1), ("b", 2)])
assert d == MappingProxyType({"a": 1, "b": 2})
assert other_dict == d
assert other_ordered_dict == d
assert other_proxy == d
def test_eq_and_hash_ignore_insertion_order(self) -> None:
"""Test equality/hash contract for same items in different orders."""
d1 = ImmutableMap(a=1, b=2)
d2 = ImmutableMap(b=2, a=1)
assert d1 == d2
assert hash(d1) == hash(d2)
def test_keys(self, d: ImmutableMap[str, Any]) -> None:
"""Test `keys`."""
assert list(d.keys()) == ["a", "b"]
def test_values(self, d: ImmutableMap[str, Any]) -> None:
"""Test `values`."""
assert list(d.values()) == [1, 2]
def test_items(self, d: ImmutableMap[str, Any]) -> None:
"""Test `items`."""
assert list(d.items()) == [("a", 1), ("b", 2)]
def test_repr(self, d: ImmutableMap[str, Any]) -> None:
"""Test `__repr__`."""
assert repr(d) == "ImmutableMap({'a': 1, 'b': 2})"
def test_or(self, d: ImmutableMap[str, Any]) -> None:
"""Test `__or__`."""
assert d | ImmutableMap(c=3) == ImmutableMap(a=1, b=2, c=3)
assert d | {"c": 3} == ImmutableMap(a=1, b=2, c=3)
assert d | OrderedDict([("c", 3)]) == ImmutableMap(a=1, b=2, c=3)
assert d | MappingProxyType({"c": 3}) == ImmutableMap(a=1, b=2, c=3)
# Should raise TypeError if not a mapping.
with pytest.raises(TypeError, match="unsupported operand type"):
_ = d | 1
def test_ror(self, d: ImmutableMap[str, Any]) -> None:
"""Test `__ror__`."""
# Reverse order
assert {"c": 3} | d == {"c": 3, "a": 1, "b": 2}
assert OrderedDict([("c", 3)]) | d == OrderedDict(
[("c", 3), ("a", 1), ("b", 2)],
)
# === Test pytree methods ===
def test_tree_flatten(self, d: ImmutableMap[str, Any]) -> None:
"""Test `tree_flatten`."""
assert d.tree_flatten() == ((1, 2), ("a", "b"))
def test_tree_unflatten(self, d: ImmutableMap[str, Any]) -> None:
"""Test `tree_unflatten`."""
d1 = ImmutableMap.tree_unflatten(("a", "b"), (1, 2))
assert d1 == ImmutableMap(a=1, b=2)
# round-trip
d = ImmutableMap(a=1, b=2)
flattened = d.tree_flatten()
d2 = ImmutableMap.tree_unflatten(flattened[1], flattened[0])
assert d2 == d