Skip to content
Permalink
Browse files

[IMP][TESTS] test_new_api: pre/post computes fields tests

  • Loading branch information
Feyensv committed Jan 14, 2020
1 parent 2ba1827 commit 62d139263791af7bc2673236cf55ceb634606100
@@ -800,3 +800,65 @@ class ModelParentM2o(models.Model):

name = fields.Char('Name')
child_ids = fields.One2many('test_new_api.model_child_m2o', 'parent_id', string="Children")


class ModelAdvancedComputes(models.Model):
_name = 'test_new_api.model_advanced_computes'
_description = 'model with advanced computes'

name1 = fields.Char('First Name')
name2 = fields.Char('Last Name')
title = fields.Char('Job Function')

upper_name_1 = fields.Char(compute="_compute_uppers", store=True)
upper_name_2 = fields.Char(compute="_compute_uppers", store=True)

create_month = fields.Integer(compute="_compute_create_month", store=True)

full_upper_name = fields.Char(compute="_compute_full_upper", pre_compute=False, store=True)
full_card_content = fields.Text(compute="_compute_full_card_content", store=True)

duplicates = fields.Many2many(
comodel_name='test_new_api.model_advanced_computes',
relation='test_new_api_advanced_computes_rel', column1="rec1", column2="rec2",
compute="_compute_duplicates", pre_compute=False, store=True)

@api.depends('name1', 'name2')
def _compute_uppers(self):
for rec in self:
if rec.id or not rec.env.context.get('creation', False):
raise ValidationError("Should be computed before record creation")
rec.upper_name_1 = rec.name1.title()
rec.upper_name_2 = rec.name2.title()

@api.depends('create_date')
def _compute_create_month(self):
for rec in self:
if not rec.id or not rec.env.context.get('creation', False):
raise ValidationError("Should be computed after record creation")
rec.create_month = rec.create_date and rec.create_date.month

@api.depends('upper_name_1', 'upper_name_2')
def _compute_full_upper(self):
for rec in self:
if not rec.id or not rec.env.context.get('creation', False):
raise ValidationError("Should be computed after creation because specified as pre_compute=False")
rec.full_upper_name = rec.upper_name_1 + " " + rec.upper_name_2

@api.depends('title', 'full_upper_name')
def _compute_full_card_content(self):
for rec in self:
if not rec.id or not rec.env.context.get('creation', False):
raise ValidationError("Shouldn't be computed before record creation.")
rec.full_card_content = rec.full_upper_name + "\n" + rec.title

@api.depends('upper_name_1', 'upper_name_2')
def _compute_duplicates(self):
for rec in self:
if not rec.id or not rec.env.context.get('creation', False):
raise ValidationError("Shouldn't be computed before record creation")
rec.duplicates = rec.search([
('upper_name_1', '=', rec.upper_name_1),
('upper_name_2', '=', rec.upper_name_2),
('id', '!=', rec.id),
])
@@ -47,3 +47,4 @@ access_test_new_api_inverse_m2o_ref,access_test_new_api_inverse_m2o_ref,model_te
access_test_new_api_model_binary,access_test_new_api_model_binary,model_test_new_api_model_binary,,1,1,1,1
access_test_new_api_model_child_m2o,access_test_new_api_model_child_m2o,model_test_new_api_model_child_m2o,,1,1,1,1
access_test_new_api_model_parent_m2o,access_test_new_api_model_parent_m2o,model_test_new_api_model_parent_m2o,,1,1,1,1
access_test_new_api_model_advanced_computes,access_test_new_api_model_advanced_computes,model_test_new_api_model_advanced_computes,,1,0,0,0
@@ -2242,3 +2242,50 @@ def test_delete_m2o_reference_records(self):
# fake record to emulate the unlink of a non-existant record
foo = m.browse(1 if not ids[0] else (ids[0] + 1))
self.assertTrue(foo.unlink())


@common.tagged('prepostcomputes')
class TestPrePostComputes(common.TransactionCase):

def test_pre_post_create_computes(self):
Model = self.env["test_new_api.model_advanced_computes"]

# Ensure automatic assignation of pre_compute=False works
# 1) dependency on create_date/create_uid/write_date/write_uid
self.assertFalse(Model._fields.get('create_month').pre_compute)
# 2) dependency on a pre_compute=False field
self.assertFalse(Model._fields.get('full_card_content').pre_compute)

# Force computation on a new and assertRaises Error
new_record = Model.new({
'name1': 'Nathan',
'name2': 'Algren',
'title': 'Military Advisor',
})
with self.assertRaises(ValidationError):
new_record.duplicates
# Create two records and check duplicates are correctly assigned
# If they were computed pre_create, duplicates fields would be empty.
# Context key ensure the computes are all called during the create call.
records = Model.with_context(creation=True).create([
{
'name1': 'Hans',
'name2': 'zimmer',
'title': 'Musical Composer'
}, {
'name1': 'hans',
'name2': 'Zimmer',
'title': 'Artist'
}
])
self.assertEqual(len(records), 2)
self.assertEqual(records.duplicates, records)
self.assertEqual(records[0].duplicates, records[1])
self.assertEqual(records[1].duplicates, records[0])

self.assertIn("Hans Zimmer\nMusical Composer", records.mapped('full_card_content'))
self.assertIn("Hans Zimmer\nArtist", records.mapped('full_card_content'))

self.assertEqual(records[0].full_upper_name, records[1].full_upper_name)
self.assertTrue(records[0].create_month)
self.assertTrue(records[1].create_month)

0 comments on commit 62d1392

Please sign in to comment.
You can’t perform that action at this time.