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.

93 lines
3.1 KiB

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