tests.py 6.4 KB

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