| 1 |
from datetime import timedelta |
| 2 |
from time import sleep |
| 3 |
|
| 4 |
import pytest |
| 5 |
from conftest import TestData |
| 6 |
from conftest import under_uwsgi |
| 7 |
|
| 8 |
|
| 9 |
class CommonTests(TestData): |
| 10 |
"""A base set of tests to be run for all cache types""" |
| 11 |
|
| 12 |
def test_set_get(self): |
| 13 |
cache = self.cache_factory() |
| 14 |
for k, v in self.sample_pairs.items(): |
| 15 |
assert cache.set(k, v) |
| 16 |
assert cache.get(k) == v |
| 17 |
|
| 18 |
def test_set_get_many(self): |
| 19 |
cache = self.cache_factory() |
| 20 |
result = cache.set_many(self.sample_pairs) |
| 21 |
assert result == list(self.sample_pairs.keys()) |
| 22 |
values = cache.get_many(*self.sample_pairs) |
| 23 |
assert values == list(self.sample_pairs.values()) |
| 24 |
|
| 25 |
def test_get_dict(self): |
| 26 |
cache = self.cache_factory() |
| 27 |
cache.set_many(self.sample_pairs) |
| 28 |
d = cache.get_dict(*self.sample_pairs) |
| 29 |
assert d == self.sample_pairs |
| 30 |
|
| 31 |
def test_delete(self): |
| 32 |
cache = self.cache_factory() |
| 33 |
for k, v in self.sample_pairs.items(): |
| 34 |
cache.set(k, v) |
| 35 |
assert cache.delete(k) |
| 36 |
assert not cache.get(k) |
| 37 |
|
| 38 |
def test_delete_many(self): |
| 39 |
cache = self.cache_factory() |
| 40 |
cache.set_many(self.sample_pairs) |
| 41 |
result = cache.delete_many(*self.sample_pairs) |
| 42 |
assert result == list(self.sample_pairs.keys()) |
| 43 |
assert not any(cache.get_many(*self.sample_pairs)) |
| 44 |
|
| 45 |
def test_delete_many_ignore_errors(self): |
| 46 |
cache = self.cache_factory() |
| 47 |
cache.set("bacon", "spam") |
| 48 |
cache.delete_many("eggs", "bacon") |
| 49 |
assert cache.get("bacon") is None |
| 50 |
|
| 51 |
def test_delete_many_raise_errors_nonexistent_key(self): |
| 52 |
cache = self.cache_factory(ignore_delete_many_errors=False) |
| 53 |
cache.set("bacon", "spam") |
| 54 |
# nonexistent keys are considered deleted and must not raise |
| 55 |
result = cache.delete_many("eggs", "bacon") |
| 56 |
assert result == ["eggs", "bacon"] |
| 57 |
assert cache.get("bacon") is None |
| 58 |
|
| 59 |
def test_add(self): |
| 60 |
cache = self.cache_factory() |
| 61 |
cache.set_many(self.sample_pairs) |
| 62 |
for k in self.sample_pairs: |
| 63 |
assert not cache.add(k, "updated") |
| 64 |
assert cache.get_many(*self.sample_pairs) == list(self.sample_pairs.values()) |
| 65 |
for k, v in self.sample_pairs.items(): |
| 66 |
assert cache.add(f"{k}-new", v) |
| 67 |
assert cache.get(f"{k}-new") == v |
| 68 |
|
| 69 |
def test_inc_dec(self): |
| 70 |
cache = self.cache_factory() |
| 71 |
for n in self.sample_numbers: |
| 72 |
assert not cache.get(f"{n}-key-inc") |
| 73 |
assert cache.inc(f"{n}-key-inc", n) == n |
| 74 |
assert cache.get(f"{n}-key-inc") == n |
| 75 |
assert cache.dec(f"{n}-key-dec", n) == -n |
| 76 |
assert cache.get(f"{n}-key-dec") == -n |
| 77 |
assert cache.dec(f"{n}-key-inc", 5) == n - 5 |
| 78 |
|
| 79 |
def test_inc_dec_after_set(self): |
| 80 |
cache = self.cache_factory() |
| 81 |
assert cache.set("count", 10) |
| 82 |
assert cache.inc("count", 5) == 15 |
| 83 |
assert cache.dec("count", 3) == 12 |
| 84 |
assert cache.get("count") == 12 |
| 85 |
|
| 86 |
def test_bool_roundtrip(self): |
| 87 |
cache = self.cache_factory() |
| 88 |
assert cache.set("true-key", True) |
| 89 |
assert cache.get("true-key") is True |
| 90 |
assert cache.set("false-key", False) |
| 91 |
assert cache.get("false-key") is False |
| 92 |
|
| 93 |
def test_expiration(self): |
| 94 |
if under_uwsgi(): |
| 95 |
pytest.skip( |
| 96 |
"uwsgi uses a separate sweeper thread to clean" |
| 97 |
" expired cache entries, thus the testing" |
| 98 |
" of such feature must be handled differently" |
| 99 |
" from other cache types." |
| 100 |
) |
| 101 |
cache = self.cache_factory() |
| 102 |
for k, v in self.sample_pairs.items(): |
| 103 |
cache.set(f"{k}-t0", v, timeout=0) |
| 104 |
cache.set(f"{k}-t1", v, timeout=1) |
| 105 |
# timedeltas are equivalent to the same amount of seconds |
| 106 |
cache.set(f"{k}-td0", v, timeout=timedelta()) |
| 107 |
cache.set(f"{k}-td1", v, timeout=timedelta(seconds=1)) |
| 108 |
sleep(4) |
| 109 |
for k, v in self.sample_pairs.items(): |
| 110 |
assert cache.get(f"{k}-t0") == v |
| 111 |
assert not cache.get(f"{k}-t1") |
| 112 |
assert cache.get(f"{k}-td0") == v |
| 113 |
assert not cache.get(f"{k}-td1") |