'Queries for fields in multiple tables use multiple joins'

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

    s3db = current.s3db
    assertEqual = self.assertEqual
    assertTrue = self.assertTrue
    resource = s3db.resource('project_project')
    q = ((FS('organisation_id$name') == 'test') & (FS('task.description') == 'test'))
    (joins, distinct) = q._joins(resource)
    assertEqual(joins.keys(), [])
    project_project = resource.table
    project_task_project = s3db.project_task_project
    project_task = s3db.project_task
    org_organisation = s3db.org_organisation
    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))
    (joins, distinct) = q._joins(resource, left=True)
    assertEqual(joins.keys(), ['org_organisation', 'project_task'])
    assertTrue(isinstance(joins['org_organisation'], list))
    assertEqual(len(joins['org_organisation']), 1)
    assertEqual(str(joins['org_organisation'][0]), str(expected))
    assertTrue(isinstance(joins['project_task'], list))
    assertEqual(len(joins['project_task']), 2)
    assertEqual(str(joins['project_task'][0]), str(expected_l))
    assertEqual(str(joins['project_task'][1]), str(expected_r))
    assertTrue(distinct)
