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.

62 lines
2.6 KiB

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. from dateutil.rrule import MONTHLY, rrule
  5. from openerp import SUPERUSER_ID, fields, models
  6. from openerp.tests.common import TransactionCase
  7. from openerp.addons.field_rrule import FieldRRule
  8. from openerp.addons.field_rrule.field_rrule import SerializableRRuleSet
  9. class RRuleTest(models.TransientModel):
  10. _name = 'test.field.rrule'
  11. # either use a default in object notation
  12. rrule_with_default = FieldRRule(default=[{
  13. "type": "rrule",
  14. "dtstart": '2016-01-02 00:00:00',
  15. "count": 1,
  16. "freq": MONTHLY,
  17. "interval": 1,
  18. "bymonthday": [1],
  19. }])
  20. # or pass a SerializableRRuleSet.
  21. # Rember that this class is callable, so passing it directly as default
  22. # would actually pass an rruleset, because odoo uses the result of the
  23. # callable. But in __call__, we check for this case, so nothing to do
  24. rrule_with_default2 = FieldRRule(default=SerializableRRuleSet(
  25. rrule(
  26. dtstart=fields.Datetime.from_string('2016-01-02 00:00:00'),
  27. interval=1,
  28. freq=MONTHLY,
  29. count=1,
  30. bymonthday=[1],
  31. )))
  32. # also fiddle with an empty one
  33. rrule = FieldRRule()
  34. class TestFieldRrule(TransactionCase):
  35. def test_field_rrule(self):
  36. model = RRuleTest._build_model(self.registry, self.cr)
  37. model._prepare_setup(self.cr, SUPERUSER_ID, False)
  38. model._setup_base(self.cr, SUPERUSER_ID, False)
  39. model._setup_fields(self.cr, SUPERUSER_ID)
  40. model._auto_init(self.cr)
  41. record_id = model.create(self.cr, SUPERUSER_ID, {'rrule': None})
  42. self.cr.execute(
  43. 'select rrule, rrule_with_default, rrule_with_default2 from '
  44. '%s where id=%%s' % model._table, (record_id,))
  45. data = self.cr.fetchall()[0]
  46. self.assertEqual(data[0], 'null')
  47. self.assertEqual(data[1], data[2])
  48. record = model.browse(self.cr, SUPERUSER_ID, record_id)
  49. self.assertFalse(record.rrule)
  50. self.assertTrue(record.rrule_with_default)
  51. self.assertTrue(record.rrule_with_default2)
  52. self.assertEqual(record.rrule_with_default, record.rrule_with_default2)
  53. self.assertEqual(record.rrule_with_default.count(), 1)
  54. self.assertFalse(record.rrule_with_default.after(
  55. fields.Datetime.from_string('2016-02-01 00:00:00')))
  56. self.assertTrue(record.rrule_with_default.after(
  57. fields.Datetime.from_string('2016-02-01 00:00:00'), inc=True))