def test_filter_with_filterables_of_differing_attributes_fails_if_setting_enabled(self): """Test filtering with filterables of differing attributes raises an error if any filterables lack the filtered-for attribute when `ignore_items_without_attribute` is False. """ filter_set = FilterSet({FilterableThing(a=3), FilterableThing(b=90), FilterableThing(a=77)}) for kwarg in {"a__gt": 2}, {"b__equals": 90}, {"b__equals": 0}: with self.assertRaises(AttributeError): filter_set.filter(**kwarg, ignore_items_without_attribute=False)
def test_filter_with_filterables_of_differing_attributes(self): """Test filtering with filterables of differing attributes ignores the filterables lacking the filtered-for attribute. """ filterables = {FilterableThing(a=3), FilterableThing(b=90), FilterableThing(a=77)} filter_set = FilterSet(filterables) self.assertEqual(filter_set.filter(a__gt=2), {FilterableThing(a=3), FilterableThing(a=77)}) self.assertEqual(filter_set.filter(b__equals=90), {FilterableThing(b=90)}) self.assertEqual(filter_set.filter(b__equals=0), set())
def test_error_raised_if_any_items_are_not_filterable(self): """Test that an error is raised if any items in the FilterSet are not of type Filterable.""" filter_set = FilterSet([1, 2, 3]) with self.assertRaises(TypeError): filter_set.filter(a__equals=2)