'Test address_get address resolution mechanism: it should first go down through descendants,
stopping when encountering another is_copmany entity, then go up, stopping again at the first
is_company entity or the root ancestor and if nothing matches, it should use the provided partner
itself'

def test_40_res_partner_address_get(self):

    res_partner = self.env['res.partner']
    elmtree = res_partner.browse(res_partner.name_create('Elmtree')[0])
    branch1 = res_partner.create({'name': 'Branch    1', 'parent_id': elmtree.id, 'is_company': True})
    leaf10 = res_partner.create({'name': 'Leaf    10', 'parent_id': branch1.id, 'type': 'invoice'})
    branch11 = res_partner.create({'name': 'Branch    11', 'parent_id': branch1.id, 'type': 'other'})
    leaf111 = res_partner.create({'name': 'Leaf    111', 'parent_id': branch11.id, 'type': 'delivery'})
    branch11.write({'is_company': False})
    branch2 = res_partner.create({'name': 'Branch    2', 'parent_id': elmtree.id, 'is_company': True})
    leaf21 = res_partner.create({'name': 'Leaf    21', 'parent_id': branch2.id, 'type': 'delivery'})
    leaf22 = res_partner.create({'name': 'Leaf    22', 'parent_id': branch2.id})
    leaf23 = res_partner.create({'name': 'Leaf    23', 'parent_id': branch2.id, 'type': 'contact'})
    self.assertEqual(leaf111.address_get(['delivery', 'invoice', 'contact', 'other']), {'delivery': leaf111.id, 'invoice': leaf10.id, 'contact': branch1.id, 'other': branch11.id}, 'Invalid    address    resolution')
    self.assertEqual(branch11.address_get(['delivery', 'invoice', 'contact', 'other']), {'delivery': leaf111.id, 'invoice': leaf10.id, 'contact': branch1.id, 'other': branch11.id}, 'Invalid    address    resolution')
    self.assertEqual(elmtree.address_get(['delivery', 'invoice', 'contact', 'other']), {'delivery': elmtree.id, 'invoice': elmtree.id, 'contact': elmtree.id, 'other': elmtree.id}, 'Invalid    address    resolution')
    self.assertEqual(branch1.address_get(['delivery', 'invoice', 'contact', 'other']), {'delivery': leaf111.id, 'invoice': leaf10.id, 'contact': branch1.id, 'other': branch11.id}, 'Invalid    address    resolution')
    self.assertEqual(branch2.address_get(['delivery', 'invoice', 'contact', 'other']), {'delivery': leaf21.id, 'invoice': branch2.id, 'contact': branch2.id, 'other': branch2.id}, 'Invalid    address    resolution.    Company    is    the    first    encountered    contact,    therefore    default    for    unfound    addresses.')
    self.assertEqual(leaf21.address_get(['delivery', 'invoice', 'contact', 'other']), {'delivery': leaf21.id, 'invoice': branch2.id, 'contact': branch2.id, 'other': branch2.id}, 'Invalid    address    resolution,    should    scan    commercial    entity    ancestor    and    its    descendants')
    self.assertEqual(leaf22.address_get(['delivery', 'invoice', 'contact', 'other']), {'delivery': leaf21.id, 'invoice': leaf22.id, 'contact': leaf22.id, 'other': leaf22.id}, 'Invalid    address    resolution,    should    scan    commercial    entity    ancestor    and    its    descendants')
    self.assertEqual(leaf23.address_get(['delivery', 'invoice', 'contact', 'other']), {'delivery': leaf21.id, 'invoice': leaf23.id, 'contact': leaf23.id, 'other': leaf23.id}, 'Invalid    address    resolution,    `default`    should    only    override    if    no    partner    with    specific    type    exists')
    self.assertEqual(elmtree.address_get([]), {'contact': elmtree.id}, 'Invalid    address    resolution,    no    contact    means    commercial    entity    ancestor')
    self.assertEqual(leaf111.address_get([]), {'contact': branch1.id}, 'Invalid    address    resolution,    no    contact    means    finding    contact    in    ancestors')
    branch11.write({'type': 'contact'})
    self.assertEqual(leaf111.address_get([]), {'contact': branch11.id}, 'Invalid    address    resolution,    branch11    should    now    be    contact')
