tests.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. from __future__ import unicode_literals
  2. import datetime
  3. from decimal import Decimal
  4. from unittest import skipIf
  5. try:
  6. import pytz
  7. except ImportError:
  8. pytz = None
  9. from django.contrib.humanize.templatetags import humanize
  10. from django.template import Template, Context, defaultfilters
  11. from django.test import TestCase, override_settings
  12. from django.utils.html import escape
  13. from django.utils.timezone import utc, get_fixed_timezone
  14. from django.utils import translation
  15. from django.utils.translation import ugettext as _
  16. # Mock out datetime in some tests so they don't fail occasionally when they
  17. # run too slow. Use a fixed datetime for datetime.now(). DST change in
  18. # America/Chicago (the default time zone) happened on March 11th in 2012.
  19. now = datetime.datetime(2012, 3, 9, 22, 30)
  20. class MockDateTime(datetime.datetime):
  21. @classmethod
  22. def now(cls, tz=None):
  23. if tz is None or tz.utcoffset(now) is None:
  24. return now
  25. else:
  26. # equals now.replace(tzinfo=utc)
  27. return now.replace(tzinfo=tz) + tz.utcoffset(now)
  28. class HumanizeTests(TestCase):
  29. def humanize_tester(self, test_list, result_list, method, normalize_result_func=escape):
  30. for test_content, result in zip(test_list, result_list):
  31. t = Template('{%% load humanize %%}{{ test_content|%s }}' % method)
  32. rendered = t.render(Context(locals())).strip()
  33. self.assertEqual(rendered, normalize_result_func(result),
  34. msg="%s test failed, produced '%s', should've produced '%s'" % (method, rendered, result))
  35. def test_ordinal(self):
  36. test_list = ('1', '2', '3', '4', '11', '12',
  37. '13', '101', '102', '103', '111',
  38. 'something else', None)
  39. result_list = ('1st', '2nd', '3rd', '4th', '11th',
  40. '12th', '13th', '101st', '102nd', '103rd',
  41. '111th', 'something else', None)
  42. with translation.override('en'):
  43. self.humanize_tester(test_list, result_list, 'ordinal')
  44. def test_i18n_html_ordinal(self):
  45. """Allow html in output on i18n strings"""
  46. test_list = ('1', '2', '3', '4', '11', '12',
  47. '13', '101', '102', '103', '111',
  48. 'something else', None)
  49. result_list = ('1<sup>er</sup>', '2<sup>e</sup>', '3<sup>e</sup>', '4<sup>e</sup>',
  50. '11<sup>e</sup>', '12<sup>e</sup>', '13<sup>e</sup>', '101<sup>er</sup>',
  51. '102<sup>e</sup>', '103<sup>e</sup>', '111<sup>e</sup>', 'something else',
  52. 'None')
  53. with translation.override('fr-fr'):
  54. self.humanize_tester(test_list, result_list, 'ordinal', lambda x: x)
  55. def test_intcomma(self):
  56. test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
  57. '100', '1000', '10123', '10311', '1000000', '1234567.1234567', Decimal('1234567.1234567'),
  58. 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', '1,234,567.1234567', '1,234,567.1234567',
  61. None)
  62. with translation.override('en'):
  63. self.humanize_tester(test_list, result_list, 'intcomma')
  64. def test_l10n_intcomma(self):
  65. test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
  66. '100', '1000', '10123', '10311', '1000000', '1234567.1234567', Decimal('1234567.1234567'),
  67. None)
  68. result_list = ('100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.25',
  69. '100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567', '1,234,567.1234567',
  70. None)
  71. with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False):
  72. with translation.override('en'):
  73. self.humanize_tester(test_list, result_list, 'intcomma')
  74. def test_intcomma_without_number_grouping(self):
  75. # Regression for #17414
  76. with translation.override('ja'), self.settings(USE_L10N=True):
  77. self.humanize_tester([100], ['100'], 'intcomma')
  78. def test_intword(self):
  79. test_list = ('100', '1000000', '1200000', '1290000',
  80. '1000000000', '2000000000', '6000000000000',
  81. '1300000000000000', '3500000000000000000000',
  82. '8100000000000000000000000000000000', None)
  83. result_list = ('100', '1.0 million', '1.2 million', '1.3 million',
  84. '1.0 billion', '2.0 billion', '6.0 trillion',
  85. '1.3 quadrillion', '3.5 sextillion',
  86. '8.1 decillion', None)
  87. with translation.override('en'):
  88. self.humanize_tester(test_list, result_list, 'intword')
  89. def test_i18n_intcomma(self):
  90. test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
  91. '100', '1000', '10123', '10311', '1000000', None)
  92. result_list = ('100', '1.000', '10.123', '10.311', '1.000.000', '1.234.567,25',
  93. '100', '1.000', '10.123', '10.311', '1.000.000', None)
  94. with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
  95. with translation.override('de'):
  96. self.humanize_tester(test_list, result_list, 'intcomma')
  97. def test_i18n_intword(self):
  98. test_list = ('100', '1000000', '1200000', '1290000',
  99. '1000000000', '2000000000', '6000000000000')
  100. result_list = ('100', '1,0 Million', '1,2 Millionen', '1,3 Millionen',
  101. '1,0 Milliarde', '2,0 Milliarden', '6,0 Billionen')
  102. with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
  103. with translation.override('de'):
  104. self.humanize_tester(test_list, result_list, 'intword')
  105. def test_apnumber(self):
  106. test_list = [str(x) for x in range(1, 11)]
  107. test_list.append(None)
  108. result_list = ('one', 'two', 'three', 'four', 'five', 'six',
  109. 'seven', 'eight', 'nine', '10', None)
  110. with translation.override('en'):
  111. self.humanize_tester(test_list, result_list, 'apnumber')
  112. def test_naturalday(self):
  113. today = datetime.date.today()
  114. yesterday = today - datetime.timedelta(days=1)
  115. tomorrow = today + datetime.timedelta(days=1)
  116. someday = today - datetime.timedelta(days=10)
  117. notdate = "I'm not a date value"
  118. test_list = (today, yesterday, tomorrow, someday, notdate, None)
  119. someday_result = defaultfilters.date(someday)
  120. result_list = (_('today'), _('yesterday'), _('tomorrow'),
  121. someday_result, "I'm not a date value", None)
  122. self.humanize_tester(test_list, result_list, 'naturalday')
  123. def test_naturalday_tz(self):
  124. today = datetime.date.today()
  125. tz_one = get_fixed_timezone(-720)
  126. tz_two = get_fixed_timezone(720)
  127. # Can be today or yesterday
  128. date_one = datetime.datetime(today.year, today.month, today.day, tzinfo=tz_one)
  129. naturalday_one = humanize.naturalday(date_one)
  130. # Can be today or tomorrow
  131. date_two = datetime.datetime(today.year, today.month, today.day, tzinfo=tz_two)
  132. naturalday_two = humanize.naturalday(date_two)
  133. # As 24h of difference they will never be the same
  134. self.assertNotEqual(naturalday_one, naturalday_two)
  135. @skipIf(pytz is None, "this test requires pytz")
  136. def test_naturalday_uses_localtime(self):
  137. # Regression for #18504
  138. # This is 2012-03-08HT19:30:00-06:00 in America/Chicago
  139. dt = datetime.datetime(2012, 3, 9, 1, 30, tzinfo=utc)
  140. orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
  141. try:
  142. with override_settings(TIME_ZONE="America/Chicago", USE_TZ=True):
  143. with translation.override('en'):
  144. self.humanize_tester([dt], ['yesterday'], 'naturalday')
  145. finally:
  146. humanize.datetime = orig_humanize_datetime
  147. def test_naturaltime(self):
  148. class naive(datetime.tzinfo):
  149. def utcoffset(self, dt):
  150. return None
  151. test_list = [
  152. now,
  153. now - datetime.timedelta(seconds=1),
  154. now - datetime.timedelta(seconds=30),
  155. now - datetime.timedelta(minutes=1, seconds=30),
  156. now - datetime.timedelta(minutes=2),
  157. now - datetime.timedelta(hours=1, minutes=30, seconds=30),
  158. now - datetime.timedelta(hours=23, minutes=50, seconds=50),
  159. now - datetime.timedelta(days=1),
  160. now - datetime.timedelta(days=500),
  161. now + datetime.timedelta(seconds=1),
  162. now + datetime.timedelta(seconds=30),
  163. now + datetime.timedelta(minutes=1, seconds=30),
  164. now + datetime.timedelta(minutes=2),
  165. now + datetime.timedelta(hours=1, minutes=30, seconds=30),
  166. now + datetime.timedelta(hours=23, minutes=50, seconds=50),
  167. now + datetime.timedelta(days=1),
  168. now + datetime.timedelta(days=2, hours=6),
  169. now + datetime.timedelta(days=500),
  170. now.replace(tzinfo=naive()),
  171. now.replace(tzinfo=utc),
  172. ]
  173. result_list = [
  174. 'now',
  175. 'a second ago',
  176. '30\xa0seconds ago',
  177. 'a minute ago',
  178. '2\xa0minutes ago',
  179. 'an hour ago',
  180. '23\xa0hours ago',
  181. '1\xa0day ago',
  182. '1\xa0year, 4\xa0months ago',
  183. 'a second from now',
  184. '30\xa0seconds from now',
  185. 'a minute from now',
  186. '2\xa0minutes from now',
  187. 'an hour from now',
  188. '23\xa0hours from now',
  189. '1\xa0day from now',
  190. '2\xa0days, 6\xa0hours from now',
  191. '1\xa0year, 4\xa0months from now',
  192. 'now',
  193. 'now',
  194. ]
  195. # Because of the DST change, 2 days and 6 hours after the chosen
  196. # date in naive arithmetic is only 2 days and 5 hours after in
  197. # aware arithmetic.
  198. result_list_with_tz_support = result_list[:]
  199. assert result_list_with_tz_support[-4] == '2\xa0days, 6\xa0hours from now'
  200. result_list_with_tz_support[-4] == '2\xa0days, 5\xa0hours from now'
  201. orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
  202. try:
  203. with translation.override('en'):
  204. self.humanize_tester(test_list, result_list, 'naturaltime')
  205. with override_settings(USE_TZ=True):
  206. self.humanize_tester(
  207. test_list, result_list_with_tz_support, 'naturaltime')
  208. finally:
  209. humanize.datetime = orig_humanize_datetime