|
9 | 9 |
|
10 | 10 |
|
11 | 11 | @pytest.fixture
|
12 |
| -def populated_cache() -> LRUDict: |
| 12 | +def populated_cache(): |
13 | 13 | cache_dict = LRUDict(max_items=MAX_CACHE_ITEMS, **{f"key_{i}": f"val_{i}" for i in range(0, PREFILL_CACHE_ITEMS)})
|
14 | 14 | return cache_dict
|
15 | 15 |
|
16 | 16 |
|
17 |
| -def test_cache_order_init(populated_cache: LRUDict): |
18 |
| - first_item = list(populated_cache._cache)[0] |
19 |
| - last_item = list(populated_cache._cache)[-1] |
| 17 | +def test_cache_order_init(populated_cache): |
| 18 | + first_item = list(populated_cache)[0] |
| 19 | + last_item = list(populated_cache)[-1] |
20 | 20 |
|
21 | 21 | assert first_item == "key_0"
|
22 | 22 | assert last_item == f"key_{MAX_CACHE_ITEMS - 1}"
|
23 | 23 |
|
24 | 24 |
|
25 |
| -def test_cache_order_getitem(populated_cache: LRUDict): |
| 25 | +def test_cache_order_getitem(populated_cache): |
26 | 26 | random_value = random.randrange(0, MAX_CACHE_ITEMS)
|
27 | 27 | _ = populated_cache[f"key_{random_value}"]
|
28 | 28 |
|
29 |
| - last_item = list(populated_cache._cache)[-1] |
| 29 | + last_item = list(populated_cache)[-1] |
30 | 30 |
|
31 | 31 | assert last_item == f"key_{random_value}"
|
32 | 32 |
|
33 | 33 |
|
34 |
| -def test_cache_order_get(populated_cache: LRUDict): |
| 34 | +def test_cache_order_get(populated_cache): |
35 | 35 | random_value = random.randrange(0, MAX_CACHE_ITEMS)
|
36 | 36 | _ = populated_cache.get(f"key_{random_value}")
|
37 | 37 |
|
38 |
| - last_item = list(populated_cache._cache)[-1] |
| 38 | + last_item = list(populated_cache)[-1] |
39 | 39 |
|
40 | 40 | assert last_item == f"key_{random_value}"
|
41 | 41 |
|
42 | 42 |
|
43 |
| -def test_cache_evict_over_max_items(populated_cache: LRUDict): |
44 |
| - assert "key_0" in populated_cache._cache |
45 |
| - assert len(populated_cache._cache) == MAX_CACHE_ITEMS |
| 43 | +def test_cache_evict_over_max_items(populated_cache): |
| 44 | + assert "key_0" in populated_cache |
| 45 | + assert len(populated_cache) == MAX_CACHE_ITEMS |
46 | 46 | populated_cache["new_item"] = "new_value"
|
47 |
| - assert len(populated_cache._cache) == MAX_CACHE_ITEMS |
48 |
| - assert "key_0" not in populated_cache._cache |
49 |
| - assert "key_1" in populated_cache._cache |
| 47 | + assert len(populated_cache) == MAX_CACHE_ITEMS |
| 48 | + assert "key_0" not in populated_cache |
| 49 | + assert "key_1" in populated_cache |
50 | 50 |
|
51 | 51 |
|
52 |
| -def test_setitem_moves_to_end(populated_cache: LRUDict): |
| 52 | +def test_setitem_moves_to_end(populated_cache): |
53 | 53 | random_value = random.randrange(0, MAX_CACHE_ITEMS)
|
54 | 54 | populated_cache[f"key_{random_value}"] = f"new_val_{random_value}"
|
55 |
| - last_item = list(populated_cache._cache)[-1] |
| 55 | + last_item = list(populated_cache)[-1] |
56 | 56 |
|
57 | 57 | assert last_item == f"key_{random_value}"
|
58 | 58 | assert populated_cache[f"key_{random_value}"] == f"new_val_{random_value}"
|
59 | 59 |
|
60 | 60 |
|
61 |
| -def test_setitem_none_not_moved(populated_cache: LRUDict): |
62 |
| - populated_cache["value_is_none"] = None |
63 |
| - |
64 |
| - first_item = list(populated_cache._cache)[0] |
65 |
| - last_item = list(populated_cache._cache)[-1] |
66 |
| - |
67 |
| - assert first_item == "key_0" |
68 |
| - assert last_item == f"key_{MAX_CACHE_ITEMS - 1}" |
| 61 | +def test_lru_pop_failing(): |
| 62 | + cache = LRUDict() |
| 63 | + key = "test" |
| 64 | + cache[key] = "value" |
| 65 | + try: |
| 66 | + cache.pop(key, None) |
| 67 | + pytest.fail("GitHub #300: LRUDict pop bug has been fixed :)") |
| 68 | + except KeyError as e: |
| 69 | + assert e.args[0] == key |
| 70 | + |
| 71 | + |
| 72 | +def test_lru_del(): |
| 73 | + cache = LRUDict() |
| 74 | + key = "test" |
| 75 | + cache[key] = "value" |
| 76 | + assert len(cache) == 1 |
| 77 | + if key in cache: |
| 78 | + del cache[key] |
| 79 | + assert key not in cache |
| 80 | + assert len(cache) == 0 |
0 commit comments