File: test_plugins.py

package info (click to toggle)
aiocache 0.12.3-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 692 kB
  • sloc: python: 5,044; makefile: 221; sh: 7
file content (77 lines) | stat: -rw-r--r-- 2,933 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
from unittest.mock import create_autospec

import pytest

from aiocache.base import API, BaseCache
from aiocache.plugins import BasePlugin, HitMissRatioPlugin, TimingPlugin
from ..utils import Keys


class TestBasePlugin:
    async def test_interface_methods(self):
        for method in API.CMDS:
            pre = await getattr(BasePlugin, "pre_{}".format(method.__name__))(None)
            assert pre is None
            post = await getattr(BasePlugin, "post_{}".format(method.__name__))(None)
            assert post is None

    async def test_do_nothing(self):
        assert await BasePlugin().do_nothing() is None


class TestTimingPlugin:
    async def test_save_time(self, mock_cache):
        do_save_time = TimingPlugin().save_time("get")
        await do_save_time("self", mock_cache, took=1)
        await do_save_time("self", mock_cache, took=2)

        assert mock_cache.profiling["get_total"] == 2
        assert mock_cache.profiling["get_max"] == 2
        assert mock_cache.profiling["get_min"] == 1
        assert mock_cache.profiling["get_avg"] == 1.5

    async def test_save_time_post_set(self, mock_cache):
        await TimingPlugin().post_set(mock_cache, took=1)
        await TimingPlugin().post_set(mock_cache, took=2)

        assert mock_cache.profiling["set_total"] == 2
        assert mock_cache.profiling["set_max"] == 2
        assert mock_cache.profiling["set_min"] == 1
        assert mock_cache.profiling["set_avg"] == 1.5

    async def test_interface_methods(self):
        for method in API.CMDS:
            assert hasattr(TimingPlugin, "pre_{}".format(method.__name__))
            assert hasattr(TimingPlugin, "post_{}".format(method.__name__))


class TestHitMissRatioPlugin:
    @pytest.fixture
    def plugin(self):
        return HitMissRatioPlugin()

    async def test_post_get(self, plugin):
        client = create_autospec(BaseCache, instance=True)
        await plugin.post_get(client, Keys.KEY)

        assert client.hit_miss_ratio["hits"] == 0
        assert client.hit_miss_ratio["total"] == 1
        assert client.hit_miss_ratio["hit_ratio"] == 0

        await plugin.post_get(client, Keys.KEY, ret="value")
        assert client.hit_miss_ratio["hits"] == 1
        assert client.hit_miss_ratio["total"] == 2
        assert client.hit_miss_ratio["hit_ratio"] == 0.5

    async def test_post_multi_get(self, plugin):
        client = create_autospec(BaseCache, instance=True)
        await plugin.post_multi_get(client, [Keys.KEY, Keys.KEY_1], ret=[None, None])

        assert client.hit_miss_ratio["hits"] == 0
        assert client.hit_miss_ratio["total"] == 2
        assert client.hit_miss_ratio["hit_ratio"] == 0

        await plugin.post_multi_get(client, [Keys.KEY, Keys.KEY_1], ret=["value", "random"])
        assert client.hit_miss_ratio["hits"] == 2
        assert client.hit_miss_ratio["total"] == 4
        assert client.hit_miss_ratio["hit_ratio"] == 0.5