'Test Representation of reference, multiple values,
with linkto'

def testRepresentReferenceMultipleLinktoOn(self):

    s3db = current.s3db
    tablename = self.tablename
    fname = ('%s.location_id' % tablename)
    ftable = current.db[tablename]
    renderer = S3Represent(lookup='gis_location', show_link=True)
    ftable.location_id.represent = renderer
    resource = s3db.resource('org_organisation', id=self.org.id)
    data = resource.select(['id', 'name', 'test.location_id'], limit=None, represent=True)
    result = data['rows']
    self.assertEqual(renderer.queries, 1)
    self.assertTrue(len(result), 1)
    output = result[0]
    self.assertTrue((fname in output))
    names = output[fname]
    self.assertTrue(isinstance(names, DIV))
    from lxml import etree
    tree = etree.fromstring(('<div>%s</div>' % names))
    links = tree.findall('a')
    self.assertEqual(len(links), 2)
    appname = current.request.application
    a = (lambda location: (location.name, ('/%s/gis/location/%s' % (appname, location.id))))
    types = dict((a(location) for location in self.locations))
    for link in links:
        name = link.text
        self.assertTrue((name in types))
        self.assertEqual(link.get('href', None), types[name])
