summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2019-06-09 12:48:20 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-06-11 20:34:59 +0200
commit9e38ed0536c7dc598a6c2c1bb774d0a8db3cdddc (patch)
tree735fc06110b76c86b0090a8dd53e2a6a1fcfae4e /tests
parent175656e166712db019a4704c4031510b9fd6b00a (diff)
Fixed #27486 -- Fixed Python 3.7 DeprecationWarning in intword and filesizeformat filters.
intword and filesizeformat passed floats to ngettext() which is deprecated in Python 3.7. The rationale for this warning is documented in BPO-28692: https://bugs.python.org/issue28692. For filesizeformat, the filesize value is expected to be an int -- it fills %d string formatting placeholders. It was likely coerced to a float to ensure floating point division on Python 2. Python 3 always does floating point division, so coerce to an int instead of a float to fix the warning. For intword, the number may contain a decimal component. In English, a decimal component makes the noun plural. A helper function, round_away_from_one(), was added to convert the float to an integer that is appropriate for ngettext().
Diffstat (limited to 'tests')
-rw-r--r--tests/i18n/tests.py34
1 files changed, 31 insertions, 3 deletions
diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py
index 4302e01fda..8bb284e0c3 100644
--- a/tests/i18n/tests.py
+++ b/tests/i18n/tests.py
@@ -34,9 +34,9 @@ from django.utils.translation import (
LANGUAGE_SESSION_KEY, activate, check_for_language, deactivate,
get_language, get_language_bidi, get_language_from_request,
get_language_info, gettext, gettext_lazy, ngettext, ngettext_lazy,
- npgettext, npgettext_lazy, pgettext, to_language, to_locale, trans_null,
- trans_real, ugettext, ugettext_lazy, ugettext_noop, ungettext,
- ungettext_lazy,
+ npgettext, npgettext_lazy, pgettext, round_away_from_one, to_language,
+ to_locale, trans_null, trans_real, ugettext, ugettext_lazy, ugettext_noop,
+ ungettext, ungettext_lazy,
)
from django.utils.translation.reloader import (
translation_file_changed, watch_for_translation_changes,
@@ -1883,3 +1883,31 @@ class TranslationFileChangedTests(SimpleTestCase):
self.assertEqual(trans_real._translations, {})
self.assertIsNone(trans_real._default)
self.assertIsInstance(trans_real._active, _thread._local)
+
+
+class UtilsTests(SimpleTestCase):
+ def test_round_away_from_one(self):
+ tests = [
+ (0, 0),
+ (0., 0),
+ (0.25, 0),
+ (0.5, 0),
+ (0.75, 0),
+ (1, 1),
+ (1., 1),
+ (1.25, 2),
+ (1.5, 2),
+ (1.75, 2),
+ (-0., 0),
+ (-0.25, -1),
+ (-0.5, -1),
+ (-0.75, -1),
+ (-1, -1),
+ (-1., -1),
+ (-1.25, -2),
+ (-1.5, -2),
+ (-1.75, -2),
+ ]
+ for value, expected in tests:
+ with self.subTest(value=value):
+ self.assertEqual(round_away_from_one(value), expected)