forked from jieter/django-tables2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_templatetags.py
190 lines (149 loc) · 6.84 KB
/
test_templatetags.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# coding: utf-8
from __future__ import unicode_literals
import django_tables2 as tables
import pytest
from django.core.exceptions import ImproperlyConfigured
from django.template import (Context, RequestContext, Template,
TemplateSyntaxError)
from django.utils import six
from django.utils.six.moves.urllib.parse import parse_qs
from django_tables2.config import RequestConfig
from .app.models import Person, Region
from .test_templates import MEMORY_DATA, CountryTable
from .utils import assertNumQueries, build_request, parse
def test_render_table_templatetag_invalid_type():
template = Template('{% load django_tables2 %}{% render_table table %}')
with pytest.raises(ValueError):
template.render(Context({
'request': build_request(),
'table': dict()
}))
def test_render_table_templatetag(settings):
request = build_request('/')
# ensure it works with a multi-order-by
table = CountryTable(MEMORY_DATA, order_by=('name', 'population'))
RequestConfig(request).configure(table)
template = Template('{% load django_tables2 %}{% render_table table %}')
html = template.render(Context({'request': request, 'table': table}))
root = parse(html)
assert len(root.findall('.//thead/tr')) == 1
assert len(root.findall('.//thead/tr/th')) == 4
assert len(root.findall('.//tbody/tr')) == 4
assert len(root.findall('.//tbody/tr/td')) == 16
# no data with no empty_text
table = CountryTable([])
template = Template('{% load django_tables2 %}{% render_table table %}')
html = template.render(Context({'request': build_request('/'), 'table': table}))
root = parse(html)
assert len(root.findall('.//thead/tr')) == 1
assert len(root.findall('.//thead/tr/th')) == 4
assert len(root.findall('.//tbody/tr')) == 0
# no data WITH empty_text
request = build_request('/')
table = CountryTable([], empty_text='this table is empty')
RequestConfig(request).configure(table)
template = Template('{% load django_tables2 %}{% render_table table %}')
html = template.render(Context({'request': request, 'table': table}))
root = parse(html)
assert len(root.findall('.//thead/tr')) == 1
assert len(root.findall('.//thead/tr/th')) == 4
assert len(root.findall('.//tbody/tr')) == 1
assert len(root.findall('.//tbody/tr/td')) == 1
assert int(root.find('.//tbody/tr/td').get('colspan')) == len(root.findall('.//thead/tr/th'))
assert root.find('.//tbody/tr/td').text == 'this table is empty'
# variable that doesn't exist (issue #8)
template = Template('{% load django_tables2 %}'
'{% render_table this_doesnt_exist %}')
with pytest.raises(ValueError):
settings.DEBUG = True
template.render(Context())
# Should still be noisy with debug off
with pytest.raises(ValueError):
settings.DEBUG = False
template.render(Context())
def test_render_table_should_support_template_argument():
table = CountryTable(MEMORY_DATA, order_by=('name', 'population'))
template = Template('{% load django_tables2 %}'
'{% render_table table "dummy.html" %}')
context = RequestContext(build_request(), {'table': table})
assert template.render(context) == 'dummy template contents\n'
def test_render_table_template_argument_list():
template = Template('{% load django_tables2 %}'
'{% render_table table template_list %}')
context = RequestContext(build_request(), {
'table': CountryTable(MEMORY_DATA, order_by=('name', 'population')),
'template_list': ('dummy.html', 'child/foo.html')
})
assert template.render(context) == 'dummy template contents\n'
@pytest.mark.django_db
def test_render_table_supports_queryset():
for name in ('Mackay', 'Brisbane', 'Maryborough'):
Region.objects.create(name=name)
template = Template('{% load django_tables2 %}{% render_table qs %}')
html = template.render(Context({'qs': Region.objects.all(),
'request': build_request('/')}))
root = parse(html)
assert [e.text for e in root.findall('.//thead/tr/th/a')] == ['ID', 'Name', 'Mayor']
td = [[td.text for td in tr.findall('td')] for tr in root.findall('.//tbody/tr')]
db = []
for region in Region.objects.all():
db.append([six.text_type(region.id), region.name, "—"])
assert td == db
def test_querystring_templatetag():
template = Template('{% load django_tables2 %}'
'<b>{% querystring "name"="Brad" foo.bar=value %}</b>')
# Should be something like: <root>?name=Brad&a=b&c=5&age=21</root>
xml = template.render(Context({
'request': build_request('/?a=b&name=dog&c=5'),
'foo': {'bar': 'age'},
'value': 21,
}))
# Ensure it's valid XML, retrieve the URL
url = parse(xml).text
qs = parse_qs(url[1:]) # everything after the ?
assert qs['name'] == ['Brad']
assert qs['age'] == ['21']
assert qs['a'] == ['b']
assert qs['c'] == ['5']
def test_querystring_templatetag_requires_request():
template = Template('{% load django_tables2 %}{% querystring "name"="Brad" %}')
with pytest.raises(ImproperlyConfigured):
template.render(Context())
def test_querystring_templatetag_supports_without():
context = Context({
'request': build_request('/?a=b&name=dog&c=5'),
'a_var': 'a',
})
template = Template('{% load django_tables2 %}'
'<b>{% querystring "name"="Brad" without a_var %}</b>')
url = parse(template.render(context)).text
qs = parse_qs(url[1:]) # trim the ?
assert set(qs.keys()) == set(['name', 'c'])
# Try with only exclusions
template = Template('{% load django_tables2 %}'
'<b>{% querystring without "a" "name" %}</b>')
url = parse(template.render(context)).text
qs = parse_qs(url[1:]) # trim the ?
assert set(qs.keys()) == set(["c"])
def test_querystring_syntax_error():
with pytest.raises(TemplateSyntaxError):
Template('{% load django_tables2 %}{% querystring foo= %}')
def test_title_should_only_apply_to_words_without_uppercase_letters():
expectations = {
'a brown fox': 'A Brown Fox',
'a brown foX': 'A Brown foX',
'black FBI': 'Black FBI',
'f.b.i': 'F.B.I',
'start 6pm': 'Start 6pm',
}
for raw, expected in expectations.items():
template = Template('{% load django_tables2 %}{{ x|title }}')
assert template.render(Context({'x': raw})) == expected
@pytest.mark.django_db
def test_as_html_db_queries(transactional_db):
class PersonTable(tables.Table):
class Meta:
model = Person
Person.objects.create(first_name='John', last_name='Doo')
with assertNumQueries(count=2):
PersonTable(Person.objects.all()).as_html(build_request())