Skip to content

Commit

Permalink
#6 Empty display filter raises Parser Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
bytebutcher committed Jul 6, 2023
1 parent 94a30d9 commit eb5d06d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
9 changes: 6 additions & 3 deletions pydictdisplayfilter/display_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ def _evaluate_expressions(self, expressions, item) -> bool:
raise EvaluationError(err)

def _filter_data(self, data: List, expressions: List[Union[Expression, str]]) -> List:
for item in data:
if self._evaluate_expressions(expressions, item):
yield item
if expressions:
for item in data:
if self._evaluate_expressions(expressions, item):
yield item
else:
yield from data

@property
def field_names(self) -> List[str]:
Expand Down
2 changes: 2 additions & 0 deletions pydictdisplayfilter/parsers/display_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ def parse(self, format: str) -> List[Union[Expression, str]]:
:raises ParserError, when the given display filter could not be parsed correctly.
"""
try:
if not format or not format.strip():
return []
return self._display_filter_format.parseString(format, parseAll=True).asList()
except Exception:
# This error indicates that there is something wrong with the given display filter.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# This call to setup() does all the work
setup(
name="python-dict-display-filter",
version="1.0.0",
version="1.1.0",
description="A Wireshark-like display filter for dictionaries.",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
9 changes: 9 additions & 0 deletions tests/test_dict_display_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ class TestDictDisplayFilter(unittest.TestCase):
{"value": "False"},
]

@parameterized.expand([
[None],
[''],
[' ']
])
def test_empty_display_filter_return_all_items(self, display_filter):
actual_result = list(DictDisplayFilter(self.data).filter(display_filter))
self.assertEqual(len(actual_result), len(self.data))

@parameterized.expand([
# Field existence
['name', 4],
Expand Down
8 changes: 8 additions & 0 deletions tests/test_display_filter_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class TestFilterStringParser(TestCase):
display_filter_parser = DisplayFilterParser(['address', 'port', 'protocol', 'banner'])
generic_display_filter_parser = DisplayFilterParser()

@parameterized.expand([
[None],
[''],
[' ']
])
def test_empty_query_returns_empty_list(self, display_filter):
self.assertEqual(self.display_filter_parser.parse(display_filter), [])

@parameterized.expand([
['127.0.0.1'],
['127.0.0.1/24'],
Expand Down
8 changes: 8 additions & 0 deletions tests/test_sql_display_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ def _build_insert_table_data_command():
data = tuple(table_data_row.values())
connection.execute(insert_table_data_command, data)

@parameterized.expand([
[None],
[''],
[' ']
])
def test_empty_display_filter_returns_all_items(self, display_filter):
self.assertEqual(len(list(SQLDisplayFilter(self._connection, 'data').filter(display_filter))), len(self.data))

@parameterized.expand([
# Field existence
['name', 4],
Expand Down

0 comments on commit eb5d06d

Please sign in to comment.