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.

63 lines
2.6 KiB

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