'Test XML Export with Sync Filters'

def testExportXMLWithSyncFilters(self):

    assertEqual = self.assertEqual
    assertTrue = self.assertTrue
    assertFalse = self.assertFalse
    auth = current.auth
    s3db = current.s3db
    auth.override = True
    xmlstr = '\n<s3xml>\n                <resource    name="org_office_type"    uuid="SFT1">\n                                <data    field="name">SFT1</data>\n                </resource>\n                <resource    name="org_office_type"    uuid="SFT2">\n                                <data    field="name">SFT2</data>\n                </resource>\n                <resource    name="org_organisation"    uuid="SFO1">\n                                <data    field="name">Sync1FilterOrganisation</data>\n                                <resource    name="org_office"    uuid="S1FO1">\n                                                <data    field="name">Sync1FilterOffice1</data>\n                                                <reference    field="office_type_id"    resource="org_office_type"    uuid="SFT1"/>\n                                </resource>\n                </resource>\n                <resource    name="org_organisation"    uuid="SFO2">\n                                <data    field="name">Sync2FilterOrganisation</data>\n                                <resource    name="org_office"    uuid="S1FO2">\n                                                <data    field="name">Sync1FilterOffice2</data>\n                                                <reference    field="office_type_id"    resource="org_office_type"    uuid="SFT2"/>\n                                </resource>\n                </resource>\n                <resource    name="org_organisation"    uuid="SFO3">\n                                <data    field="name">Sync3FilterOrganisation</data>\n                                <resource    name="org_office"    uuid="S1FO3">\n                                                <data    field="name">Sync1FilterOffice3</data>\n                                </resource>\n                                <resource    name="org_office"    uuid="S2FO1">\n                                                <data    field="name">Sync2FilterOffice1</data>\n                                </resource>\n                </resource>\n</s3xml>'
    try:
        xmltree = etree.ElementTree(etree.fromstring(xmlstr))
        resource = current.s3db.resource('org_organisation')
        resource.import_xml(xmltree)
        resource = s3db.resource(resource, uid=['SFO1', 'SFO2', 'SFO3'])
        filters = {'org_organisation': {'organisation.name__like': 'Sync1*'}}
        xmlexport = resource.export_xml(filters=filters, mcomponents=['office'], dereference=False)
        xmltree = etree.ElementTree(etree.fromstring(xmlexport))
        orgs = xmltree.xpath("resource[@name='org_organisation']")
        assertEqual(len(orgs), 1)
        assertEqual(orgs[0].get('uuid'), 'SFO1')
        offices = xmltree.xpath("//resource[@name='org_office']")
        assertEqual(len(offices), 1)
        assertEqual(offices[0].get('uuid'), 'S1FO1')
        resource = s3db.resource(resource, uid=['SFO1', 'SFO2', 'SFO3'])
        filters = {'org_organisation': {'office.name__like': 'Sync2*'}}
        xmlexport = resource.export_xml(filters=filters, mcomponents=['office'], dereference=False)
        xmltree = etree.ElementTree(etree.fromstring(xmlexport))
        orgs = xmltree.xpath("resource[@name='org_organisation']")
        assertEqual(len(orgs), 1)
        assertEqual(orgs[0].get('uuid'), 'SFO3')
        offices = xmltree.xpath("//resource[@name='org_office']")
        assertEqual(len(offices), 1)
        assertEqual(offices[0].get('uuid'), 'S2FO1')
        resource = s3db.resource(resource, uid=['SFO1', 'SFO2', 'SFO3'])
        filters = {'org_office': {'office.name__like': 'Sync1*'}}
        xmlexport = resource.export_xml(filters=filters, mcomponents=['office'], dereference=False)
        xmltree = etree.ElementTree(etree.fromstring(xmlexport))
        orgs = xmltree.xpath("resource[@name='org_organisation']")
        assertEqual(len(orgs), 3)
        uids = [org.get('uuid') for org in orgs]
        assertTrue(('SFO1' in uids))
        assertTrue(('SFO2' in uids))
        assertTrue(('SFO3' in uids))
        offices = xmltree.xpath("//resource[@name='org_office']")
        assertEqual(len(offices), 3)
        uids = [office.get('uuid') for office in offices]
        assertTrue(('S1FO1' in uids))
        assertTrue(('S1FO2' in uids))
        assertTrue(('S1FO3' in uids))
        assertFalse(('S2FO1' in uids))
        resource = s3db.resource(resource, uid=['SFO1', 'SFO2'])
        xmlexport = resource.export_xml(filters=None, mcomponents=['office'])
        xmltree = etree.ElementTree(etree.fromstring(xmlexport))
        types = xmltree.xpath("resource[@name='org_office_type']")
        assertEqual(len(types), 2)
        uids = [t.get('uuid') for t in types]
        assertTrue(('SFT1' in uids))
        assertTrue(('SFT2' in uids))
        resource = s3db.resource(resource, uid=['SFO1', 'SFO2'])
        filters = {'org_office_type': {'office_type.name__like': 'SFT1*'}}
        xmlexport = resource.export_xml(filters=filters, mcomponents=['office'])
        xmltree = etree.ElementTree(etree.fromstring(xmlexport))
        types = xmltree.xpath("resource[@name='org_office_type']")
        assertEqual(len(types), 1)
        assertEqual(types[0].get('uuid'), 'SFT1')
    finally:
        current.db.rollback()
        auth.override = False
