~jonas-drange/ubuntu-start-page/1252899-mobile-friendly

« back to all changes in this revision

Viewing changes to src/Mako-0.1.9/test/cache.py

  • Committer: Matthew Nuzum
  • Date: 2008-04-18 01:58:53 UTC
  • Revision ID: matthew.nuzum@canonical.com-20080418015853-2b8rf979z2c2exxl
adding files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from mako.template import Template
 
2
from mako import lookup
 
3
import unittest, os
 
4
from util import result_lines
 
5
 
 
6
if not os.access('./test_htdocs', os.F_OK):
 
7
    os.mkdir('./test_htdocs')
 
8
 
 
9
class MockCache(object):
 
10
    def __init__(self, realcache):
 
11
        self.realcache = realcache
 
12
    def get(self, key, **kwargs):
 
13
        self.key = key
 
14
        self.kwargs = kwargs.copy()
 
15
        self.kwargs.pop('createfunc', None)
 
16
        return self.realcache.get(key, **kwargs)
 
17
    
 
18
class CacheTest(unittest.TestCase):
 
19
    def test_def(self):
 
20
        t = Template("""
 
21
        <%!
 
22
            callcount = [0]
 
23
        %>
 
24
        <%def name="foo()" cached="True">
 
25
            this is foo
 
26
            <%
 
27
            callcount[0] += 1
 
28
            %>
 
29
        </%def>
 
30
    
 
31
        ${foo()}
 
32
        ${foo()}
 
33
        ${foo()}
 
34
        callcount: ${callcount}
 
35
""")
 
36
        m = self._install_mock_cache(t)
 
37
        assert result_lines(t.render()) == [
 
38
            'this is foo',
 
39
            'this is foo',
 
40
            'this is foo',
 
41
            'callcount: [1]',
 
42
        ]
 
43
        assert m.kwargs == {}
 
44
 
 
45
    def test_nested_def(self):
 
46
        t = Template("""
 
47
        <%!
 
48
            callcount = [0]
 
49
        %>
 
50
        <%def name="foo()">
 
51
            <%def name="bar()" cached="True">
 
52
                this is foo
 
53
                <%
 
54
                callcount[0] += 1
 
55
                %>
 
56
            </%def>
 
57
            ${bar()}
 
58
        </%def>
 
59
 
 
60
        ${foo()}
 
61
        ${foo()}
 
62
        ${foo()}
 
63
        callcount: ${callcount}
 
64
""")
 
65
        m = self._install_mock_cache(t)
 
66
        assert result_lines(t.render()) == [
 
67
            'this is foo',
 
68
            'this is foo',
 
69
            'this is foo',
 
70
            'callcount: [1]',
 
71
        ]
 
72
        assert m.kwargs == {}
 
73
        
 
74
    def test_page(self):
 
75
        t = Template("""
 
76
        <%!
 
77
            callcount = [0]
 
78
        %>
 
79
        <%page cached="True"/>
 
80
        this is foo
 
81
        <%
 
82
        callcount[0] += 1
 
83
        %>
 
84
        callcount: ${callcount}
 
85
""")
 
86
        m = self._install_mock_cache(t)
 
87
        t.render()
 
88
        t.render()
 
89
        assert result_lines(t.render()) == [
 
90
            "this is foo",
 
91
            "callcount: [1]"
 
92
        ]
 
93
        assert m.kwargs == {}
 
94
        
 
95
    def test_fileargs_implicit(self):
 
96
        l = lookup.TemplateLookup(module_directory='./test_htdocs')
 
97
        l.put_string("test","""
 
98
                <%!
 
99
                    callcount = [0]
 
100
                %>
 
101
                <%def name="foo()" cached="True" cache_type='dbm'>
 
102
                    this is foo
 
103
                    <%
 
104
                    callcount[0] += 1
 
105
                    %>
 
106
                </%def>
 
107
 
 
108
                ${foo()}
 
109
                ${foo()}
 
110
                ${foo()}
 
111
                callcount: ${callcount}
 
112
        """)
 
113
        
 
114
        m = self._install_mock_cache(l.get_template('test'))
 
115
        assert result_lines(l.get_template('test').render()) == [
 
116
            'this is foo',
 
117
            'this is foo',
 
118
            'this is foo',
 
119
            'callcount: [1]',
 
120
        ]
 
121
        assert m.kwargs == {'type':'dbm', 'data_dir':'./test_htdocs'}
 
122
        
 
123
    def test_fileargs_deftag(self):
 
