PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 10ms RepoModel.GetById 31ms app.codeStats 0ms

/tests/regressiontests/humanize/tests.py

https://code.google.com/p/mango-py/
Python | 77 lines | 59 code | 16 blank | 2 comment | 5 complexity | 12efd5a29db2bd9ff394b8d94f1cf1dc MD5 | raw file
 1from datetime import timedelta, date
 2
 3from django.template import Template, Context, add_to_builtins
 4from django.utils import unittest
 5from django.utils.dateformat import DateFormat
 6from django.utils.translation import ugettext as _
 7from django.utils.html import escape
 8
 9add_to_builtins('django.contrib.humanize.templatetags.humanize')
10
11class HumanizeTests(unittest.TestCase):
12
13    def humanize_tester(self, test_list, result_list, method):
14        # Using max below ensures we go through both lists
15        # However, if the lists are not equal length, this raises an exception
16        for index in xrange(max(len(test_list), len(result_list))):
17            test_content = test_list[index]
18            t = Template('{{ test_content|%s }}' % method)
19            rendered = t.render(Context(locals())).strip()
20            self.assertEqual(rendered, escape(result_list[index]),
21                             msg="%s test failed, produced %s, should've produced %s" % (method, rendered, result_list[index]))
22
23    def test_ordinal(self):
24        test_list = ('1','2','3','4','11','12',
25                     '13','101','102','103','111',
26                     'something else', None)
27        result_list = ('1st', '2nd', '3rd', '4th', '11th',
28                       '12th', '13th', '101st', '102nd', '103rd',
29                       '111th', 'something else', None)
30
31        self.humanize_tester(test_list, result_list, 'ordinal')
32
33    def test_intcomma(self):
34        test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
35                     '100', '1000', '10123', '10311', '1000000', '1234567.1234567',
36                     None)
37        result_list = ('100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.25',
38                       '100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567',
39                     None)
40
41        self.humanize_tester(test_list, result_list, 'intcomma')
42
43    def test_intword(self):
44        test_list = ('100', '1000000', '1200000', '1290000',
45                     '1000000000','2000000000','6000000000000',
46                     None)
47        result_list = ('100', '1.0 million', '1.2 million', '1.3 million',
48                       '1.0 billion', '2.0 billion', '6.0 trillion',
49                       None)
50
51        self.humanize_tester(test_list, result_list, 'intword')
52
53    def test_apnumber(self):
54        test_list = [str(x) for x in range(1, 11)]
55        test_list.append(None)
56        result_list = (u'one', u'two', u'three', u'four', u'five', u'six',
57                       u'seven', u'eight', u'nine', u'10', None)
58
59        self.humanize_tester(test_list, result_list, 'apnumber')
60
61    def test_naturalday(self):
62        from django.template import defaultfilters
63        today = date.today()
64        yesterday = today - timedelta(days=1)
65        tomorrow = today + timedelta(days=1)
66        someday = today - timedelta(days=10)
67        notdate = u"I'm not a date value"
68
69        test_list = (today, yesterday, tomorrow, someday, notdate, None)
70        someday_result = defaultfilters.date(someday)
71        result_list = (_(u'today'), _(u'yesterday'), _(u'tomorrow'),
72                       someday_result, u"I'm not a date value", None)
73        self.humanize_tester(test_list, result_list, 'naturalday')
74
75if __name__ == '__main__':
76    unittest.main()
77