summaryrefslogtreecommitdiff
path: root/grc/tests/test_block_templates.py
blob: df9ab37550cad6c64ae4bc16dcf80570f2cb71b5 (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
import pytest

from grc.core.blocks._templates import MakoTemplates
from grc.core.errors import TemplateError


class Block(object):
    namespace_templates = {}

    templates = MakoTemplates(None)

    def __init__(self, **kwargs):
        self.namespace_templates.update(kwargs)


def test_simple():
    t = MakoTemplates(_bind_to=Block(num='123'), test='abc${num}')
    assert t['test'] == 'abc${num}'
    assert t.render('test') == 'abc123'
    assert 'abc${num}' in t._template_cache


def test_instance():
    block = Block(num='123')
    block.templates['test'] = 'abc${num}'
    assert block.templates.render('test') == 'abc123'
    assert block.templates is block.__dict__['templates']


def test_list():
    templates = ['abc${num}', '${2 * num}c']
    t = MakoTemplates(_bind_to=Block(num='123'), test=templates)
    assert t['test'] == templates
    assert t.render('test') == ['abc123', '123123c']
    assert set(templates) == set(t._template_cache.keys())


def test_parse_error():
    with pytest.raises(TemplateError):
        MakoTemplates(_bind_to=Block(num='123'), test='abc${num NOT CLOSING').render('test')


def test_parse_error2():
    with pytest.raises(TemplateError):
        MakoTemplates(_bind_to=Block(num='123'), test='abc${ WRONG_VAR }').render('test')