Example #1
0
    def test_multivaluedict_not_used_for_nonlisty_filtering(self):
        filters = Filter('field', lookups=['exact', 'in'])

        data = MultiValueDict(dict(field=['foo', 'bar']))
        filters.parse(data)
        expected = dict(field='bar')  # last one wins
        actual = dict(flatten_qobj(filters.Q))
        assert expected == actual

        data = MultiValueDict(dict(field__in=['foo', 'bar']))
        filters.parse(data)
        expected = dict(field__in=['foo', 'bar'])
        actual = dict(flatten_qobj(filters.Q))
        assert expected == actual
Example #2
0
    def test_other_lookups_ignored_when_default_used(self):
        filters = Filter('count', lookups=['lte', 'gte'], default=3)
        filters.parse({})  # no value for 'count'

        keys = dict(flatten_qobj(filters.Q)).keys()

        assert 'lte' not in keys
        assert 'gte' not in keys
Example #3
0
    def test_default_value_used_if_no_sourcedata_found(self):
        filters = Filter('count', lookups=['lte', 'gte'], default=3)
        filters.parse({})  # no value for 'count'

        expected = {'count__exact': 3}
        actual = dict(flatten_qobj(filters.Q))

        assert expected == actual
Example #4
0
 def test_many_sources_one_dest(self):
     filters = (Filter(source='nominee_1', dest='field')
                | Filter(source='nominee_2', dest='field')
                | Filter(source='nominee_3', dest='field'))
     filters.parse(dict(nominee_1='one', nominee_2='two',
                        nominee_3='three'))
     expected = dict(field='three')  # last one wins
     actual = dict(flatten_qobj(filters.Q))
     assert expected == actual
Example #5
0
 def test_one_source_to_many_dests(self):
     filters = (Filter(source='search', dest='username')
                | Filter(source='search', dest='email')
                | Filter(source='search', dest='name'))
     filters.parse(dict(search='mynbaev-scheiner'))
     expected = dict(username='******',
                     email='mynbaev-scheiner',
                     name='mynbaev-scheiner')
     actual = dict(flatten_qobj(filters.Q))
     assert expected == actual
Example #6
0
 def test_multivaluedict_as_source(self):
     """
     We don't expect multiple values to be pulled from a multi-value source
     without explicit instruction
     """
     filters = Filter('name')
     data = MultiValueDict(dict(name=['foo', 'bar']))
     filters.parse(data)
     expected = dict(name='bar')  # last one wins
     actual = dict(flatten_qobj(filters.Q))
     assert expected == actual
Example #7
0
    def test_default_ignored_if_sourcedata_found(self):
        filters = Filter('count',
                         lookups=['lte', 'gte'],
                         default=3,
                         default_lookup='foobarbaz')
        filters.parse({'count__gte': 4})

        expected = {'count__gte': 4}
        actual = dict(flatten_qobj(filters.Q))

        assert expected == actual
Example #8
0
    def test_default_lookup_type_can_be_changed(self):
        filters = Filter('count',
                         lookups=['lte', 'gte'],
                         default=3,
                         default_lookup='gt')
        filters.parse({})  # no value for 'count'

        expected = {'count__gt': 3}
        actual = dict(flatten_qobj(filters.Q))

        assert expected == actual
Example #9
0
 def test_multivaluedict_as_source_when_many_values_required(self):
     """
     Having a dest ending in '__in' /does/ constitute explicit instruction
     to pull multiple values from a source.
     """
     filters = Filter('field', lookups=['in'])
     data = MultiValueDict(dict(field=['foo', 'bar']))
     filters.parse(data)
     expected = dict(field__in=['foo', 'bar'])
     actual = dict(flatten_qobj(filters.Q))
     assert expected == actual
Example #10
0
 def test_listlike_values_cleaned_individually(self):
     f = CharFilter('fieldname', lookups='exact,in')
     data = MultiValueDict({
         'fieldname': ['single value'],  # MVDict treats 1-list as single
         'fieldname__in': ['multiple', 'values'],
     })
     expected = {
         'fieldname': 'single value',
         'fieldname__in': ['multiple', 'values']
     }
     f.parse(data)
     actual = dict(flatten_qobj(f.Q))
     assert expected == actual
Example #11
0
 def test_more_extensive_parsing(self):
     filters = (Filter('field_1', lookups=['contains', 'icontains'])
                | Filter(source='field_2', dest='field_2a')
                | Filter(source='field_3', dest='f3', lookups=['lt', 'gt'])
                | Filter(source='id', dest='relationship__spanning__id'))
     data = {
         'field_1__contains': 'first',
         'field_2': 'second',
         'field_3__lt': 'third',
         'field_3__gt': 'fourth',
         'id': 'fifth'
     }
     expected = {
         'field_1__contains': 'first',
         'field_2a': 'second',
         'f3__lt': 'third',
         'f3__gt': 'fourth',
         'relationship__spanning__id': 'fifth'
     }
     filters.parse(data)
     actual = dict(flatten_qobj(filters.Q))
     assert expected == actual
     assert filters.valid