|
| 1 | +import pickle |
| 2 | +import typing |
| 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 arraymap import AutoMap |
| 17 | +from arraymap import FrozenAutoMap |
| 18 | +from arraymap import NonUniqueError |
| 19 | + |
| 20 | +Keys = typing.Set[typing.Hashable] |
| 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(strategy(contiguous=True), strategy(contiguous=False)) |
| 56 | + |
| 57 | + |
| 58 | +@given(keys=hypothesis.infer) |
| 59 | +def test_am___len__(keys: Keys) -> None: |
| 60 | + assert len(AutoMap(keys)) == len(keys) |
| 61 | + |
| 62 | + |
| 63 | +@given(keys=get_array()) |
| 64 | +def test_fam_array___len__(keys: Keys) -> None: |
| 65 | + assert len(FrozenAutoMap(keys)) == len(keys) |
| 66 | + |
| 67 | + |
| 68 | +@given(keys=hypothesis.infer, others=hypothesis.infer) |
| 69 | +def test_am___contains__(keys: Keys, others: Keys) -> None: |
| 70 | + a = AutoMap(keys) |
| 71 | + for key in keys: |
| 72 | + assert key in a |
| 73 | + others -= keys |
| 74 | + for key in others: |
| 75 | + assert key not in a |
| 76 | + |
| 77 | + |
| 78 | +@given(keys=get_array()) |
| 79 | +def test_fam_array___contains__(keys: Keys) -> None: |
| 80 | + fam = FrozenAutoMap(keys) |
| 81 | + for key in keys: |
| 82 | + assert key in fam |
| 83 | + |
| 84 | + |
| 85 | +@given(keys=hypothesis.infer, others=hypothesis.infer) |
| 86 | +def test_am___getitem__(keys: Keys, others: Keys) -> None: |
| 87 | + a = AutoMap(keys) |
| 88 | + for index, key in enumerate(keys): |
| 89 | + assert a[key] == index |
| 90 | + others -= keys |
| 91 | + for key in others: |
| 92 | + with pytest.raises(KeyError): |
| 93 | + a[key] |
| 94 | + |
| 95 | + |
| 96 | +@given(keys=hypothesis.infer) |
| 97 | +def test_am___hash__(keys: Keys) -> None: |
| 98 | + assert hash(FrozenAutoMap(keys)) == hash(FrozenAutoMap(keys)) |
| 99 | + |
| 100 | + |
| 101 | +@given(keys=get_array()) |
| 102 | +def test_fam_array___hash__(keys: Keys) -> None: |
| 103 | + assert hash(FrozenAutoMap(keys)) == hash(FrozenAutoMap(keys)) |
| 104 | + |
| 105 | + |
| 106 | +@given(keys=hypothesis.infer) |
| 107 | +def test_am___iter__(keys: Keys) -> None: |
| 108 | + assert [*AutoMap(keys)] == [*keys] |
| 109 | + |
| 110 | + |
| 111 | +@given(keys=hypothesis.infer) |
| 112 | +def test_fam_array___iter__(keys: Keys) -> None: |
| 113 | + assert [*FrozenAutoMap(keys)] == [*keys] |
| 114 | + |
| 115 | + |
| 116 | +@given(keys=hypothesis.infer) |
| 117 | +def test_am___reversed__(keys: Keys) -> None: |
| 118 | + assert [*reversed(AutoMap(keys))] == [*reversed([*keys])] |
| 119 | + |
| 120 | + |
| 121 | +@given(keys=get_array()) |
| 122 | +def test_fam_array___reversed__(keys: Keys) -> None: |
| 123 | + assert [*reversed(FrozenAutoMap(keys))] == [*reversed([*keys])] |
| 124 | + |
| 125 | + |
| 126 | +@given(keys=hypothesis.infer) |
| 127 | +def test_am_add(keys: Keys) -> None: |
| 128 | + a = AutoMap() |
| 129 | + for l, key in enumerate(keys): |
| 130 | + assert a.add(key) is None |
| 131 | + assert len(a) == l + 1 |
| 132 | + assert a[key] == l |
| 133 | + |
| 134 | + |
| 135 | +@given(keys=hypothesis.infer) |
| 136 | +def test_am_pickle(keys: Keys) -> None: |
| 137 | + try: |
| 138 | + hypothesis.assume(pickle.loads(pickle.dumps(keys)) == keys) |
| 139 | + except (TypeError, pickle.PicklingError): |
| 140 | + hypothesis.assume(False) |
| 141 | + a = AutoMap(keys) |
| 142 | + assert pickle.loads(pickle.dumps(a)) == a |
| 143 | + |
| 144 | + |
| 145 | +@given(keys=get_array()) |
| 146 | +def test_fam_array_pickle(keys: Keys) -> None: |
| 147 | + a = FrozenAutoMap(keys) |
| 148 | + assert list(pickle.loads(pickle.dumps(a))) == list(a) |
| 149 | + |
| 150 | + |
| 151 | +@given(keys=hypothesis.infer) |
| 152 | +def test_issue_3(keys: Keys) -> None: |
| 153 | + hypothesis.assume(keys) |
| 154 | + key = keys.pop() |
| 155 | + a = AutoMap(keys) |
| 156 | + a |= (key,) |
| 157 | + with pytest.raises(ValueError): |
| 158 | + a |= (key,) |
| 159 | + |
| 160 | + |
| 161 | +@given(keys=hypothesis.infer) |
| 162 | +def test_am_non_unique_exception(keys: Keys): |
| 163 | + hypothesis.assume(keys) |
| 164 | + duplicate = next(iter(keys)) |
| 165 | + |
| 166 | + with pytest.raises(ValueError): |
| 167 | + AutoMap([*keys, duplicate]) |
| 168 | + |
| 169 | + with pytest.raises(NonUniqueError): |
| 170 | + AutoMap([*keys, duplicate]) |
| 171 | + |
| 172 | + |
| 173 | +@given(keys=get_array()) |
| 174 | +def test_fam_array_non_unique_exception(keys: Keys): |
| 175 | + with warnings.catch_warnings(): |
| 176 | + warnings.simplefilter("ignore") |
| 177 | + |
| 178 | + hypothesis.assume(keys) |
| 179 | + duplicate = next(iter(keys)) |
| 180 | + |
| 181 | + with pytest.raises(ValueError): |
| 182 | + FrozenAutoMap([*keys, duplicate]) |
| 183 | + |
| 184 | + with pytest.raises(NonUniqueError): |
| 185 | + FrozenAutoMap([*keys, duplicate]) |
0 commit comments