def testMultiargFilter(self): '''Test calling filter with multiply args''' multiarg_template = Template(name='multiarg-filter.html') multiarg_template.parse( '{{ simple_var|multiarg_filter:"John" "Peter" }}') result = multiarg_template.execute({'simple_var': 'Hello'}) self.assertResult(result, 'Hello, John, Peter')
def testMultiFilter(self): '''Test calling sequnce of filters''' multifilter_template = Template(name='multifilter.html') multifilter_template.parse( '{{ simple_var|simple_filter|argument_filter:"world" }}') result = multifilter_template.execute({'simple_var': 'Hello'}) self.assertResult(result, 'HELLO, world')
def testVaribaleArgFilter(self): '''Test calling filter with variable argument''' varargfilter_template = Template(name='vararg-filter.html') varargfilter_template.parse('{{ simple_var|argument_filter:arg }}') result = varargfilter_template.execute({ 'simple_var': 'Hello', 'arg': 'world' }) self.assertResult(result, 'Hello, world')
class VariableFieldTestCase(unittest.TestCase): """Test case for block template tag """ def setUp(self): self.value = 'value' self.variable_template = Template(name='base.html') self.variable_template.parse("{{ simple_var }}") self.object_field_template = Template(name='object-field.html') self.object_field_template.parse('{{ object.field }}') self.deep_template = Template(name='deep-field.html') self.deep_template.parse('{{ object.field.field }}') def assertResult(self, result): assert result == self.value, 'Error emplate execution: %s' % ' '.join(( result, 'except', self.value)) def testSimpleVariable(self): '''Test simple variable accessing from template''' result = self.variable_template.execute({'simple_var': 'value'}) self.assertResult(result) def testObjectField(self): '''Test object's field accessing from template''' class TestClass(object): field = self.value result = self.object_field_template.execute({'object': TestClass()}) self.assertResult(result) def testDictValue(self): '''Test dict item accessing from template''' obj = {'field': self.value} result = self.object_field_template.execute({'object': obj}) self.assertResult(result) def testMultilevelField(self): '''Test accesing to dict item as object field''' class TestClass(object): field = {'field': self.value} result = self.deep_template.execute({'object': TestClass()}) self.assertResult(result)
class BlockTestCase(unittest.TestCase): """Test case for block template tag """ def setUp(self): self.base_template = Template(name='base.html') self.base_template.parse(BASE) def testExecuteTemplate(self): '''Test base template execution''' result = self.base_template.execute({'title': 'Hello'}) needed = BASE_RESULT % ('Hello') is_eq = fuzzy_equals(result, needed) assert is_eq, "Error template execution:\n%s" % ( "\n".join((result, "except", needed)))
class ExtendTestCase(BlockTestCase): """Test case for extend template tag """ def setUp(self): super(ExtendTestCase, self).setUp() self.extend_template = Template(loader=self.base_template.loader) self.extend_template.parse(EXTEND) def testExecuteTemplate(self): '''Test extending template execution''' result = self.extend_template.execute({'title': 'Hello'}) needed = EXTEND_RESULT % ('Hello') is_eq = fuzzy_equals(result, needed) assert is_eq, "Error template execution:\n%s" % ( "\n".join((result, "except", needed)))
def testArgFilter(self): '''Test calling template filter with arg value specified''' argument_template = Template(name='argument-filter.html') argument_template.parse('{{ simple_var|argument_filter:"world" }}') result = argument_template.execute({'simple_var': 'Hello'}) self.assertResult(result, 'Hello, world')
def testNumericConstFilter(self): '''Test simple template filter function applied to number''' simple_template = Template(name="consts-filter.html") simple_template.parse('{{ 1|simple_filter }}') result = simple_template.execute({}) self.assertResult(result, '1')
def testStringConstFilter(self): '''Test simple template filter function applied to string constant''' simple_template = Template(name="consts-filter.html") simple_template.parse('{{ "hello"|simple_filter }}') result = simple_template.execute({}) self.assertResult(result, 'HELLO')
def testSimpleFilter(self): '''Test simple template filter function applied to variable''' simple_template = Template(name='simple-filter.html') simple_template.parse("{{ simple_var|simple_filter }}") result = simple_template.execute({'simple_var': 'Hello'}) self.assertResult(result, 'HELLO')