Permalink
Browse files

Fixed #23958 -- Rewrote filter tests as unit tests.

  • Loading branch information...
prestontimmons authored and timgraham committed Dec 4, 2014
1 parent 34a06d9 commit 5c68870169deec9eff392c53a8e74e3482f02ade
Showing with 1,682 additions and 543 deletions.
  1. 0 tests/template_tests/filter_tests/__init__.py
  2. +46 −0 tests/template_tests/filter_tests/test_add.py
  3. +17 −0 tests/template_tests/filter_tests/test_addslashes.py
  4. +29 −0 tests/template_tests/filter_tests/test_autoescape.py
  5. +17 −0 tests/template_tests/filter_tests/test_capfirst.py
  6. +18 −0 tests/template_tests/filter_tests/test_center.py
  7. +86 −0 tests/template_tests/filter_tests/test_chaining.py
  8. +39 −0 tests/template_tests/filter_tests/test_cut.py
  9. +60 −0 tests/template_tests/filter_tests/test_date.py
  10. +47 −0 tests/template_tests/filter_tests/test_default.py
  11. +33 −0 tests/template_tests/filter_tests/test_escape.py
  12. +20 −0 tests/template_tests/filter_tests/test_escapejs.py
  13. +17 −0 tests/template_tests/filter_tests/test_first.py
  14. +18 −0 tests/template_tests/filter_tests/test_floatformat.py
  15. +52 −0 tests/template_tests/filter_tests/test_force_escape.py
  16. +30 −0 tests/template_tests/filter_tests/test_iriencode.py
  17. +49 −0 tests/template_tests/filter_tests/test_join.py
  18. +17 −0 tests/template_tests/filter_tests/test_last.py
  19. +43 −0 tests/template_tests/filter_tests/test_length.py
  20. +63 −0 tests/template_tests/filter_tests/test_length_is.py
  21. +22 −0 tests/template_tests/filter_tests/test_linebreaks.py
  22. +22 −0 tests/template_tests/filter_tests/test_linebreaksbr.py
  23. +30 −0 tests/template_tests/filter_tests/test_linenumbers.py
  24. +17 −0 tests/template_tests/filter_tests/test_ljust.py
  25. +17 −0 tests/template_tests/filter_tests/test_lower.py
  26. +33 −0 tests/template_tests/filter_tests/test_make_list.py
  27. +35 −0 tests/template_tests/filter_tests/test_phone2numeric.py
  28. +17 −0 tests/template_tests/filter_tests/test_random.py
  29. +37 −0 tests/template_tests/filter_tests/test_removetags.py
  30. +17 −0 tests/template_tests/filter_tests/test_rjust.py
  31. +16 −0 tests/template_tests/filter_tests/test_safe.py
  32. +17 −0 tests/template_tests/filter_tests/test_safeseq.py
  33. +17 −0 tests/template_tests/filter_tests/test_slice.py
  34. +21 −0 tests/template_tests/filter_tests/test_slugify.py
  35. +22 −0 tests/template_tests/filter_tests/test_stringformat.py
  36. +29 −0 tests/template_tests/filter_tests/test_striptags.py
  37. +42 −0 tests/template_tests/filter_tests/test_time.py
  38. +118 −0 tests/template_tests/filter_tests/test_timesince.py
  39. +94 −0 tests/template_tests/filter_tests/test_timeuntil.py
  40. +16 −0 tests/template_tests/filter_tests/test_title.py
  41. +16 −0 tests/template_tests/filter_tests/test_truncatechars.py
  42. +18 −0 tests/template_tests/filter_tests/test_truncatewords.py
  43. +75 −0 tests/template_tests/filter_tests/test_unordered_list.py
  44. +21 −0 tests/template_tests/filter_tests/test_upper.py
  45. +16 −0 tests/template_tests/filter_tests/test_urlencode.py
  46. +70 −0 tests/template_tests/filter_tests/test_urlize.py
  47. +38 −0 tests/template_tests/filter_tests/test_urlizetrunc.py
  48. +17 −0 tests/template_tests/filter_tests/test_wordcount.py
  49. +18 −0 tests/template_tests/filter_tests/test_wordwrap.py
  50. +17 −0 tests/template_tests/filter_tests/timezone_utils.py
  51. +0 −387 tests/template_tests/filters.py
  52. +1 −1 tests/template_tests/syntax_tests/test_autoescape.py
  53. +1 −1 tests/template_tests/syntax_tests/test_basic.py
  54. +1 −1 tests/template_tests/syntax_tests/test_builtins.py
  55. +1 −1 tests/template_tests/syntax_tests/test_cache.py
  56. +1 −1 tests/template_tests/syntax_tests/test_comment.py
  57. +1 −1 tests/template_tests/syntax_tests/test_cycle.py
  58. +1 −1 tests/template_tests/syntax_tests/test_exceptions.py
  59. +1 −1 tests/template_tests/syntax_tests/test_extends.py
  60. +1 −1 tests/template_tests/syntax_tests/test_filter_syntax.py
  61. +1 −1 tests/template_tests/syntax_tests/test_filter_tag.py
  62. +1 −1 tests/template_tests/syntax_tests/test_firstof.py
  63. +1 −1 tests/template_tests/syntax_tests/test_for.py
  64. +1 −1 tests/template_tests/syntax_tests/test_i18n.py
  65. +1 −1 tests/template_tests/syntax_tests/test_if.py
  66. +1 −1 tests/template_tests/syntax_tests/test_if_changed.py
  67. +1 −1 tests/template_tests/syntax_tests/test_if_equal.py
  68. +1 −1 tests/template_tests/syntax_tests/test_include.py
  69. +1 −1 tests/template_tests/syntax_tests/test_invalid_string.py
  70. +1 −1 tests/template_tests/syntax_tests/test_list_index.py
  71. +1 −1 tests/template_tests/syntax_tests/test_load.py
  72. +1 −1 tests/template_tests/syntax_tests/test_lorem.py
  73. +1 −1 tests/template_tests/syntax_tests/test_multiline.py
  74. +1 −1 tests/template_tests/syntax_tests/test_named_endblock.py
  75. +1 −1 tests/template_tests/syntax_tests/test_now.py
  76. +1 −1 tests/template_tests/syntax_tests/test_numpy.py
  77. +1 −1 tests/template_tests/syntax_tests/test_regroup.py
  78. +1 −1 tests/template_tests/syntax_tests/test_setup.py
  79. +1 −1 tests/template_tests/syntax_tests/test_simple_tag.py
  80. +1 −1 tests/template_tests/syntax_tests/test_spaceless.py
  81. +1 −1 tests/template_tests/syntax_tests/test_ssi.py
  82. +1 −1 tests/template_tests/syntax_tests/test_static.py
  83. +1 −1 tests/template_tests/syntax_tests/test_template_tag.py
  84. +1 −1 tests/template_tests/syntax_tests/test_url.py
  85. +1 −1 tests/template_tests/syntax_tests/test_verbatim.py
  86. +1 −1 tests/template_tests/syntax_tests/test_width_ratio.py
  87. +1 −1 tests/template_tests/syntax_tests/test_with.py
  88. +0 −120 tests/template_tests/tests.py
  89. 0 tests/template_tests/{syntax_tests → }/utils.py
