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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
import pytest
from collections_extended.indexed_dict import IndexedDict
def assert_internal_state(self):
"""Asserts that the inner state of the data structure is consistent.
Returns True, so it can be used in an assert expression itself."""
assert len(self._dict) == len(self._list)
for k, (i, v) in self._dict.items(): # noqa
k2, v2 = self._list[i]
assert k2 == k
assert v2 is v
def test_empty_construction():
d = IndexedDict()
assert list(d) == []
assert_internal_state(d)
def test_dict_construction():
d = IndexedDict({1: 2, 3: 4})
assert set(d) == {1, 3} # Not necessarily ordered for python < 3.6
assert_internal_state(d)
def test_kwargs_construction():
d = IndexedDict(a=1, b=2, c=3)
assert set(d) == set("abc") # Not necessarily ordered for python < 3.6
assert_internal_state(d)
def test_tuples_construction():
d = IndexedDict([(1, 2), (3, 4)])
assert list(d) == [1, 3] # Must have correct order
assert_internal_state(d)
def test_clear():
d = IndexedDict(a=1, b=2, c=3)
d.clear()
assert len(d) == 0
assert list(d) == []
assert_internal_state(d)
@pytest.fixture()
def d(request):
ret = IndexedDict([(chr(ord("a") + i), 10 + i) for i in range(5)])
request.addfinalizer(lambda: assert_internal_state(ret))
return ret
@pytest.mark.parametrize("indexing", [{"key": "b"}, {"index": 1}, {"index": -4}])
def test_get_key_found(d, indexing):
assert d.get(**indexing) == 11
@pytest.mark.parametrize("indexing", [{"key": "x"}, {"index": 100}, {"index": -6}])
def test_get_specifying_missing_default(d, indexing):
assert d.get(default=5, **indexing) == 5
def test_get_deprecated_param(d):
with pytest.deprecated_call():
assert d.get('x', d='XXX') == 'XXX'
@pytest.mark.parametrize("indexing", [{"key": "x"}, {"index": 100}, {"index": -6}])
def test_get_missing_default(d, indexing):
assert d.get(**indexing) is None
def test_get_duplicate_default(d):
with pytest.raises(ValueError):
d.get(d=None, default=None)
with pytest.raises(ValueError):
d.get(d='XXX', default=None)
with pytest.raises(ValueError):
d.get(d=None, default='XXX')
with pytest.raises(ValueError):
d.get(d='XXX', default='XXX')
def test_get_both_key_and_index(d):
with pytest.raises(TypeError):
d.get(key="a", index=4)
def test_get_no_key_or_index(d):
with pytest.raises(TypeError):
d.get()
@pytest.mark.parametrize("indexing", [{"key": "b"}, {"index": 1}, {"index": -4}])
def test_pop_found(d, indexing):
assert d.pop(**indexing) == 11
assert list(d) == list("acde")
def test_pop_last(d):
assert d.pop() == 14
assert list(d) == list("abcd")
@pytest.mark.parametrize("indexing", [{"key": "x"}, {"index": 100}, {"index": -6}])
def test_pop_missing_default(d, indexing):
assert d.pop(d="XXX", **indexing) == "XXX"
assert list(d) == list("abcde")
def test_pop_duplicate_default(d):
with pytest.raises(ValueError):
d.pop(d='XXX', default='XXX')
def test_pop_missing_key_no_default(d):
with pytest.raises(KeyError):
d.pop("X")
assert list(d) == list("abcde")
@pytest.mark.parametrize("index", [100, -6])
def test_pop_missing_index_no_default(d, index):
with pytest.raises(IndexError):
d.pop(index=index)
assert list(d) == list("abcde")
def test_deprecated_pop_default(d):
with pytest.deprecated_call():
assert d.pop(999, d='XXX') == 'XXX'
def test_pop_empty_default():
d = IndexedDict()
assert d.pop(d="XXX") == "XXX"
def test_pop_empty_no_default():
d = IndexedDict()
with pytest.raises(IndexError):
d.pop()
def test_pop_both_key_and_index(d):
with pytest.raises(TypeError):
d.pop(key="a", index=4)
@pytest.mark.parametrize("indexing", [{"key": "b"}, {"index": 1}, {"index": -4}])
def test_fast_pop_found(d, indexing):
assert d.fast_pop(**indexing) == (11, 1, "e", 14)
assert set(d) == set("acde")
def test_fast_pop_last(d):
assert d.fast_pop() == (14, 4, "e", 14)
assert set(d) == set("abcd")
def test_fast_pop_last_key(d):
assert d.fast_pop("e") == (14, 4, "e", 14)
assert set(d) == set("abcd")
def test_fast_pop_missing_key(d):
with pytest.raises(KeyError):
d.fast_pop("X")
assert list(d) == list("abcde")
def test_fast_pop_missing_index(d):
with pytest.raises(IndexError):
d.fast_pop(index=100)
assert list(d) == list("abcde")
def test_fast_pop_empty():
d = IndexedDict()
with pytest.raises(IndexError):
d.fast_pop()
def test_fast_pop_both_key_and_index(d):
with pytest.raises(TypeError):
d.fast_pop(key="a", index=4)
def test_popitem(d):
assert d.popitem() == ("e", 14)
assert list(d) == list("abcd")
def test_popitem_first(d):
assert d.popitem(last=False) == ("a", 10)
assert list(d) == list("bcde")
def test_popitem_last(d):
assert d.popitem(last=True) == ("e", 14)
assert list(d) == list("abcd")
def test_popitem_index(d):
assert d.popitem(index=2) == ('c', 12)
assert list(d) == list('abde')
def test_popitem_key(d):
assert d.popitem(key='d') == ('d', 13)
assert list(d) == list('abce')
def test_popitem_multiple_params(d):
with pytest.raises(ValueError):
d.popitem(last=True, index=-1)
def test_popitem_empty():
d = IndexedDict()
with pytest.raises(KeyError):
d.popitem()
def test_copy(d):
l = list(d)
d2 = d.copy()
assert_internal_state(d2)
d.fast_pop("e")
assert_internal_state(d)
assert_internal_state(d2)
assert list(d) != l
assert list(d2) == l
d["X"] = "y"
assert_internal_state(d)
assert_internal_state(d2)
assert list(d) != l
assert list(d2) == l
d2["Z"] = "w"
assert_internal_state(d)
assert_internal_state(d2)
@pytest.mark.parametrize("indexing", [{"key": "b"}, {"index": 1}, {"index": -4}])
def test_move_to_end_key_found(d, indexing):
d.move_to_end(**indexing)
assert list(d) == list("acdeb")
def test_move_to_end_noop(d):
d.move_to_end("e")
assert list(d) == list("abcde")
@pytest.mark.parametrize("indexing", [{"key": "b"}, {"index": 1}, {"index": -4}])
def test_move_to_begin_key_found(d, indexing):
d.move_to_end(last=False, **indexing)
assert list(d) == list("bacde")
def test_move_to_begin_noop(d):
d.move_to_end("a", last=False)
assert list(d) == list("abcde")
def test_move_to_end_missing_key(d):
with pytest.raises(KeyError):
d.move_to_end(key="X")
assert list(d) == list("abcde")
@pytest.mark.parametrize("index", [100, -6])
def test_move_to_end_missing_index(d, index):
with pytest.raises(IndexError):
d.move_to_end(index=index)
assert list(d) == list("abcde")
def test_move_to_end_both_key_and_index(d):
with pytest.raises(TypeError):
d.move_to_end(key="a", index=4)
def test_move_to_end_no_key_or_index(d):
with pytest.raises(TypeError):
d.move_to_end()
def test_index(d):
assert d.index("c") == 2
def test_index_missing(d):
with pytest.raises(KeyError):
d.index("X")
def test_key(d):
assert d.key(3) == "d"
def test_key_negative(d):
assert d.key(-2) == "d"
def test_key_missing(d):
with pytest.raises(IndexError):
d.key(100)
def test_len(d):
assert len(d) == 5
def test_getitem(d):
assert d["a"] == 10
assert d["c"] == 12
def test_getitem_missing(d):
with pytest.raises(KeyError):
d["X"] # noqa
def test_setitem_overwrite(d):
d["a"] = 110
assert list(d) == list("abcde")
assert d["a"] == 110
def test_setitem_create(d):
d["x"] = 500
assert list(d) == list("abcdex")
assert d["x"] == 500
def test_delitem(d):
del d["c"]
assert list(d) == list("abde")
def test_delitem_missing(d):
with pytest.raises(KeyError):
del d["x"]
def test_contains(d):
assert "d" in d
assert "x" not in d
def test_keys(d):
assert list(d.keys()) == list("abcde")
def test_values(d):
assert list(d.values()) == [10, 11, 12, 13, 14]
def test_none_key(d):
d[None] = None
assert d[None] is None
assert list(d) == list("abcde") + [None]
def test_repr():
d = IndexedDict()
d[1] = "X"
d["Y"] = 2
d[None] = None
assert repr(d) == "IndexedDict([(1, 'X'), ('Y', 2), (None, None)])"
def test_str():
d = IndexedDict()
d[1] = "X"
d["Y"] = 2
d[None] = None
assert str(d) == "IndexedDict({1: 'X', 'Y': 2, None: None})"
def test_items(d):
assert list(d.items()) == [(chr(ord("a") + i), 10 + i) for i in range(5)]
|