tests.py 20 KB

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