@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")

        # Define a filtered component
        s3db.add_components("org_organisation",
                            org_office = {"name": "test",
                                          "joinby": "organisation_id",
                                          "filterby": "office_type_id",
                                          "filterfor": 5,
                                         },
                           )

        # Define the resource
        resource = s3db.resource("org_organisation")

        # Translate a URL query into a DAL query, check that
        # the correct table alias is used
        query = S3URLQuery.parse(resource, {"test.name__like": "xyz*"})
        assertEqual(str(query.test[0].query(resource)),
                    str(org_test_office.name.lower().like("xyz%")))

        # Add the query to the resource
        auth.override = True
        resource.add_filter(query.test[0])
        rfilter = resource.rfilter

        # Check that the aliased table is properly joined
        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))

        # ...and the effective query of the master contains the filter
        # and is using the correct alias
        expected = (((org_organisation.deleted != True) &
                     (org_organisation.id > 0)) &
                    (org_test_office.name.lower().like("xyz%")))
        assertEqual(str(resource.get_query()), str(expected))

        # Check the query of the component
        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))

        # Remove the component hook
        del current.model.components["org_organisation"]["test"]

        auth.override = False

    # -------------------------------------------------------------------------
Test resolution of URL queries for fields in filtered components
dataset/ETHPy150Open sahana/eden/modules/unit_tests/s3/s3resource.py/ResourceFilteredComponentTests.testURLQueryWithFilteredComponent