'Test environment on records.'

@mute_logger('odoo.models')
@mute_logger('odoo.addons.base.ir.ir_model')
def test_50_environment(self):

    partners = self.env['res.partner'].search([('name', 'ilike', 'j')])
    self.assertEqual(partners.env, self.env)
    for x in (partners, partners[0], partners[0].company_id):
        self.assertEqual(x.env, self.env)
    for p in partners:
        self.assertEqual(p.env, self.env)
    partners[0].company_id.name
    partners[0].company_id.write({'name': 'Fools'})
    demo = self.env['res.users'].search([('login', '=', 'demo')])[0]
    demo_env = self.env(user=demo)
    self.assertNotEqual(demo_env, self.env)
    self.assertEqual(partners.env, self.env)
    for x in (partners, partners[0], partners[0].company_id):
        self.assertEqual(x.env, self.env)
    for p in partners:
        self.assertEqual(p.env, self.env)
    demo_partners = partners.sudo(demo)
    self.assertEqual(demo_partners.env, demo_env)
    for x in (demo_partners, demo_partners[0], demo_partners[0].company_id):
        self.assertEqual(x.env, demo_env)
    for p in demo_partners:
        self.assertEqual(p.env, demo_env)
    demo_partners[0].company_id.name
    with self.assertRaises(AccessError):
        demo_partners[0].company_id.write({'name': 'Pricks'})
    demo.write({'groups_id': [(5,)]})
    with self.assertRaises(AccessError):
        demo_partners[0].company_id.name
