PageRenderTime 134ms CodeModel.GetById 29ms RepoModel.GetById 7ms app.codeStats 0ms

/tests/regressiontests/humanize/tests.py

https://code.google.com/p/mango-py/
Python | 77 lines | 59 code | 16 blank | 2 comment | 3 complexity | 12efd5a29db2bd9ff394b8d94f1cf1dc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from datetime import timedelta, date
  2. from django.template import Template, Context, add_to_builtins
  3. from django.utils import unittest
  4. from django.utils.dateformat import DateFormat
  5. from django.utils.translation import ugettext as _
  6. from django.utils.html import escape
  7. add_to_builtins('django.contrib.humanize.templatetags.humanize')
  8. class HumanizeTests(unittest.TestCase):
  9. def humanize_tester(self, test_list, result_list, method):
  10. # Using max below ensures we go through both lists
  11. # However, if the lists are not equal length, this raises an exception
  12. for index in xrange(max(len(test_list), len(result_list))):
  13. test_content = test_list[index]
  14. t = Template('{{ test_content|%s }}' % method)
  15. rendered = t.render(Context(locals())).strip()
  16. self.assertEqual(rendered, escape(result_list[index]),
  17. msg="%s test failed, produced %s, should've produced %s" % (method, rendered, result_list[index]))
  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_apnumber(self):
  43. test_list = [str(x) for x in range(1, 11)]
  44. test_list.append(None)
  45. result_list = (u'one', u'two', u'three', u'four', u'five', u'six',
  46. u'seven', u'eight', u'nine', u'10', None)
  47. self.humanize_tester(test_list, result_list, 'apnumber')
  48. def test_naturalday(self):
  49. from django.template import defaultfilters
  50. today = date.today()
  51. yesterday = today - timedelta(days=1)
  52. tomorrow = today + timedelta(days=1)
  53. someday = today - timedelta(days=10)
  54. notdate = u"I'm not a date value"
  55. test_list = (today, yesterday, tomorrow, someday, notdate, None)
  56. someday_result = defaultfilters.date(someday)
  57. result_list = (_(u'today'), _(u'yesterday'), _(u'tomorrow'),
  58. someday_result, u"I'm not a date value", None)
  59. self.humanize_tester(test_list, result_list, 'naturalday')
  60. if __name__ == '__main__':
  61. unittest.main()