1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
|
import datetime
from decimal import Decimal
from django.contrib.humanize.templatetags import humanize
from django.template import Context, Template, defaultfilters
from django.test import SimpleTestCase, modify_settings, override_settings
from django.utils import translation
from django.utils.html import escape
from django.utils.timezone import get_fixed_timezone
from django.utils.translation import gettext as _
# 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(cls, 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)
@modify_settings(INSTALLED_APPS={"append": "django.contrib.humanize"})
class HumanizeTests(SimpleTestCase):
def humanize_tester(
self, test_list, result_list, method, normalize_result_func=escape
):
for test_content, result in zip(test_list, result_list):
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),
)
def test_ordinal(self):
test_list = (
"1",
"2",
"3",
"4",
"11",
"12",
"13",
"101",
"102",
"103",
"111",
"-0",
"-1",
"-105",
"something else",
None,
)
result_list = (
"1st",
"2nd",
"3rd",
"4th",
"11th",
"12th",
"13th",
"101st",
"102nd",
"103rd",
"111th",
"0th",
"-1",
"-105",
"something else",
None,
)
with translation.override("en"):
self.humanize_tester(test_list, result_list, "ordinal")
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)
def test_intcomma(self):
test_list = (
100,
-100,
1000,
-1000,
10123,
-10123,
10311,
-10311,
1000000,
-1000000,
1234567.25,
-1234567.25,
"100",
"-100",
"100.1",
"-100.1",
"100.13",
"-100.13",
"1000",
"-1000",
"10123",
"-10123",
"10311",
"-10311",
"100000.13",
"-100000.13",
"1000000",
"-1000000",
"1234567.1234567",
"-1234567.1234567",
Decimal("1234567.1234567"),
Decimal("-1234567.1234567"),
None,
"1234567",
"-1234567",
"1234567.12",
"-1234567.12",
"the quick brown fox jumped over the lazy dog",
)
result_list = (
"100",
"-100",
"1,000",
"-1,000",
"10,123",
"-10,123",
"10,311",
"-10,311",
"1,000,000",
"-1,000,000",
"1,234,567.25",
"-1,234,567.25",
"100",
"-100",
"100.1",
"-100.1",
"100.13",
"-100.13",
"1,000",
"-1,000",
"10,123",
"-10,123",
"10,311",
"-10,311",
"100,000.13",
"-100,000.13",
"1,000,000",
"-1,000,000",
"1,234,567.1234567",
"-1,234,567.1234567",
"1,234,567.1234567",
"-1,234,567.1234567",
None,
"1,234,567",
"-1,234,567",
"1,234,567.12",
"-1,234,567.12",
"the quick brown fox jumped over the lazy dog",
)
with translation.override("en"):
self.humanize_tester(test_list, result_list, "intcomma")
def test_l10n_intcomma(self):
test_list = (
100,
-100,
1000,
-1000,
10123,
-10123,
10311,
-10311,
1000000,
-1000000,
1234567.25,
-1234567.25,
"100",
"-100",
"1000",
"-1000",
"10123",
"-10123",
"10311",
"-10311",
"1000000",
"-1000000",
"1234567.1234567",
"-1234567.1234567",
Decimal("1234567.1234567"),
-Decimal("1234567.1234567"),
None,
"1234567",
"-1234567",
"1234567.12",
"-1234567.12",
"the quick brown fox jumped over the lazy dog",
)
result_list = (
"100",
"-100",
"1,000",
"-1,000",
"10,123",
"-10,123",
"10,311",
"-10,311",
"1,000,000",
"-1,000,000",
"1,234,567.25",
"-1,234,567.25",
"100",
"-100",
"1,000",
"-1,000",
"10,123",
"-10,123",
"10,311",
"-10,311",
"1,000,000",
"-1,000,000",
"1,234,567.1234567",
"-1,234,567.1234567",
"1,234,567.1234567",
"-1,234,567.1234567",
None,
"1,234,567",
"-1,234,567",
"1,234,567.12",
"-1,234,567.12",
"the quick brown fox jumped over the lazy dog",
)
with self.settings(USE_THOUSAND_SEPARATOR=False):
with translation.override("en"):
self.humanize_tester(test_list, result_list, "intcomma")
def test_intcomma_without_number_grouping(self):
# Regression for #17414
with translation.override("ja"):
self.humanize_tester([100], ["100"], "intcomma")
def test_intword(self):
# Positive integers.
test_list_positive = (
"100",
"1000000",
"1200000",
"1290000",
"1000000000",
"2000000000",
"6000000000000",
"1300000000000000",
"3500000000000000000000",
"8100000000000000000000000000000000",
("1" + "0" * 100),
("1" + "0" * 104),
)
result_list_positive = (
"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",
"8.1 decillion",
"1.0 googol",
("1" + "0" * 104),
)
# Negative integers.
test_list_negative = ("-" + test for test in test_list_positive)
result_list_negative = ("-" + result for result in result_list_positive)
with translation.override("en"):
self.humanize_tester(
(*test_list_positive, *test_list_negative, None),
(*result_list_positive, *result_list_negative, None),
"intword",
)
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_THOUSAND_SEPARATOR=True):
with translation.override("de"):
self.humanize_tester(test_list, result_list, "intcomma")
def test_i18n_intword(self):
# Positive integers.
test_list_positive = (
"100",
"1000000",
"1200000",
"1290000",
"1000000000",
"2000000000",
"6000000000000",
)
result_list_positive = (
"100",
"1,0 Million",
"1,2 Millionen",
"1,3 Millionen",
"1,0 Milliarde",
"2,0 Milliarden",
"6,0 Billionen",
)
# Negative integers.
test_list_negative = ("-" + test for test in test_list_positive)
result_list_negative = ("-" + result for result in result_list_positive)
with self.settings(USE_THOUSAND_SEPARATOR=True):
with translation.override("de"):
self.humanize_tester(
(*test_list_positive, *test_list_negative),
(*result_list_positive, *result_list_negative),
"intword",
)
def test_apnumber(self):
test_list = [str(x) for x in range(1, 11)]
test_list.append(None)
result_list = (
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"10",
None,
)
with translation.override("en"):
self.humanize_tester(test_list, result_list, "apnumber")
def test_naturalday(self):
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
tomorrow = today + datetime.timedelta(days=1)
someday = today - datetime.timedelta(days=10)
notdate = "I'm not a date value"
test_list = (today, yesterday, tomorrow, someday, notdate, None)
someday_result = defaultfilters.date(someday)
result_list = (
_("today"),
_("yesterday"),
_("tomorrow"),
someday_result,
"I'm not a date value",
None,
)
self.humanize_tester(test_list, result_list, "naturalday")
def test_naturalday_tz(self):
today = datetime.date.today()
tz_one = get_fixed_timezone(-720)
tz_two = get_fixed_timezone(720)
# Can be today or yesterday
date_one = datetime.datetime(today.year, today.month, today.day, tzinfo=tz_one)
naturalday_one = humanize.naturalday(date_one)
# Can be today or tomorrow
date_two = datetime.datetime(today.year, today.month, today.day, tzinfo=tz_two)
naturalday_two = humanize.naturalday(date_two)
# As 24h of difference they will never be the same
self.assertNotEqual(naturalday_one, naturalday_two)
def test_naturalday_uses_localtime(self):
# Regression for #18504
# This is 2012-03-08HT19:30:00-06:00 in America/Chicago
dt = datetime.datetime(2012, 3, 9, 1, 30, tzinfo=datetime.timezone.utc)
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
try:
with override_settings(TIME_ZONE="America/Chicago", USE_TZ=True):
with translation.override("en"):
self.humanize_tester([dt], ["yesterday"], "naturalday")
finally:
humanize.datetime = orig_humanize_datetime
def test_naturaltime(self):
class naive(datetime.tzinfo):
def utcoffset(self, dt):
return None
test_list = [
"test",
now,
now - datetime.timedelta(microseconds=1),
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),
now + datetime.timedelta(days=2, hours=6),
now + datetime.timedelta(days=500),
now.replace(tzinfo=naive()),
now.replace(tzinfo=datetime.timezone.utc),
]
result_list = [
"test",
"now",
"now",
"a second ago",
"30\xa0seconds ago",
"a minute ago",
"2\xa0minutes ago",
"an hour ago",
"23\xa0hours ago",
"1\xa0day ago",
"1\xa0year, 4\xa0months ago",
"a second from now",
"30\xa0seconds from now",
"a minute from now",
"2\xa0minutes from now",
"an hour from now",
"23\xa0hours from now",
"1\xa0day from now",
"2\xa0days, 6\xa0hours from now",
"1\xa0year, 4\xa0months from now",
"now",
"now",
]
# 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\xa0days, 6\xa0hours from now"
result_list_with_tz_support[-4] == "2\xa0days, 5\xa0hours from now"
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
try:
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"
)
finally:
humanize.datetime = orig_humanize_datetime
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)
orig_humanize_datetime = humanize.datetime
humanize.datetime = DocumentedMockDateTime
try:
for test_time_string, expected_natural_time in test_data:
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)
finally:
humanize.datetime = orig_humanize_datetime
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.
"""
test_list = [
# "%(delta)s ago" translations
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),
# "%(delta)s from now" translations
now + datetime.timedelta(days=1),
now + datetime.timedelta(days=2),
now + datetime.timedelta(days=31),
now + datetime.timedelta(days=61),
now + datetime.timedelta(days=500),
now + datetime.timedelta(days=865),
]
result_list = [
"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",
]
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime
try:
# Choose a language with different
# naturaltime-past/naturaltime-future translations.
with translation.override("cs"):
self.humanize_tester(test_list, result_list, "naturaltime")
finally:
humanize.datetime = orig_humanize_datetime
|