'Referenced table field queries use left joins + distinct'

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

    s3db = current.s3db
    assertEqual = self.assertEqual
    assertTrue = self.assertTrue
    resource = s3db.resource('project_project')
    q = (FS('organisation_id$name') == 'test')
    (joins, distinct) = q._joins(resource)
    assertEqual(joins, {})
    assertTrue(distinct)
    project_project = resource.table
    org_organisation = s3db.org_organisation
    expected = org_organisation.on((project_project.organisation_id == org_organisation.id))
    (joins, distinct) = q._joins(resource, left=True)
    assertTrue(isinstance(joins, dict))
    assertEqual(joins.keys(), ['org_organisation'])
    assertTrue(isinstance(joins['org_organisation'], list))
    assertEqual(len(joins['org_organisation']), 1)
    assertEqual(str(joins['org_organisation'][0]), str(expected))
    assertTrue(distinct)
