2011-05-06 19:49:06 +00:00
|
|
|
from __future__ import with_statement
|
2011-05-17 10:15:58 +00:00
|
|
|
from datetime import timedelta, date, datetime, tzinfo
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2011-05-17 10:15:58 +00:00
|
|
|
from django.template import Template, Context, add_to_builtins, defaultfilters
|
2011-05-06 13:29:58 +00:00
|
|
|
from django.test import TestCase
|
2011-05-17 10:15:58 +00:00
|
|
|
from django.utils import translation, tzinfo
|
2007-08-20 08:50:08 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2007-11-14 12:58:53 +00:00
|
|
|
from django.utils.html import escape
|
2007-02-25 16:09:30 +00:00
|
|
|
|
|
|
|
add_to_builtins('django.contrib.humanize.templatetags.humanize')
|
|
|
|
|
2011-04-22 12:02:55 +00:00
|
|
|
|
2011-05-06 13:29:58 +00:00
|
|
|
class HumanizeTests(TestCase):
|
2007-02-25 16:09:30 +00:00
|
|
|
|
|
|
|
def humanize_tester(self, test_list, result_list, method):
|
|
|
|
# Using max below ensures we go through both lists
|
|
|
|
# However, if the lists are not equal length, this raises an exception
|
2011-05-17 10:15:58 +00:00
|
|
|
for test_content, result in zip(test_list, result_list):
|
2007-02-25 16:09:30 +00:00
|
|
|
t = Template('{{ test_content|%s }}' % method)
|
|
|
|
rendered = t.render(Context(locals())).strip()
|
2011-05-17 10:15:58 +00:00
|
|
|
self.assertEqual(rendered, escape(result),
|
|
|
|
msg="%s test failed, produced '%s', should've produced '%s'" % (method, rendered, result))
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
|
2007-02-25 16:09:30 +00:00
|
|
|
def test_ordinal(self):
|
|
|
|
test_list = ('1','2','3','4','11','12',
|
|
|
|
'13','101','102','103','111',
|
2010-01-10 21:37:20 +00:00
|
|
|
'something else', None)
|
2007-02-25 16:09:30 +00:00
|
|
|
result_list = ('1st', '2nd', '3rd', '4th', '11th',
|
|
|
|
'12th', '13th', '101st', '102nd', '103rd',
|
2010-01-10 21:37:20 +00:00
|
|
|
'111th', 'something else', None)
|
2007-02-25 16:09:30 +00:00
|
|
|
|
|
|
|
self.humanize_tester(test_list, result_list, 'ordinal')
|
|
|
|
|
|
|
|
def test_intcomma(self):
|
2007-03-31 11:45:24 +00:00
|
|
|
test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
|
2010-12-21 15:07:43 +00:00
|
|
|
'100', '1000', '10123', '10311', '1000000', '1234567.1234567',
|
|
|
|
None)
|
2007-04-01 05:23:08 +00:00
|
|
|
result_list = ('100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.25',
|
2010-12-21 15:07:43 +00:00
|
|
|
'100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567',
|
|
|
|
None)
|
2007-02-25 16:09:30 +00:00
|
|
|
|
|
|
|
self.humanize_tester(test_list, result_list, 'intcomma')
|
|
|
|
|
|
|
|
def test_intword(self):
|
|
|
|
test_list = ('100', '1000000', '1200000', '1290000',
|
2011-05-06 13:29:58 +00:00
|
|
|
'1000000000', '2000000000', '6000000000000',
|
2010-12-21 15:07:43 +00:00
|
|
|
None)
|
2007-02-25 16:09:30 +00:00
|
|
|
result_list = ('100', '1.0 million', '1.2 million', '1.3 million',
|
2010-12-21 15:07:43 +00:00
|
|
|
'1.0 billion', '2.0 billion', '6.0 trillion',
|
|
|
|
None)
|
2007-02-25 16:09:30 +00:00
|
|
|
self.humanize_tester(test_list, result_list, 'intword')
|
|
|
|
|
2011-05-06 13:29:58 +00:00
|
|
|
def test_i18n_intcomma(self):
|
|
|
|
test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
|
|
|
|
'100', '1000', '10123', '10311', '1000000', None)
|
|
|
|
result_list = ('100', '1.000', '10.123', '10.311', '1.000.000', '1.234.567,25',
|
|
|
|
'100', '1.000', '10.123', '10.311', '1.000.000', None)
|
|
|
|
with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
|
|
|
|
with translation.override('de'):
|
|
|
|
self.humanize_tester(test_list, result_list, 'intcomma')
|
|
|
|
|
|
|
|
def test_i18n_intword(self):
|
|
|
|
test_list = ('100', '1000000', '1200000', '1290000',
|
|
|
|
'1000000000','2000000000','6000000000000')
|
|
|
|
result_list = ('100', '1,0 Million', '1,2 Millionen', '1,3 Millionen',
|
|
|
|
'1,0 Milliarde', '2,0 Milliarden', '6,0 Billionen')
|
|
|
|
with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True):
|
|
|
|
with translation.override('de'):
|
|
|
|
self.humanize_tester(test_list, result_list, 'intword')
|
|
|
|
|
2007-02-25 16:09:30 +00:00
|
|
|
def test_apnumber(self):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
test_list = [str(x) for x in range(1, 11)]
|
2010-12-21 15:07:43 +00:00
|
|
|
test_list.append(None)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
result_list = (u'one', u'two', u'three', u'four', u'five', u'six',
|
2010-12-21 15:07:43 +00:00
|
|
|
u'seven', u'eight', u'nine', u'10', None)
|
2007-02-25 16:09:30 +00:00
|
|
|
|
|
|
|
self.humanize_tester(test_list, result_list, 'apnumber')
|
|
|
|
|
2007-08-20 08:50:08 +00:00
|
|
|
def test_naturalday(self):
|
|
|
|
today = date.today()
|
|
|
|
yesterday = today - timedelta(days=1)
|
|
|
|
tomorrow = today + timedelta(days=1)
|
|
|
|
someday = today - timedelta(days=10)
|
|
|
|
notdate = u"I'm not a date value"
|
|
|
|
|
2010-12-21 15:07:43 +00:00
|
|
|
test_list = (today, yesterday, tomorrow, someday, notdate, None)
|
2007-08-20 08:50:08 +00:00
|
|
|
someday_result = defaultfilters.date(someday)
|
|
|
|
result_list = (_(u'today'), _(u'yesterday'), _(u'tomorrow'),
|
2010-12-21 15:07:43 +00:00
|
|
|
someday_result, u"I'm not a date value", None)
|
2007-08-20 08:50:08 +00:00
|
|
|
self.humanize_tester(test_list, result_list, 'naturalday')
|
|
|
|
|
2011-04-22 12:02:55 +00:00
|
|
|
def test_naturalday_tz(self):
|
|
|
|
from django.contrib.humanize.templatetags.humanize import naturalday
|
2011-04-22 12:02:47 +00:00
|
|
|
|
2011-04-22 12:02:55 +00:00
|
|
|
today = date.today()
|
2011-05-17 10:15:58 +00:00
|
|
|
tz_one = tzinfo.FixedOffset(timedelta(hours=-12))
|
|
|
|
tz_two = tzinfo.FixedOffset(timedelta(hours=12))
|
2011-04-22 12:02:55 +00:00
|
|
|
|
|
|
|
# Can be today or yesterday
|
|
|
|
date_one = datetime(today.year, today.month, today.day, tzinfo=tz_one)
|
|
|
|
naturalday_one = naturalday(date_one)
|
|
|
|
# Can be today or tomorrow
|
|
|
|
date_two = datetime(today.year, today.month, today.day, tzinfo=tz_two)
|
|
|
|
naturalday_two = naturalday(date_two)
|
|
|
|
|
|
|
|
# As 24h of difference they will never be the same
|
|
|
|
self.assertNotEqual(naturalday_one, naturalday_two)
|
2011-05-17 10:16:12 +00:00
|
|
|
|
|
|
|
def test_naturaltime(self):
|
|
|
|
now = datetime.now()
|
|
|
|
test_list = [
|
|
|
|
now,
|
|
|
|
now - timedelta(seconds=1),
|
|
|
|
now - timedelta(seconds=30),
|
|
|
|
now - timedelta(minutes=1, seconds=30),
|
|
|
|
now - timedelta(minutes=2),
|
|
|
|
now - timedelta(hours=1, minutes=30, seconds=30),
|
|
|
|
now - timedelta(hours=23, minutes=50, seconds=50),
|
|
|
|
now - timedelta(days=1),
|
|
|
|
now - timedelta(days=500),
|
|
|
|
now + timedelta(seconds=1),
|
|
|
|
now + timedelta(seconds=30),
|
|
|
|
now + timedelta(minutes=1, seconds=30),
|
|
|
|
now + timedelta(minutes=2),
|
|
|
|
now + timedelta(hours=1, minutes=30, seconds=30),
|
|
|
|
now + timedelta(hours=23, minutes=50, seconds=50),
|
|
|
|
now + timedelta(days=1),
|
|
|
|
now + timedelta(days=500),
|
|
|
|
]
|
|
|
|
result_list = [
|
|
|
|
'now',
|
|
|
|
'a second ago',
|
|
|
|
'30 seconds ago',
|
|
|
|
'a minute ago',
|
|
|
|
'2 minutes ago',
|
|
|
|
'an hour ago',
|
|
|
|
'23 hours ago',
|
|
|
|
'1 day ago',
|
|
|
|
'1 year, 4 months ago',
|
|
|
|
'a second from now',
|
|
|
|
'30 seconds from now',
|
|
|
|
'a minute from now',
|
|
|
|
'2 minutes from now',
|
|
|
|
'an hour from now',
|
|
|
|
'23 hours from now',
|
|
|
|
'1 day from now',
|
|
|
|
'1 year, 4 months from now',
|
|
|
|
]
|
|
|
|
self.humanize_tester(test_list, result_list, 'naturaltime')
|