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