'Test resolution of URL queries for fields in filtered components'

@unittest.skipIf((not current.deployment_settings.has_module('org')), 'org    module    disabled')
def testURLQueryWithFilteredComponent(self):

    assertEqual = self.assertEqual
    auth = current.auth
    s3db = current.s3db
    org_organisation = s3db.org_organisation
    org_test_office = s3db.org_office.with_alias('org_test_office')
    s3db.add_components('org_organisation', org_office={'name': 'test', 'joinby': 'organisation_id', 'filterby': {'office_type_id': 5}})
    resource = s3db.resource('org_organisation')
    query = S3URLQuery.parse(resource, {'test.name__like': 'xyz*'})
    assertEqual(str(query.test[0].query(resource)), str(org_test_office.name.lower().like('xyz%')))
    auth.override = True
    resource.add_filter(query.test[0])
    rfilter = resource.rfilter
    expected = org_test_office.on((((org_test_office.organisation_id == org_organisation.id) & (org_test_office.deleted != True)) & (org_test_office.office_type_id == 5)))
    assertEqual(str(rfilter.get_joins(left=True)[0]), str(expected))
    expected = (((org_organisation.deleted != True) & (org_organisation.id > 0)) & org_test_office.name.lower().like('xyz%'))
    assertEqual(str(resource.get_query()), str(expected))
    component = resource.components['test']
    expected = (((org_test_office.deleted != True) & (org_test_office.id > 0)) & (((org_organisation.deleted != True) & (org_organisation.id > 0)) & org_test_office.name.lower().like('xyz%')))
    assertEqual(str(component.get_query()), str(expected))
    rfilter = component.rfilter
    expected = org_organisation.on(((org_test_office.organisation_id == org_organisation.id) & (org_test_office.office_type_id == 5)))
    assertEqual(str(rfilter.get_joins(left=True)[0]), str(expected))
    del current.model.components['org_organisation']['test']
    auth.override = False
