Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/template_tests/syntax_tests/test_cache.py
blob: 2039e7808cc500f32d35c52ed2089c81b112c7be (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from django.core.cache import cache
from django.template import Context, Engine, TemplateSyntaxError
from django.test import SimpleTestCase, override_settings

from ..utils import setup


class CacheTagTests(SimpleTestCase):
    libraries = {
        'cache': 'django.templatetags.cache',
        'custom': 'template_tests.templatetags.custom',
    }

    def tearDown(self):
        cache.clear()

    @setup({'cache03': '{% load cache %}{% cache 2 test %}cache03{% endcache %}'})
    def test_cache03(self):
        output = self.engine.render_to_string('cache03')
        self.assertEqual(output, 'cache03')

    @setup({
        'cache03': '{% load cache %}{% cache 2 test %}cache03{% endcache %}',
        'cache04': '{% load cache %}{% cache 2 test %}cache04{% endcache %}',
    })
    def test_cache04(self):
        self.engine.render_to_string('cache03')
        output = self.engine.render_to_string('cache04')
        self.assertEqual(output, 'cache03')

    @setup({'cache05': '{% load cache %}{% cache 2 test foo %}cache05{% endcache %}'})
    def test_cache05(self):
        output = self.engine.render_to_string('cache05', {'foo': 1})
        self.assertEqual(output, 'cache05')

    @setup({'cache06': '{% load cache %}{% cache 2 test foo %}cache06{% endcache %}'})
    def test_cache06(self):
        output = self.engine.render_to_string('cache06', {'foo': 2})
        self.assertEqual(output, 'cache06')

    @setup({
        'cache05': '{% load cache %}{% cache 2 test foo %}cache05{% endcache %}',
        'cache07': '{% load cache %}{% cache 2 test foo %}cache07{% endcache %}',
    })
    def test_cache07(self):
        context = {'foo': 1}
        self.engine.render_to_string('cache05', context)
        output = self.engine.render_to_string('cache07', context)
        self.assertEqual(output, 'cache05')

    @setup({
        'cache06': '{% load cache %}{% cache 2 test foo %}cache06{% endcache %}',
        'cache08': '{% load cache %}{% cache time test foo %}cache08{% endcache %}',
    })
    def test_cache08(self):
        """
        Allow first argument to be a variable.
        """
        context = {'foo': 2, 'time': 2}
        self.engine.render_to_string('cache06', context)
        output = self.engine.render_to_string('cache08', context)
        self.assertEqual(output, 'cache06')

    # Raise exception if we don't have at least 2 args, first one integer.
    @setup({'cache11': '{% load cache %}{% cache %}{% endcache %}'})
    def test_cache11(self):
        with self.assertRaises(TemplateSyntaxError):
            self.engine.get_template('cache11')

    @setup({'cache12': '{% load cache %}{% cache 1 %}{% endcache %}'})
    def test_cache12(self):
        with self.assertRaises(TemplateSyntaxError):
            self.engine.get_template('cache12')

    @setup({'cache13': '{% load cache %}{% cache foo bar %}{% endcache %}'})
    def test_cache13(self):
        with self.assertRaises(TemplateSyntaxError):
            self.engine.render_to_string('cache13')

    @setup({'cache14': '{% load cache %}{% cache foo bar %}{% endcache %}'})
    def test_cache14(self):
        with self.assertRaises(TemplateSyntaxError):
            self.engine.render_to_string('cache14', {'foo': 'fail'})

    @setup({'cache15': '{% load cache %}{% cache foo bar %}{% endcache %}'})
    def test_cache15(self):
        with self.assertRaises(TemplateSyntaxError):
            self.engine.render_to_string('cache15', {'foo': []})

    @setup({'cache16': '{% load cache %}{% cache 1 foo bar %}{% endcache %}'})
    def test_cache16(self):
        """
        Regression test for #7460.
        """
        output = self.engine.render_to_string('cache16', {'foo': 'foo', 'bar': 'with spaces'})
        self.assertEqual(output, '')

    @setup({'cache17': '{% load cache %}{% cache 10 long_cache_key poem %}Some Content{% endcache %}'})
    def test_cache17(self):
        """
        Regression test for #11270.
        """
        output = self.engine.render_to_string('cache17', {'poem': 'Oh freddled gruntbuggly/'
                                            'Thy micturations are to me/'
                                            'As plurdled gabbleblotchits/'
                                            'On a lurgid bee/'
                                            'That mordiously hath bitled out/'
                                            'Its earted jurtles/'
                                            'Into a rancid festering/'
                                            'Or else I shall rend thee in the gobberwarts'
                                            'with my blurglecruncheon/'
                                            'See if I dont.'})
        self.assertEqual(output, 'Some Content')

    @setup({'cache18': '{% load cache custom %}{% cache 2|noop:"x y" cache18 %}cache18{% endcache %}'})
    def test_cache18(self):
        """
        Test whitespace in filter arguments
        """
        output = self.engine.render_to_string('cache18')
        self.assertEqual(output, 'cache18')


class CacheTests(SimpleTestCase):

    @classmethod
    def setUpClass(cls):
        cls.engine = Engine(libraries={'cache': 'django.templatetags.cache'})
        super(CacheTests, cls).setUpClass()

    def test_cache_regression_20130(self):
        t = self.engine.from_string('{% load cache %}{% cache 1 regression_20130 %}foo{% endcache %}')
        cachenode = t.nodelist[1]
        self.assertEqual(cachenode.fragment_name, 'regression_20130')

    @override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'default',
        },
        'template_fragments': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'fragments',
        },
    })
    def test_cache_fragment_cache(self):
        """
        When a cache called "template_fragments" is present, the cache tag
        will use it in preference to 'default'
        """
        t1 = self.engine.from_string('{% load cache %}{% cache 1 fragment %}foo{% endcache %}')
        t2 = self.engine.from_string('{% load cache %}{% cache 1 fragment using="default" %}bar{% endcache %}')

        ctx = Context()
        o1 = t1.render(ctx)
        o2 = t2.render(ctx)

        self.assertEqual(o1, 'foo')
        self.assertEqual(o2, 'bar')

    def test_cache_missing_backend(self):
        """
        When a cache that doesn't exist is specified, the cache tag will
        raise a TemplateSyntaxError
        '"""
        t = self.engine.from_string('{% load cache %}{% cache 1 backend using="unknown" %}bar{% endcache %}')

        ctx = Context()
        with self.assertRaises(TemplateSyntaxError):
            t.render(ctx)