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
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
def test_parse_simple_source(self): filter = Filter('word') data = {'word': 'a_value', 'unrelated_key': ''} filter.parse(data) assert not filter.errors assert 'word' in filter._filters assert filter._filters['word'] == 'a_value'
def test_validity_from_filtertree(self): """ FilterTree's validity should be a proxy to the validities of the filters it contains. """ filters = Filter('one') | Filter('two') | Filter('three') filters.parse(dict()) assert filters.valid
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
def test_required_filter_requires_at_least_one_key(self): """ One filter can have many possible sources due to the different lookup affixes. One of them being present should be enough to satisfy a 'required' field. """ f = Filter('fieldname', required=True, lookups=['contains']) data = {'fieldname__gte': 'foo', 'fieldname__lte': 'bar'} f.parse(data) assert f.errors assert 'fieldname' in f.errors
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
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
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
def test_parse_source_with_lookups(self): filter = Filter('word', lookups=['gte', 'gt', 'lte', 'lt']) data = {'word__gte': 1, 'word__gt': 2, 'word__lte': 3, 'word__lt': 4} filter.parse(data) assert not filter.errors for key in data: assert key in filter._filters for key, value in data.items(): assert filter._filters[key] == value assert filter.valid
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
def test_required_filter_is_satisfied_by_one_key_being_present(self): f = Filter('fieldname', required=True, lookups=['gte']) data = {'fieldname__gte': 'foo', 'fieldname__lte': 'bar'} f.parse(data) assert not f.errors