2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-11-18 21:01:06 +08:00
|
|
|
import datetime
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2012-10-28 16:34:05 +08:00
|
|
|
try:
|
|
|
|
import pytz
|
|
|
|
except ImportError:
|
|
|
|
pytz = None
|
|
|
|
|
|
|
|
from django.conf import settings
|
2012-07-20 05:02:22 +08:00
|
|
|
from django.contrib.humanize.templatetags import humanize
|
2011-10-25 05:30:55 +08:00
|
|
|
from django.template import Template, Context, defaultfilters
|
2011-05-06 21:29:58 +08:00
|
|
|
from django.test import TestCase
|
2012-07-20 05:02:22 +08:00
|
|
|
from django.test.utils import override_settings
|
2007-11-14 20:58:53 +08:00
|
|
|
from django.utils.html import escape
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.utils.timezone import utc
|
2012-07-20 05:02:22 +08:00
|
|
|
from django.utils import translation
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.utils import tzinfo
|
2012-10-28 16:34:05 +08:00
|
|
|
from django.utils.unittest import skipIf
|
2012-07-20 05:02:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Mock out datetime in some tests so they don't fail occasionally when they
|
|
|
|
# run too slow. Use a fixed datetime for datetime.now(). DST change in
|
|
|
|
# America/Chicago (the default time zone) happened on March 11th in 2012.
|
|
|
|
|
|
|
|
now = datetime.datetime(2012, 3, 9, 22, 30)
|
|
|
|
|
|
|
|
class MockDateTime(datetime.datetime):
|
|
|
|
@classmethod
|
|
|
|
def now(self, tz=None):
|
|
|
|
if tz is None or tz.utcoffset(now) is None:
|
|
|
|
return now
|
|
|
|
else:
|
|
|
|
# equals now.replace(tzinfo=utc)
|
|
|
|
return now.replace(tzinfo=tz) + tz.utcoffset(now)
|
2007-02-26 00:09:30 +08:00
|
|
|
|
2011-04-22 20:02:55 +08:00
|
|
|
|
2011-05-06 21:29:58 +08:00
|
|
|
class HumanizeTests(TestCase):
|
2007-02-26 00:09:30 +08: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 18:15:58 +08:00
|
|
|
for test_content, result in zip(test_list, result_list):
|
2011-10-25 05:30:55 +08:00
|
|
|
t = Template('{%% load humanize %%}{{ test_content|%s }}' % method)
|
2007-02-26 00:09:30 +08:00
|
|
|
rendered = t.render(Context(locals())).strip()
|
2011-05-17 18:15:58 +08: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 20:11:04 +08:00
|
|
|
|
2007-02-26 00:09:30 +08:00
|
|
|
def test_ordinal(self):
|
2011-09-24 00:45:40 +08:00
|
|
|
test_list = ('1', '2', '3', '4', '11', '12',
|
|
|
|
'13', '101', '102', '103', '111',
|
2010-01-11 05:37:20 +08:00
|
|
|
'something else', None)
|
2007-02-26 00:09:30 +08:00
|
|
|
result_list = ('1st', '2nd', '3rd', '4th', '11th',
|
|
|
|
'12th', '13th', '101st', '102nd', '103rd',
|
2010-01-11 05:37:20 +08:00
|
|
|
'111th', 'something else', None)
|
2007-02-26 00:09:30 +08:00
|
|
|
|
|
|
|
self.humanize_tester(test_list, result_list, 'ordinal')
|
|
|
|
|
|
|
|
def test_intcomma(self):
|
2007-03-31 19:45:24 +08:00
|
|
|
test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
|
2010-12-21 23:07:43 +08:00
|
|
|
'100', '1000', '10123', '10311', '1000000', '1234567.1234567',
|
|
|
|
None)
|
2007-04-01 13:23:08 +08:00
|
|
|
result_list = ('100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.25',
|
2010-12-21 23:07:43 +08:00
|
|
|
'100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567',
|
|
|
|
None)
|
2007-02-26 00:09:30 +08:00
|
|
|
|
|
|
|
self.humanize_tester(test_list, result_list, 'intcomma')
|
|
|
|
|
2011-09-10 01:57:45 +08:00
|
|
|
def test_l10n_intcomma(self):
|
|
|
|
test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
|
|
|
|
'100', '1000', '10123', '10311', '1000000', '1234567.1234567',
|
|
|
|
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', '1,234,567.1234567',
|
|
|
|
None)
|
|
|
|
|
|
|
|
with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False):
|
|
|
|
self.humanize_tester(test_list, result_list, 'intcomma')
|
|
|
|
|
2011-12-24 19:01:57 +08:00
|
|
|
def test_intcomma_without_number_grouping(self):
|
|
|
|
# Regression for #17414
|
|
|
|
with translation.override('ja'):
|
|
|
|
with self.settings(USE_L10N=True):
|
|
|
|
self.humanize_tester([100], ['100'], 'intcomma')
|
|
|
|
|
2007-02-26 00:09:30 +08:00
|
|
|
def test_intword(self):
|
|
|
|
test_list = ('100', '1000000', '1200000', '1290000',
|
2011-05-06 21:29:58 +08:00
|
|
|
'1000000000', '2000000000', '6000000000000',
|
2011-09-24 00:45:40 +08:00
|
|
|
'1300000000000000', '3500000000000000000000',
|
|
|
|
'8100000000000000000000000000000000', None)
|
2007-02-26 00:09:30 +08:00
|
|
|
result_list = ('100', '1.0 million', '1.2 million', '1.3 million',
|
2010-12-21 23:07:43 +08:00
|
|
|
'1.0 billion', '2.0 billion', '6.0 trillion',
|
2011-09-24 00:45:40 +08:00
|
|
|
'1.3 quadrillion', '3.5 sextillion',
|
|
|
|
'8.1 decillion', None)
|
2007-02-26 00:09:30 +08:00
|
|
|
self.humanize_tester(test_list, result_list, 'intword')
|
|
|
|
|
2011-05-06 21:29:58 +08: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',
|
2011-09-24 00:45:40 +08:00
|
|
|
'1000000000', '2000000000', '6000000000000')
|
2011-05-06 21:29:58 +08:00
|
|
|
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-26 00:09:30 +08: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 20:11:04 +08:00
|
|
|
test_list = [str(x) for x in range(1, 11)]
|
2010-12-21 23:07:43 +08:00
|
|
|
test_list.append(None)
|
2012-06-08 00:08:47 +08:00
|
|
|
result_list = ('one', 'two', 'three', 'four', 'five', 'six',
|
|
|
|
'seven', 'eight', 'nine', '10', None)
|
2007-02-26 00:09:30 +08:00
|
|
|
|
|
|
|
self.humanize_tester(test_list, result_list, 'apnumber')
|
|
|
|
|
2007-08-20 16:50:08 +08:00
|
|
|
def test_naturalday(self):
|
2011-11-18 21:01:06 +08:00
|
|
|
today = datetime.date.today()
|
|
|
|
yesterday = today - datetime.timedelta(days=1)
|
|
|
|
tomorrow = today + datetime.timedelta(days=1)
|
|
|
|
someday = today - datetime.timedelta(days=10)
|
2012-06-08 00:08:47 +08:00
|
|
|
notdate = "I'm not a date value"
|
2007-08-20 16:50:08 +08:00
|
|
|
|
2010-12-21 23:07:43 +08:00
|
|
|
test_list = (today, yesterday, tomorrow, someday, notdate, None)
|
2007-08-20 16:50:08 +08:00
|
|
|
someday_result = defaultfilters.date(someday)
|
2012-06-08 00:08:47 +08:00
|
|
|
result_list = (_('today'), _('yesterday'), _('tomorrow'),
|
|
|
|
someday_result, "I'm not a date value", None)
|
2007-08-20 16:50:08 +08:00
|
|
|
self.humanize_tester(test_list, result_list, 'naturalday')
|
|
|
|
|
2011-04-22 20:02:55 +08:00
|
|
|
def test_naturalday_tz(self):
|
2011-11-18 21:01:06 +08:00
|
|
|
today = datetime.date.today()
|
|
|
|
tz_one = tzinfo.FixedOffset(datetime.timedelta(hours=-12))
|
|
|
|
tz_two = tzinfo.FixedOffset(datetime.timedelta(hours=12))
|
2011-04-22 20:02:55 +08:00
|
|
|
|
|
|
|
# Can be today or yesterday
|
2011-11-18 21:01:06 +08:00
|
|
|
date_one = datetime.datetime(today.year, today.month, today.day, tzinfo=tz_one)
|
2012-07-20 05:02:22 +08:00
|
|
|
naturalday_one = humanize.naturalday(date_one)
|
2011-04-22 20:02:55 +08:00
|
|
|
# Can be today or tomorrow
|
2011-11-18 21:01:06 +08:00
|
|
|
date_two = datetime.datetime(today.year, today.month, today.day, tzinfo=tz_two)
|
2012-07-20 05:02:22 +08:00
|
|
|
naturalday_two = humanize.naturalday(date_two)
|
2011-04-22 20:02:55 +08:00
|
|
|
|
|
|
|
# As 24h of difference they will never be the same
|
|
|
|
self.assertNotEqual(naturalday_one, naturalday_two)
|
2011-05-17 18:16:12 +08:00
|
|
|
|
2012-10-29 05:35:01 +08:00
|
|
|
@skipIf(settings.TIME_ZONE != "America/Chicago" and pytz is None,
|
2012-10-28 16:34:05 +08:00
|
|
|
"this test requires pytz when a non-default time zone is set")
|
2012-07-20 05:02:22 +08:00
|
|
|
def test_naturalday_uses_localtime(self):
|
|
|
|
# Regression for #18504
|
2012-10-29 05:35:01 +08:00
|
|
|
# This is 2012-03-08HT19:30:00-06:00 in America/Chicago
|
2012-07-20 05:02:22 +08:00
|
|
|
dt = datetime.datetime(2012, 3, 9, 1, 30, tzinfo=utc)
|
|
|
|
|
|
|
|
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
|
|
|
|
try:
|
2012-10-28 16:34:05 +08:00
|
|
|
with override_settings(TIME_ZONE="America/Chicago", USE_TZ=True):
|
2012-07-20 05:02:22 +08:00
|
|
|
self.humanize_tester([dt], ['yesterday'], 'naturalday')
|
|
|
|
finally:
|
|
|
|
humanize.datetime = orig_humanize_datetime
|
|
|
|
|
2011-05-17 18:16:12 +08:00
|
|
|
def test_naturaltime(self):
|
2011-11-18 21:01:06 +08:00
|
|
|
class naive(datetime.tzinfo):
|
|
|
|
def utcoffset(self, dt):
|
|
|
|
return None
|
2011-05-17 18:16:12 +08:00
|
|
|
test_list = [
|
|
|
|
now,
|
2011-11-18 21:01:06 +08:00
|
|
|
now - datetime.timedelta(seconds=1),
|
|
|
|
now - datetime.timedelta(seconds=30),
|
|
|
|
now - datetime.timedelta(minutes=1, seconds=30),
|
|
|
|
now - datetime.timedelta(minutes=2),
|
|
|
|
now - datetime.timedelta(hours=1, minutes=30, seconds=30),
|
|
|
|
now - datetime.timedelta(hours=23, minutes=50, seconds=50),
|
|
|
|
now - datetime.timedelta(days=1),
|
|
|
|
now - datetime.timedelta(days=500),
|
|
|
|
now + datetime.timedelta(seconds=1),
|
|
|
|
now + datetime.timedelta(seconds=30),
|
|
|
|
now + datetime.timedelta(minutes=1, seconds=30),
|
|
|
|
now + datetime.timedelta(minutes=2),
|
|
|
|
now + datetime.timedelta(hours=1, minutes=30, seconds=30),
|
|
|
|
now + datetime.timedelta(hours=23, minutes=50, seconds=50),
|
|
|
|
now + datetime.timedelta(days=1),
|
2012-07-20 05:02:22 +08:00
|
|
|
now + datetime.timedelta(days=2, hours=6),
|
2011-11-18 21:01:06 +08:00
|
|
|
now + datetime.timedelta(days=500),
|
|
|
|
now.replace(tzinfo=naive()),
|
|
|
|
now.replace(tzinfo=utc),
|
2011-05-17 18:16:12 +08:00
|
|
|
]
|
|
|
|
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',
|
2012-07-20 05:02:22 +08:00
|
|
|
'2 days, 6 hours from now',
|
2011-05-17 18:16:12 +08:00
|
|
|
'1 year, 4 months from now',
|
2011-11-18 21:01:06 +08:00
|
|
|
'now',
|
|
|
|
'now',
|
2011-05-17 18:16:12 +08:00
|
|
|
]
|
2012-07-20 05:02:22 +08:00
|
|
|
# Because of the DST change, 2 days and 6 hours after the chosen
|
|
|
|
# date in naive arithmetic is only 2 days and 5 hours after in
|
|
|
|
# aware arithmetic.
|
|
|
|
result_list_with_tz_support = result_list[:]
|
|
|
|
assert result_list_with_tz_support[-4] == '2 days, 6 hours from now'
|
|
|
|
result_list_with_tz_support[-4] == '2 days, 5 hours from now'
|
|
|
|
|
|
|
|
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
|
2011-10-22 14:06:44 +08:00
|
|
|
try:
|
|
|
|
self.humanize_tester(test_list, result_list, 'naturaltime')
|
2012-07-20 05:02:22 +08:00
|
|
|
with override_settings(USE_TZ=True):
|
|
|
|
self.humanize_tester(test_list, result_list_with_tz_support, 'naturaltime')
|
2011-10-22 14:06:44 +08:00
|
|
|
finally:
|
2011-10-26 23:17:43 +08:00
|
|
|
humanize.datetime = orig_humanize_datetime
|