Skip to content

Commit

Permalink
fix: prevent KeyError: 'title' in filter_keyvals_regex() (#123)
Browse files Browse the repository at this point in the history
* Prevent `KeyError: 'title'` from happening in `filter_keyvals_regex` function

* Add a test to ensure `KeyError: 'title'` doesn't happen
  • Loading branch information
BelKed authored Jul 7, 2023
1 parent 6f5597e commit c0bd507
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion aw_transform/filter_keyvals.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def filter_keyvals_regex(events: List[Event], key: str, regex: str) -> List[Even
r = re.compile(regex)

def predicate(event):
return bool(r.findall(event.data[key]))
return key in event.data and bool(r.findall(event.data[key]))

return [e for e in events if predicate(e)]
10 changes: 10 additions & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ def test_filter_keyval_regex():
assert len(events_re) == 2


def test_filter_keyval_regex_keyerror():
events = [
Event(data={"label": "aa"}),
Event(),
Event(data={"label": "cc"}),
]
events_re = filter_keyvals_regex(events, "label", "aa|cc")
assert len(events_re) == 2


def test_intersecting_eventpairs():
td1h = timedelta(hours=1)
now = datetime.now()
Expand Down

0 comments on commit c0bd507

Please sign in to comment.