Skip to content

Commit c16d7d0

Browse files
committed
added property tests
1 parent ac1f10e commit c16d7d0

File tree

1 file changed

+283
-0
lines changed

1 file changed

+283
-0
lines changed

test/test_auto_map_property.py

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
import pickle
2+
import typing as tp
3+
from functools import partial
4+
import sys
5+
import warnings
6+
7+
import numpy as np
8+
import hypothesis
9+
from hypothesis.extra.numpy import arrays
10+
from hypothesis.extra.numpy import scalar_dtypes
11+
from hypothesis import strategies as st
12+
from hypothesis import given
13+
14+
import pytest
15+
16+
from arraykit import AutoMap
17+
from arraykit import FrozenAutoMap
18+
from arraykit import NonUniqueError
19+
20+
Keys = tp.Set[tp.Union[int, str, float, bool, bytes, tp.Tuple[int, ...]]]
21+
22+
NATIVE_BYTE_ORDER = "<" if sys.byteorder == "little" else ">"
23+
VALID_BYTE_ORDERS = ("=", NATIVE_BYTE_ORDER)
24+
25+
26+
def get_array() -> st.SearchStrategy:
27+
"""
28+
Labels are suitable for creating non-date Indices (though they might include dates); these labels might force an object array result.
29+
"""
30+
31+
def proc(a: np.ndarray, contiguous: bool):
32+
if a.dtype.kind in ("f", "c"):
33+
a = a[~np.isnan(a)]
34+
elif a.dtype.kind in ("m", "M"):
35+
a = a[~np.isnat(a)]
36+
37+
if a.dtype.byteorder not in VALID_BYTE_ORDERS:
38+
a = a.astype(a.dtype.newbyteorder(NATIVE_BYTE_ORDER))
39+
40+
if not contiguous:
41+
a = np.lib.stride_tricks.as_strided(
42+
a,
43+
shape=(len(a) // 2,),
44+
strides=(a.dtype.itemsize * 2,),
45+
)
46+
47+
a.flags.writeable = False
48+
return a
49+
50+
def strategy(contiguous: bool):
51+
return arrays(
52+
shape=1, unique=True, fill=st.nothing(), dtype=scalar_dtypes()
53+
).map(partial(proc, contiguous=contiguous))
54+
55+
return st.one_of(
56+
strategy(contiguous=True),
57+
strategy(contiguous=False),
58+
)
59+
60+
61+
# -------------------------------------------------------------------------------
62+
@given(keys=hypothesis.infer)
63+
def test_am___len__(keys: Keys) -> None:
64+
assert len(AutoMap(keys)) == len(keys)
65+
66+
67+
@given(keys=get_array())
68+
def test_fam_array___len__(keys: Keys) -> None:
69+
assert len(FrozenAutoMap(keys)) == len(keys)
70+
71+
72+
# -------------------------------------------------------------------------------
73+
@given(keys=hypothesis.infer, others=hypothesis.infer)
74+
def test_am___contains__(keys: Keys, others: Keys) -> None:
75+
a = AutoMap(keys)
76+
for key in keys:
77+
assert key in a
78+
others -= keys
79+
for key in others:
80+
assert key not in a
81+
82+
83+
@given(keys=get_array())
84+
def test_fam_array___contains__(keys: Keys) -> None:
85+
fam = FrozenAutoMap(keys)
86+
for key in keys:
87+
assert key in fam
88+
89+
90+
# -------------------------------------------------------------------------------
91+
@given(keys=hypothesis.infer, others=hypothesis.infer)
92+
def test_am___getitem__(keys: Keys, others: Keys) -> None:
93+
a = AutoMap(keys)
94+
for index, key in enumerate(keys):
95+
assert a[key] == index
96+
others -= keys
97+
for key in others:
98+
with pytest.raises(KeyError):
99+
a[key]
100+
101+
102+
@given(keys=get_array())
103+
def test_fam_array___getitem__(keys: Keys) -> None:
104+
a = FrozenAutoMap(keys)
105+
for index, key in enumerate(keys):
106+
assert a[key] == index
107+
108+
109+
# -------------------------------------------------------------------------------
110+
@given(keys=hypothesis.infer)
111+
def test_am___hash__(keys: Keys) -> None:
112+
assert hash(FrozenAutoMap(keys)) == hash(FrozenAutoMap(keys))
113+
114+
115+
@given(keys=get_array())
116+
def test_fam_array___hash__(keys: Keys) -> None:
117+
assert hash(FrozenAutoMap(keys)) == hash(FrozenAutoMap(keys))
118+
119+
120+
# -------------------------------------------------------------------------------
121+
@given(keys=hypothesis.infer)
122+
def test_am___iter__(keys: Keys) -> None:
123+
assert [*AutoMap(keys)] == [*keys]
124+
125+
126+
@given(keys=hypothesis.infer)
127+
def test_fam_array___iter__(keys: Keys) -> None:
128+
assert [*FrozenAutoMap(keys)] == [*keys]
129+
130+
131+
# -------------------------------------------------------------------------------
132+
@given(keys=hypothesis.infer)
133+
def test_am___reversed__(keys: Keys) -> None:
134+
assert [*reversed(AutoMap(keys))] == [*reversed([*keys])]
135+
136+
137+
@given(keys=get_array())
138+
def test_fam_array___reversed__(keys: Keys) -> None:
139+
assert [*reversed(FrozenAutoMap(keys))] == [*reversed([*keys])]
140+
141+
142+
# -------------------------------------------------------------------------------
143+
@given(keys=hypothesis.infer)
144+
def test_am_add(keys: Keys) -> None:
145+
a = AutoMap()
146+
for l, key in enumerate(keys):
147+
assert a.add(key) is None
148+
assert len(a) == l + 1
149+
assert a[key] == l
150+
151+
152+
# -------------------------------------------------------------------------------
153+
@given(keys=hypothesis.infer)
154+
def test_am_pickle(keys: Keys) -> None:
155+
try:
156+
hypothesis.assume(pickle.loads(pickle.dumps(keys)) == keys)
157+
except (TypeError, pickle.PicklingError):
158+
hypothesis.assume(False)
159+
a = AutoMap(keys)
160+
assert pickle.loads(pickle.dumps(a)) == a
161+
162+
163+
@given(keys=get_array())
164+
def test_fam_array_pickle(keys: Keys) -> None:
165+
a = FrozenAutoMap(keys)
166+
assert list(pickle.loads(pickle.dumps(a))) == list(a)
167+
168+
169+
# -------------------------------------------------------------------------------
170+
@given(keys=hypothesis.infer)
171+
def test_issue_3(keys: Keys) -> None:
172+
hypothesis.assume(keys)
173+
key = keys.pop()
174+
a = AutoMap(keys)
175+
a |= (key,)
176+
with pytest.raises(ValueError):
177+
a |= (key,)
178+
179+
180+
@given(keys=hypothesis.infer)
181+
def test_am_non_unique_exception(keys: Keys):
182+
hypothesis.assume(keys)
183+
duplicate = next(iter(keys))
184+
185+
with pytest.raises(ValueError):
186+
AutoMap([*keys, duplicate])
187+
188+
with pytest.raises(NonUniqueError):
189+
AutoMap([*keys, duplicate])
190+
191+
192+
@given(keys=get_array())
193+
def test_fam_array_non_unique_exception(keys: Keys):
194+
with warnings.catch_warnings():
195+
warnings.simplefilter("ignore")
196+
197+
hypothesis.assume(keys)
198+
duplicate = next(iter(keys))
199+
200+
with pytest.raises(ValueError):
201+
FrozenAutoMap([*keys, duplicate])
202+
203+
with pytest.raises(NonUniqueError):
204+
FrozenAutoMap([*keys, duplicate])
205+
206+
207+
# -------------------------------------------------------------------------------
208+
@given(keys=get_array())
209+
def test_fam_array_get_all(keys: Keys) -> None:
210+
fam = FrozenAutoMap(keys)
211+
keys_list = list(keys)
212+
213+
post1 = fam.get_all(keys_list)
214+
assert list(post1) == list(fam.values())
215+
216+
post2 = fam.get_all(keys)
217+
assert list(post2) == list(fam.values())
218+
219+
220+
@given(keys=get_array())
221+
def test_fam_array_get_any(keys: Keys) -> None:
222+
fam = FrozenAutoMap(keys)
223+
keys_list = list(keys)
224+
225+
post1 = fam.get_any(keys_list)
226+
assert post1 == list(fam.values())
227+
228+
post2 = fam.get_any(keys)
229+
assert post2 == list(fam.values())
230+
231+
232+
# -------------------------------------------------------------------------------
233+
234+
235+
@given(keys=get_array())
236+
def test_am_array_get_all(keys: Keys) -> None:
237+
fam = AutoMap(keys)
238+
keys_list = list(keys)
239+
240+
post1 = fam.get_all(keys_list)
241+
assert list(post1) == list(fam.values())
242+
243+
post2 = fam.get_all(keys)
244+
assert list(post2) == list(fam.values())
245+
246+
247+
@given(keys=get_array())
248+
def test_am_array_get_any(keys: Keys) -> None:
249+
fam = AutoMap(keys)
250+
keys_list = list(keys)
251+
252+
post1 = fam.get_any(keys_list)
253+
assert post1 == list(fam.values())
254+
255+
post2 = fam.get_any(keys)
256+
assert post2 == list(fam.values())
257+
258+
259+
# -------------------------------------------------------------------------------
260+
261+
262+
@given(keys=get_array())
263+
def test_am_get_all(keys: Keys) -> None:
264+
keys_list = list(keys)
265+
fam = AutoMap(keys_list)
266+
267+
post1 = fam.get_all(keys_list)
268+
assert list(post1) == list(fam.values())
269+
270+
post2 = fam.get_all(keys_list)
271+
assert list(post2) == list(fam.values())
272+
273+
274+
@given(keys=get_array())
275+
def test_am_get_any(keys: Keys) -> None:
276+
keys_list = list(keys)
277+
fam = AutoMap(keys_list)
278+
279+
post1 = fam.get_any(keys_list)
280+
assert post1 == list(fam.values())
281+
282+
post2 = fam.get_any(keys_list)
283+
assert post2 == list(fam.values())

0 commit comments

Comments
 (0)