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})
|