1# -*- coding: utf-8 -*-
2import os
3
4import webapp2
5from webapp2_extras import jinja2
6
7import test_base
8
9current_dir = os.path.abspath(os.path.dirname(__file__))
10template_path = os.path.join(current_dir, 'resources', 'jinja2_templates')
11compiled_path = os.path.join(current_dir, 'resources',
12                             'jinja2_templates_compiled')
13
14class TestJinja2(test_base.BaseTestCase):
15    def test_render_template_with_i18n(self):
16        app = webapp2.WSGIApplication(config={
17            'webapp2_extras.jinja2': {
18                'template_path': template_path,
19                'environment_args': {
20                    'autoescape': True,
21                    'extensions': [
22                        'jinja2.ext.autoescape',
23                        'jinja2.ext.with_',
24                        'jinja2.ext.i18n',
25                    ],
26                },
27            },
28        })
29        req = webapp2.Request.blank('/')
30        app.set_globals(app=app, request=req)
31        j = jinja2.Jinja2(app)
32
33        message = 'Hello, i18n World!'
34        res = j.render_template('template2.html', message=message)
35        self.assertEqual(res, message)
36
37    def test_render_template_globals_filters(self):
38        app = webapp2.WSGIApplication(config={
39            'webapp2_extras.jinja2': {
40                'template_path': template_path,
41                'globals': dict(foo='fooglobal'),
42                'filters': dict(foo=lambda x: x + '-foofilter'),
43            },
44        })
45        req = webapp2.Request.blank('/')
46        app.set_globals(app=app, request=req)
47        j = jinja2.Jinja2(app)
48
49        message = 'fooglobal-foofilter'
50        res = j.render_template('template3.html', message=message)
51        self.assertEqual(res, message)
52
53    def test_render_template_force_compiled(self):
54        app = webapp2.WSGIApplication(config={
55            'webapp2_extras.jinja2': {
56                'template_path': template_path,
57                'compiled_path': compiled_path,
58                'force_compiled': True,
59            }
60        })
61        req = webapp2.Request.blank('/')
62        app.set_globals(app=app, request=req)
63        j = jinja2.Jinja2(app)
64
65        message = 'Hello, World!'
66        res = j.render_template('template1.html', message=message)
67        self.assertEqual(res, message)
68
69    def test_get_template_attribute(self):
70        app = webapp2.WSGIApplication(config={
71            'webapp2_extras.jinja2': {
72                'template_path': template_path,
73            }
74        })
75        j = jinja2.Jinja2(app)
76        hello = j.get_template_attribute('hello.html', 'hello')
77        self.assertEqual(hello('World'), 'Hello, World!')
78
79    def test_set_jinja2(self):
80        app = webapp2.WSGIApplication()
81        self.assertEqual(len(app.registry), 0)
82        jinja2.set_jinja2(jinja2.Jinja2(app), app=app)
83        self.assertEqual(len(app.registry), 1)
84        j = jinja2.get_jinja2(app=app)
85        self.assertTrue(isinstance(j, jinja2.Jinja2))
86
87    def test_get_jinja2(self):
88        app = webapp2.WSGIApplication()
89        self.assertEqual(len(app.registry), 0)
90        j = jinja2.get_jinja2(app=app)
91        self.assertEqual(len(app.registry), 1)
92        self.assertTrue(isinstance(j, jinja2.Jinja2))
93
94
95if __name__ == '__main__':
96    test_base.main()
97