8909971a450562133c5da7f223086cb6949a0309
[django-tables2.git] / tests / templates.py
1 # -*- coding: utf8 -*-
2 from django.template import Template, Context, VariableDoesNotExist
3 from django.http import HttpRequest
4 from django.conf import settings
5 import django_tables2 as tables
6 from attest import Tests, Assert
7 from xml.etree import ElementTree as ET
8
9
10 templates = Tests()
11
12
13 class CountryTable(tables.Table):
14     name = tables.Column()
15     capital = tables.Column(sortable=False)
16     population = tables.Column(verbose_name='Population Size')
17     currency = tables.Column(visible=False)
18     tld = tables.Column(visible=False, verbose_name='Domain')
19     calling_code = tables.Column(accessor='cc',
20                                  verbose_name='Phone Ext.')
21
22
23 MEMORY_DATA = [
24     {'name': 'Germany', 'capital': 'Berlin', 'population': 83,
25      'currency': 'Euro (€)', 'tld': 'de', 'cc': 49},
26     {'name': 'France', 'population': 64, 'currency': 'Euro (€)',
27      'tld': 'fr', 'cc': 33},
28     {'name': 'Netherlands', 'capital': 'Amsterdam', 'cc': '31'},
29     {'name': 'Austria', 'cc': 43, 'currency': 'Euro (€)',
30      'population': 8}
31 ]
32
33
34 @templates.test
35 def as_html():
36     table = CountryTable(MEMORY_DATA)
37     root = ET.fromstring(table.as_html())
38     Assert(len(root.findall('.//thead/tr'))) == 1
39     Assert(len(root.findall('.//thead/tr/th'))) == 4
40     Assert(len(root.findall('.//tbody/tr'))) == 4
41     Assert(len(root.findall('.//tbody/tr/td'))) == 16
42
43     # no data with no empty_text
44     table = CountryTable([])
45     root = ET.fromstring(table.as_html())
46     Assert(1) == len(root.findall('.//thead/tr'))
47     Assert(4) == len(root.findall('.//thead/tr/th'))
48     Assert(0) == len(root.findall('.//tbody/tr'))
49
50     # no data WITH empty_text
51     table = CountryTable([], empty_text='this table is empty')
52     root = ET.fromstring(table.as_html())
53     Assert(1) == len(root.findall('.//thead/tr'))
54     Assert(4) == len(root.findall('.//thead/tr/th'))
55     Assert(1) == len(root.findall('.//tbody/tr'))
56     Assert(1) == len(root.findall('.//tbody/tr/td'))
57     Assert(int(root.find('.//tbody/tr/td').attrib['colspan'])) == len(root.findall('.//thead/tr/th'))
58     Assert(root.find('.//tbody/tr/td').text) == 'this table is empty'
59
60
61 @templates.test
62 def custom_rendering():
63     """For good measure, render some actual templates."""
64     countries = CountryTable(MEMORY_DATA)
65     context = Context({'countries': countries})
66
67     # automatic and manual column verbose names
68     template = Template('{% for column in countries.columns %}{{ column }}/'
69                         '{{ column.name }} {% endfor %}')
70     result = ('Name/name Capital/capital Population Size/population '
71               'Phone Ext./calling_code ')
72     Assert(result) == template.render(context)
73
74     # row values
75     template = Template('{% for row in countries.rows %}{% for value in row %}'
76                         '{{ value }} {% endfor %}{% endfor %}')
77     result = ('Germany Berlin 83 49 France None 64 33 Netherlands Amsterdam '
78               'None 31 Austria None 8 43 ')
79     Assert(result) == template.render(context)
80
81
82 @templates.test
83 def templatetag():
84     # ensure it works with a multi-order-by
85     table = CountryTable(MEMORY_DATA, order_by=('name', 'population'))
86     t = Template('{% load django_tables2 %}{% render_table table %}')
87     html = t.render(Context({'request': HttpRequest(), 'table': table}))
88
89     root = ET.fromstring(html)
90     Assert(len(root.findall('.//thead/tr'))) == 1
91     Assert(len(root.findall('.//thead/tr/th'))) == 4
92     Assert(len(root.findall('.//tbody/tr'))) == 4
93     Assert(len(root.findall('.//tbody/tr/td'))) == 16
94
95     # no data with no empty_text
96     table = CountryTable([])
97     t = Template('{% load django_tables2 %}{% render_table table %}')
98     html = t.render(Context({'request': HttpRequest(), 'table': table}))
99     root = ET.fromstring(html)
100     Assert(len(root.findall('.//thead/tr'))) == 1
101     Assert(len(root.findall('.//thead/tr/th'))) == 4
102     Assert(len(root.findall('.//tbody/tr'))) == 0
103
104     # no data WITH empty_text
105     table = CountryTable([], empty_text='this table is empty')
106     t = Template('{% load django_tables2 %}{% render_table table %}')
107     html = t.render(Context({'request': HttpRequest(), 'table': table}))
108     root = ET.fromstring(html)
109     Assert(len(root.findall('.//thead/tr'))) == 1
110     Assert(len(root.findall('.//thead/tr/th'))) == 4
111     Assert(len(root.findall('.//tbody/tr'))) == 1
112     Assert(len(root.findall('.//tbody/tr/td'))) == 1
113     Assert(int(root.find('.//tbody/tr/td').attrib['colspan'])) == len(root.findall('.//thead/tr/th'))
114     Assert(root.find('.//tbody/tr/td').text) == 'this table is empty'
115
116     # variable that doesn't exist (issue #8)
117     t = Template('{% load django_tables2 %}{% render_table this_doesnt_exist %}')
118     with Assert.raises(VariableDoesNotExist):
119         settings.DEBUG = True
120         t.render(Context())
121
122     # Should be silent with debug off
123     settings.DEBUG = False
124     t.render(Context())