def test_should_be_representable_as_a_dict(self): filter_instance = Filter(tokens=['test_field', '>', 42]) self.assertDictEqual(filter_instance.as_dict(), { 'field': 'test_field', 'operator': { 'raw': '>' }, 'condition': 42 })
def test_should_fail_filtering_entries_when_invalid_field_is_provided( self): spec = {'test_field': int, 'other_field': int} filter_instance = Filter(tokens=['missing-field', '>', '42']) with self.assertRaises(Abort): self.assertTrue( filter_instance.apply(entry={ 'test_field': 50, 'other_field': 40 }, spec=spec))
def test_should_be_representable_as_a_string(self): group = FilterGroup( left=Filter(tokens=['test_field', '>', 42]), operator='or', right=Filter(tokens=['other_field', 'like', 'test'])) self.assertEqual( str(group), 'FilterGroup(left=[{}], operator=[{}], right=[{}]'.format( 'Filter(field=[test_field], operator=[FilterBinaryOperator(raw=[>])], condition=[42])', 'or', 'Filter(field=[other_field], operator=[FilterBinaryOperator(raw=[like])], condition=[test])' ))
def test_should_filter_entries(self): spec = {'test_field': int, 'other_field': int} filter_instance = Filter(tokens=['test_field', '>', '42']) self.assertTrue( filter_instance.apply(entry={ 'test_field': 50, 'other_field': 40 }, spec=spec)) self.assertFalse( filter_instance.apply(entry={ 'test_field': 40, 'other_field': 40 }, spec=spec)) spec = {'test_field': str, 'other_field': int} filter_instance = Filter(tokens=['test_field', 'like', '-b-']) self.assertTrue( filter_instance.apply(entry={ 'test_field': 'a-b-c', 'other_field': 40 }, spec=spec)) self.assertFalse( filter_instance.apply(entry={ 'test_field': 'other', 'other_field': 40 }, spec=spec))
def test_should_fail_to_filter_entries_when_invalid_group_operator_is_provided( self): spec = {'test_field': int, 'other_field': str} group = FilterGroup( left=Filter(tokens=['test_field', '>', 42]), operator='?', right=Filter(tokens=['other_field', 'like', 'test'])) with self.assertRaises(Abort): group.apply(entry={ 'test_field': 50, 'other_field': 'some-test-value' }, spec=spec)
def test_should_be_representable_as_a_dict(self): group = FilterGroup( left=Filter(tokens=['test_field', '>', 42]), operator='or', right=Filter(tokens=['other_field', 'like', 'test'])) self.assertDictEqual( group.as_dict(), { 'left': { 'field': 'test_field', 'operator': { 'raw': '>' }, 'condition': 42 }, 'operator': 'or', 'right': { 'field': 'other_field', 'operator': { 'raw': 'like' }, 'condition': 'test' } })
def test_should_filter_entries(self): spec = {'test_field': int, 'other_field': str} or_group = FilterGroup( left=Filter(tokens=['test_field', '>', 42]), operator='or', right=Filter(tokens=['other_field', 'like', 'test'])) self.assertTrue( or_group.apply(entry={ 'test_field': 50, 'other_field': 'some-test-value' }, spec=spec)) self.assertTrue( or_group.apply(entry={ 'test_field': 50, 'other_field': 'some-value' }, spec=spec)) self.assertTrue( or_group.apply(entry={ 'test_field': 40, 'other_field': 'some-test-value' }, spec=spec)) self.assertFalse( or_group.apply(entry={ 'test_field': 40, 'other_field': 'some-value' }, spec=spec)) and_group = FilterGroup( left=Filter(tokens=['test_field', '>', 42]), operator='and', right=Filter(tokens=['other_field', 'like', 'test'])) self.assertTrue( or_group.apply(entry={ 'test_field': 50, 'other_field': 'some-test-value' }, spec=spec)) self.assertFalse( and_group.apply(entry={ 'test_field': 50, 'other_field': 'some-value' }, spec=spec)) self.assertFalse( and_group.apply(entry={ 'test_field': 40, 'other_field': 'some-test-value' }, spec=spec)) self.assertFalse( and_group.apply(entry={ 'test_field': 40, 'other_field': 'some-value' }, spec=spec))
def test_should_be_representable_as_a_string(self): filter_instance = Filter(tokens=['test_field', '>', 42]) self.assertEqual( str(filter_instance), 'Filter(field=[test_field], operator=[FilterBinaryOperator(raw=[>])], condition=[42])' )