2011-11-18 13:01:06 +00:00
|
|
|
|
import datetime
|
2013-02-23 16:19:21 +00:00
|
|
|
|
from decimal import Decimal
|
2010-10-11 12:55:17 +00:00
|
|
|
|
|
2012-07-19 21:02:22 +00:00
|
|
|
|
from django.contrib.humanize.templatetags import humanize
|
2015-01-28 12:35:27 +00:00
|
|
|
|
from django.template import Context, Template, defaultfilters
|
2016-06-06 03:47:17 +00:00
|
|
|
|
from django.test import SimpleTestCase, modify_settings, override_settings
|
2012-07-19 21:02:22 +00:00
|
|
|
|
from django.utils import translation
|
2015-01-28 12:35:27 +00:00
|
|
|
|
from django.utils.html import escape
|
2022-03-23 11:15:36 +00:00
|
|
|
|
from django.utils.timezone import get_fixed_timezone
|
2017-01-26 19:58:33 +00:00
|
|
|
|
from django.utils.translation import gettext as _
|
2012-07-19 21:02:22 +00: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)
|
|
|
|
|
|
2013-09-06 17:59:44 +00:00
|
|
|
|
|
2012-07-19 21:02:22 +00:00
|
|
|
|
class MockDateTime(datetime.datetime):
|
|
|
|
|
@classmethod
|
2013-12-08 01:13:53 +00:00
|
|
|
|
def now(cls, tz=None):
|
2012-07-19 21:02:22 +00:00
|
|
|
|
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-25 16:09:30 +00:00
|
|
|
|
|
2011-04-22 12:02:55 +00:00
|
|
|
|
|
2015-02-10 13:44:52 +00:00
|
|
|
|
@modify_settings(INSTALLED_APPS={"append": "django.contrib.humanize"})
|
2016-06-06 03:47:17 +00:00
|
|
|
|
class HumanizeTests(SimpleTestCase):
|
2013-09-06 17:59:44 +00:00
|
|
|
|
def humanize_tester(
|
|
|
|
|
self, test_list, result_list, method, normalize_result_func=escape
|
|
|
|
|
):
|
2011-05-17 10:15:58 +00:00
|
|
|
|
for test_content, result in zip(test_list, result_list):
|
2019-06-09 23:07:19 +00:00
|
|
|
|
with self.subTest(test_content):
|
|
|
|
|
t = Template("{%% load humanize %%}{{ test_content|%s }}" % method)
|
|
|
|
|
rendered = t.render(Context(locals())).strip()
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
rendered,
|
|
|
|
|
normalize_result_func(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):
|
2011-09-23 16:45:40 +00:00
|
|
|
|
test_list = (
|
|
|
|
|
"1",
|
|
|
|
|
"2",
|
|
|
|
|
"3",
|
|
|
|
|
"4",
|
|
|
|
|
"11",
|
|
|
|
|
"12",
|
|
|
|
|
"13",
|
|
|
|
|
"101",
|
|
|
|
|
"102",
|
|
|
|
|
"103",
|
|
|
|
|
"111",
|
2024-05-16 08:09:09 +00:00
|
|
|
|
"-0",
|
|
|
|
|
"-1",
|
|
|
|
|
"-105",
|
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",
|
2024-05-16 08:09:09 +00:00
|
|
|
|
"0th",
|
|
|
|
|
"-1",
|
|
|
|
|
"-105",
|
2010-01-10 21:37:20 +00:00
|
|
|
|
"something else",
|
|
|
|
|
None,
|
|
|
|
|
)
|
2007-02-25 16:09:30 +00:00
|
|
|
|
|
2013-03-17 21:17:35 +00:00
|
|
|
|
with translation.override("en"):
|
|
|
|
|
self.humanize_tester(test_list, result_list, "ordinal")
|
2007-02-25 16:09:30 +00:00
|
|
|
|
|
2013-09-06 17:59:44 +00:00
|
|
|
|
def test_i18n_html_ordinal(self):
|
|
|
|
|
"""Allow html in output on i18n strings"""
|
|
|
|
|
test_list = (
|
|
|
|
|
"1",
|
|
|
|
|
"2",
|
|
|
|
|
"3",
|
|
|
|
|
"4",
|
|
|
|
|
"11",
|
|
|
|
|
"12",
|
|
|
|
|
"13",
|
|
|
|
|
"101",
|
|
|
|
|
"102",
|
|
|
|
|
"103",
|
|
|
|
|
"111",
|
|
|
|
|
"something else",
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
result_list = (
|
|
|
|
|
"1<sup>er</sup>",
|
|
|
|
|
"2<sup>e</sup>",
|
|
|
|
|
"3<sup>e</sup>",
|
|
|
|
|
"4<sup>e</sup>",
|
|
|
|
|
"11<sup>e</sup>",
|
|
|
|
|
"12<sup>e</sup>",
|
|
|
|
|
"13<sup>e</sup>",
|
|
|
|
|
"101<sup>er</sup>",
|
|
|
|
|
"102<sup>e</sup>",
|
|
|
|
|
"103<sup>e</sup>",
|
|
|
|
|
"111<sup>e</sup>",
|
|
|
|
|
"something else",
|
|
|
|
|
"None",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with translation.override("fr-fr"):
|
|
|
|
|
self.humanize_tester(test_list, result_list, "ordinal", lambda x: x)
|
|
|
|
|
|
2007-02-25 16:09:30 +00:00
|
|
|
|
def test_intcomma(self):
|
2016-04-08 02:04:45 +00:00
|
|
|
|
test_list = (
|
|
|
|
|
100,
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-100,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
1000,
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-1000,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
10123,
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-10123,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
10311,
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-10311,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
1000000,
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-1000000,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
1234567.25,
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-1234567.25,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"100",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-100",
|
2024-02-08 09:58:54 +00:00
|
|
|
|
"100.1",
|
|
|
|
|
"-100.1",
|
|
|
|
|
"100.13",
|
|
|
|
|
"-100.13",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1000",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1000",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"10123",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-10123",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"10311",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-10311",
|
2024-02-08 09:58:54 +00:00
|
|
|
|
"100000.13",
|
|
|
|
|
"-100000.13",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1000000",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1000000",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1234567.1234567",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1234567.1234567",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
Decimal("1234567.1234567"),
|
2024-01-22 13:21:13 +00:00
|
|
|
|
Decimal("-1234567.1234567"),
|
2016-04-08 02:04:45 +00:00
|
|
|
|
None,
|
2021-11-26 11:40:25 +00:00
|
|
|
|
"1234567",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1234567",
|
2021-11-26 11:40:25 +00:00
|
|
|
|
"1234567.12",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1234567.12",
|
|
|
|
|
"the quick brown fox jumped over the lazy dog",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
)
|
|
|
|
|
result_list = (
|
|
|
|
|
"100",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-100",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1,000",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,000",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"10,123",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-10,123",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"10,311",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-10,311",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1,000,000",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,000,000",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1,234,567.25",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,234,567.25",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"100",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-100",
|
2024-02-08 09:58:54 +00:00
|
|
|
|
"100.1",
|
|
|
|
|
"-100.1",
|
|
|
|
|
"100.13",
|
|
|
|
|
"-100.13",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1,000",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,000",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"10,123",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-10,123",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"10,311",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-10,311",
|
2024-02-08 09:58:54 +00:00
|
|
|
|
"100,000.13",
|
|
|
|
|
"-100,000.13",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1,000,000",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,000,000",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1,234,567.1234567",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,234,567.1234567",
|
2021-11-26 11:40:25 +00:00
|
|
|
|
"1,234,567.1234567",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,234,567.1234567",
|
2022-02-03 19:24:19 +00:00
|
|
|
|
None,
|
|
|
|
|
"1,234,567",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,234,567",
|
2021-11-26 11:40:25 +00:00
|
|
|
|
"1,234,567.12",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,234,567.12",
|
|
|
|
|
"the quick brown fox jumped over the lazy dog",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
)
|
2013-03-17 21:17:35 +00:00
|
|
|
|
with translation.override("en"):
|
|
|
|
|
self.humanize_tester(test_list, result_list, "intcomma")
|
2007-02-25 16:09:30 +00:00
|
|
|
|
|
2011-09-09 17:57:45 +00:00
|
|
|
|
def test_l10n_intcomma(self):
|
2016-04-08 02:04:45 +00:00
|
|
|
|
test_list = (
|
|
|
|
|
100,
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-100,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
1000,
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-1000,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
10123,
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-10123,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
10311,
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-10311,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
1000000,
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-1000000,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
1234567.25,
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-1234567.25,
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"100",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-100",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1000",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1000",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"10123",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-10123",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"10311",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-10311",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1000000",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1000000",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1234567.1234567",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1234567.1234567",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
Decimal("1234567.1234567"),
|
2024-01-22 13:21:13 +00:00
|
|
|
|
-Decimal("1234567.1234567"),
|
2016-04-08 02:04:45 +00:00
|
|
|
|
None,
|
2021-11-26 11:40:25 +00:00
|
|
|
|
"1234567",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1234567",
|
2021-11-26 11:40:25 +00:00
|
|
|
|
"1234567.12",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1234567.12",
|
|
|
|
|
"the quick brown fox jumped over the lazy dog",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
)
|
|
|
|
|
result_list = (
|
|
|
|
|
"100",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-100",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1,000",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,000",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"10,123",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-10,123",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"10,311",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-10,311",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1,000,000",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,000,000",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1,234,567.25",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,234,567.25",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"100",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-100",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1,000",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,000",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"10,123",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-10,123",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"10,311",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-10,311",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1,000,000",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,000,000",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"1,234,567.1234567",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,234,567.1234567",
|
2021-11-26 11:40:25 +00:00
|
|
|
|
"1,234,567.1234567",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,234,567.1234567",
|
2022-02-03 19:24:19 +00:00
|
|
|
|
None,
|
|
|
|
|
"1,234,567",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,234,567",
|
2021-11-26 11:40:25 +00:00
|
|
|
|
"1,234,567.12",
|
2024-01-22 13:21:13 +00:00
|
|
|
|
"-1,234,567.12",
|
|
|
|
|
"the quick brown fox jumped over the lazy dog",
|
2016-04-08 02:04:45 +00:00
|
|
|
|
)
|
2021-09-09 05:42:05 +00:00
|
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=False):
|
2013-09-22 11:49:46 +00:00
|
|
|
|
with translation.override("en"):
|
|
|
|
|
self.humanize_tester(test_list, result_list, "intcomma")
|
2011-09-09 17:57:45 +00:00
|
|
|
|
|
2011-12-24 11:01:57 +00:00
|
|
|
|
def test_intcomma_without_number_grouping(self):
|
|
|
|
|
# Regression for #17414
|
2021-09-09 05:42:05 +00:00
|
|
|
|
with translation.override("ja"):
|
2013-08-16 18:12:10 +00:00
|
|
|
|
self.humanize_tester([100], ["100"], "intcomma")
|
2011-12-24 11:01:57 +00:00
|
|
|
|
|
2007-02-25 16:09:30 +00:00
|
|
|
|
def test_intword(self):
|
2020-03-14 16:32:19 +00:00
|
|
|
|
# Positive integers.
|
|
|
|
|
test_list_positive = (
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"100",
|
|
|
|
|
"1000000",
|
|
|
|
|
"1200000",
|
|
|
|
|
"1290000",
|
|
|
|
|
"1000000000",
|
|
|
|
|
"2000000000",
|
|
|
|
|
"6000000000000",
|
|
|
|
|
"1300000000000000",
|
|
|
|
|
"3500000000000000000000",
|
2020-03-14 16:32:19 +00:00
|
|
|
|
"8100000000000000000000000000000000",
|
|
|
|
|
("1" + "0" * 100),
|
2018-11-11 00:09:37 +00:00
|
|
|
|
("1" + "0" * 104),
|
2016-04-08 02:04:45 +00:00
|
|
|
|
)
|
2020-03-14 16:32:19 +00:00
|
|
|
|
result_list_positive = (
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"100",
|
|
|
|
|
"1.0 million",
|
|
|
|
|
"1.2 million",
|
|
|
|
|
"1.3 million",
|
|
|
|
|
"1.0 billion",
|
|
|
|
|
"2.0 billion",
|
|
|
|
|
"6.0 trillion",
|
|
|
|
|
"1.3 quadrillion",
|
|
|
|
|
"3.5 sextillion",
|
2020-03-14 16:32:19 +00:00
|
|
|
|
"8.1 decillion",
|
|
|
|
|
"1.0 googol",
|
|
|
|
|
("1" + "0" * 104),
|
2016-04-08 02:04:45 +00:00
|
|
|
|
)
|
2020-03-14 16:32:19 +00:00
|
|
|
|
# Negative integers.
|
|
|
|
|
test_list_negative = ("-" + test for test in test_list_positive)
|
|
|
|
|
result_list_negative = ("-" + result for result in result_list_positive)
|
2013-03-17 21:17:35 +00:00
|
|
|
|
with translation.override("en"):
|
2020-03-14 16:32:19 +00:00
|
|
|
|
self.humanize_tester(
|
|
|
|
|
(*test_list_positive, *test_list_negative, None),
|
|
|
|
|
(*result_list_positive, *result_list_negative, None),
|
|
|
|
|
"intword",
|
|
|
|
|
)
|
2007-02-25 16:09:30 +00:00
|
|
|
|
|
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,
|
|
|
|
|
)
|
2021-09-09 05:42:05 +00:00
|
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
2013-09-22 11:49:46 +00:00
|
|
|
|
with translation.override("de"):
|
|
|
|
|
self.humanize_tester(test_list, result_list, "intcomma")
|
2011-05-06 13:29:58 +00:00
|
|
|
|
|
|
|
|
|
def test_i18n_intword(self):
|
2020-03-14 16:32:19 +00:00
|
|
|
|
# Positive integers.
|
|
|
|
|
test_list_positive = (
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"100",
|
|
|
|
|
"1000000",
|
|
|
|
|
"1200000",
|
|
|
|
|
"1290000",
|
|
|
|
|
"1000000000",
|
|
|
|
|
"2000000000",
|
|
|
|
|
"6000000000000",
|
|
|
|
|
)
|
2020-03-14 16:32:19 +00:00
|
|
|
|
result_list_positive = (
|
2016-04-08 02:04:45 +00:00
|
|
|
|
"100",
|
|
|
|
|
"1,0 Million",
|
|
|
|
|
"1,2 Millionen",
|
|
|
|
|
"1,3 Millionen",
|
|
|
|
|
"1,0 Milliarde",
|
|
|
|
|
"2,0 Milliarden",
|
|
|
|
|
"6,0 Billionen",
|
|
|
|
|
)
|
2020-03-14 16:32:19 +00:00
|
|
|
|
# Negative integers.
|
|
|
|
|
test_list_negative = ("-" + test for test in test_list_positive)
|
|
|
|
|
result_list_negative = ("-" + result for result in result_list_positive)
|
2021-09-09 05:42:05 +00:00
|
|
|
|
with self.settings(USE_THOUSAND_SEPARATOR=True):
|
2013-09-22 11:49:46 +00:00
|
|
|
|
with translation.override("de"):
|
2020-03-14 16:32:19 +00:00
|
|
|
|
self.humanize_tester(
|
|
|
|
|
(*test_list_positive, *test_list_negative),
|
|
|
|
|
(*result_list_positive, *result_list_negative),
|
|
|
|
|
"intword",
|
|
|
|
|
)
|
2011-05-06 13:29:58 +00:00
|
|
|
|
|
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)
|
2016-04-08 02:04:45 +00:00
|
|
|
|
result_list = (
|
|
|
|
|
"one",
|
|
|
|
|
"two",
|
|
|
|
|
"three",
|
|
|
|
|
"four",
|
|
|
|
|
"five",
|
|
|
|
|
"six",
|
|
|
|
|
"seven",
|
|
|
|
|
"eight",
|
|
|
|
|
"nine",
|
|
|
|
|
"10",
|
|
|
|
|
None,
|
|
|
|
|
)
|
2013-03-17 21:17:35 +00:00
|
|
|
|
with translation.override("en"):
|
|
|
|
|
self.humanize_tester(test_list, result_list, "apnumber")
|
2007-02-25 16:09:30 +00:00
|
|
|
|
|
2007-08-20 08:50:08 +00:00
|
|
|
|
def test_naturalday(self):
|
2011-11-18 13:01:06 +00: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-07 16:08:47 +00:00
|
|
|
|
notdate = "I'm not a date value"
|
2007-08-20 08:50:08 +00:00
|
|
|
|
|
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)
|
2012-06-07 16:08:47 +00:00
|
|
|
|
result_list = (
|
|
|
|
|
_("today"),
|
|
|
|
|
_("yesterday"),
|
|
|
|
|
_("tomorrow"),
|
|
|
|
|
someday_result,
|
|
|
|
|
"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):
|
2011-11-18 13:01:06 +00:00
|
|
|
|
today = datetime.date.today()
|
2013-09-08 08:43:33 +00:00
|
|
|
|
tz_one = get_fixed_timezone(-720)
|
|
|
|
|
tz_two = get_fixed_timezone(720)
|
2011-04-22 12:02:55 +00:00
|
|
|
|
|
|
|
|
|
# Can be today or yesterday
|
2011-11-18 13:01:06 +00:00
|
|
|
|
date_one = datetime.datetime(today.year, today.month, today.day, tzinfo=tz_one)
|
2012-07-19 21:02:22 +00:00
|
|
|
|
naturalday_one = humanize.naturalday(date_one)
|
2011-04-22 12:02:55 +00:00
|
|
|
|
# Can be today or tomorrow
|
2011-11-18 13:01:06 +00:00
|
|
|
|
date_two = datetime.datetime(today.year, today.month, today.day, tzinfo=tz_two)
|
2012-07-19 21:02:22 +00:00
|
|
|
|
naturalday_two = humanize.naturalday(date_two)
|
2011-04-22 12:02:55 +00:00
|
|
|
|
|
|
|
|
|
# As 24h of difference they will never be the same
|
|
|
|
|
self.assertNotEqual(naturalday_one, naturalday_two)
|
2011-05-17 10:16:12 +00:00
|
|
|
|
|
2012-07-19 21:02:22 +00:00
|
|
|
|
def test_naturalday_uses_localtime(self):
|
|
|
|
|
# Regression for #18504
|
2012-10-28 21:35:01 +00:00
|
|
|
|
# This is 2012-03-08HT19:30:00-06:00 in America/Chicago
|
2022-03-23 11:15:36 +00:00
|
|
|
|
dt = datetime.datetime(2012, 3, 9, 1, 30, tzinfo=datetime.timezone.utc)
|
2012-07-19 21:02:22 +00:00
|
|
|
|
|
|
|
|
|
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
|
|
|
|
|
try:
|
2013-09-22 11:49:46 +00:00
|
|
|
|
with override_settings(TIME_ZONE="America/Chicago", USE_TZ=True):
|
|
|
|
|
with translation.override("en"):
|
|
|
|
|
self.humanize_tester([dt], ["yesterday"], "naturalday")
|
2012-07-19 21:02:22 +00:00
|
|
|
|
finally:
|
|
|
|
|
humanize.datetime = orig_humanize_datetime
|
|
|
|
|
|
2011-05-17 10:16:12 +00:00
|
|
|
|
def test_naturaltime(self):
|
2011-11-18 13:01:06 +00:00
|
|
|
|
class naive(datetime.tzinfo):
|
|
|
|
|
def utcoffset(self, dt):
|
|
|
|
|
return None
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
2011-05-17 10:16:12 +00:00
|
|
|
|
test_list = [
|
2018-11-11 00:09:37 +00:00
|
|
|
|
"test",
|
2011-05-17 10:16:12 +00:00
|
|
|
|
now,
|
2018-11-11 00:09:37 +00:00
|
|
|
|
now - datetime.timedelta(microseconds=1),
|
2011-11-18 13:01:06 +00: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-19 21:02:22 +00:00
|
|
|
|
now + datetime.timedelta(days=2, hours=6),
|
2011-11-18 13:01:06 +00:00
|
|
|
|
now + datetime.timedelta(days=500),
|
|
|
|
|
now.replace(tzinfo=naive()),
|
2022-03-23 11:15:36 +00:00
|
|
|
|
now.replace(tzinfo=datetime.timezone.utc),
|
2011-05-17 10:16:12 +00:00
|
|
|
|
]
|
|
|
|
|
result_list = [
|
2018-11-11 00:09:37 +00:00
|
|
|
|
"test",
|
|
|
|
|
"now",
|
2011-05-17 10:16:12 +00:00
|
|
|
|
"now",
|
|
|
|
|
"a second ago",
|
2013-05-18 11:58:45 +00:00
|
|
|
|
"30\xa0seconds ago",
|
2011-05-17 10:16:12 +00:00
|
|
|
|
"a minute ago",
|
2013-05-18 11:58:45 +00:00
|
|
|
|
"2\xa0minutes ago",
|
2011-05-17 10:16:12 +00:00
|
|
|
|
"an hour ago",
|
2013-05-18 11:58:45 +00:00
|
|
|
|
"23\xa0hours ago",
|
|
|
|
|
"1\xa0day ago",
|
|
|
|
|
"1\xa0year, 4\xa0months ago",
|
2011-05-17 10:16:12 +00:00
|
|
|
|
"a second from now",
|
2013-05-18 11:58:45 +00:00
|
|
|
|
"30\xa0seconds from now",
|
2011-05-17 10:16:12 +00:00
|
|
|
|
"a minute from now",
|
2013-05-18 11:58:45 +00:00
|
|
|
|
"2\xa0minutes from now",
|
2011-05-17 10:16:12 +00:00
|
|
|
|
"an hour from now",
|
2013-05-18 11:58:45 +00:00
|
|
|
|
"23\xa0hours from now",
|
|
|
|
|
"1\xa0day from now",
|
|
|
|
|
"2\xa0days, 6\xa0hours from now",
|
|
|
|
|
"1\xa0year, 4\xa0months from now",
|
2011-11-18 13:01:06 +00:00
|
|
|
|
"now",
|
|
|
|
|
"now",
|
2011-05-17 10:16:12 +00:00
|
|
|
|
]
|
2012-07-19 21:02:22 +00: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[:]
|
2013-05-18 11:58:45 +00:00
|
|
|
|
assert result_list_with_tz_support[-4] == "2\xa0days, 6\xa0hours from now"
|
|
|
|
|
result_list_with_tz_support[-4] == "2\xa0days, 5\xa0hours from now"
|
2012-07-19 21:02:22 +00:00
|
|
|
|
|
|
|
|
|
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
|
2011-10-22 06:06:44 +00:00
|
|
|
|
try:
|
2013-03-17 21:17:35 +00:00
|
|
|
|
with translation.override("en"):
|
|
|
|
|
self.humanize_tester(test_list, result_list, "naturaltime")
|
|
|
|
|
with override_settings(USE_TZ=True):
|
|
|
|
|
self.humanize_tester(
|
|
|
|
|
test_list, result_list_with_tz_support, "naturaltime"
|
|
|
|
|
)
|
2011-10-22 06:06:44 +00:00
|
|
|
|
finally:
|
2011-10-26 15:17:43 +00:00
|
|
|
|
humanize.datetime = orig_humanize_datetime
|
2014-09-05 23:55:24 +00:00
|
|
|
|
|
|
|
|
|
def test_naturaltime_as_documented(self):
|
|
|
|
|
"""
|
|
|
|
|
#23340 -- Verify the documented behavior of humanize.naturaltime.
|
|
|
|
|
"""
|
|
|
|
|
time_format = "%d %b %Y %H:%M:%S"
|
|
|
|
|
documented_now = datetime.datetime.strptime("17 Feb 2007 16:30:00", time_format)
|
|
|
|
|
|
|
|
|
|
test_data = (
|
|
|
|
|
("17 Feb 2007 16:30:00", "now"),
|
|
|
|
|
("17 Feb 2007 16:29:31", "29 seconds ago"),
|
|
|
|
|
("17 Feb 2007 16:29:00", "a minute ago"),
|
|
|
|
|
("17 Feb 2007 16:25:35", "4 minutes ago"),
|
|
|
|
|
("17 Feb 2007 15:30:29", "59 minutes ago"),
|
|
|
|
|
("17 Feb 2007 15:30:01", "59 minutes ago"),
|
|
|
|
|
("17 Feb 2007 15:30:00", "an hour ago"),
|
|
|
|
|
("17 Feb 2007 13:31:29", "2 hours ago"),
|
|
|
|
|
("16 Feb 2007 13:31:29", "1 day, 2 hours ago"),
|
|
|
|
|
("16 Feb 2007 13:30:01", "1 day, 2 hours ago"),
|
|
|
|
|
("16 Feb 2007 13:30:00", "1 day, 3 hours ago"),
|
|
|
|
|
("17 Feb 2007 16:30:30", "30 seconds from now"),
|
|
|
|
|
("17 Feb 2007 16:30:29", "29 seconds from now"),
|
|
|
|
|
("17 Feb 2007 16:31:00", "a minute from now"),
|
|
|
|
|
("17 Feb 2007 16:34:35", "4 minutes from now"),
|
|
|
|
|
("17 Feb 2007 17:30:29", "an hour from now"),
|
|
|
|
|
("17 Feb 2007 18:31:29", "2 hours from now"),
|
|
|
|
|
("18 Feb 2007 16:31:29", "1 day from now"),
|
|
|
|
|
("26 Feb 2007 18:31:29", "1 week, 2 days from now"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
class DocumentedMockDateTime(datetime.datetime):
|
|
|
|
|
@classmethod
|
|
|
|
|
def now(cls, tz=None):
|
|
|
|
|
if tz is None or tz.utcoffset(documented_now) is None:
|
|
|
|
|
return documented_now
|
|
|
|
|
else:
|
|
|
|
|
return documented_now.replace(tzinfo=tz) + tz.utcoffset(now)
|
|
|
|
|
|
2014-09-06 11:59:27 +00:00
|
|
|
|
orig_humanize_datetime = humanize.datetime
|
2014-09-05 23:55:24 +00:00
|
|
|
|
humanize.datetime = DocumentedMockDateTime
|
2014-09-06 11:59:27 +00:00
|
|
|
|
try:
|
|
|
|
|
for test_time_string, expected_natural_time in test_data:
|
2019-06-09 23:07:19 +00:00
|
|
|
|
with self.subTest(test_time_string):
|
|
|
|
|
test_time = datetime.datetime.strptime(
|
|
|
|
|
test_time_string, time_format
|
|
|
|
|
)
|
|
|
|
|
natural_time = humanize.naturaltime(test_time).replace("\xa0", " ")
|
|
|
|
|
self.assertEqual(expected_natural_time, natural_time)
|
2014-09-06 11:59:27 +00:00
|
|
|
|
finally:
|
|
|
|
|
humanize.datetime = orig_humanize_datetime
|
2018-03-13 15:28:39 +00:00
|
|
|
|
|
2018-09-29 13:06:11 +00:00
|
|
|
|
def test_inflection_for_timedelta(self):
|
|
|
|
|
"""
|
|
|
|
|
Translation of '%d day'/'%d month'/… may differ depending on the context
|
|
|
|
|
of the string it is inserted in.
|
|
|
|
|
"""
|
2018-03-13 15:28:39 +00:00
|
|
|
|
test_list = [
|
2018-09-29 13:06:11 +00:00
|
|
|
|
# "%(delta)s ago" translations
|
2018-03-13 15:28:39 +00:00
|
|
|
|
now - datetime.timedelta(days=1),
|
|
|
|
|
now - datetime.timedelta(days=2),
|
|
|
|
|
now - datetime.timedelta(days=30),
|
|
|
|
|
now - datetime.timedelta(days=60),
|
|
|
|
|
now - datetime.timedelta(days=500),
|
|
|
|
|
now - datetime.timedelta(days=865),
|
2018-09-29 13:06:11 +00:00
|
|
|
|
# "%(delta)s from now" translations
|
|
|
|
|
now + datetime.timedelta(days=1),
|
|
|
|
|
now + datetime.timedelta(days=2),
|
2023-01-04 10:14:06 +00:00
|
|
|
|
now + datetime.timedelta(days=31),
|
|
|
|
|
now + datetime.timedelta(days=61),
|
2018-09-29 13:06:11 +00:00
|
|
|
|
now + datetime.timedelta(days=500),
|
|
|
|
|
now + datetime.timedelta(days=865),
|
2018-03-13 15:28:39 +00:00
|
|
|
|
]
|
|
|
|
|
result_list = [
|
2018-09-29 13:06:11 +00:00
|
|
|
|
"před 1\xa0dnem",
|
|
|
|
|
"před 2\xa0dny",
|
|
|
|
|
"před 1\xa0měsícem",
|
|
|
|
|
"před 2\xa0měsíci",
|
|
|
|
|
"před 1\xa0rokem, 4\xa0měsíci",
|
|
|
|
|
"před 2\xa0lety, 4\xa0měsíci",
|
|
|
|
|
"za 1\xa0den",
|
|
|
|
|
"za 2\xa0dny",
|
|
|
|
|
"za 1\xa0měsíc",
|
|
|
|
|
"za 2\xa0měsíce",
|
|
|
|
|
"za 1\xa0rok, 4\xa0měsíce",
|
|
|
|
|
"za 2\xa0roky, 4\xa0měsíce",
|
2018-03-13 15:28:39 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
|
|
|
|
|
try:
|
2018-09-29 13:06:11 +00:00
|
|
|
|
# Choose a language with different
|
|
|
|
|
# naturaltime-past/naturaltime-future translations.
|
2021-09-09 05:42:05 +00:00
|
|
|
|
with translation.override("cs"):
|
2018-03-13 15:28:39 +00:00
|
|
|
|
self.humanize_tester(test_list, result_list, "naturaltime")
|
|
|
|
|
finally:
|
|
|
|
|
humanize.datetime = orig_humanize_datetime
|