87 lines
3.0 KiB
Python
87 lines
3.0 KiB
Python
from django.template import Context, Template
|
|
|
|
|
|
def test_get():
|
|
t = Template('{{ foo|get:"foo-bar" }}')
|
|
context = Context({'foo': {'foo-bar': 'hello'}})
|
|
assert t.render(context) == 'hello'
|
|
context = Context({'foo': {'bar-foo': 'hello'}})
|
|
assert t.render(context) == 'None'
|
|
context = Context({'foo': None})
|
|
assert t.render(context) == 'None'
|
|
|
|
t = Template('{{ foo|get:"foo-bar"|default:"" }}')
|
|
context = Context({'foo': {'rab': 'hello'}})
|
|
assert t.render(context) == ''
|
|
|
|
t = Template('{{ foo|get:key }}')
|
|
context = Context({'foo': {'foo-bar': 'hello'}, 'key': 'foo-bar'})
|
|
assert t.render(context) == 'hello'
|
|
|
|
|
|
def test_getlist():
|
|
# nothing in context
|
|
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
|
|
context = Context()
|
|
assert t.render(context) == ''
|
|
# non value
|
|
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
|
|
context = Context({'values': None})
|
|
assert t.render(context) == ''
|
|
# not a list
|
|
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
|
|
context = Context({'values': 'foo'})
|
|
assert t.render(context) == 'None,None,None,'
|
|
# not a list of dict
|
|
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
|
|
context = Context({'values': ['foo']})
|
|
assert t.render(context) == 'None,'
|
|
|
|
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
|
|
context = Context({'values': [{'foo': 'bar'}, {'foo': 'baz'}]})
|
|
assert t.render(context) == 'bar,baz,'
|
|
t = Template('{% for v in values|getlist:"unknown" %}{{ v }},{% endfor %}')
|
|
context = Context({'values': [{'foo': 'bar'}, {'foo': 'baz'}]})
|
|
assert t.render(context) == 'None,None,'
|
|
t = Template('{% for v in values|getlist:"k"|getlist:"v" %}{{ v }},{% endfor %}')
|
|
context = Context({'values': [{'k': {'v': 'bar'}}, {'k': {'v': 'baz'}}]})
|
|
assert t.render(context) == 'bar,baz,'
|
|
t = Template('{% for v in values|getlist:"k"|getlist:"unknown" %}{{ v }},{% endfor %}')
|
|
context = Context({'values': [{'k': {'v': 'bar'}}, {'k': {'v': 'baz'}}]})
|
|
assert t.render(context) == 'None,None,'
|
|
t = Template('{% for v in values|getlist:"k"|getlist:"v" %}{{ v }},{% endfor %}')
|
|
context = Context({'values': [{'k': None}, {'k': {'v': 'baz'}}]})
|
|
assert t.render(context) == 'None,baz,'
|
|
|
|
|
|
def test_first():
|
|
t = Template('{{ foo|first }}')
|
|
|
|
context = Context({'foo': ['foo']})
|
|
assert t.render(context) == 'foo'
|
|
|
|
context = Context({'foo': 'foo'})
|
|
assert t.render(context) == 'f'
|
|
|
|
context = Context({'foo': ''})
|
|
assert t.render(context) == ''
|
|
|
|
context = Context({'foo': None})
|
|
assert t.render(context) == ''
|
|
|
|
|
|
def test_last():
|
|
t = Template('{{ foo|last }}')
|
|
|
|
context = Context({'foo': ['foo']})
|
|
assert t.render(context) == 'foo'
|
|
|
|
context = Context({'foo': 'foo'})
|
|
assert t.render(context) == 'o'
|
|
|
|
context = Context({'foo': ''})
|
|
assert t.render(context) == ''
|
|
|
|
context = Context({'foo': None})
|
|
assert t.render(context) == ''
|