tests.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_naturalday_tz(self):
  76. from django.contrib.humanize.templatetags.humanize import naturalday
  77. today = date.today()
  78. tz_one = tzinfo.FixedOffset(timedelta(hours=-12))
  79. tz_two = tzinfo.FixedOffset(timedelta(hours=12))
  80. # Can be today or yesterday
  81. date_one = datetime(today.year, today.month, today.day, tzinfo=tz_one)
  82. naturalday_one = naturalday(date_one)
  83. # Can be today or tomorrow
  84. date_two = datetime(today.year, today.month, today.day, tzinfo=tz_two)
  85. naturalday_two = naturalday(date_two)
  86. # As 24h of difference they will never be the same
  87. self.assertNotEqual(naturalday_one, naturalday_two)
  88. def test_naturaltime(self):
  89. now = datetime.now()
  90. test_list = [
  91. now,
  92. now - timedelta(seconds=1),
  93. now - timedelta(seconds=30),
  94. now - timedelta(minutes=1, seconds=30),
  95. now - timedelta(minutes=2),
  96. now - timedelta(hours=1, minutes=30, seconds=30),
  97. now - timedelta(hours=23, minutes=50, seconds=50),
  98. now - timedelta(days=1),
  99. now - timedelta(days=500),
  100. now + timedelta(seconds=1),
  101. now + timedelta(seconds=30),
  102. now + timedelta(minutes=1, seconds=30),
  103. now + timedelta(minutes=2),
  104. now + timedelta(hours=1, minutes=30, seconds=30),
  105. now + timedelta(hours=23, minutes=50, seconds=50),
  106. now + timedelta(days=1),
  107. now + timedelta(days=500),
  108. ]
  109. result_list = [
  110. 'now',
  111. 'a second ago',
  112. '30 seconds ago',
  113. 'a minute ago',
  114. '2 minutes ago',
  115. 'an hour ago',
  116. '23 hours ago',
  117. '1 day ago',
  118. '1 year, 4 months ago',
  119. 'a second from now',
  120. '30 seconds from now',
  121. 'a minute from now',
  122. '2 minutes from now',
  123. 'an hour from now',
  124. '23 hours from now',
  125. '1 day from now',
  126. '1 year, 4 months from now',
  127. ]
  128. self.humanize_tester(test_list, result_list, 'naturaltime')