From 0282562a22945cca36329b04ff8d1f5d6588ba82 Mon Sep 17 00:00:00 2001 From: Artem Kostyuk Date: Wed, 17 Oct 2018 15:59:40 +0300 Subject: [PATCH] [COV] Improve coverage --- record_archiver/models/record_lifespan.py | 10 ++- record_archiver/tests/test_archive.py | 94 ++++++++++++++++++++--- 2 files changed, 88 insertions(+), 16 deletions(-) diff --git a/record_archiver/models/record_lifespan.py b/record_archiver/models/record_lifespan.py index b191c4671..79693c559 100644 --- a/record_archiver/models/record_lifespan.py +++ b/record_archiver/models/record_lifespan.py @@ -52,10 +52,12 @@ class RecordLifespan(models.Model): continue model = self.env[lifespan.model_id.model] state_field = model.fields_get().get('state', {}) - if not state_field: + if not state_field or state_field['type'] != 'selection': continue - allowed_states \ - = [x[0] for x in state_field.get('selection', [('')])] + allowed_states = [ + state[0] + for state in state_field.get('selection', [('')]) + ] if not all(archive_state in allowed_states for archive_state in lifespan._get_archive_states()): raise exceptions.ValidationError(_( @@ -85,7 +87,7 @@ class RecordLifespan(models.Model): self.ensure_one() if not self.archive_states: return ['done', 'cancel'] - return [s.strip() for s in self.archive_states.split(',')] + return [s.strip() for s in self.archive_states.split(',') if s.strip()] @api.multi def _archive_domain(self, expiration_date): diff --git a/record_archiver/tests/test_archive.py b/record_archiver/tests/test_archive.py index 841b0fa00..441bee8f7 100644 --- a/record_archiver/tests/test_archive.py +++ b/record_archiver/tests/test_archive.py @@ -2,6 +2,7 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from datetime import datetime, timedelta import odoo.tests.common as common +from odoo.exceptions import ValidationError class TestArchive(common.SavepointCase): @@ -17,24 +18,82 @@ class TestArchive(common.SavepointCase): tracking_disable=True, )) Partner = cls.env['res.partner'] - cls.partner1 = Partner.create( - {'name': 'test user 1'}) - cls.partner2 = Partner.create( - {'name': 'test user 2'}) - cls.partner3 = Partner.create( - {'name': 'test user 3'}) + Cron = cls.env['ir.cron'] # has both `active` and `state` fields + + cls.Lifespan = cls.env['record.lifespan'] + cls.partner_model = cls.env.ref('base.model_res_partner') + cls.cron_model = cls.env.ref('base.model_ir_cron') + + cls.partner1 = Partner.create({ + 'name': 'test user 1', + }) + cls.partner2 = Partner.create({ + 'name': 'test user 2', + }) + cls.partner3 = Partner.create({ + 'name': 'test user 3', + }) + cls.cron1 = Cron.create({ + 'active': True, + 'model_id': cls.partner_model.id, + 'name': 'Dummy cron 1', + 'state': 'code', + 'code': 'model.browse()', + }) + cls.cron2 = cls.cron1.copy({ + 'name': 'Dummy cron 2', + 'state': 'multi', + }) + cls.cron3 = cls.cron1.copy({ + 'name': 'Dummy cron 3', + 'state': 'object_create', + }) old_date = datetime.now() - timedelta(days=365) cls.env.cr.execute( 'UPDATE res_partner SET write_date = %s ' - 'WHERE id IN %s', (old_date, tuple([cls.partner2.id, - cls.partner3.id])) + 'WHERE id IN %s', (old_date, (cls.partner2.id, cls.partner3.id)) ) - cls.Lifespan = cls.env['record.lifespan'] - cls.model = cls.env.ref('base.model_res_partner') + cls.env.cr.execute( + 'UPDATE ir_cron SET write_date = %s ' + 'WHERE id IN %s', (old_date, (cls.cron2.id, cls.cron3.id))) + + def test_get_archive_states(self): + # Valid ir.cron states: code, object_create, object_write, multi + archive_states_valid_variants = [ + 'code, multi, object_create', + 'code,multi,object_create', + 'code,multi,object_create,', + ' code , multi, object_create', + ] + xpected = ['code', 'multi', 'object_create'] + guineapig = self.Lifespan.create({ + 'model_id': self.cron_model.id, + 'months': 12, + }) + for variant in archive_states_valid_variants: + guineapig.archive_states = variant + self.assertEqual(guineapig._get_archive_states(), xpected) + + def test_states_constraint_valid(self): + # Valid ir.cron states: code, object_create, object_write, multi + self.Lifespan.create({ + 'model_id': self.cron_model.id, + 'months': 12, + 'archive_states': 'code', + }) + + def test_states_constraint_invalid(self): + with self.assertRaises(ValidationError): + # Valid ir.cron states: code, object_create, object_write, multi + self.Lifespan.create({ + 'model_id': self.cron_model.id, + 'months': 12, + 'archive_states': 'none, of, these, are, valid, states', + }) def test_lifespan(self): lifespan = self.Lifespan.create({ - 'model_id': self.model.id, + 'model_id': self.partner_model.id, 'months': 3, }) lifespan.archive_records() @@ -42,9 +101,20 @@ class TestArchive(common.SavepointCase): self.assertFalse(self.partner2.active) self.assertFalse(self.partner3.active) + def test_lifespan_states(self): + lifespan = self.Lifespan.create({ + 'model_id': self.cron_model.id, + 'months': 3, + 'archive_states': 'code, multi', + }) + lifespan.archive_records() + self.assertTrue(self.cron1.active) # state: code, fresh + self.assertFalse(self.cron2.active) # state: multi, fresh + self.assertTrue(self.cron3.active) # state: object_create, outdated + def test_scheduler(self): self.Lifespan.create({ - 'model_id': self.model.id, + 'model_id': self.partner_model.id, 'months': 3, }) self.Lifespan._scheduler_archive_records()