2014-11-12 01:32:44 +00:00
|
|
|
from datetime import date
|
|
|
|
|
2014-12-07 08:43:10 +00:00
|
|
|
from django.template import TemplateSyntaxError
|
2014-12-03 20:36:17 +00:00
|
|
|
from django.test import SimpleTestCase
|
2014-11-12 01:32:44 +00:00
|
|
|
|
2014-12-07 08:43:10 +00:00
|
|
|
from ..utils import setup
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
|
2014-12-03 20:36:17 +00:00
|
|
|
class RegroupTagTests(SimpleTestCase):
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'regroup01': ''
|
|
|
|
'{% regroup data by bar as grouped %}'
|
|
|
|
'{% for group in grouped %}'
|
|
|
|
'{{ group.grouper }}:'
|
|
|
|
'{% for item in group.list %}'
|
|
|
|
'{{ item.foo }}'
|
|
|
|
'{% endfor %},'
|
|
|
|
'{% endfor %}'})
|
|
|
|
def test_regroup01(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('regroup01', {
|
2014-11-12 01:32:44 +00:00
|
|
|
'data': [{'foo': 'c', 'bar': 1},
|
|
|
|
{'foo': 'd', 'bar': 1},
|
|
|
|
{'foo': 'a', 'bar': 2},
|
|
|
|
{'foo': 'b', 'bar': 2},
|
|
|
|
{'foo': 'x', 'bar': 3}],
|
|
|
|
})
|
|
|
|
self.assertEqual(output, '1:cd,2:ab,3:x,')
|
|
|
|
|
|
|
|
@setup({'regroup02': ''
|
|
|
|
'{% regroup data by bar as grouped %}'
|
|
|
|
'{% for group in grouped %}'
|
|
|
|
'{{ group.grouper }}:'
|
|
|
|
'{% for item in group.list %}'
|
|
|
|
'{{ item.foo }}'
|
|
|
|
'{% endfor %}'
|
|
|
|
'{% endfor %}'})
|
|
|
|
def test_regroup02(self):
|
|
|
|
"""
|
|
|
|
Test for silent failure when target variable isn't found
|
|
|
|
"""
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('regroup02', {})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, '')
|
|
|
|
|
|
|
|
@setup({'regroup03': ''
|
|
|
|
'{% regroup data by at|date:"m" as grouped %}'
|
|
|
|
'{% for group in grouped %}'
|
|
|
|
'{{ group.grouper }}:'
|
|
|
|
'{% for item in group.list %}'
|
|
|
|
'{{ item.at|date:"d" }}'
|
|
|
|
'{% endfor %},'
|
|
|
|
'{% endfor %}'})
|
|
|
|
def test_regroup03(self):
|
|
|
|
"""
|
|
|
|
Regression tests for #17675
|
|
|
|
The date template filter has expects_localtime = True
|
|
|
|
"""
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('regroup03', {
|
2014-11-12 01:32:44 +00:00
|
|
|
'data': [{'at': date(2012, 2, 14)},
|
|
|
|
{'at': date(2012, 2, 28)},
|
|
|
|
{'at': date(2012, 7, 4)}],
|
|
|
|
})
|
|
|
|
self.assertEqual(output, '02:1428,07:04,')
|
|
|
|
|
|
|
|
@setup({'regroup04': ''
|
|
|
|
'{% regroup data by bar|join:"" as grouped %}'
|
|
|
|
'{% for group in grouped %}'
|
|
|
|
'{{ group.grouper }}:'
|
|
|
|
'{% for item in group.list %}'
|
|
|
|
'{{ item.foo|first }}'
|
|
|
|
'{% endfor %},'
|
|
|
|
'{% endfor %}'})
|
|
|
|
def test_regroup04(self):
|
|
|
|
"""
|
|
|
|
The join template filter has needs_autoescape = True
|
|
|
|
"""
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('regroup04', {
|
2014-11-12 01:32:44 +00:00
|
|
|
'data': [{'foo': 'x', 'bar': ['ab', 'c']},
|
|
|
|
{'foo': 'y', 'bar': ['a', 'bc']},
|
|
|
|
{'foo': 'z', 'bar': ['a', 'd']}],
|
|
|
|
})
|
|
|
|
self.assertEqual(output, 'abc:xy,ad:z,')
|
|
|
|
|
|
|
|
# Test syntax errors
|
|
|
|
@setup({'regroup05': '{% regroup data by bar as %}'})
|
|
|
|
def test_regroup05(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 08:43:10 +00:00
|
|
|
self.engine.get_template('regroup05')
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'regroup06': '{% regroup data by bar thisaintright grouped %}'})
|
|
|
|
def test_regroup06(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 08:43:10 +00:00
|
|
|
self.engine.get_template('regroup06')
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'regroup07': '{% regroup data thisaintright bar as grouped %}'})
|
|
|
|
def test_regroup07(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 08:43:10 +00:00
|
|
|
self.engine.get_template('regroup07')
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'regroup08': '{% regroup data by bar as grouped toomanyargs %}'})
|
|
|
|
def test_regroup08(self):
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 08:43:10 +00:00
|
|
|
self.engine.get_template('regroup08')
|