Debian Codegraph @

tests/test_uwsgi_cache.py

raw
1 import pytest
2 from clear import ClearTests
3 from common import CommonTests
4 from has import HasTests
5
6 from cachelib import UWSGICache
7 from cachelib.serializers import BaseSerializer
8
9
10 class SillySerializer(BaseSerializer):
11 """A pointless serializer only for testing"""
12
13 def dumps(self, value):
14 return repr(value).encode()
15
16 def loads(self, bvalue):
17 return eval(bvalue.decode())
18
19
20 class CustomCache(UWSGICache):
21 """Our custom cache client with non-default serializer"""
22
23 # overwrite serializer
24 serializer = SillySerializer()
25
26 def __init__(self, *args, **kwargs):
27 super().__init__(*args, **kwargs)
28
29
30 @pytest.fixture(autouse=True, params=[UWSGICache, CustomCache])
31 def cache_factory(request):
32 def _factory(self, *args, **kwargs):
33 uwc = request.param(*args, **kwargs)
34 uwc.clear()
35 return uwc
36
37 request.cls.cache_factory = _factory
38
39
40 class TestUwsgiCache(CommonTests, ClearTests, HasTests):
41 pytest.importorskip(
42 "uwsgi",
43 reason="could not import 'uwsgi'. Make sure to "
44 "run pytest under uwsgi for testing UWSGICache",
45 )