You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.1 KiB

  1. # Copyright 2015, 2017 Jairo Llopis <jairo.llopis@tecnativa.com>
  2. # Copyright 2016 Tecnativa, S.L. - Vicent Cubells
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import datetime
  5. from random import random
  6. from odoo.tests.common import TransactionCase
  7. from odoo.tools import (DEFAULT_SERVER_DATE_FORMAT,
  8. DEFAULT_SERVER_TIME_FORMAT,
  9. DEFAULT_SERVER_DATETIME_FORMAT)
  10. from ..models.res_lang import MODE_DATE, MODE_TIME, MODE_DATETIME
  11. class FormatterCase(TransactionCase):
  12. def setUp(self):
  13. super().setUp()
  14. self.rl = self.env["res.lang"]
  15. self.bm = self.rl.best_match()
  16. self.dt = datetime.datetime.now()
  17. self.d_fmt = self.bm.date_format or DEFAULT_SERVER_DATE_FORMAT
  18. self.t_fmt = self.bm.time_format or DEFAULT_SERVER_TIME_FORMAT
  19. self.kwargs = dict()
  20. def tearDown(self):
  21. # This should be returned
  22. self.expected = self.dt.strftime(self.format)
  23. # Pass a datetime object
  24. self.assertEqual(
  25. self.expected,
  26. self.rl.datetime_formatter(
  27. self.dt,
  28. **self.kwargs))
  29. # When the date comes as a string
  30. if isinstance(self.dt, datetime.datetime):
  31. self.dt_str = self.dt.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
  32. elif isinstance(self.dt, datetime.date):
  33. self.dt_str = self.dt.strftime(DEFAULT_SERVER_DATE_FORMAT)
  34. elif isinstance(self.dt, datetime.time):
  35. self.dt_str = self.dt.strftime(DEFAULT_SERVER_TIME_FORMAT)
  36. # Pass a string
  37. self.assertEqual(
  38. self.expected,
  39. self.rl.datetime_formatter(
  40. self.dt_str,
  41. **self.kwargs))
  42. # Pass a unicode
  43. self.assertEqual(
  44. self.expected,
  45. self.rl.datetime_formatter(
  46. str(self.dt_str),
  47. **self.kwargs))
  48. super().tearDown()
  49. def test_datetime(self):
  50. """Format a datetime."""
  51. self.format = "%s %s" % (self.d_fmt, self.t_fmt)
  52. self.kwargs = {"template": MODE_DATETIME}
  53. def test_date(self):
  54. """Format a date."""
  55. self.format = self.d_fmt
  56. self.kwargs = {"template": MODE_DATE}
  57. self.dt = self.dt.date()
  58. def test_time(self):
  59. """Format times, including float ones."""
  60. self.format = self.t_fmt
  61. self.kwargs = {"template": MODE_TIME}
  62. self.dt = self.dt.time()
  63. # Test float times
  64. for n in range(50):
  65. n = n + random()
  66. # Patch values with >= 24 hours
  67. fmt = self.format.replace("%H", "%02d" % n)
  68. time = (datetime.datetime.min +
  69. datetime.timedelta(hours=n)).time()
  70. self.assertEqual(
  71. time.strftime(fmt),
  72. self.rl.datetime_formatter(n, **self.kwargs))
  73. def test_custom_separator(self):
  74. """Format a datetime with a custom separator."""
  75. sep = "T"
  76. self.format = "%s%s%s" % (self.d_fmt, sep, self.t_fmt)
  77. self.kwargs = {"template": MODE_DATETIME, "separator": sep}