@@ -0,0 +1,46 @@
from datetime import date, timedelta
from django.test import SimpleTestCase
from ..utils import render, setup
class AddTests(SimpleTestCase):
"""
Tests for #11687 and #16676
"""
@setup({'add01': '{{ i|add:"5" }}'})
def test_add01(self):
output = render('add01', {'i': 2000})
self.assertEqual(output, '2005')
@setup({'add02': '{{ i|add:"napis" }}'})
def test_add02(self):
output = render('add02', {'i': 2000})
self.assertEqual(output, '')
@setup({'add03': '{{ i|add:16 }}'})
def test_add03(self):
output = render('add03', {'i': 'not_an_int'})
self.assertEqual(output, '')
@setup({'add04': '{{ i|add:"16" }}'})
def test_add04(self):
output = render('add04', {'i': 'not_an_int'})
self.assertEqual(output, 'not_an_int16')
@setup({'add05': '{{ l1|add:l2 }}'})
def test_add05(self):
output = render('add05', {'l1': [1, 2], 'l2': [3, 4]})
self.assertEqual(output, '[1, 2, 3, 4]')
@setup({'add06': '{{ t1|add:t2 }}'})
def test_add06(self):
output = render('add06', {'t1': (3, 4), 't2': (1, 2)})
self.assertEqual(output, '(3, 4, 1, 2)')
@setup({'add07': '{{ d|add:t }}'})
def test_add07(self):
output = render('add07', {'d': date(2000, 1, 1), 't': timedelta(10)})
self.assertEqual(output, 'Jan. 11, 2000')
@@ -0,0 +1,17 @@
from django.test import SimpleTestCase
from django.utils.safestring import mark_safe
from ..utils import render, setup
class AddslashesTests(SimpleTestCase):
@setup({'addslashes01': '{% autoescape off %}{{ a|addslashes }} {{ b|addslashes }}{% endautoescape %}'})
def test_addslashes01(self):
output = render('addslashes01', {"a": "<a>'", "b": mark_safe("<a>'")})
self.assertEqual(output, r"<a>\' <a>\'")
@setup({'addslashes02': '{{ a|addslashes }} {{ b|addslashes }}'})
def test_addslashes02(self):
output = render('addslashes02', {"a": "<a>'", "b": mark_safe("<a>'")})
self.assertEqual(output, r"&lt;a&gt;\&#39; <a>\'")
@@ -0,0 +1,29 @@
from django.test import SimpleTestCase
from ..utils import render, setup, SafeClass, UnsafeClass
class AutoescapeStringfilterTests(SimpleTestCase):
"""
Filters decorated with stringfilter still respect is_safe.
"""
@setup({'autoescape-stringfilter01': '{{ unsafe|capfirst }}'})
def test_autoescape_stringfilter01(self):
output = render('autoescape-stringfilter01', {'unsafe': UnsafeClass()})
self.assertEqual(output, 'You &amp; me')
@setup({'autoescape-stringfilter02': '{% autoescape off %}{{ unsafe|capfirst }}{% endautoescape %}'})
def test_autoescape_stringfilter02(self):
output = render('autoescape-stringfilter02', {'unsafe': UnsafeClass()})
self.assertEqual(output, 'You & me')
@setup({'autoescape-stringfilter03': '{{ safe|capfirst }}'})
def test_autoescape_stringfilter03(self):
output = render('autoescape-stringfilter03', {'safe': SafeClass()})
self.assertEqual(output, 'You &gt; me')
@setup({'autoescape-stringfilter04': '{% autoescape off %}{{ safe|capfirst }}{% endautoescape %}'})
def test_autoescape_stringfilter04(self):
output = render('autoescape-stringfilter04', {'safe': SafeClass()})
self.assertEqual(output, 'You &gt; me')
@@ -0,0 +1,17 @@
from django.test import SimpleTestCase
from django.utils.safestring import mark_safe
from ..utils import render, setup
class CapfirstTests(SimpleTestCase):
@setup({'capfirst01': '{% autoescape off %}{{ a|capfirst }} {{ b|capfirst }}{% endautoescape %}'})
def test_capfirst01(self):
output = render('capfirst01', {'a': 'fred>', 'b': mark_safe('fred&gt;')})
self.assertEqual(output, 'Fred> Fred&gt;')
@setup({'capfirst02': '{{ a|capfirst }} {{ b|capfirst }}'})
def test_capfirst02(self):
output = render('capfirst02', {'a': 'fred>', 'b': mark_safe('fred&gt;')})
self.assertEqual(output, 'Fred&gt; Fred&gt;')
@@ -0,0 +1,18 @@
from django.test import SimpleTestCase
from django.utils.safestring import mark_safe
from ..utils import render, setup
class CenterTests(SimpleTestCase):
@setup({'center01':
'{% autoescape off %}.{{ a|center:"5" }}. .{{ b|center:"5" }}.{% endautoescape %}'})
def test_center01(self):
output = render('center01', {"a": "a&b", "b": mark_safe("a&b")})
self.assertEqual(output, ". a&b . . a&b .")
@setup({'center02': '.{{ a|center:"5" }}. .{{ b|center:"5" }}.'})
def test_center02(self):
output = render('center02', {"a": "a&b", "b": mark_safe("a&b")})
self.assertEqual(output, ". a&amp;b . . a&b .")
@@ -0,0 +1,86 @@
from django.test import SimpleTestCase
from django.utils.safestring import mark_safe
from ..utils import render, setup
class ChainingTests(SimpleTestCase):
"""
Chaining safeness-preserving filters should not alter the safe status.
"""
@setup({'chaining01': '{{ a|capfirst|center:"7" }}.{{ b|capfirst|center:"7" }}'})
def test_chaining01(self):
output = render('chaining01', {'a': 'a < b', 'b': mark_safe('a < b')})
self.assertEqual(output, ' A &lt; b . A < b ')
@setup({'chaining02':
'{% autoescape off %}{{ a|capfirst|center:"7" }}.{{ b|capfirst|center:"7" }}{% endautoescape %}'})
def test_chaining02(self):
output = render('chaining02', {'a': 'a < b', 'b': mark_safe('a < b')})
self.assertEqual(output, ' A < b . A < b ')
# Using a filter that forces a string back to unsafe:
@setup({'chaining03': '{{ a|cut:"b"|capfirst }}.{{ b|cut:"b"|capfirst }}'})
def test_chaining03(self):
output = render('chaining03', {'a': 'a < b', 'b': mark_safe('a < b')})
self.assertEqual(output, 'A &lt; .A < ')
@setup({'chaining04':
'{% autoescape off %}{{ a|cut:"b"|capfirst }}.{{ b|cut:"b"|capfirst }}{% endautoescape %}'})
def test_chaining04(self):
output = render('chaining04', {'a': 'a < b', 'b': mark_safe('a < b')})
self.assertEqual(output, 'A < .A < ')
# Using a filter that forces safeness does not lead to double-escaping
@setup({'chaining05': '{{ a|escape|capfirst }}'})
def test_chaining05(self):
output = render('chaining05', {'a': 'a < b'})
self.assertEqual(output, 'A &lt; b')
@setup({'chaining06': '{% autoescape off %}{{ a|escape|capfirst }}{% endautoescape %}'})
def test_chaining06(self):
output = render('chaining06', {'a': 'a < b'})
self.assertEqual(output, 'A &lt; b')
# Force to safe, then back (also showing why using force_escape too
# early in a chain can lead to unexpected results).
@setup({'chaining07': '{{ a|force_escape|cut:";" }}'})
def test_chaining07(self):
output = render('chaining07', {'a': 'a < b'})
self.assertEqual(output, 'a &amp;lt b')
@setup({'chaining08': '{% autoescape off %}{{ a|force_escape|cut:";" }}{% endautoescape %}'})
def test_chaining08(self):
output = render('chaining08', {'a': 'a < b'})
self.assertEqual(output, 'a &lt b')
@setup({'chaining09': '{{ a|cut:";"|force_escape }}'})
def test_chaining09(self):
output = render('chaining09', {'a': 'a < b'})
self.assertEqual(output, 'a &lt; b')
@setup({'chaining10': '{% autoescape off %}{{ a|cut:";"|force_escape }}{% endautoescape %}'})
def test_chaining10(self):
output = render('chaining10', {'a': 'a < b'})
self.assertEqual(output, 'a &lt; b')
@setup({'chaining11': '{{ a|cut:"b"|safe }}'})
def test_chaining11(self):
output = render('chaining11', {'a': 'a < b'})
self.assertEqual(output, 'a < ')
@setup({'chaining12': '{% autoescape off %}{{ a|cut:"b"|safe }}{% endautoescape %}'})
def test_chaining12(self):
output = render('chaining12', {'a': 'a < b'})
self.assertEqual(output, 'a < ')
@setup({'chaining13': '{{ a|safe|force_escape }}'})
def test_chaining13(self):
output = render('chaining13', {"a": "a < b"})
self.assertEqual(output, 'a &lt; b')
@setup({'chaining14': '{% autoescape off %}{{ a|safe|force_escape }}{% endautoescape %}'})
def test_chaining14(self):
output = render('chaining14', {"a": "a < b"})
self.assertEqual(output, 'a &lt; b')
@@ -0,0 +1,39 @@
from django.test import SimpleTestCase
from django.utils.safestring import mark_safe
from ..utils import render, setup
class CutTests(SimpleTestCase):
@setup({'cut01': '{% autoescape off %}{{ a|cut:"x" }} {{ b|cut:"x" }}{% endautoescape %}'})
def test_cut01(self):
output = render('cut01', {"a": "x&y", "b": mark_safe("x&amp;y")})
self.assertEqual(output, "&y &amp;y")
@setup({'cut02': '{{ a|cut:"x" }} {{ b|cut:"x" }}'})
def test_cut02(self):
output = render('cut02', {"a": "x&y", "b": mark_safe("x&amp;y")})
self.assertEqual(output, "&amp;y &amp;y")
@setup({'cut03': '{% autoescape off %}{{ a|cut:"&" }} {{ b|cut:"&" }}{% endautoescape %}'})
def test_cut03(self):
output = render('cut03', {"a": "x&y", "b": mark_safe("x&amp;y")})
self.assertEqual(output, "xy xamp;y")
@setup({'cut04': '{{ a|cut:"&" }} {{ b|cut:"&" }}'})
def test_cut04(self):
output = render('cut04', {"a": "x&y", "b": mark_safe("x&amp;y")})
self.assertEqual(output, "xy xamp;y")
# Passing ';' to cut can break existing HTML entities, so those strings
# are auto-escaped.
@setup({'cut05': '{% autoescape off %}{{ a|cut:";" }} {{ b|cut:";" }}{% endautoescape %}'})
def test_cut05(self):
output = render('cut05', {"a": "x&y", "b": mark_safe("x&amp;y")})
self.assertEqual(output, "x&y x&ampy")
@setup({'cut06': '{{ a|cut:";" }} {{ b|cut:";" }}'})
def test_cut06(self):
output = render('cut06', {"a": "x&y", "b": mark_safe("x&amp;y")})
self.assertEqual(output, "x&amp;y x&amp;ampy")
@@ -0,0 +1,60 @@
from datetime import datetime, time
from django.utils import timezone
from .timezone_utils import TimezoneTestCase
from ..utils import render, setup
class DateTests(TimezoneTestCase):
@setup({'date01': '{{ d|date:"m" }}'})
def test_date01(self):
output = render('date01', {'d': datetime(2008, 1, 1)})
self.assertEqual(output, '01')
@setup({'date02': '{{ d|date }}'})
def test_date02(self):
output = render('date02', {'d': datetime(2008, 1, 1)})
self.assertEqual(output, 'Jan. 1, 2008')
@setup({'date03': '{{ d|date:"m" }}'})
def test_date03(self):
"""
#9520: Make sure |date doesn't blow up on non-dates
"""
output = render('date03', {'d': 'fail_string'})
self.assertEqual(output, '')
# ISO date formats
@setup({'date04': '{{ d|date:"o" }}'})
def test_date04(self):
output = render('date04', {'d': datetime(2008, 12, 29)})
self.assertEqual(output, '2009')
@setup({'date05': '{{ d|date:"o" }}'})
def test_date05(self):
output = render('date05', {'d': datetime(2010, 1, 3)})
self.assertEqual(output, '2009')
# Timezone name
@setup({'date06': '{{ d|date:"e" }}'})
def test_date06(self):
output = render('date06', {'d': datetime(2009, 3, 12, tzinfo=timezone.get_fixed_timezone(30))})
self.assertEqual(output, '+0030')
@setup({'date07': '{{ d|date:"e" }}'})
def test_date07(self):
output = render('date07', {'d': datetime(2009, 3, 12)})
self.assertEqual(output, '')
# #19370: Make sure |date doesn't blow up on a midnight time object
@setup({'date08': '{{ t|date:"H:i" }}'})
def test_date08(self):
output = render('date08', {'t': time(0, 1)})
self.assertEqual(output, '00:01')
@setup({'date09': '{{ t|date:"H:i" }}'})
def test_date09(self):
output = render('date09', {'t': time(0, 0)})
self.assertEqual(output, '00:00')
@@ -0,0 +1,47 @@
from django.test import SimpleTestCase
from django.utils.safestring import mark_safe
from ..utils import render, setup
class DefaultTests(SimpleTestCase):
"""
Literal string arguments to the default filter are always treated as
safe strings, regardless of the auto-escaping state.
Note: we have to use {"a": ""} here, otherwise the invalid template
variable string interferes with the test result.
"""
@setup({'default01': '{{ a|default:"x<" }}'})
def test_default01(self):
output = render('default01', {"a": ""})
self.assertEqual(output, "x<")
@setup({'default02': '{% autoescape off %}{{ a|default:"x<" }}{% endautoescape %}'})
def test_default02(self):
output = render('default02', {"a": ""})
self.assertEqual(output, "x<")
@setup({'default03': '{{ a|default:"x<" }}'})
def test_default03(self):
output = render('default03', {"a": mark_safe("x>")})
self.assertEqual(output, "x>")
@setup({'default04': '{% autoescape off %}{{ a|default:"x<" }}{% endautoescape %}'})
def test_default04(self):
output = render('default04', {"a": mark_safe("x>")})
self.assertEqual(output, "x>")
class DefaultIfNoneTests(SimpleTestCase):
@setup({'default_if_none01': '{{ a|default:"x<" }}'})
def test_default_if_none01(self):
output = render('default_if_none01', {"a": None})
self.assertEqual(output, "x<")
@setup({'default_if_none02': '{% autoescape off %}{{ a|default:"x<" }}{% endautoescape %}'})
def test_default_if_none02(self):
output = render('default_if_none02', {"a": None})
self.assertEqual(output, "x<")
Oops, something went wrong.

0 comments on commit 5c68870

Please sign in to comment.