From f5f03e1b8913fae33db3f0fd31dcbd110fda758e Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Tue, 19 Jul 2016 17:10:16 +0200 Subject: [PATCH] [ADD] tests --- field_rrule/README.rst | 2 +- field_rrule/field_rrule.py | 5 +-- field_rrule/tests/test_field_rrule.py | 55 ++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/field_rrule/README.rst b/field_rrule/README.rst index a568c48a2..b0ab11786 100644 --- a/field_rrule/README.rst +++ b/field_rrule/README.rst @@ -25,7 +25,7 @@ If you want to pass a default, use the internal representation you'll find in th In case you work with defaults and want to dumb down the UI a bit, use ``{'no_add_rule': true}``. -Further, as this is a serialized field, a value of `not set` will be represented in the database as ``'null'`` - this is then also what you have to search for when you need records with your field unset. +Further, as this is a serialized field, a value of `not set` will be represented in the database as ``'null'`` if the value was set and unset afterwards, or a database ``null`` if the value was never set - this is then also what you have to search for when you need records with your field unset. Known issues / Roadmap ====================== diff --git a/field_rrule/field_rrule.py b/field_rrule/field_rrule.py index f52896177..1ec65d653 100644 --- a/field_rrule/field_rrule.py +++ b/field_rrule/field_rrule.py @@ -46,7 +46,7 @@ class SerializableRRuleSet(rruleset, list): """convert self to a proper rruleset for iteration. If we use defaults on our field, this will be called too with and empty recordset as parameter. In this case, we need self""" - if isinstance(default_self, models.Model): + if isinstance(default_self, models.BaseModel): return self result = rruleset() result._rrule = self._rrule @@ -55,9 +55,6 @@ class SerializableRRuleSet(rruleset, list): result._exdate = self._exdate return result - def __exit__(self): - pass - def __nonzero__(self): return bool(self._rrule) diff --git a/field_rrule/tests/test_field_rrule.py b/field_rrule/tests/test_field_rrule.py index 40d5dbc86..ebd893fe9 100644 --- a/field_rrule/tests/test_field_rrule.py +++ b/field_rrule/tests/test_field_rrule.py @@ -1,9 +1,62 @@ # -*- coding: utf-8 -*- # © 2016 Therp BV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from dateutil.rrule import MONTHLY, rrule +from openerp import SUPERUSER_ID, fields, models from openerp.tests.common import TransactionCase +from openerp.addons.field_rrule import FieldRRule +from openerp.addons.field_rrule.field_rrule import SerializableRRuleSet + + +class RRuleTest(models.TransientModel): + _name = 'test.field.rrule' + + # either use a default in object notation + rrule_with_default = FieldRRule(default=[{ + "type": "rrule", + "dtstart": '2016-01-02 00:00:00', + "count": 1, + "freq": MONTHLY, + "interval": 1, + "bymonthday": [1], + }]) + # or pass a SerializableRRuleSet. + # Rember that this class is callable, so passing it directly as default + # would actually pass an rruleset, because odoo uses the result of the + # callable. But in __call__, we check for this case, so nothing to do + rrule_with_default2 = FieldRRule(default=SerializableRRuleSet( + rrule( + dtstart=fields.Datetime.from_string('2016-01-02 00:00:00'), + interval=1, + freq=MONTHLY, + count=1, + bymonthday=[1], + ))) + # also fiddle with an empty one + rrule = FieldRRule() class TestFieldRrule(TransactionCase): def test_field_rrule(self): - pass + model = RRuleTest._build_model(self.registry, self.cr) + model._prepare_setup(self.cr, SUPERUSER_ID, False) + model._setup_base(self.cr, SUPERUSER_ID, False) + model._setup_fields(self.cr, SUPERUSER_ID) + model._auto_init(self.cr) + record_id = model.create(self.cr, SUPERUSER_ID, {'rrule': None}) + self.cr.execute( + 'select rrule, rrule_with_default, rrule_with_default2 from ' + '%s where id=%%s' % model._table, (record_id,)) + data = self.cr.fetchall()[0] + self.assertEqual(data[0], 'null') + self.assertEqual(data[1], data[2]) + record = model.browse(self.cr, SUPERUSER_ID, record_id) + self.assertFalse(record.rrule) + self.assertTrue(record.rrule_with_default) + self.assertTrue(record.rrule_with_default2) + self.assertEqual(record.rrule_with_default, record.rrule_with_default2) + self.assertEqual(record.rrule_with_default.count(), 1) + self.assertFalse(record.rrule_with_default.after( + fields.Datetime.from_string('2016-02-01 00:00:00'))) + self.assertTrue(record.rrule_with_default.after( + fields.Datetime.from_string('2016-02-01 00:00:00'), inc=True))