'Test the root organisation is updated when adding a branch link'

def testRootOrgUpdate(self):

    db = current.db
    s3db = current.s3db
    otable = s3db.org_organisation
    ltable = s3db.org_organisation_branch
    org1 = Storage(name='RootOrgUpdateTest1')
    org1_id = otable.insert(**org1)
    self.assertNotEqual(org1_id, None)
    org1['id'] = org1_id
    s3db.update_super(otable, org1)
    s3db.onaccept(otable, org1, method='create')
    org2 = Storage(name='RootOrgUpdateTest2')
    org2_id = otable.insert(**org2)
    self.assertNotEqual(org2_id, None)
    org2['id'] = org2_id
    s3db.update_super(otable, org2)
    s3db.onaccept(otable, org2, method='create')
    org3 = Storage(name='RootOrgUpdateTest3')
    org3_id = otable.insert(**org3)
    self.assertNotEqual(org3_id, None)
    org3['id'] = org3_id
    s3db.update_super(otable, org3)
    s3db.onaccept(otable, org3, method='create')
    link = Storage(organisation_id=org2_id, branch_id=org3_id)
    link_id = ltable.insert(**link)
    self.assertNotEqual(link_id, None)
    link['id'] = link_id
    s3db.onaccept(ltable, link, method='create')
    check = (org2_id, org3_id)
    rows = db(otable.id.belongs(check)).select(otable.id, otable.root_organisation)
    self.assertNotEqual(rows, None)
    self.assertEqual(len(rows), 2)
    for row in rows:
        self.assertEqual(row.root_organisation, org2_id)
    link = Storage(organisation_id=org1_id, branch_id=org2_id)
    link_id = ltable.insert(**link)
    self.assertNotEqual(link_id, None)
    link['id'] = link_id
    s3db.onaccept(ltable, link, method='create')
    check = (org1_id, org2_id, org3_id)
    rows = db(otable.id.belongs(check)).select(otable.id, otable.root_organisation)
    self.assertNotEqual(rows, None)
    self.assertEqual(len(rows), 3)
    for row in rows:
        self.assertEqual(row.root_organisation, org1_id)
