Skip to content

Commit 233914e

Browse files
committed
add tests for asserting number of queries
1 parent 0f22092 commit 233914e

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/sentry/workflow_engine/processors/workflow.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ def evaluate_workflows_action_filters(
133133
workflow_event_data = event_data
134134

135135
# each DataConditionGroup here has 1 WorkflowDataConditionGroup
136-
workflow_data_condition_group = action_condition.workflowdataconditiongroup_set.first()
136+
workflow_data_condition_group = (
137+
list(action_condition.workflowdataconditiongroup_set.all())[0]
138+
if action_condition.workflowdataconditiongroup_set.exists()
139+
else None
140+
)
137141

138142
# Populate the workflow_env in the event_data for the action_condition evaluation
139143
if workflow_data_condition_group:
@@ -228,7 +232,6 @@ def process_workflows(event_data: WorkflowEventData) -> set[Workflow]:
228232
enabled=True,
229233
)
230234
.select_related("when_condition_group")
231-
.prefetch_related("when_condition_group__conditions")
232235
.distinct()
233236
)
234237

tests/sentry/workflow_engine/processors/test_workflow.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,10 @@ def test_workflow_filtered_out(self):
361361

362362
def test_many_workflows(self):
363363
workflow_two, _, _, _ = self.create_detector_and_workflow(name_prefix="two")
364-
triggered_workflows = evaluate_workflow_triggers(
365-
{self.workflow, workflow_two}, self.event_data
366-
)
364+
workflows = set(Workflow.objects.all().select_related("when_condition_group").distinct())
365+
with self.assertNumQueries(2):
366+
# 1 query per workflow to fetch data conditions
367+
triggered_workflows = evaluate_workflow_triggers(workflows, self.event_data)
367368

368369
assert triggered_workflows == {self.workflow, workflow_two}
369370

@@ -535,6 +536,14 @@ def test_basic__no_filter(self):
535536
triggered_actions = evaluate_workflows_action_filters({self.workflow}, self.event_data)
536537
assert set(triggered_actions) == {self.action}
537538

539+
@patch("sentry.workflow_engine.processors.workflow.filter_recently_fired_workflow_actions")
540+
def test_basic__num_queries(self, mock_filter):
541+
# isolate the number of queries in the outer function
542+
with self.assertNumQueries(3):
543+
# 2 queries for action conditions (including subquery to prefetch)
544+
# 1 query for workflow data conditions
545+
evaluate_workflows_action_filters({self.workflow}, self.event_data)
546+
538547
def test_basic__with_filter__passes(self):
539548
self.create_data_condition(
540549
condition_group=self.action_group,

0 commit comments

Comments
 (0)