File: test_mako_cache.py

package info (click to toggle)
python-dogpile.cache 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 772 kB
  • sloc: python: 5,462; makefile: 155; sh: 104
file content (52 lines) | stat: -rw-r--r-- 1,362 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
from unittest import mock

from mako.cache import register_plugin
from mako.template import Template
import pytest

from dogpile.testing import eq_


try:
    import mako  # noqa
except ImportError:
    raise pytest.skip("this test suite requires mako templates")

register_plugin(
    "dogpile.cache", "dogpile.cache.plugins.mako_cache", "MakoPlugin"
)


class MakoPluginTest:
    def _mock_fixture(self):
        reg = mock.MagicMock()
        reg.get_or_create.return_value = "hello world"
        my_regions = {"myregion": reg}
        return (
            {
                "cache_impl": "dogpile.cache",
                "cache_args": {"regions": my_regions},
            },
            reg,
        )

    def test_basic(self):
        kw, reg = self._mock_fixture()
        t = Template('<%page cached="True" cache_region="myregion"/>hi', **kw)
        t.render()
        eq_(reg.get_or_create.call_count, 1)

    def test_timeout(self):
        kw, reg = self._mock_fixture()
        t = Template(
            """
                <%def name="mydef()" cached="True" cache_region="myregion"
                        cache_timeout="20">
                    some content
                </%def>
                ${mydef()}
                """,
            **kw,
        )
        t.render()
        eq_(reg.get_or_create.call_args[1], {"expiration_time": 20})