[IMP] fields: improve _determine_default() and add test for inherited fields
[odoo/odoo.git] / openerp / addons / test_inherit / tests / test_inherit.py
1 # -*- coding: utf-8 -*-
2 from openerp.tests import common
3
4 class test_inherits(common.TransactionCase):
5
6     def test_access_from_child_to_parent_model(self):
7         """ check whether added field in model is accessible from children models (_inherits) """
8         # This test checks if the new added column of a parent model
9         # is accessible from the child model. This test has been written
10         # to verify the purpose of the inheritance computing of the class
11         # in the openerp.osv.orm._build_model.
12         mother = self.env['test.inherit.mother']
13         daughter = self.env['test.inherit.daughter']
14
15         self.assertIn('field_in_mother', mother._fields)
16         self.assertIn('field_in_mother', daughter._fields)
17
18     def test_field_extension(self):
19         """ check the extension of a field in an inherited model """
20         mother = self.env['test.inherit.mother']
21         daughter = self.env['test.inherit.daughter']
22
23         # the field mother.name must have required=True and "Bar" as default
24         field = mother._fields['name']
25         self.assertTrue(field.required)
26         self.assertEqual(field.default(mother), "Bar")
27         self.assertEqual(mother._defaults.get('name'), "Bar")
28         self.assertEqual(mother.default_get(['name']), {'name': "Bar"})
29
30         # the field daughter.name must have required=False and "Baz" as default
31         field = daughter._fields['name']
32         self.assertFalse(field.required)
33         self.assertEqual(field.default(mother), "Baz")
34         self.assertEqual(daughter._defaults.get('name'), "Baz")
35         self.assertEqual(daughter.default_get(['name']), {'name': "Baz"})
36
37         # the field daughter.template_id should have
38         # comodel_name='test.inherit.mother', string='Template', required=True
39         field = daughter._fields['template_id']
40         self.assertEqual(field.comodel_name, 'test.inherit.mother')
41         self.assertEqual(field.string, "Template")
42         self.assertTrue(field.required)
43
44     def test_depends_extension(self):
45         """ check that @depends on overridden compute methods extends dependencies """
46         mother = self.env['test.inherit.mother']
47         field = mother._fields['surname']
48
49         # the field dependencies are added
50         self.assertItemsEqual(field.depends, ['name', 'field_in_mother'])
51
52     def test_selection_extension(self):
53         """ check that attribute selection_add=... extends selection on fields. """
54         mother = self.env['test.inherit.mother']
55
56         # the extra values are added, both in the field and the column
57         self.assertEqual(mother._fields['state'].selection,
58                          [('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')])
59         self.assertEqual(mother._columns['state'].selection,
60                          [('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')])
61
62
63 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: