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