Skip to content

Commit

Permalink
Enable removal of signal filters from signal definition. (#4885)
Browse files Browse the repository at this point in the history
* Enable removal of signal filters from signal definition.

* Simplifies check.

* Fixes C401 lint error
  • Loading branch information
metroid-samus committed Sep 25, 2024
1 parent d233a6d commit f6aada8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/dispatch/signal/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,11 @@ def update(*, db_session: Session, signal: Signal, signal_in: SignalUpdate) -> S

signal.engagements = engagements

if signal_in.filters:
is_filters_updated = {filter.id for filter in signal.filters} != {
filter.id for filter in signal_in.filters
}

if is_filters_updated:
filters = []
for f in signal_in.filters:
signal_filter = get_signal_filter_by_name_or_raise(
Expand Down Expand Up @@ -605,7 +609,6 @@ def create_instance(
)
signal_instance.case_type = case_type


db_session.add(signal_instance)
db_session.commit()
return signal_instance
Expand Down
48 changes: 48 additions & 0 deletions tests/signal/test_signal_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,54 @@ def test_update(session, project, signal):
assert signal.name == name


def test_update__add_filter(session, signal, signal_filter):
from dispatch.signal.models import SignalUpdate, SignalFilterRead
from dispatch.signal.service import update

signal_filter.project = signal.project

signal_in = SignalUpdate(
id=signal.id,
name=signal.name,
project=signal.project,
owner="example.com",
external_id="foo",
filters=[SignalFilterRead.from_orm(signal_filter)],
)
signal = update(
db_session=session,
signal=signal,
signal_in=signal_in,
)
assert len(signal.filters) == 1


def test_update__delete_filter(session, signal, signal_filter):
from dispatch.signal.models import SignalUpdate
from dispatch.signal.service import update

# Set up conditions to delete a signal filter.
signal_filter.project = signal.project
signal.filters.append(signal_filter)

assert len(signal.filters) == 1

signal_in = SignalUpdate(
id=signal.id,
name=signal.name,
project=signal.project,
owner="example.com",
external_id="foo",
filters=[],
)
signal = update(
db_session=session,
signal=signal,
signal_in=signal_in,
)
assert len(signal.filters) == 0


def test_delete(session, signal):
from dispatch.signal.service import delete, get

Expand Down

0 comments on commit f6aada8

Please sign in to comment.