test_date_range.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. """Test for date ranges."""
  3. from odoo.tests import common
  4. class TestDateRange(common.TransactionCase):
  5. """Test for date ranges.
  6. When grouping on date/datetime fields, group.__range is populated with
  7. formatted string dates which can be accurately converted to date objects
  8. (backend and frontend), since the display value format can vary greatly and
  9. it is not always possible to translate that display value to a real date.
  10. """
  11. @classmethod
  12. def setUpClass(cls):
  13. super().setUpClass()
  14. cls.Model = cls.env['test_read_group.on_date']
  15. def test_undefined_range(self):
  16. """Test an undefined range.
  17. Records with an unset date value should be grouped in a group whose
  18. range is False.
  19. """
  20. self.Model.create({'date': False, 'value': 1})
  21. expected = [{
  22. '__domain': [('date', '=', False)],
  23. '__range': {'date': False},
  24. 'date': False,
  25. 'date_count': 1,
  26. 'value': 1
  27. }]
  28. groups = self.Model.read_group([], fields=['date', 'value'], groupby=['date'])
  29. self.assertEqual(groups, expected)
  30. def test_with_default_granularity(self):
  31. """Test a range with the default granularity.
  32. The default granularity is 'month' and is implied when not specified.
  33. The key in group.__range should match the key in group.
  34. """
  35. self.Model.create({'date': '1916-02-11', 'value': 1})
  36. expected = [{
  37. '__domain': ['&', ('date', '>=', '1916-02-01'), ('date', '<', '1916-03-01')],
  38. '__range': {'date': {'from': '1916-02-01', 'to': '1916-03-01'}},
  39. 'date': 'February 1916',
  40. 'date_count': 1,
  41. 'value': 1
  42. }]
  43. groups = self.Model.read_group([], fields=['date', 'value'], groupby=['date'])
  44. self.assertEqual(groups, expected)
  45. def test_lazy_with_multiple_granularities(self):
  46. """Test a range with multiple granularities in lazy mode
  47. The only value stored in __range should be the granularity of the first
  48. groupby.
  49. """
  50. self.Model.create({'date': '1916-02-11', 'value': 1})
  51. expected = [{
  52. '__domain': ['&', ('date', '>=', '1916-01-01'), ('date', '<', '1916-04-01')],
  53. '__context': {'group_by': ['date:day']},
  54. '__range': {'date:quarter': {'from': '1916-01-01', 'to': '1916-04-01'}},
  55. 'date:quarter': 'Q1 1916',
  56. 'date_count': 1,
  57. 'value': 1
  58. }]
  59. groups = self.Model.read_group([], fields=['date', 'value'], groupby=['date:quarter', 'date:day'])
  60. self.assertEqual(groups, expected)
  61. expected = [{
  62. '__domain': ['&', ('date', '>=', '1916-02-11'), ('date', '<', '1916-02-12')],
  63. '__context': {'group_by': ['date:quarter']},
  64. '__range': {'date:day': {'from': '1916-02-11', 'to': '1916-02-12'}},
  65. 'date:day': '11 Feb 1916',
  66. 'date_count': 1,
  67. 'value': 1
  68. }]
  69. groups = self.Model.read_group([], fields=['date', 'value'], groupby=['date:day', 'date:quarter'])
  70. self.assertEqual(groups, expected)
  71. def test_not_lazy_with_multiple_granularities(self):
  72. """Test a range with multiple granularities (not lazy)
  73. There should be a range for each granularity.
  74. """
  75. self.Model.create({'date': '1916-02-11', 'value': 1})
  76. expected = [{
  77. '__domain': ['&',
  78. '&', ('date', '>=', '1916-01-01'), ('date', '<', '1916-04-01'),
  79. '&', ('date', '>=', '1916-02-11'), ('date', '<', '1916-02-12')
  80. ],
  81. '__range': {
  82. 'date:quarter': {'from': '1916-01-01', 'to': '1916-04-01'},
  83. 'date:day': {'from': '1916-02-11', 'to': '1916-02-12'}
  84. },
  85. 'date:quarter': 'Q1 1916',
  86. 'date:day': '11 Feb 1916',
  87. '__count': 1,
  88. 'value': 1
  89. }]
  90. groups = self.Model.read_group([], fields=['date', 'value'], groupby=['date:quarter', 'date:day'], lazy=False)
  91. self.assertEqual(groups, expected)