'Test recursive deletion of a hierarchy branch'

def testDeleteBranch(self):

    xmlstr = '\n<s3xml>\n                <resource    name="test_hierarchy"    uuid="HIERARCHY1-3">\n                                <data    field="name">Type    1-3</data>\n                                <reference    field="parent"    resource="test_hierarchy"    uuid="HIERARCHY1"/>\n                </resource>\n                <resource    name="test_hierarchy"    uuid="HIERARCHY1-3-1">\n                                <data    field="name">Type    1-3-1</data>\n                                <reference    field="parent"    resource="test_hierarchy"    uuid="HIERARCHY1-3"/>\n                </resource>\n                <resource    name="test_hierarchy"    uuid="HIERARCHY1-3-2">\n                                <data    field="name">Type    1-3-2</data>\n                                <reference    field="parent"    resource="test_hierarchy"    uuid="HIERARCHY1-3"/>\n                </resource>\n</s3xml>\n'
    xmltree = etree.ElementTree(etree.fromstring(xmlstr))
    resource = current.s3db.resource('test_hierarchy')
    resource.import_xml(xmltree)
    db = current.db
    db.commit()
    assertTrue = self.assertTrue
    assertFalse = self.assertFalse
    assertEqual = self.assertEqual
    table = db.test_hierarchy
    try:
        rows = db(table.uuid.like('HIERARCHY1-3%')).select()
        uids = {}
        for row in rows:
            assertFalse(row.deleted)
            uids[row.uuid] = row.id
        h = S3Hierarchy('test_hierarchy')
        h.dirty('test_hierarchy')
        branches = h.children(self.uids['HIERARCHY1'])
        assertTrue((uids['HIERARCHY1-3'] in branches))
        children = h.children(uids['HIERARCHY1-3'])
        assertEqual(len(children), 2)
        success = h.delete([uids['HIERARCHY1-3']])
        assertEqual(success, 3)
        branches = h.children(self.uids['HIERARCHY1'])
        assertFalse((uids['HIERARCHY1-3'] in branches))
        nodes = h.nodes
        assertTrue(all(((uids[uid] not in nodes) for uid in uids)))
        rows = db(table.uuid.like('HIERARCHY1-3%')).select()
        for row in rows:
            assertTrue(row.deleted)
            uids[row.uuid] = row.id
    finally:
        db(table.uuid.like('HIERARCHY1-3%')).delete()
