'Check the record cache behavior'

@mute_logger('odoo.models')
def test_60_cache(self):

    Partners = self.env['res.partner']
    pids = []
    data = {'partner    One': ['Partner    One    -    One', 'Partner    One    -    Two'], 'Partner    Two': ['Partner    Two    -    One'], 'Partner    Three': ['Partner    Three    -    One']}
    for p in data:
        pids.append(Partners.create({'name': p, 'child_ids': [(0, 0, {'name': c}) for c in data[p]]}).id)
    partners = Partners.search([('id', 'in', pids)])
    (partner1, partner2) = (partners[0], partners[1])
    (children1, children2) = (partner1.child_ids, partner2.child_ids)
    self.assertTrue(children1)
    self.assertTrue(children2)
    child = children1[0]
    self.assertEqual(child.parent_id, partner1)
    self.assertIn(child, partner1.child_ids)
    self.assertNotIn(child, partner2.child_ids)
    for p in partners:
        (p.name, p.company_id.name, p.user_id.name, p.contact_address)
    self.env.check_cache()
    child.write({'parent_id': partner2.id})
    self.env.check_cache()
    self.assertEqual(child.parent_id, partner2)
    self.assertNotIn(child, partner1.child_ids)
    self.assertIn(child, partner2.child_ids)
    self.assertEqual(set((partner1.child_ids + child)), set(children1))
    self.assertEqual(set(partner2.child_ids), set((children2 + child)))
    self.env.check_cache()
    child.unlink()
    self.env.check_cache()
    self.assertEqual(set(partner1.child_ids), (set(children1) - set([child])))
    self.assertEqual(set(partner2.child_ids), set(children2))
    self.env.check_cache()
    partner = partner1
    (partner.country_id, partner.child_ids)
    data = partner._convert_to_write(partner._cache)
    self.assertEqual(data['country_id'], partner.country_id.id)
    self.assertEqual(data['child_ids'], [(6, 0, partner.child_ids.ids)])
