From f46a1110fbdf66af3545c657b889b632b824570c Mon Sep 17 00:00:00 2001 From: bytebutcher Date: Tue, 4 Jul 2023 11:51:56 +0200 Subject: [PATCH] #5 Allow to unset functions in DisplayFilter --- pydictdisplayfilter/display_filters.py | 11 +++--- tests/test_dict_display_filter.py | 52 ++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/pydictdisplayfilter/display_filters.py b/pydictdisplayfilter/display_filters.py index f8945c3..e498aa2 100644 --- a/pydictdisplayfilter/display_filters.py +++ b/pydictdisplayfilter/display_filters.py @@ -45,16 +45,15 @@ def __init__(self, :param evaluator: The evaluator used to evaluate the expressions. If no evaluator is specified the DefaultEvaluator is used. """ - functions = functions if functions else { + self._slicer_factory = SlicerFactory(slicers) + self._evaluator = evaluator if evaluator else DefaultEvaluator() + self._functions = functions if functions is not None else { "len": lambda value: len(value), "lower": lambda value: value.lower(), "upper": lambda value: value.upper() } - self._slicer_factory = SlicerFactory(slicers) - self._evaluator = evaluator if evaluator else DefaultEvaluator() - self._functions = functions or [] - self._field_names = field_names or [] - self._display_filter_parser = DisplayFilterParser(field_names=field_names, functions=functions) + self._field_names = field_names if field_names is not None else [] + self._display_filter_parser = DisplayFilterParser(field_names=self._field_names, functions=self._functions) def _get_item_value(self, expression, item) -> str: """ diff --git a/tests/test_dict_display_filter.py b/tests/test_dict_display_filter.py index 121632b..092f18b 100644 --- a/tests/test_dict_display_filter.py +++ b/tests/test_dict_display_filter.py @@ -19,6 +19,7 @@ from parameterized import parameterized from pydictdisplayfilter.display_filters import DictDisplayFilter +from pydictdisplayfilter.exceptions import ParserError class TestDictDisplayFilter(unittest.TestCase): @@ -266,3 +267,54 @@ def test_net_dict_display_filter_returns_correct_number_of_items(self, display_f def test_mixed_data_display_filter_returns_correct_number_of_items(self, display_filter, no_items): items = list(DictDisplayFilter(self.mixed_data).filter(display_filter)) self.assertEqual(len(items), no_items) + + @parameterized.expand([ + # Fields + ["lower(value) == foobar", 2], + ["upper(value) == FOOBAR", 2], + ["upper(value) ~= FOO", 3], + ["lower(value) ~= foo", 3], + ["len(value) == 6", 2], + ["len(value) == 3", 2], + ]) + def test_functions_default(self, display_filter, no_items): + data = [{'value': 'foobar'}, {'value': 'FOOBAR'}, {'value': 'FOO'}, {'value': 'BAR'}] + items = list(DictDisplayFilter(data).filter(display_filter)) + self.assertEqual(len(items), no_items) + + @parameterized.expand([ + # Fields + ["ltrim(value) == foobar"], + ["rtrim(value) == foobar"], + ["trim(value) == foobar"], + ]) + def test_functions_undefined_raises_parser_error(self, display_filter): + data = [{'value': 'foobar'}, {'value': 'FOOBAR'}, {'value': 'FOO'}, {'value': 'BAR'}] + self.assertRaises(ParserError, lambda: list(DictDisplayFilter(data).filter(display_filter))) + + @parameterized.expand([ + # Fields + ["value == foobar", 0], + ["ltrim(value) == foobar", 1], + ["rtrim(value) == foobar", 1], + ["trim(value) == foobar", 3] + ]) + def test_functions_custom(self, display_filter, no_items): + data = [{'value': ' foobar'}, {'value': ' foobar '}, {'value': 'foobar '}] + functions = { + 'ltrim': lambda v: v.lstrip(), + 'rtrim': lambda v: v.rstrip(), + 'trim': lambda v: v.strip() + } + items = list(DictDisplayFilter(data, functions=functions).filter(display_filter)) + self.assertEqual(len(items), no_items) + + @parameterized.expand([ + # Fields + ["lower(value) == foobar"], + ["upper(value) == FOOBAR"], + ["len(value) == 6"], + ]) + def test_functions_none(self, display_filter): + data = [{'value': 'foobar'}, {'value': 'FOOBAR'}, {'value': 'FOO'}, {'value': 'BAR'}] + self.assertRaises(ParserError, lambda: list(DictDisplayFilter(data, functions={}).filter(display_filter)))