squashed commit

This commit is contained in:
hoangvv
2026-09-18 13:55:25 +07:00
commit 039c98d4d0
45882 changed files with 24235585 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import models
+16
View File
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
{
'name': 'test-inherits',
'version': '0.1',
'category': 'Hidden/Tests',
'description': """A module to verify the inheritance using _inherits.""",
'author': 'Camptocamp',
'website': 'http://www.camptocamp.com',
'depends': ['base'],
'data': [
'ir.model.access.csv',
'demo_data.xml',
],
'installable': True,
'license': 'LGPL-3',
}
+19
View File
@@ -0,0 +1,19 @@
<odoo>
<data>
<record id="unit_a" model="test.unit">
<field name="name">Unit A</field>
<field name="state">a</field>
</record>
<record id="box_a" model="test.box">
<field name="unit_id" ref="unit_a"/>
<field name="field_in_box">A</field>
</record>
<record id="pallet_a" model="test.pallet">
<field name="box_id" ref="box_a"/>
<field name="field_in_pallet">A</field>
</record>
</data>
</odoo>
@@ -0,0 +1,7 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
access_test_unit,access_test_unit,model_test_unit,base.group_system,1,1,1,1
access_test_unit_line,access_test_unit_line,model_test_unit_line,base.group_system,1,1,1,1
access_test_box,access_test_box,model_test_box,base.group_system,1,1,1,1
access_test_pallet,access_test_pallet,model_test_pallet,base.group_system,1,1,1,1
access_test_another_unit,access_test_another_unit,model_test_another_unit,base.group_system,1,1,1,1
access_test_another_box,access_test_another_box,model_test_another_box,base.group_system,1,1,1,1
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_test_unit access_test_unit model_test_unit base.group_system 1 1 1 1
3 access_test_unit_line access_test_unit_line model_test_unit_line base.group_system 1 1 1 1
4 access_test_box access_test_box model_test_box base.group_system 1 1 1 1
5 access_test_pallet access_test_pallet model_test_pallet base.group_system 1 1 1 1
6 access_test_another_unit access_test_another_unit model_test_another_unit base.group_system 1 1 1 1
7 access_test_another_box access_test_another_box model_test_another_box base.group_system 1 1 1 1
+78
View File
@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-
from odoo import models, fields, api
from odoo.exceptions import ValidationError
# We just create a new model
class Unit(models.Model):
_name = 'test.unit'
_description = 'Test Unit'
name = fields.Char('Name', required=True, translate=True)
state = fields.Selection([('a', 'A'), ('b', 'B')], string='State')
surname = fields.Char(compute='_compute_surname')
line_ids = fields.One2many('test.unit.line', 'unit_id')
readonly_name = fields.Char('Readonly Name', readonly=True)
size = fields.Integer()
@api.depends('name')
def _compute_surname(self):
for unit in self:
unit.surname = unit.name or ''
class UnitLine(models.Model):
_name = 'test.unit.line'
_description = 'Test Unit Line'
name = fields.Char('Name', required=True)
unit_id = fields.Many2one('test.unit', required=True)
# We want to _inherits from the parent model and we add some fields
# in the child object
class Box(models.Model):
_name = 'test.box'
_inherits = {'test.unit': 'unit_id'}
_description = 'Test Box'
unit_id = fields.Many2one('test.unit', 'Unit', required=True,
ondelete='cascade')
field_in_box = fields.Char('Field1')
size = fields.Integer()
# We add a third level of _inherits
class Pallet(models.Model):
_name = 'test.pallet'
_inherits = {'test.box': 'box_id'}
_description = 'Test Pallet'
box_id = fields.Many2one('test.box', 'Box', required=True,
ondelete='cascade')
field_in_pallet = fields.Char('Field2')
# Another model for another test suite
class AnotherUnit(models.Model):
_name = 'test.another_unit'
_description = 'Another Test Unit'
val1 = fields.Integer('Value 1', required=True)
# We want to _inherits from the parent model, add a field and check
# the new field is always equals to the first one
class AnotherBox(models.Model):
_name = 'test.another_box'
_inherits = {'test.another_unit': 'another_unit_id'}
_description = 'Another Test Box'
another_unit_id = fields.Many2one('test.another_unit', 'Another Unit',
required=True, ondelete='cascade')
val2 = fields.Integer('Value 2', required=True)
@api.constrains('val1', 'val2')
def _check_values(self):
if any(box.val1 != box.val2 for box in self):
raise ValidationError("The two values must be equals")
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
from . import test_inherits
@@ -0,0 +1,151 @@
# -*- coding: utf-8 -*-
from odoo.tests import common
from odoo.exceptions import ValidationError
from odoo import Command
class test_inherits(common.TransactionCase):
def test_create_3_levels_inherits(self):
""" Check that we can create an inherits on 3 levels """
pallet = self.env['test.pallet'].create({
'name': 'B',
'field_in_box': 'box',
'field_in_pallet': 'pallet',
})
self.assertTrue(pallet)
self.assertEqual(pallet.name, 'B')
self.assertEqual(pallet.field_in_box, 'box')
self.assertEqual(pallet.field_in_pallet, 'pallet')
def test_create_3_levels_inherits_with_defaults(self):
unit = self.env['test.unit'].create({
'name': 'U',
'state': 'a',
'size': 1,
})
ctx = {
'default_state': 'b', # 'state' is inherited from 'test.unit'
'default_size': 2, # 'size' is inherited from 'test.box'
}
pallet = self.env['test.pallet'].with_context(ctx).create({
'name': 'P',
'unit_id': unit.id, # grand-parent field is set
})
# default 'state' should be ignored, but default 'size' should not
self.assertEqual(pallet.state, 'a')
self.assertEqual(pallet.size, 2)
def test_read_3_levels_inherits(self):
""" Check that we can read an inherited field on 3 levels """
pallet = self.env.ref('test_inherits.pallet_a')
self.assertEqual(pallet.read(['name']), [{'id': pallet.id, 'name': 'Unit A'}])
def test_write_3_levels_inherits(self):
""" Check that we can create an inherits on 3 levels """
pallet = self.env.ref('test_inherits.pallet_a')
pallet.write({'name': 'C'})
self.assertEqual(pallet.name, 'C')
def test_write_4_one2many(self):
""" Check that we can write on an inherited one2many field. """
box = self.env.ref('test_inherits.box_a')
box.write({'line_ids': [Command.create({'name': 'Line 1'})]})
self.assertTrue(all(box.line_ids._ids))
self.assertEqual(box.line_ids.mapped('name'), ['Line 1'])
self.assertEqual(box.line_ids, box.unit_id.line_ids)
self.env.flush_all()
box.invalidate_model(['line_ids'])
box.write({'line_ids': [Command.create({'name': 'Line 2'})]})
self.assertTrue(all(box.line_ids._ids))
self.assertEqual(box.line_ids.mapped('name'), ['Line 1', 'Line 2'])
self.assertEqual(box.line_ids, box.unit_id.line_ids)
self.env.flush_all()
box.invalidate_model(['line_ids'])
box.write({'line_ids': [Command.update(box.line_ids[0].id, {'name': 'First line'})]})
self.assertTrue(all(box.line_ids._ids))
self.assertEqual(box.line_ids.mapped('name'), ['First line', 'Line 2'])
self.assertEqual(box.line_ids, box.unit_id.line_ids)
def test_write_5_field_readonly(self):
""" Check that we can write on an inherited readonly field. """
self.assertTrue(self.env['test.box']._fields['readonly_name'])
box = self.env.ref('test_inherits.box_a')
box.write({'readonly_name': "Superuser's box"})
self.assertEqual(box.readonly_name, "Superuser's box")
self.assertEqual(box.unit_id.readonly_name, "Superuser's box")
def test_ir_model_data_inherits(self):
""" Check the existence of the correct ir.model.data """
IrModelData = self.env['ir.model.data']
field = IrModelData.search([('name', '=', 'field_test_unit__name')])
self.assertEqual(len(field), 1)
self.assertEqual(field.module, 'test_inherits')
field = IrModelData.search([('name', '=', 'field_test_box__name')])
self.assertEqual(len(field), 1)
self.assertEqual(field.module, 'test_inherits')
def test_constraint_inherits(self):
"""Validate constraints on inherits when the parent is not updated"""
Model = self.env['test.another_box']
with self.assertRaises(ValidationError):
another_box = Model.create({'val1': 1, 'val2': 2})
another_box = Model.create({'val1': 1, 'val2': 1})
with self.assertRaises(ValidationError):
another_box.write({'val2': 2})
another_box.write({'val1': 2, 'val2': 2})
def test_constraint_inherits_parent_change(self):
"""Validate constraints on inherits when parent is updated too"""
UnitModel = self.env['test.another_unit']
BoxModel = self.env['test.another_box']
unit1 = UnitModel.create({'val1': 1})
box = BoxModel.create({'another_unit_id': unit1.id, 'val2': 1})
unit2 = UnitModel.create({'val1': 2})
box.write({'another_unit_id': unit2.id, 'val2': 2})
unit3 = UnitModel.create({'val1': 3})
box.write({'another_unit_id': unit3.id, 'val1': 4, 'val2': 4})
unit4 = UnitModel.create({'val1': 5})
with self.assertRaises(ValidationError):
box.write({'another_unit_id': unit4.id, 'val2': 6})
unit5 = UnitModel.create({'val1': 7})
with self.assertRaises(ValidationError):
box.write({'another_unit_id': unit5.id, 'val1': 8, 'val2': 7})
def test_display_name(self):
""" Check the 'display_name' of an inherited translated 'name'. """
self.env['res.lang']._activate_lang('fr_FR')
# concrete check
pallet_en = self.env['test.pallet'].create({'name': 'Bread'})
pallet_fr = pallet_en.with_context(lang='fr_FR')
pallet_fr.box_id.unit_id.name = 'Pain'
self.assertEqual(pallet_en.display_name, 'Bread')
self.assertEqual(pallet_fr.display_name, 'Pain')
# check model
Unit = type(self.env['test.unit'])
Box = type(self.env['test.box'])
Pallet = type(self.env['test.pallet'])
self.assertTrue(Unit.name.translate)
self.assertIn('lang', self.registry.field_depends_context[Unit.display_name])
self.assertIn('lang', self.registry.field_depends_context[Box.display_name])
self.assertIn('lang', self.registry.field_depends_context[Pallet.display_name])
def test_multi_write_m2o_inherits(self):
"""Verify that an inherits m2o field can be written to in batch"""
unit_foo = self.env['test.unit'].create({'name': 'foo'})
boxes = self.env['test.box'].create([{'unit_id': unit_foo.id}] * 5)
unit_bar = self.env['test.unit'].create({'name': 'bar'})
boxes.unit_id = unit_bar
self.assertEqual(boxes.mapped('unit_id.name'), ['bar'])