Source

tipfy.ext.tenjin / tests / test_ext_tenjin.py

Full commit
# -*- coding: utf-8 -*-
"""
    Tests for tipfyext.tenjin
"""
import os
import sys
import unittest

""" Skip init() for GaeMemcacheStore """
import tenjin.gae
def _empty(): pass
tenjin.gae.init = _empty

from gaetestbed import MemcacheTestCase

#from tenjin import FileSystemLoader, Environment

from tipfy import RequestHandler, Request, Response, Tipfy
from tipfy.app import local
from tipfyext.tenjin import Tenjin, TenjinMixin

current_dir = os.path.abspath(os.path.dirname(__file__))
templates_dir = os.path.join(current_dir, 'resources', 'templates')
templates_compiled_target = os.path.join(current_dir, 'resources', 'templates_compiled')
i18m_dir = os.path.join(current_dir, 'resources', 'catalogs')


class TestTenjin(unittest.TestCase):
    def tearDown(self):
        local.__release_local__()

    def test_render_template(self):
        app = Tipfy(config={'tipfyext.tenjin': {'templates_dir': templates_dir}})
        request = Request.from_values()
        local.current_handler = handler = RequestHandler(app, request)
        tenjin = Tenjin(app)

        message = 'Hello, World!'
        local.current_handler.context = {}
        res = tenjin.render_template(local.current_handler, 'template1.html', dict(message=message))
        self.assertEqual(res, message+'\n')

    def test_render_template_with_i18n(self):
         app = Tipfy(config={
             'tipfyext.tenjin': {
                 'templates_dir': templates_dir,
                 'i18m_dir': i18m_dir,
             },
             'tipfy.sessions': {
                 'secret_key': 'secret',
             },
         })
         request = Request.from_values()
         local.current_handler = handler = RequestHandler(app, request)
         tenjin = Tenjin(app)
 
         message = 'Hello, i18n World!'
         res = tenjin.render_template(local.current_handler, 'template2.html', dict(message=message))
         self.assertEqual(res, message)
 
    #def test_render_response(self):
        #app = Tipfy(config={'tipfyext.tenjin': {'templates_dir': templates_dir}})
        #request = Request.from_values()
        #local.current_handler = handler = RequestHandler(app, request)
        #tenjin = Tenjin(app)

        #message = 'Hello, World!'
        #response = Tenjin.render_response(local.current_handler, 'template1.html', message=message)
        #self.assertEqual(isinstance(response, Response), True)
        #self.assertEqual(response.mimetype, 'text/html')
        #self.assertEqual(response.data, message)

    #def test_render_response_force_compiled(self):
        #app = Tipfy(config={
            #'tipfyext.tenjin': {
                #'templates_compiled_target': templates_compiled_target,
                #'force_use_compiled': True,
            #}
        #}, debug=False)
        #request = Request.from_values()
        #local.current_handler = handler = RequestHandler(app, request)
        #tenjin = Tenjin(app)

        #message = 'Hello, World!'
        #response = tenjin.render_response(local.current_handler, 'template1.html', message=message)
        #self.assertEqual(isinstance(response, Response), True)
        #self.assertEqual(response.mimetype, 'text/html')
        #self.assertEqual(response.data, message)

    #def test_tenjin_mixin_render_template(self):
        #class MyHandler(RequestHandler, TenjinMixin):
            #def __init__(self, app, request):
                #self.app = app
                #self.request = request
                #self.context = {}

        #app = Tipfy(config={'tipfyext.tenjin': {'templates_dir': templates_dir}})
        #request = Request.from_values()
        #local.current_handler = handler = MyHandler(app, request)
        #tenjin = Tenjin(app)
        #message = 'Hello, World!'

        #response = handler.render_template('template1.html', message=message)
        #self.assertEqual(response, message)

    #def test_tenjin_mixin_render_response(self):
        #class MyHandler(RequestHandler, TenjinMixin):
            #def __init__(self, app, request):
                #self.app = app
                #self.request = request
                #self.context = {}

        #app = Tipfy(config={'tipfyext.tenjin': {'templates_dir': templates_dir}})
        #request = Request.from_values()
        #local.current_handler = handler = MyHandler(app, request)
        #tenjin = Tenjin(app)
        #message = 'Hello, World!'

        #response = handler.render_response('template1.html', message=message)
        #self.assertEqual(isinstance(response, Response), True)
        #self.assertEqual(response.mimetype, 'text/html')
        #self.assertEqual(response.data, message)

    #def test_get_template_attribute(self):
        #app = Tipfy(config={'tipfyext.tenjin': {'templates_dir': templates_dir}})
        #request = Request.from_values()
        #local.current_handler = handler = RequestHandler(app, request)
        #tenjin = Tenjin(app)

        #hello = tenjin.get_template_attribute('hello.html', 'hello')
        #self.assertEqual(hello('World'), 'Hello, World!')

    #def test_engine_factory(self):
        #def get_tenjin_env():
            #app = local.current_handler.app
            #cfg = app.get_config('tipfyext.tenjin')

            #loader = FileSystemLoader(cfg.get( 'templates_dir'))

            #return Environment(loader=loader)

        #app = Tipfy(config={'tipfyext.tenjin': {
            #'templates_dir': templates_dir,
            #'engine_factory': get_tenjin_env,
        #}})
        #request = Request.from_values()
        #local.current_handler = handler = RequestHandler(app, request)
        #tenjin = Tenjin(app)

        #message = 'Hello, World!'
        #res = Tenjin.render_template(local.current_handler, 'template1.html', message=message)
        #self.assertEqual(res, message)

    #def test_engine_factory2(self):
        #old_sys_path = sys.path[:]
        #sys.path.insert(0, current_dir)

        #app = Tipfy(config={'tipfyext.tenjin': {
            #'templates_dir': templates_dir,
            #'engine_factory': 'resources.get_tenjin_env',
        #}})
        #request = Request.from_values()
        #local.current_handler = handler = RequestHandler(app, request)
        #tenjin = Tenjin(app)

        #message = 'Hello, World!'
        #res = tenjin.render_template(local.current_handler, 'template1.html', message=message)
        #self.assertEqual(res, message)

        #sys.path = old_sys_path

    #def test_engine_factory3(self):
        #app = Tipfy()
        #request = Request.from_values()
        #local.current_handler = handler = RequestHandler(app, request)
        #_globals = {'message': 'Hey there!'}
        #filters = {'ho': lambda e: e + ' Ho!'}
        #tenjin = Tenjin(app, _globals=_globals, filters=filters)

        #template = tenjin.environment.from_string("""{{ message|ho }}""")

        #self.assertEqual(template.render(), 'Hey there! Ho!')

    #def test_after_environment_created(self):
        #def after_creation(environment):
            #environment.filters['ho'] = lambda x: x + ', Ho!'

        #app = Tipfy(config={'tipfyext.tenjin': {'after_environment_created': after_creation}})
        #request = Request.from_values()
        #local.current_handler = handler = RequestHandler(app, request)
        #tenjin = Tenjin(app)

        #template = tenjin.environment.from_string("""{{ 'Hey'|ho }}""")
        #self.assertEqual(template.render(), 'Hey, Ho!')

    #def test_after_environment_created_using_string(self):
        #app = Tipfy(config={'tipfyext.tenjin': {'after_environment_created': 'resources.tenjin_after_environment_created.after_creation'}})
        #request = Request.from_values()
        #local.current_handler = handler = RequestHandler(app, request)
        #tenjin = Tenjin(app)

        #template = tenjin.environment.from_string("""{{ 'Hey'|ho }}""")
        #self.assertEqual(template.render(), 'Hey, Ho!')

    #def test_translations(self):
        #app = Tipfy(config={
            #'tipfyext.tenjin': {
                #'environment_args': {
                    #'extensions': ['tenjin.ext.i18n',],
                #},
            #},
            #'tipfy.sessions': {
                #'secret_key': 'foo',
            #},
        #})
        #request = Request.from_values()
        #local.current_handler = handler = RequestHandler(app, request)
        #tenjin = Tenjin(app)

        #template = tenjin.environment.from_string("""{{ _('foo = %(bar)s', bar='foo') }}""")
        #self.assertEqual(template.render(), 'foo = foo')

if __name__ == '__main__':
    unittest.main()