Skip to content
Permalink
Browse files

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

  • Loading branch information
Feyensv committed Mar 23, 2020
1 parent e0a700a commit c0113a516627daa67c1c299505708d6c6ae9a7dc
@@ -846,6 +846,7 @@ class City(models.Model):
name = fields.Char()
country_id = fields.Many2one('test_new_api.country')


# abstract model with a selection field
class StateMixin(models.AbstractModel):
_name = 'test_new_api.state_mixin'
@@ -856,3 +857,66 @@ class StateMixin(models.AbstractModel):
('confirmed', 'Confirmed'),
('done', 'Done'),
])


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

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),
])
@@ -51,3 +51,4 @@ access_test_new_api_model_private_address_onchange,access_test_new_api_model_pri
access_test_new_api_req_m2o_transient,access_test_new_api_req_m2o_transient,model_test_new_api_req_m2o_transient,,0,0,0,0
access_test_new_api_country,access_test_new_api_country,model_test_new_api_country,base.group_user,1,1,1,1
access_test_new_api_city,access_test_new_api_city,model_test_new_api_city,base.group_user,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
@@ -193,10 +193,10 @@ def check_stored(disc):
check_stored(discussion1)

# switch message from discussion, and check again

# See YTI FIXME
discussion1.invalidate_cache()

discussion2 = discussion1.copy({'name': 'Another discussion'})
message2 = discussion1.messages[0]
message2.discussion = discussion2
@@ -2441,3 +2441,50 @@ def test_unlink_asbtract(self):
('field_id.name', '=', 'state'),
('value', '=', 'confirmed'),
], limit=1).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 c0113a5

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