~ubuntu-branches/ubuntu/hardy/mako/hardy-backports

« back to all changes in this revision

Viewing changes to test/cache.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2008-09-09 02:09:15 UTC
  • mfrom: (5.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080909020915-y0zgvojsponlnwx8
Tags: 0.2.2-1~hardy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from mako.template import Template
 
2
from mako.lookup import TemplateLookup
2
3
from mako import lookup
3
 
import unittest, os
 
4
import shutil, unittest, os
4
5
from util import result_lines
5
6
 
6
7
if not os.access('./test_htdocs', os.F_OK):
7
8
    os.mkdir('./test_htdocs')
 
9
for cache_dir in ('container_dbm', 'container_dbm_lock', 'container_file',
 
10
            'container_file_lock'):
 
11
    fullpath = os.path.join('./test_htdocs', cache_dir)
 
12
    if os.path.exists(fullpath):
 
13
        shutil.rmtree(fullpath)
8
14
 
9
15
class MockCache(object):
10
16
    def __init__(self, realcache):
91
97
            "callcount: [1]"
92
98
        ]
93
99
        assert m.kwargs == {}
 
100
 
 
101
    def test_dynamic_key_with_funcargs(self):
 
102
        t = Template("""
 
103
            <%def name="foo(num=5)" cached="True" cache_key="foo_${str(num)}">
 
104
             hi
 
105
            </%def>
 
106
 
 
107
            ${foo()}
 
108
        """)
 
109
        m = self._install_mock_cache(t)
 
110
        t.render()
 
111
        t.render()
 
112
        assert result_lines(t.render()) == ['hi']
 
113
        assert m.key == "foo_5"
 
114
 
 
115
        t = Template("""
 
116
            <%def name="foo(*args, **kwargs)" cached="True" cache_key="foo_${kwargs['bar']}">
 
117
             hi
 
118
            </%def>
 
119
 
 
120
            ${foo(1, 2, bar='lala')}
 
121
        """)
 
122
        m = self._install_mock_cache(t)
 
123
        t.render()
 
124
        assert result_lines(t.render()) == ['hi']
 
125
        assert m.key == "foo_lala"
 
126
 
 
127
        t = Template('''
 
128
        <%page args="bar='hi'" cache_key="foo_${bar}" cached="True"/>
 
129
         hi
 
130
        ''')
 
131
        m = self._install_mock_cache(t)
 
132
        t.render()
 
133
        assert result_lines(t.render()) == ['hi']
 
134
        assert m.key == "foo_hi"
 
135
 
 
136
        
 
137
    def test_dynamic_key_with_imports(self):
 
138
        lookup = TemplateLookup()
 
139
        lookup.put_string("foo.html", """
 
140
        <%!
 
141
            callcount = [0]
 
142
        %>
 
143
        <%namespace file="ns.html" import="*"/>
 
144
        <%page cached="True" cache_key="${foo}"/>
 
145
        this is foo
 
146
        <%
 
147
        callcount[0] += 1
 
148
        %>
 
149
        callcount: ${callcount}
 
150
""")
 
151
        lookup.put_string("ns.html", """""")
 
152
        t = lookup.get_template("foo.html")
 
153
        m = self._install_mock_cache(t)
 
154
        t.render(foo='somekey')
 
155
        t.render(foo='somekey')
 
156
        assert result_lines(t.render(foo='somekey')) == [
 
157
            "this is foo",
 
158
            "callcount: [1]"
 
159
        ]
 
160
        assert m.kwargs == {}
94
161
        
95
162
    def test_fileargs_implicit(self):
96
163
        l = lookup.TemplateLookup(module_directory='./test_htdocs')
235
302
        </%def>
236
303
        """, buffer_filters=["a"])
237
304
        assert result_lines(t.render()) == ["this is a this is a test", "this is a this is a test"]
 
305
    
 
306
    def test_load_from_expired(self):
 
307
        """test that the cache callable can be called safely after the
 
308
        originating template has completed rendering.
 
309
        
 
310
        """
 
311
        t = Template("""
 
312
        ${foo()}
 
313
        <%def name="foo()" cached="True" cache_timeout="2">
 
314
            foo
 
315
        </%def>
 
316
        """)
 
317
        
 
318
        import time
 
319
        x1 = t.render()
 
320
        time.sleep(3)
 
321
        x2 = t.render()
 
322
        assert x1.strip() == x2.strip() == "foo"
 
323
    
 
324
    def test_cache_uses_current_context(self):
 
325
        t = Template("""
 
326
        ${foo()}
 
327
        <%def name="foo()" cached="True" cache_timeout="2">
 
328
            foo: ${x}
 
329
        </%def>
 
330
        """)
 
331
        
 
332
        import time
 
333
        x1 = t.render(x=1)
 
334
        time.sleep(3)
 
335
        x2 = t.render(x=2)
 
336
        assert x1.strip() == "foo: 1"
 
337
        assert x2.strip() == "foo: 2"
 
338
        
238
339
        
239
340
    def _install_mock_cache(self, template):
240
341
        m = MockCache(template.module._template_cache)