From f6aada89b57e24caa87b31c69ca95ec3348fd6b8 Mon Sep 17 00:00:00 2001 From: Avery Date: Wed, 26 Jun 2024 14:39:27 -0700 Subject: [PATCH] Enable removal of signal filters from signal definition. (#4885) * Enable removal of signal filters from signal definition. * Simplifies check. * Fixes C401 lint error --- src/dispatch/signal/service.py | 7 +++-- tests/signal/test_signal_service.py | 48 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/dispatch/signal/service.py b/src/dispatch/signal/service.py index 755e567df12e..af516ef16e89 100644 --- a/src/dispatch/signal/service.py +++ b/src/dispatch/signal/service.py @@ -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( @@ -605,7 +609,6 @@ def create_instance( ) signal_instance.case_type = case_type - db_session.add(signal_instance) db_session.commit() return signal_instance diff --git a/tests/signal/test_signal_service.py b/tests/signal/test_signal_service.py index d3184e5fd73e..b1e301d42d07 100644 --- a/tests/signal/test_signal_service.py +++ b/tests/signal/test_signal_service.py @@ -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