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.

128 lines
4.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2016 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import datetime
  5. from psycopg2.extensions import AsIs
  6. from dateutil.rrule import MONTHLY, rrule
  7. from dateutil.tz import gettz
  8. from openerp import SUPERUSER_ID, fields, models
  9. from openerp.tests.common import TransactionCase
  10. from ..field_rrule import FieldRRule, SerializableRRuleSet
  11. class RRuleTest(models.TransientModel):
  12. _name = 'test.field.rrule'
  13. # either use a default in object notation
  14. rrule_with_default = FieldRRule(default=[{
  15. "type": "rrule",
  16. "dtstart": '2016-01-02 00:00:00',
  17. "count": 1,
  18. "freq": MONTHLY,
  19. "interval": 1,
  20. "bymonthday": [1],
  21. }])
  22. # or pass a SerializableRRuleSet.
  23. # Rember that this class is callable, so passing it directly as default
  24. # would actually pass an rruleset, because odoo uses the result of the
  25. # callable. But in __call__, we check for this case, so nothing to do
  26. rrule_with_default2 = FieldRRule(default=SerializableRRuleSet(
  27. rrule(
  28. dtstart=fields.Datetime.from_string('2016-01-02 00:00:00'),
  29. interval=1,
  30. freq=MONTHLY,
  31. count=1,
  32. bymonthday=[1],
  33. )))
  34. # also fiddle with an empty one
  35. rrule = FieldRRule()
  36. # and the timezone aware version
  37. rrule_with_tz = FieldRRule(stable_times=True)
  38. class TestFieldRrule(TransactionCase):
  39. def test_field_rrule(self):
  40. model = RRuleTest._build_model(self.registry, self.cr)
  41. model._prepare_setup(self.cr, SUPERUSER_ID, False)
  42. model._setup_base(self.cr, SUPERUSER_ID, False)
  43. model._setup_fields(self.cr, SUPERUSER_ID)
  44. model._auto_init(self.cr)
  45. record_id = model.create(self.cr, SUPERUSER_ID, {'rrule': None})
  46. self.cr.execute(
  47. 'select rrule, rrule_with_default, rrule_with_default2 from '
  48. '%s where id=%s', (AsIs(model._table), record_id))
  49. data = self.cr.fetchall()[0]
  50. self.assertEqual(data[0], 'null')
  51. self.assertEqual(data[1], data[2])
  52. record = model.browse(self.cr, SUPERUSER_ID, record_id)
  53. self.assertFalse(record.rrule)
  54. self.assertTrue(record.rrule_with_default)
  55. self.assertTrue(record.rrule_with_default2)
  56. self.assertEqual(record.rrule_with_default, record.rrule_with_default2)
  57. self.assertEqual(record.rrule_with_default.count(), 1)
  58. self.assertFalse(record.rrule_with_default.after(
  59. fields.Datetime.from_string('2016-02-01 00:00:00')))
  60. self.assertTrue(record.rrule_with_default.after(
  61. fields.Datetime.from_string('2016-02-01 00:00:00'), inc=True))
  62. record.env.user.write({'tz': 'Europe/Amsterdam'})
  63. record.rrule_with_tz = SerializableRRuleSet(
  64. rrule(
  65. dtstart=fields.Datetime.from_string('2017-02-02 23:00:00'),
  66. interval=1,
  67. freq=MONTHLY,
  68. count=2,
  69. bymonthday=[1],
  70. )
  71. )
  72. # this rruleset should receive Amsterdam as timezone, and should yield
  73. # different naive utc times depending on dst (NL switches in March)
  74. self.assertTrue(record.rrule_with_tz.tz)
  75. self.assertEqual(
  76. list(record.rrule_with_tz()),
  77. [
  78. datetime.datetime(2017, 2, 28, 23, 0),
  79. datetime.datetime(2017, 3, 31, 22, 0),
  80. ]
  81. )
  82. # doing the same with a timezone-aware datetime should work too
  83. record.rrule_with_tz = SerializableRRuleSet(
  84. rrule(
  85. dtstart=fields.Datetime.from_string('2017-02-03 00:00:00')
  86. .replace(tzinfo=gettz('Europe/Amsterdam')),
  87. interval=1,
  88. freq=MONTHLY,
  89. count=2,
  90. bymonthday=[1],
  91. )
  92. )
  93. self.assertTrue(record.rrule_with_tz.tz)
  94. self.assertEqual(
  95. list(record.rrule_with_tz()),
  96. [
  97. datetime.datetime(2017, 2, 28, 23, 0),
  98. datetime.datetime(2017, 3, 31, 22, 0),
  99. ]
  100. )
  101. # and the same again with the json representation
  102. record.rrule_with_tz = [
  103. {
  104. 'type': 'rrule',
  105. 'dtstart': '2017-02-02 23:00:00',
  106. 'interval': 1,
  107. 'freq': MONTHLY,
  108. 'count': 2,
  109. 'bymonthday': [1],
  110. },
  111. {
  112. 'type': 'tz',
  113. 'tz': 'Europe/Amsterdam',
  114. }
  115. ]
  116. self.assertTrue(record.rrule_with_tz.tz)
  117. self.assertEqual(
  118. list(record.rrule_with_tz()),
  119. [
  120. datetime.datetime(2017, 2, 28, 23, 0),
  121. datetime.datetime(2017, 3, 31, 22, 0),
  122. ]
  123. )