'Test master resource filter construction'

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

    s3db = current.s3db
    assertEqual = self.assertEqual
    assertNotEqual = self.assertNotEqual
    assertTrue = self.assertTrue
    project_project = s3db.project_project
    org_organisation = s3db.org_organisation
    project_task_project = s3db.project_task_project
    project_task = s3db.project_task
    q = ((FS('organisation_id$name') == 'test') & (FS('task.description') == 'test'))
    resource = current.s3db.resource('project_project', filter=q)
    rfilter = resource.rfilter
    assertNotEqual(rfilter, None)
    expected = ((project_project.deleted != True) & (project_project.id > 0))
    assertEqual(str(rfilter.mquery), str(expected))
    expected = (((project_project.deleted != True) & (project_project.id > 0)) & ((org_organisation.name == 'test') & (project_task.description == 'test')))
    query = rfilter.get_query()
    assertEqual(str(query), str(expected))
    join = rfilter.get_joins(left=False, as_list=False)
    assertEqual(join, {})
    expected = org_organisation.on((project_project.organisation_id == org_organisation.id))
    expected_l = project_task_project.on(((project_task_project.project_id == project_project.id) & (project_task_project.deleted != True)))
    expected_r = project_task.on((project_task_project.task_id == project_task.id))
    left = rfilter.get_joins(left=True, as_list=False)
    assertEqual(left.keys(), ['org_organisation', 'project_task'])
    assertTrue(isinstance(left['org_organisation'], list))
    assertEqual(len(left['org_organisation']), 1)
    assertEqual(str(left['org_organisation'][0]), str(expected))
    assertTrue(isinstance(left['project_task'], list))
    assertEqual(len(left['project_task']), 2)
    assertEqual(str(left['project_task'][0]), str(expected_l))
    assertEqual(str(left['project_task'][1]), str(expected_r))
