'Test handling of failed explicit reference'

def testFailedReferenceExplicit(self):

    xmlstr = '\n<s3xml>\n                <resource    name="org_office">\n                                <data    field="name">FRTestOffice1</data>\n                                <reference    field="organisation_id">\n                                                <resource    name="org_organisation"    uuid="TROX">\n                                                                <data    field="name">FRTestOrgX</data>\n                                                </resource>\n                                </reference>\n                                <reference    field="location_id"    resource="gis_location"    tuid="FRLOCATION"/>\n                </resource>\n                <resource    name="gis_location"    tuid="FRLOCATION">\n                                <!--    Error    -->\n                                <data    field="lat">283746.285753</data>\n                                <data    field="lon">172834.334556</data>\n                </resource>\n</s3xml>'
    from lxml import etree
    tree = etree.ElementTree(etree.fromstring(xmlstr))
    s3db = current.s3db
    org = s3db.resource('org_organisation', uid='TROX')
    before = org.count()
    resource = current.s3db.resource('org_office')
    result = resource.import_xml(tree)
    msg = json.loads(result)
    self.assertEqual(msg['status'], 'failed')
    error_resources = msg['tree'].keys()
    self.assertEqual(len(error_resources), 2)
    self.assertTrue(('$_gis_location' in error_resources))
    self.assertTrue(('$_org_office' in error_resources))
    self.assertTrue(('@error' in msg['tree']['$_org_office'][0]['$k_location_id']))
    org = s3db.resource('org_organisation', uid='TROX')
    self.assertEqual(before, org.count())
    org.delete()
