1# -*- coding: utf-8 -*- 2# Part of Odoo. See LICENSE file for full copyright and licensing details. 3 4from odoo.addons.base.tests.common import TransactionCaseWithUserDemo 5from odoo.tests import common 6 7class test_inherits(common.TransactionCase): 8 9 def test_00_inherits(self): 10 """ Check that a many2one field with delegate=True adds an entry in _inherits """ 11 daughter = self.env['test.inherit.daughter'] 12 13 self.assertEqual(daughter._inherits, {'test.inherit.mother': 'template_id'}) 14 15 def test_10_access_from_child_to_parent_model(self): 16 """ check whether added field in model is accessible from children models (_inherits) """ 17 # This test checks if the new added column of a parent model 18 # is accessible from the child model. This test has been written 19 # to verify the purpose of the inheritance computing of the class 20 # in the openerp.osv.orm._build_model. 21 mother = self.env['test.inherit.mother'] 22 daughter = self.env['test.inherit.daughter'] 23 24 self.assertIn('field_in_mother', mother._fields) 25 self.assertIn('field_in_mother', daughter._fields) 26 27 def test_20_field_extension(self): 28 """ check the extension of a field in an inherited model """ 29 mother = self.env['test.inherit.mother'] 30 daughter = self.env['test.inherit.daughter'] 31 32 # the field mother.name must have required=True and "Bar" as default 33 field = mother._fields['name'] 34 self.assertTrue(field.required) 35 self.assertEqual(field.default(mother), "Bar") 36 self.assertEqual(mother.default_get(['name']), {'name': "Bar"}) 37 38 # the field daughter.name must have required=False and "Baz" as default 39 field = daughter._fields['name'] 40 self.assertFalse(field.required) 41 self.assertEqual(field.default(daughter), "Baz") 42 self.assertEqual(daughter.default_get(['name']), {'name': "Baz"}) 43 44 # the field mother.state must have no default value 45 field = mother._fields['state'] 46 self.assertFalse(field.default) 47 self.assertEqual(mother.default_get(['state']), {}) 48 49 # the field daughter.template_id should have 50 # comodel_name='test.inherit.mother', string='Template', required=True 51 field = daughter._fields['template_id'] 52 self.assertEqual(field.comodel_name, 'test.inherit.mother') 53 self.assertEqual(field.string, "Template") 54 self.assertTrue(field.required) 55 56 def test_30_depends_extension(self): 57 """ check that @depends on overridden compute methods extends dependencies """ 58 mother = self.env['test.inherit.mother'] 59 field = mother._fields['surname'] 60 61 # the field dependencies are added 62 self.assertItemsEqual(field.depends, ['name', 'field_in_mother']) 63 64 def test_40_selection_extension(self): 65 """ check that attribute selection_add=... extends selection on fields. """ 66 mother = self.env['test.inherit.mother'] 67 68 # the extra values are added, both in the field and the column 69 self.assertEqual(mother._fields['state'].selection, 70 [('a', 'A'), ('d', 'D'), ('b', 'B'), ('c', 'C')]) 71 72 def test_41_selection_extension(self): 73 """ check that attribute selection_add=... extends selection on fields. """ 74 model = self.env['test_new_api.selection'] 75 field = model._fields['other'] 76 self.assertIsInstance(field.selection, str) 77 self.assertEqual(field._description_selection(self.env), [('baz', 'Baz')]) 78 79 80class test_inherits_demo(TransactionCaseWithUserDemo): 81 82 def test_50_search_one2many(self): 83 """ check search on one2many field based on inherited many2one field. """ 84 # create a daughter record attached to partner Demo 85 partner_demo = self.partner_demo 86 daughter = self.env['test.inherit.daughter'].create({'partner_id': partner_demo.id}) 87 self.assertEqual(daughter.partner_id, partner_demo) 88 self.assertIn(daughter, partner_demo.daughter_ids) 89 90 # search the partner from the daughter record 91 partners = self.env['res.partner'].search([('daughter_ids', 'like', 'not existing daugther')]) 92 self.assertFalse(partners) 93 partners = self.env['res.partner'].search([('daughter_ids', 'not like', 'not existing daugther')]) 94 self.assertIn(partner_demo, partners) 95 partners = self.env['res.partner'].search([('daughter_ids', '!=', False)]) 96 self.assertIn(partner_demo, partners) 97 partners = self.env['res.partner'].search([('daughter_ids', 'in', daughter.ids)]) 98 self.assertIn(partner_demo, partners) 99 100 101class test_override_property(common.TransactionCase): 102 103 def test_override_with_normal_field(self): 104 """ test overriding a property field by a function field """ 105 record = self.env['test.inherit.property'].create({'name': "Stuff"}) 106 # record.property_foo is not a property field 107 self.assertFalse(record.property_foo) 108 self.assertFalse(type(record).property_foo.company_dependent) 109 self.assertTrue(type(record).property_foo.store) 110 111 def test_override_with_computed_field(self): 112 """ test overriding a property field by a computed field """ 113 record = self.env['test.inherit.property'].create({'name': "Stuff"}) 114 # record.property_bar is not a property field 115 self.assertEqual(record.property_bar, 42) 116 self.assertFalse(type(record).property_bar.company_dependent) 117 118 119class TestInherit(common.TransactionCase): 120 def test_extend_parent(self): 121 """ test whether a model extension is visible in its children models. """ 122 parent = self.env['test.inherit.parent'] 123 child = self.env['test.inherit.child'] 124 125 # check fields 126 self.assertIn('foo', parent.fields_get()) 127 self.assertNotIn('bar', parent.fields_get()) 128 self.assertIn('foo', child.fields_get()) 129 self.assertIn('bar', child.fields_get()) 130 131 # check method overriding 132 self.assertEqual(parent.stuff(), 'P1P2') 133 self.assertEqual(child.stuff(), 'P1P2C1') 134 135 # check inferred model attributes 136 self.assertEqual(parent._table, 'test_inherit_parent') 137 self.assertEqual(child._table, 'test_inherit_child') 138 self.assertEqual(len(parent._sql_constraints), 1) 139 self.assertEqual(len(child._sql_constraints), 1) 140 141 # check properties memoized on model 142 self.assertEqual(len(parent._constraint_methods), 1) 143 self.assertEqual(len(child._constraint_methods), 1) 144 145 146class TestXMLIDS(common.TransactionCase): 147 def test_xml_ids(self): 148 """ check XML ids of selection fields. """ 149 field = self.env['test_new_api.selection']._fields['state'] 150 self.assertEqual(field.selection, [('foo', 'Foo'), ('bar', 'Bar'), ('baz', 'Baz')]) 151 152 ir_field = self.env['ir.model.fields']._get('test_new_api.selection', 'state') 153 xml_ids = ir_field._get_external_ids() 154 self.assertCountEqual(xml_ids.get(ir_field.id), [ 155 'test_new_api.field_test_new_api_selection__state', 156 'test_inherit.field_test_new_api_selection__state', 157 ]) 158 159 foo, bar, baz = ir_field.selection_ids 160 xml_ids = (foo + bar + baz)._get_external_ids() 161 self.assertCountEqual(xml_ids.get(foo.id), [ 162 'test_new_api.selection__test_new_api_selection__state__foo', 163 ]) 164 self.assertCountEqual(xml_ids.get(bar.id), [ 165 'test_new_api.selection__test_new_api_selection__state__bar', 166 'test_inherit.selection__test_new_api_selection__state__bar', 167 ]) 168 self.assertCountEqual(xml_ids.get(baz.id), [ 169 'test_inherit.selection__test_new_api_selection__state__baz', 170 ]) 171 172