Skip to content

Commit 62fc6a1

Browse files
committed
add some tests and make sure things are working
1 parent 5495d63 commit 62fc6a1

File tree

2 files changed

+81
-10
lines changed

2 files changed

+81
-10
lines changed

src/sentry/workflow_engine/tasks.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
from typing import TYPE_CHECKING
2-
31
from sentry.issues.status_change_consumer import group_status_update_registry
2+
from sentry.issues.status_change_message import StatusChangeMessageData
3+
from sentry.models.activity import Activity
4+
from sentry.models.group import Group
45
from sentry.silo.base import SiloMode
56
from sentry.tasks.base import instrumented_task
67
from sentry.taskworker import config, namespaces, retry
8+
from sentry.types.activity import ActivityType
79
from sentry.utils import metrics
810

9-
if TYPE_CHECKING:
10-
from sentry.issues.status_change_message import StatusChangeMessageData
11-
from sentry.models.activity import Activity
12-
from sentry.models.group import Group
13-
from sentry.workflow_engine.models import Detector
11+
SUPPORTED_ACTIVITIES = [ActivityType.SET_RESOLVED]
1412

1513

1614
@instrumented_task(
@@ -30,7 +28,7 @@
3028
),
3129
),
3230
)
33-
def process_workflow_task(activity_id: Activity.id, detector_id: Detector.id) -> None:
31+
def process_workflow_task(activity_id: int, detector_id: int) -> None:
3432
"""
3533
Process a workflow task identified by the given Activity ID and Detector ID.
3634
@@ -56,6 +54,10 @@ def workflow_status_update_handler(
5654
Since this handler is called in process for the activity, we want
5755
to queue a task to process workflows asynchronously.
5856
"""
57+
if activity.type not in SUPPORTED_ACTIVITIES:
58+
# If the activity type is not supported, we do not need to process it.
59+
return
60+
5961
detector_id = status_change_message.get("detector_id")
6062

6163
if detector_id is None:
@@ -65,8 +67,7 @@ def workflow_status_update_handler(
6567
return
6668

6769
# TODO - implement in follow-up PR for now, just track a metric that we are seeing the activities.
70+
# process_workflow_task.delay(activity.id, detector_id)
6871
metrics.incr(
6972
"workflow_engine.process_workflow.activity_update", tags={"activity_type": activity.type}
7073
)
71-
# TODO - should this also set the group id so we can set it on WorkflowEventData at the top level? :thinking:
72-
# process_workflow_task.delay(activity.id, detector_id)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from unittest import mock
2+
3+
from sentry.issues.status_change_consumer import update_status
4+
from sentry.issues.status_change_message import StatusChangeMessageData
5+
from sentry.models.group import GroupStatus
6+
from sentry.testutils.cases import TestCase
7+
from sentry.types.activity import ActivityType
8+
from sentry.types.group import GroupSubStatus
9+
from sentry.workflow_engine.tasks import workflow_status_update_handler
10+
11+
12+
class IssuePlatformIntegrationTests(TestCase):
13+
def test_handler_invoked__when_resolved(self):
14+
"""
15+
Integration test to ensure the `update_status` method
16+
will correctly invoke the `workflow_state_update_handler`
17+
and increment the metric.
18+
"""
19+
detector = self.create_detector()
20+
group = self.create_group(
21+
project=self.project,
22+
status=GroupStatus.UNRESOLVED,
23+
substatus=GroupSubStatus.ESCALATING,
24+
)
25+
26+
message = StatusChangeMessageData(
27+
id="test_message_id",
28+
project_id=self.project.id,
29+
new_status=GroupStatus.RESOLVED,
30+
new_substatus=None,
31+
fingerprint=["test_fingerprint"],
32+
detector_id=detector.id,
33+
)
34+
35+
with mock.patch("sentry.workflow_engine.tasks.metrics.incr") as mock_incr:
36+
update_status(group, message)
37+
mock_incr.assert_called_with(
38+
"workflow_engine.process_workflow.activity_update",
39+
tags={"activity_type": ActivityType.SET_RESOLVED.value},
40+
)
41+
42+
43+
def WorkflowStatusUpdateHandlerTests(TestCase):
44+
def test__no_detector_id(self):
45+
"""
46+
Test that the workflow_status_update_handler does not crash
47+
when no detector_id is provided in the status change message.
48+
"""
49+
group = self.create_group(project=self.project)
50+
activity = self.create_activity(
51+
group=group,
52+
type=ActivityType.SET_RESOLVED,
53+
data={"fingerprint": ["test_fingerprint"]},
54+
)
55+
56+
message = StatusChangeMessageData(
57+
id="test_message_id",
58+
project_id=self.project.id,
59+
new_status=GroupStatus.RESOLVED,
60+
new_substatus=None,
61+
fingerprint=["test_fingerprint"],
62+
detector_id=None, # No detector_id provided
63+
)
64+
65+
with mock.patch("sentry.workflow_engine.tasks.metrics.incr") as mock_incr:
66+
workflow_status_update_handler(group, message, activity)
67+
mock_incr.assert_called_with(
68+
"workflow_engine.error.tasks.no_detector_id",
69+
tags={"activity_type": ActivityType.SET_RESOLVED.value},
70+
)

0 commit comments

Comments
 (0)