tests.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from __future__ import with_statement
  2. from datetime import timedelta, date, datetime, tzinfo
  3. from django.template import Template, Context, add_to_builtins, defaultfilters
  4. from django.test import TestCase
  5. from django.utils import translation, tzinfo
  6. from django.utils.translation import ugettext as _
  7. from django.utils.html import escape
  8. add_to_builtins('django.contrib.humanize.templatetags.humanize')
  9. class HumanizeTests(TestCase):
  10. def humanize_tester(self, test_list, result_list, method):
  11. # Using max below ensures we go through both lists
  12. # However, if the lists are not equal length, this raises an exception
  13. for test_content, result in zip(test_list, result_list):
  14. t = Template('{{ test_content|%s }}' % method)
  15. rendered = t.render(Context(locals())).strip()
  16. self.assertEqual(rendered, escape(result),
  17. msg="%s test failed, produced '%s', should've produced '%s'" % (method, rendered, result))
  18. def test_ordinal(self):
  19. test_list = ('1','2','3','4','11','12',
  20. '13','101','102','103','111',
  21. 'something else', None)
  22. result_list = ('1st', '2nd', '3rd', '4th', '11th',
  23. '12th', '13th', '101st', '102nd', '103rd',
  24. '111th', 'something else', None)
  25. self.humanize_tester(test_list, result_list, 'ordinal')
  26. def test_intcomma(self):
  27. test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
  28. '100', '1000', '10123', '10311', '1000000', '1234567.1234567',
  29. None)
  30. result_list = ('100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.25',
  31. '100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567',
  32. None)
  33. self.humanize_tester(test_list, result_list, 'intcomma')
  34. def test_intword(self):
  35. test_list = ('100', '1000000', '1200000', '1290000',
  36. '1000000000', '2000000000', '6000000000000',
  37. None)
  38. result_list = ('100', '1.0 million', '1.2 million', '1.3 million',
  39. '1.0 billion', '2.0 billion', '6.0 trillion',
  40. None)
  41. self.humanize_tester(test_list, result_list, 'intword')
  42. def test_i18n_intcomma(self):
  43. test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
  44. '100', '1000', '10123', '10311', '1000000', None)
  45. result_list = ('100', '1.000', '10.123', '10.311', '1.000.000', '1.234.567,25',
  46. '100', '1.000', '10.123', '10.311', '1.000.000', None)
  47. with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
  48. with translation.override('de'):
  49. self.humanize_tester(test_list, result_list, 'intcomma')
  50. def test_i18n_intword(self):
  51. test_list = ('100', '1000000', '1200000', '1290000',
  52. '1000000000','2000000000','6000000000000')
  53. result_list = ('100', '1,0 Million', '1,2 Millionen', '1,3 Millionen',
  54. '1,0 Milliarde', '2,0 Milliarden', '6,0 Billionen')
  55. with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
  56. with translation.override('de'):
  57. self.humanize_tester(test_list, result_list, 'intword')
  58. def test_apnumber(self):
  59. test_list = [str(x) for x in range(1, 11)]
  60. test_list.append(None)
  61. result_list = (u'one', u'two', u'three', u'four', u'five', u'six',
  62. u'seven', u'eight', u'nine', u'10', None)
  63. self.humanize_tester(test_list, result_list, 'apnumber')
  64. def test_naturalday(self):
  65. today = date.today()
  66. yesterday = today - timedelta(days=1)
  67. tomorrow = today + timedelta(days=1)
  68. someday = today - timedelta(days=10)
  69. notdate = u"I'm not a date value"
  70. test_list = (today, yesterday, tomorrow, someday, notdate, None)
  71. someday_result = defaultfilters.date(someday)
  72. result_list = (_(u'today'), _(u'yesterday'), _(u'tomorrow'),
  73. someday_result, u"I'm not a date value", None)
  74. self.humanize_tester(test_list, result_list, 'naturalday')
  75. def test_naturaltime(self):
  76. from django.template import defaultfilters
  77. now = datetime.now()
  78. seconds_ago = now - timedelta(seconds=30)
  79. a_minute_ago = now - timedelta(minutes=1, seconds=30)
  80. minutes_ago = now - timedelta(minutes=2)
  81. an_hour_ago = now - timedelta(hours=1, minutes=30, seconds=30)
  82. hours_ago = now - timedelta(hours=23, minutes=50, seconds=50)
  83. test_list = (now, a_minute_ago, an_hour_ago)
  84. result_list = (_(u'now'), _(u'a minute ago'), _(u'an hour ago'))
  85. self.humanize_tester(test_list, result_list, 'naturaltime')
  86. t = Template('{{ seconds_ago|%s }}' % 'naturaltime')
  87. rendered = t.render(Context(locals())).strip()
  88. self.assertTrue(u' seconds ago' in rendered)
  89. t = Template('{{ minutes_ago|%s }}' % 'naturaltime')
  90. rendered = t.render(Context(locals())).strip()
  91. self.assertTrue(u' minutes ago' in rendered)
  92. t = Template('{{ hours_ago|%s }}' % 'naturaltime')
  93. rendered = t.render(Context(locals())).strip()
  94. self.assertTrue(u' hours ago' in rendered)
  95. def test_naturalday_tz(self):
  96. from django.contrib.humanize.templatetags.humanize import naturalday
  97. today = date.today()
  98. tz_one = tzinfo.FixedOffset(timedelta(hours=-12))
  99. tz_two = tzinfo.FixedOffset(timedelta(hours=12))
  100. # Can be today or yesterday
  101. date_one = datetime(today.year, today.month, today.day, tzinfo=tz_one)
  102. naturalday_one = naturalday(date_one)
  103. # Can be today or tomorrow
  104. date_two = datetime(today.year, today.month, today.day, tzinfo=tz_two)
  105. naturalday_two = naturalday(date_two)
  106. # As 24h of difference they will never be the same
  107. self.assertNotEqual(naturalday_one, naturalday_two)