'test company-dependent fields.'

def test_27_company_dependent(self):

    company0 = self.env.ref('base.main_company')
    company1 = self.env['res.company'].create({'name': 'A', 'parent_id': company0.id})
    company2 = self.env['res.company'].create({'name': 'B', 'parent_id': company1.id})
    user0 = self.env['res.users'].create({'name': 'Foo', 'login': 'foo', 'company_id': company0.id, 'company_ids': []})
    user1 = self.env['res.users'].create({'name': 'Bar', 'login': 'bar', 'company_id': company1.id, 'company_ids': []})
    user2 = self.env['res.users'].create({'name': 'Baz', 'login': 'baz', 'company_id': company2.id, 'company_ids': []})
    field = self.env['ir.model.fields'].search([('model', '=', 'test_new_api.company'), ('name', '=', 'foo')])
    self.env['ir.property'].create({'name': 'foo', 'fields_id': field.id, 'value': 'default', 'type': 'char'})
    record = self.env['test_new_api.company'].create({'foo': 'main'})
    record.invalidate_cache()
    self.assertEqual(record.sudo(user0).foo, 'main')
    self.assertEqual(record.sudo(user1).foo, 'default')
    self.assertEqual(record.sudo(user2).foo, 'default')
    record.sudo(user1).foo = 'alpha'
    record.invalidate_cache()
    self.assertEqual(record.sudo(user0).foo, 'main')
    self.assertEqual(record.sudo(user1).foo, 'alpha')
    self.assertEqual(record.sudo(user2).foo, 'default')
    company_record = self.env['test_new_api.company'].create({'foo': 'ABC'})
    attribute_record = self.env['test_new_api.company.attr'].create({'company': company_record.id, 'quantity': 1})
    self.assertEqual(attribute_record.bar, 'ABC')
    attribute_record.quantity = 2
    self.assertEqual(attribute_record.bar, 'ABCABC')
    self.assertFalse(self.env.has_todo())
    company_record.foo = 'DEF'
    self.assertEqual(attribute_record.company.foo, 'DEF')
    self.assertEqual(attribute_record.bar, 'DEFDEF')
    self.assertFalse(self.env.has_todo())
