Source

jinja2-main / tests / test_loaders.py

# -*- coding: utf-8 -*-
"""
    unit test for the loaders
    ~~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: (c) 2009 by the Jinja Team.
    :license: BSD, see LICENSE for more details.
"""

import time
import tempfile
from jinja2 import Environment, loaders
from jinja2.loaders import split_template_path
from jinja2.exceptions import TemplateNotFound

import conftest
if conftest.NOSE:
    from nose.tools import assert_raises as raises
else:
    from py.test import raises


dict_loader = loaders.DictLoader({
    'justdict.html':        'FOO'
})
package_loader = loaders.PackageLoader('loaderres', 'templates')
filesystem_loader = loaders.FileSystemLoader('loaderres/templates')
function_loader = loaders.FunctionLoader({'justfunction.html': 'FOO'}.get)
choice_loader = loaders.ChoiceLoader([dict_loader, package_loader])
prefix_loader = loaders.PrefixLoader({
    'a':        filesystem_loader,
    'b':        dict_loader
})


def test_dict_loader():
    env = Environment(loader=dict_loader)
    tmpl = env.get_template('justdict.html')
    assert tmpl.render().strip() == 'FOO'
    raises(TemplateNotFound, env.get_template, 'missing.html')


def test_package_loader():
    env = Environment(loader=package_loader)
    tmpl = env.get_template('test.html')
    assert tmpl.render().strip() == 'BAR'
    raises(TemplateNotFound, env.get_template, 'missing.html')


def test_filesystem_loader():
    env = Environment(loader=filesystem_loader)
    tmpl = env.get_template('test.html')
    assert tmpl.render().strip() == 'BAR'
    tmpl = env.get_template('foo/test.html')
    assert tmpl.render().strip() == 'FOO'
    raises(TemplateNotFound, env.get_template, 'missing.html')


def test_choice_loader():
    env = Environment(loader=choice_loader)
    tmpl = env.get_template('justdict.html')
    assert tmpl.render().strip() == 'FOO'
    tmpl = env.get_template('test.html')
    assert tmpl.render().strip() == 'BAR'
    raises(TemplateNotFound, env.get_template, 'missing.html')


def test_function_loader():
    env = Environment(loader=function_loader)
    tmpl = env.get_template('justfunction.html')
    assert tmpl.render().strip() == 'FOO'
    raises(TemplateNotFound, env.get_template, 'missing.html')


def test_prefix_loader():
    env = Environment(loader=prefix_loader)
    tmpl = env.get_template('a/test.html')
    assert tmpl.render().strip() == 'BAR'
    tmpl = env.get_template('b/justdict.html')
    assert tmpl.render().strip() == 'FOO'
    raises(TemplateNotFound, env.get_template, 'missing')


def test_caching():
    changed = False
    class TestLoader(loaders.BaseLoader):
        def get_source(self, environment, template):
            return u'foo', None, lambda: not changed
    env = Environment(loader=TestLoader(), cache_size=-1)
    tmpl = env.get_template('template')
    assert tmpl is env.get_template('template')
    changed = True
    assert tmpl is not env.get_template('template')
    changed = False

    env = Environment(loader=TestLoader(), cache_size=0)
    assert env.get_template('template') \
           is not env.get_template('template')

    env = Environment(loader=TestLoader(), cache_size=2)
    t1 = env.get_template('one')
    t2 = env.get_template('two')
    print env.cache
    assert t2 is env.get_template('two')
    assert t1 is env.get_template('one')
    t3 = env.get_template('three')
    assert 'one' in env.cache
    assert 'two' not in env.cache
    assert 'three' in env.cache


def test_split_template_path():
    assert split_template_path('foo/bar') == ['foo', 'bar']
    assert split_template_path('./foo/bar') == ['foo', 'bar']
    raises(TemplateNotFound, split_template_path, '../foo')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.