Browse Source

[COV] Improve coverage

pull/1381/head
Artem Kostyuk 6 years ago
parent
commit
0282562a22
  1. 10
      record_archiver/models/record_lifespan.py
  2. 94
      record_archiver/tests/test_archive.py

10
record_archiver/models/record_lifespan.py

@ -52,10 +52,12 @@ class RecordLifespan(models.Model):
continue continue
model = self.env[lifespan.model_id.model] model = self.env[lifespan.model_id.model]
state_field = model.fields_get().get('state', {}) state_field = model.fields_get().get('state', {})
if not state_field:
if not state_field or state_field['type'] != 'selection':
continue 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 if not all(archive_state in allowed_states
for archive_state in lifespan._get_archive_states()): for archive_state in lifespan._get_archive_states()):
raise exceptions.ValidationError(_( raise exceptions.ValidationError(_(
@ -85,7 +87,7 @@ class RecordLifespan(models.Model):
self.ensure_one() self.ensure_one()
if not self.archive_states: if not self.archive_states:
return ['done', 'cancel'] 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 @api.multi
def _archive_domain(self, expiration_date): def _archive_domain(self, expiration_date):

94
record_archiver/tests/test_archive.py

@ -2,6 +2,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from datetime import datetime, timedelta from datetime import datetime, timedelta
import odoo.tests.common as common import odoo.tests.common as common
from odoo.exceptions import ValidationError
class TestArchive(common.SavepointCase): class TestArchive(common.SavepointCase):
@ -17,24 +18,82 @@ class TestArchive(common.SavepointCase):
tracking_disable=True, tracking_disable=True,
)) ))
Partner = cls.env['res.partner'] 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) old_date = datetime.now() - timedelta(days=365)
cls.env.cr.execute( cls.env.cr.execute(
'UPDATE res_partner SET write_date = %s ' '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): def test_lifespan(self):
lifespan = self.Lifespan.create({ lifespan = self.Lifespan.create({
'model_id': self.model.id,
'model_id': self.partner_model.id,
'months': 3, 'months': 3,
}) })
lifespan.archive_records() lifespan.archive_records()
@ -42,9 +101,20 @@ class TestArchive(common.SavepointCase):
self.assertFalse(self.partner2.active) self.assertFalse(self.partner2.active)
self.assertFalse(self.partner3.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): def test_scheduler(self):
self.Lifespan.create({ self.Lifespan.create({
'model_id': self.model.id,
'model_id': self.partner_model.id,
'months': 3, 'months': 3,
}) })
self.Lifespan._scheduler_archive_records() self.Lifespan._scheduler_archive_records()

Loading…
Cancel
Save