2014-07-06 14:44:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from openerp.tests import common
|
|
|
|
|
|
|
|
class test_inherits(common.TransactionCase):
|
|
|
|
|
2015-02-17 15:45:20 +00:00
|
|
|
def test_00_inherits(self):
|
|
|
|
""" Check that a many2one field with delegate=True adds an entry in _inherits """
|
|
|
|
daughter = self.env['test.inherit.daughter']
|
|
|
|
|
|
|
|
self.assertEqual(daughter._inherits, {'test.inherit.mother': 'template_id'})
|
|
|
|
|
|
|
|
def test_10_access_from_child_to_parent_model(self):
|
2014-07-06 14:44:26 +00:00
|
|
|
""" check whether added field in model is accessible from children models (_inherits) """
|
|
|
|
# This test checks if the new added column of a parent model
|
|
|
|
# is accessible from the child model. This test has been written
|
|
|
|
# to verify the purpose of the inheritance computing of the class
|
|
|
|
# in the openerp.osv.orm._build_model.
|
[IMP] fields: add mechanism to extend a selection field
If a selection field is defined by a list as selection, such as:
state = fields.Selection([('a', 'A'), ('b', 'B')])
one can extend it by inheritance by redefining the field, as:
state = fields.Selection(selection_add=[('c', 'C')])
The result is that the selection field will have the list
[('a', 'A'), ('b', 'B'), ('c', 'C')] as selection.
2014-07-10 20:04:03 +00:00
|
|
|
mother = self.env['test.inherit.mother']
|
2014-10-16 10:32:23 +00:00
|
|
|
daughter = self.env['test.inherit.daughter']
|
2014-07-06 14:44:26 +00:00
|
|
|
|
|
|
|
self.assertIn('field_in_mother', mother._fields)
|
2014-10-16 10:32:23 +00:00
|
|
|
self.assertIn('field_in_mother', daughter._fields)
|
2014-07-06 14:44:26 +00:00
|
|
|
|
2015-02-17 15:45:20 +00:00
|
|
|
def test_20_field_extension(self):
|
2014-07-06 14:44:26 +00:00
|
|
|
""" check the extension of a field in an inherited model """
|
[IMP] fields: add mechanism to extend a selection field
If a selection field is defined by a list as selection, such as:
state = fields.Selection([('a', 'A'), ('b', 'B')])
one can extend it by inheritance by redefining the field, as:
state = fields.Selection(selection_add=[('c', 'C')])
The result is that the selection field will have the list
[('a', 'A'), ('b', 'B'), ('c', 'C')] as selection.
2014-07-10 20:04:03 +00:00
|
|
|
mother = self.env['test.inherit.mother']
|
2014-11-06 08:59:41 +00:00
|
|
|
daughter = self.env['test.inherit.daughter']
|
|
|
|
|
|
|
|
# the field mother.name must have required=True and "Bar" as default
|
2014-07-06 14:44:26 +00:00
|
|
|
field = mother._fields['name']
|
|
|
|
self.assertTrue(field.required)
|
2014-10-02 15:01:03 +00:00
|
|
|
self.assertEqual(field.default(mother), "Bar")
|
|
|
|
self.assertEqual(mother._defaults.get('name'), "Bar")
|
2014-11-06 08:59:41 +00:00
|
|
|
self.assertEqual(mother.default_get(['name']), {'name': "Bar"})
|
2014-07-06 14:44:26 +00:00
|
|
|
|
2014-11-06 08:59:41 +00:00
|
|
|
# the field daughter.name must have required=False and "Baz" as default
|
|
|
|
field = daughter._fields['name']
|
|
|
|
self.assertFalse(field.required)
|
|
|
|
self.assertEqual(field.default(mother), "Baz")
|
|
|
|
self.assertEqual(daughter._defaults.get('name'), "Baz")
|
|
|
|
self.assertEqual(daughter.default_get(['name']), {'name': "Baz"})
|
|
|
|
|
|
|
|
# the field daughter.template_id should have
|
|
|
|
# comodel_name='test.inherit.mother', string='Template', required=True
|
2014-10-16 10:32:23 +00:00
|
|
|
field = daughter._fields['template_id']
|
2014-08-22 08:01:41 +00:00
|
|
|
self.assertEqual(field.comodel_name, 'test.inherit.mother')
|
|
|
|
self.assertEqual(field.string, "Template")
|
|
|
|
self.assertTrue(field.required)
|
|
|
|
|
2015-02-17 15:45:20 +00:00
|
|
|
def test_30_depends_extension(self):
|
2014-07-06 14:44:26 +00:00
|
|
|
""" check that @depends on overridden compute methods extends dependencies """
|
[IMP] fields: add mechanism to extend a selection field
If a selection field is defined by a list as selection, such as:
state = fields.Selection([('a', 'A'), ('b', 'B')])
one can extend it by inheritance by redefining the field, as:
state = fields.Selection(selection_add=[('c', 'C')])
The result is that the selection field will have the list
[('a', 'A'), ('b', 'B'), ('c', 'C')] as selection.
2014-07-10 20:04:03 +00:00
|
|
|
mother = self.env['test.inherit.mother']
|
2014-07-06 14:44:26 +00:00
|
|
|
field = mother._fields['surname']
|
|
|
|
|
|
|
|
# the field dependencies are added
|
|
|
|
self.assertItemsEqual(field.depends, ['name', 'field_in_mother'])
|
|
|
|
|
2015-02-17 15:45:20 +00:00
|
|
|
def test_40_selection_extension(self):
|
[IMP] fields: add mechanism to extend a selection field
If a selection field is defined by a list as selection, such as:
state = fields.Selection([('a', 'A'), ('b', 'B')])
one can extend it by inheritance by redefining the field, as:
state = fields.Selection(selection_add=[('c', 'C')])
The result is that the selection field will have the list
[('a', 'A'), ('b', 'B'), ('c', 'C')] as selection.
2014-07-10 20:04:03 +00:00
|
|
|
""" check that attribute selection_add=... extends selection on fields. """
|
|
|
|
mother = self.env['test.inherit.mother']
|
|
|
|
|
2014-10-08 08:47:25 +00:00
|
|
|
# the extra values are added, both in the field and the column
|
|
|
|
self.assertEqual(mother._fields['state'].selection,
|
|
|
|
[('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')])
|
|
|
|
self.assertEqual(mother._columns['state'].selection,
|
|
|
|
[('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')])
|
[IMP] fields: add mechanism to extend a selection field
If a selection field is defined by a list as selection, such as:
state = fields.Selection([('a', 'A'), ('b', 'B')])
one can extend it by inheritance by redefining the field, as:
state = fields.Selection(selection_add=[('c', 'C')])
The result is that the selection field will have the list
[('a', 'A'), ('b', 'B'), ('c', 'C')] as selection.
2014-07-10 20:04:03 +00:00
|
|
|
|
[FIX] expression: fix search on one2many field with inherited inverse field
Consider the following setting:
- on model A, field F is computed, stored, and depends on field G
- on model A, field one2many G to model B, with inverse field H
- on model B, field many2one H is inherited (_inherits) from model C
- on model C, field many2one H is stored
When adding records of model B, the field F must be recomputed. In order to
determine which records to recompute, one searches model A with a domain like
[(G, 'in', ids)]. In expression.py, this is resolved with an SQL query like
select H from B where id in {ids}
This query fails, since the field H is not stored in model B. This happens in
general if H is not stored (it may be any computed field). In that case, one
should instead browse records from B, and read field H through the ORM.
A test case has been added: it introduces a many2one field in a parent model,
and a one2many field using the inherited many2one on a child model. The test
checks whether one can search on the one2many field.
2015-09-04 08:06:11 +00:00
|
|
|
def test_50_search_one2many(self):
|
|
|
|
""" check search on one2many field based on inherited many2one field. """
|
|
|
|
# create a daughter record attached to partner Demo
|
|
|
|
partner_demo = self.env.ref('base.partner_demo')
|
|
|
|
daughter = self.env['test.inherit.daughter'].create({'partner_id': partner_demo.id})
|
|
|
|
self.assertEqual(daughter.partner_id, partner_demo)
|
|
|
|
self.assertIn(daughter, partner_demo.daughter_ids)
|
|
|
|
|
|
|
|
# search the partner from the daughter record
|
|
|
|
partners = self.env['res.partner'].search([('daughter_ids', 'in', daughter.ids)])
|
|
|
|
self.assertIn(partner_demo, partners)
|
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|