File: test_lru_cache.py

package info (click to toggle)
sentry-python 2.19.2-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 4,088 kB
  • sloc: python: 56,942; sh: 117; makefile: 114; xml: 2
file content (78 lines) | stat: -rw-r--r-- 1,685 bytes parent folder | download
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
import pytest
from copy import copy

from sentry_sdk._lru_cache import LRUCache


@pytest.mark.parametrize("max_size", [-10, -1, 0])
def test_illegal_size(max_size):
    with pytest.raises(AssertionError):
        LRUCache(max_size=max_size)


def test_simple_set_get():
    cache = LRUCache(1)
    assert cache.get(1) is None
    cache.set(1, 1)
    assert cache.get(1) == 1


def test_overwrite():
    cache = LRUCache(1)
    assert cache.get(1) is None
    cache.set(1, 1)
    assert cache.get(1) == 1
    cache.set(1, 2)
    assert cache.get(1) == 2


def test_cache_eviction():
    cache = LRUCache(3)
    cache.set(1, 1)
    cache.set(2, 2)
    cache.set(3, 3)
    assert cache.get(1) == 1
    assert cache.get(2) == 2
    cache.set(4, 4)
    assert cache.get(3) is None
    assert cache.get(4) == 4


def test_cache_miss():
    cache = LRUCache(1)
    assert cache.get(0) is None


def test_cache_set_overwrite():
    cache = LRUCache(3)
    cache.set(0, 0)
    cache.set(0, 1)
    assert cache.get(0) == 1


def test_cache_get_all():
    cache = LRUCache(3)
    cache.set(0, 0)
    cache.set(1, 1)
    cache.set(2, 2)
    cache.set(3, 3)
    assert cache.get_all() == [(1, 1), (2, 2), (3, 3)]
    cache.get(1)
    assert cache.get_all() == [(2, 2), (3, 3), (1, 1)]


def test_cache_copy():
    cache = LRUCache(3)
    cache.set(0, 0)
    cache.set(1, 1)

    copied = copy(cache)
    cache.set(2, 2)
    cache.set(3, 3)
    assert copied.get_all() == [(0, 0), (1, 1)]
    assert cache.get_all() == [(1, 1), (2, 2), (3, 3)]

    copied = copy(cache)
    cache.get(1)
    assert copied.get_all() == [(1, 1), (2, 2), (3, 3)]
    assert cache.get_all() == [(2, 2), (3, 3), (1, 1)]