'Test URL query with BBOX filter, location context'

def testBBOXFilterLocationContext(self):

    s3db = current.s3db
    assertEqual = self.assertEqual
    assertNotEqual = self.assertNotEqual
    assertTrue = self.assertTrue
    org_office = s3db.org_office
    gis_location = s3db.gis_location
    org_organisation = s3db.org_organisation
    org_organisation_location = s3db.org_organisation_location
    context = s3db.get_config('org_office', 'context')
    s3db.configure('org_office', context={'location': 'organisation_id$organisation_location.location_id'})
    try:
        url_query = {'bbox': '119.80,12.86,122.27,15.10'}
        resource = s3db.resource('org_office', vars=url_query)
        rfilter = resource.rfilter
        (bbox, joins) = rfilter.parse_bbox_query(resource, url_query)
        expected_bbox = None
        if current.deployment_settings.get_gis_spatialdb():
            polygon = 'POLYGON((119.8    12.86,    119.8    15.1,    122.27    15.1,    122.27    12.86,    119.8    12.86))'
            try:
                expected_bbox = gis_location.the_geom.st_intersects(polygon)
            except:
                pass
        if (expected_bbox is None):
            expected_bbox = ((((gis_location.lon > 119.8) & (gis_location.lon < 122.27)) & (gis_location.lat > 12.86)) & (gis_location.lat < 15.1))
        assertEqual(bbox, expected_bbox)
        assertTrue(isinstance(joins, dict))
        assertEqual(len(joins), 3)
        subjoins = joins.get('gis_location')
        assertNotEqual(subjoins, None)
        assertEqual(len(subjoins), 1)
        expected_join = gis_location.on((org_organisation_location.location_id == gis_location.id))
        assertEqual(str(subjoins[0]), str(expected_join))
        subjoins = joins.get('org_organisation')
        assertNotEqual(subjoins, None)
        assertEqual(len(subjoins), 1)
        expected_join = org_organisation.on((org_office.organisation_id == org_organisation.id))
        assertEqual(str(subjoins[0]), str(expected_join))
        subjoins = joins.get('org_organisation_location')
        assertNotEqual(subjoins, None)
        assertEqual(len(subjoins), 1)
        expected_join = org_organisation_location.on(((org_organisation_location.organisation_id == org_organisation.id) & (org_organisation_location.deleted != True)))
        assertEqual(str(subjoins[0]), str(expected_join))
        query = rfilter.get_query()
        expected = (((org_office.deleted != True) & (org_office.id > 0)) & expected_bbox)
        assertEqual(str(query), str(expected))
    finally:
        resource.configure(context=context)
