'Test export options, JSON, all options+hierarchy'

def testExportOptionsAllOptsJSONHierarchy(self):

    assertEqual = self.assertEqual
    assertTrue = self.assertTrue
    db = current.db
    table = db.fotest_lookup_table
    represent = S3Represent(lookup='fotest_lookup_table')
    table.parent.requires = IS_EMPTY_OR(IS_ONE_OF(db, 'fotest_lookup_table.id', represent))
    table.parent.requires = represent
    s3db = current.s3db
    s3db.configure('fotest_lookup_table', hierarchy='parent')
    S3Hierarchy.dirty('fotest_lookup_table')
    options = dict(self.records)
    child_node = {'name': 'option3', 'uuid': 'OPTION3', 'parent': options.keys()[0]}
    child_id = table.insert(**child_node)
    options[child_id] = child_node
    resource = s3db.resource('fotest_table')
    result = resource.export_options(fields=['lookup'], hierarchy=True, as_json=True)
    fo = json.loads(result)
    has_empty = False
    assertTrue(isinstance(fo, dict))
    assertTrue(('option' in fo))
    assertTrue(isinstance(fo['option'], list))
    assertEqual(len(fo['option']), (len(options) + 1))
    for opt in fo['option']:
        value = opt['@value']
        if (value == ''):
            has_empty = True
            self.assertFalse(('$' in opt))
            continue
        else:
            value = int(value)
        assertTrue((value in options))
        assertEqual(opt['$'], options[value]['name'])
        if (('parent' in options[value]) and options[value]['parent']):
            assertTrue(('@parent' in opt))
            assertEqual(opt['@parent'], str(options[value]['parent']))
    assertTrue(has_empty, msg='Empty-option    missing')
