-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_automap.py
92 lines (66 loc) · 2.39 KB
/
test_automap.py
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
import pickle
import typing
import hypothesis
import pytest
import automap
Keys = typing.Set[typing.Hashable]
@hypothesis.given(keys=hypothesis.infer)
def test_auto_map___len__(keys: Keys) -> None:
assert len(automap.AutoMap(keys)) == len(keys)
@hypothesis.given(keys=hypothesis.infer, others=hypothesis.infer)
def test_auto_map___contains__(keys: Keys, others: Keys) -> None:
a = automap.AutoMap(keys)
for key in keys:
assert key in a
others -= keys
for key in others:
assert key not in a
@hypothesis.given(keys=hypothesis.infer, others=hypothesis.infer)
def test_auto_map___getitem__(keys: Keys, others: Keys) -> None:
a = automap.AutoMap(keys)
for index, key in enumerate(keys):
assert a[key] == index
others -= keys
for key in others:
with pytest.raises(KeyError):
a[key]
@hypothesis.given(keys=hypothesis.infer)
def test_auto_map___hash__(keys: Keys) -> None:
assert hash(automap.FrozenAutoMap(keys)) == hash(automap.FrozenAutoMap(keys))
@hypothesis.given(keys=hypothesis.infer)
def test_auto_map___iter__(keys: Keys) -> None:
assert [*automap.AutoMap(keys)] == [*keys]
@hypothesis.given(keys=hypothesis.infer)
def test_auto_map___reversed__(keys: Keys) -> None:
assert [*reversed(automap.AutoMap(keys))] == [*reversed([*keys])]
@hypothesis.given(keys=hypothesis.infer)
def test_auto_map_add(keys: Keys) -> None:
a = automap.AutoMap()
for l, key in enumerate(keys):
assert a.add(key) is None
assert len(a) == l + 1
assert a[key] == l
@hypothesis.given(keys=hypothesis.infer)
def test_pickle(keys: Keys) -> None:
try:
hypothesis.assume(pickle.loads(pickle.dumps(keys)) == keys)
except (TypeError, pickle.PicklingError):
hypothesis.assume(False)
a = automap.AutoMap(keys)
assert pickle.loads(pickle.dumps(a)) == a
@hypothesis.given(keys=hypothesis.infer)
def test_issue_3(keys: Keys) -> None:
hypothesis.assume(keys)
key = keys.pop()
a = automap.AutoMap(keys)
a |= (key,)
with pytest.raises(ValueError):
a |= (key,)
@hypothesis.given(keys=hypothesis.infer)
def test_non_unique_exception(keys: Keys):
hypothesis.assume(keys)
duplicate = next(iter(keys))
with pytest.raises(ValueError):
automap.AutoMap([*keys, duplicate])
with pytest.raises(automap.NonUniqueError):
automap.AutoMap([*keys, duplicate])