124
        t = Template("""
 
125
        <%!
 
126
            callcount = [0]
 
127
        %>
 
128
        <%def name="foo()" cached="True" cache_type='file' cache_dir='./test_htdocs'>
 
129
            this is foo
 
130
            <%
 
131
            callcount[0] += 1
 
132
            %>
 
133
        </%def>
 
134
 
 
135
        ${foo()}
 
136
        ${foo()}
 
137
        ${foo()}
 
138
        callcount: ${callcount}
 
139
""")
 
140
        m = self._install_mock_cache(t)
 
141
        assert result_lines(t.render()) == [
 
142
            'this is foo',
 
143
            'this is foo',
 
144
            'this is foo',
 
145
            'callcount: [1]',
 
146
        ]
 
147
        assert m.kwargs == {'type':'file','data_dir':'./test_htdocs'}
 
148
 
 
149
    def test_fileargs_pagetag(self):
 
150
        t = Template("""
 
151
        <%page cache_dir='./test_htdocs' cache_type='dbm'/>
 
152
        <%!
 
153
            callcount = [0]
 
154
        %>
 
155
        <%def name="foo()" cached="True">
 
156
            this is foo
 
157
            <%
 
158
            callcount[0] += 1
 
159
            %>
 
160
        </%def>
 
161
 
 
162
        ${foo()}
 
163
        ${foo()}
 
164
        ${foo()}
 
165
        callcount: ${callcount}
 
166
""")
 
167
        m = self._install_mock_cache(t)
 
168
        assert result_lines(t.render()) == [
 
169
            'this is foo',
 
170
            'this is foo',
 
171
            'this is foo',
 
172
            'callcount: [1]',
 
173
        ]
 
174
        assert m.kwargs == {'data_dir':'./test_htdocs', 'type':'dbm'}
 
175
 
 
176
    def test_args_complete(self):
 
177
        t = Template("""
 
178
        <%def name="foo()" cached="True" cache_timeout="30" cache_dir="./test_htdocs" cache_type="file" cache_key='somekey'>
 
179
            this is foo
 
180
        </%def>
 
181
 
 
182
        ${foo()}
 
183
""")
 
184
        m = self._install_mock_cache(t)
 
185
        t.render()
 
186
        assert m.kwargs == {'data_dir':'./test_htdocs', 'type':'file', 'expiretime':30}
 
187
        
 
188
        t2 = Template("""
 
189
        <%page cached="True" cache_timeout="30" cache_dir="./test_htdocs" cache_type="file" cache_key='somekey'/>
 
190
        hi
 
191
        """)
 
192
        m = self._install_mock_cache(t2)
 
193
        t2.render()
 
194
        assert m.kwargs == {'data_dir':'./test_htdocs', 'type':'file', 'expiretime':30}
 
195
 
 
196
    def test_fileargs_lookup(self):
 
197
        l = lookup.TemplateLookup(cache_dir='./test_htdocs', cache_type='file')
 
198
        l.put_string("test","""
 
199
                <%!
 
200
                    callcount = [0]
 
201
                %>
 
202
                <%def name="foo()" cached="True">
 
203
                    this is foo
 
204
                    <%
 
205
                    callcount[0] += 1
 
206
                    %>
 
207
                </%def>
 
208
 
 
209
                ${foo()}
 
210
                ${foo()}
 
211
                ${foo()}
 
212
                callcount: ${callcount}
 
213
        """)
 
214
    
 
215
        t = l.get_template('test')
 
216
        m = self._install_mock_cache(t)
 
217
        assert result_lines(l.get_template('test').render()) == [
 
218
            'this is foo',
 
219
            'this is foo',
 
220
            'this is foo',
 
221
            'callcount: [1]',
 
222
        ]
 
223
        assert m.kwargs == {'data_dir':'./test_htdocs', 'type':'file'}
 
224
    
 
225
    def test_buffered(self):
 
226
        t = Template("""
 
227
        <%!
 
228
            def a(text):
 
229
                return "this is a " + text.strip()
 
230
        %>
 
231
        ${foo()}
 
232
        ${foo()}
 
233
        <%def name="foo()" cached="True" buffered="True">
 
234
            this is a test
 
235
        </%def>
 
236
        """, buffer_filters=["a"])
 
237
        assert result_lines(t.render()) == ["this is a this is a test", "this is a this is a test"]
 
238
        
 
239
    def _install_mock_cache(self, template):
 
240
        m = MockCache(template.module._template_cache)
 
241
        template.module._template_cache = m
 
242
        return m
 
243
            
 
244
if __name__ == '__main__':
 
245
    unittest.main()