'Test S3HierarchyFilter recognition of typeof queries'

def testHierarchyFilterTypeOf(self):

    uids = self.uids
    resource = current.s3db.resource('typeof_hierarchy_reference')
    filter_widget = S3HierarchyFilter('typeof_hierarchy_id')
    ids = str(uids['HIERARCHY1-1'])
    get_vars = {'~.typeof_hierarchy_id__belongs': ids}
    variable = filter_widget.variable(resource, get_vars)
    expected = set(ids)
    values = filter_widget._values(get_vars, variable)
    self.assertEqual(values, [ids])
    ids = str(uids['HIERARCHY1-1'])
    get_vars = {'~.typeof_hierarchy_id__typeof': ids}
    variable = filter_widget.variable(resource, get_vars)
    expected = set((str(uids[uid]) for uid in ('HIERARCHY1-1', 'HIERARCHY1-1-1', 'HIERARCHY1-1-2')))
    values = filter_widget._values(get_vars, variable)
    self.assertEqual(set(values), expected)
    ids = ','.join((str(_id) for _id in (uids['HIERARCHY1-1'], uids['HIERARCHY2-1'], None)))
    get_vars = {'~.typeof_hierarchy_id__typeof': ids}
    variable = filter_widget.variable(resource, get_vars)
    expected = set((str(uids[uid]) for uid in ('HIERARCHY1-1', 'HIERARCHY1-1-1', 'HIERARCHY1-1-2', 'HIERARCHY2-1', 'HIERARCHY2-1-1', 'HIERARCHY2-1-2')))
    expected.add(None)
    values = filter_widget._values(get_vars, variable)
    self.assertEqual(set(values), expected)
    get_vars = {'~.typeof_hierarchy_id$name__typeof': 'Type    1-1'}
    variable = filter_widget.variable(resource, get_vars)
    expected = set((str(uids[uid]) for uid in ('HIERARCHY1-1', 'HIERARCHY1-1-1', 'HIERARCHY1-1-2')))
    values = filter_widget._values(get_vars, variable)
    self.assertEqual(set(values), expected)
    get_vars = {'~.typeof_hierarchy_id$name__typeof': 'Type    1-1,Type    2-1'}
    variable = filter_widget.variable(resource, get_vars)
    expected = set((str(uids[uid]) for uid in ('HIERARCHY1-1', 'HIERARCHY1-1-1', 'HIERARCHY1-1-2', 'HIERARCHY2-1', 'HIERARCHY2-1-1', 'HIERARCHY2-1-2')))
    values = filter_widget._values(get_vars, variable)
    self.assertEqual(set(values), expected)
    get_vars = {'~.typeof_hierarchy_id$name__typeof': 'Type    1-3'}
    variable = filter_widget.variable(resource, get_vars)
    expected = set()
    values = filter_widget._values(get_vars, variable)
    self.assertEqual(set(values), expected)
    get_vars = {'~.typeof_hierarchy_id$name__typeof': 'None'}
    variable = filter_widget.variable(resource, get_vars)
    expected = set()
    values = filter_widget._values(get_vars, variable)
    self.assertEqual(set(values), expected)
    ids = str(uids['HIERARCHY1-1'])
    get_vars = {'~.typeof_hierarchy_id__belongs': ids, '~.typeof_hierarchy_id$name__typeof': 'Type    1-1'}
    variable = filter_widget.variable(resource, get_vars)
    expected = set(ids)
    values = filter_widget._values(get_vars, variable)
    self.assertEqual(values, [ids])
