Skip to content

Commit f5de6e9

Browse files
ceorourkeandrewshie-sentry
authored andcommitted
chore(ACI): Cleanup tests, use fixtures (#88752)
Create and use fixtures more for consistency so we're not using fixtures in some places and manually creating objects in others
1 parent cba48b3 commit f5de6e9

File tree

4 files changed

+60
-36
lines changed

4 files changed

+60
-36
lines changed

src/sentry/testutils/factories.py

+39-2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@
171171
from sentry.utils.performance_issues.performance_problem import PerformanceProblem
172172
from sentry.workflow_engine.models import (
173173
Action,
174+
ActionAlertRuleTriggerAction,
175+
AlertRuleDetector,
174176
AlertRuleWorkflow,
175177
DataCondition,
176178
DataConditionGroup,
@@ -2262,10 +2264,10 @@ def create_alert_rule_workflow(
22622264
**kwargs,
22632265
) -> AlertRuleWorkflow:
22642266
if rule_id is None and alert_rule_id is None:
2265-
raise ValueError("Either rule or alert_rule must be provided")
2267+
raise ValueError("Either rule_id or alert_rule_id must be provided")
22662268

22672269
if rule_id is not None and alert_rule_id is not None:
2268-
raise ValueError("Only one of rule or alert_rule can be provided")
2270+
raise ValueError("Only one of rule_id or alert_rule_id can be provided")
22692271

22702272
if workflow is None:
22712273
workflow = Factories.create_workflow()
@@ -2274,6 +2276,41 @@ def create_alert_rule_workflow(
22742276
alert_rule_id=alert_rule_id, rule_id=rule_id, workflow=workflow, **kwargs
22752277
)
22762278

2279+
@staticmethod
2280+
@assume_test_silo_mode(SiloMode.REGION)
2281+
def create_alert_rule_detector(
2282+
alert_rule_id: int | None = None,
2283+
rule_id: int | None = None,
2284+
detector: Detector | None = None,
2285+
**kwargs,
2286+
) -> AlertRuleDetector:
2287+
if rule_id is None and alert_rule_id is None:
2288+
raise ValueError("Either rule_id or alert_rule_id must be provided")
2289+
2290+
if rule_id is not None and alert_rule_id is not None:
2291+
raise ValueError("Only one of rule_id or alert_rule_id can be provided")
2292+
2293+
if detector is None:
2294+
detector = Factories.create_detector()
2295+
2296+
return AlertRuleDetector.objects.create(
2297+
alert_rule_id=alert_rule_id, rule_id=rule_id, detector=detector, **kwargs
2298+
)
2299+
2300+
@staticmethod
2301+
@assume_test_silo_mode(SiloMode.REGION)
2302+
def create_action_alert_rule_trigger_action(
2303+
alert_rule_trigger_action_id: int,
2304+
action: Action | None = None,
2305+
**kwargs,
2306+
) -> ActionAlertRuleTriggerAction:
2307+
if action is None:
2308+
action = Factories.create_action()
2309+
2310+
return ActionAlertRuleTriggerAction.objects.create(
2311+
action=action, alert_rule_trigger_action_id=alert_rule_trigger_action_id
2312+
)
2313+
22772314
@staticmethod
22782315
@assume_test_silo_mode(SiloMode.REGION)
22792316
def create_data_condition_group_action(

src/sentry/testutils/fixtures.py

+9
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,16 @@ def create_data_condition_group_action(self, *args, **kwargs):
668668
def create_detector_workflow(self, *args, **kwargs):
669669
return Factories.create_detector_workflow(*args, **kwargs)
670670

671+
def create_alert_rule_detector(self, *args, **kwargs):
672+
# TODO: this is only needed during the ACI migration
673+
return Factories.create_alert_rule_detector(*args, **kwargs)
674+
675+
def create_action_alert_rule_trigger_action(self, *args, **kwargs):
676+
# TODO: this is only needed during the ACI migration
677+
return Factories.create_action_alert_rule_trigger_action(*args, **kwargs)
678+
671679
def create_alert_rule_workflow(self, *args, **kwargs):
680+
# TODO: this is only needed during the ACI migration
672681
return Factories.create_alert_rule_workflow(*args, **kwargs)
673682

674683
def create_workflow_data_condition_group(self, *args, **kwargs):

src/sentry/testutils/helpers/backups.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,7 @@
114114
from sentry.users.models.userip import UserIP
115115
from sentry.users.models.userrole import UserRole, UserRoleUser
116116
from sentry.utils import json
117-
from sentry.workflow_engine.models import (
118-
Action,
119-
AlertRuleDetector,
120-
AlertRuleWorkflow,
121-
DataConditionAlertRuleTrigger,
122-
DataConditionGroup,
123-
)
117+
from sentry.workflow_engine.models import Action, DataConditionAlertRuleTrigger, DataConditionGroup
124118
from sentry.workflow_engine.models.action_group_status import ActionGroupStatus
125119

126120
__all__ = [
@@ -696,8 +690,8 @@ def create_exhaustive_organization(
696690
)
697691
detector.workflow_condition_group = detector_conditions
698692

699-
AlertRuleDetector.objects.create(detector=detector, alert_rule_id=alert.id)
700-
AlertRuleWorkflow.objects.create(workflow=workflow, alert_rule_id=alert.id)
693+
self.create_alert_rule_detector(detector=detector, alert_rule_id=alert.id)
694+
self.create_alert_rule_workflow(workflow=workflow, alert_rule_id=alert.id)
701695
ActionGroupStatus.objects.create(action=send_notification_action, group=group)
702696
DataConditionAlertRuleTrigger.objects.create(
703697
data_condition=data_condition, alert_rule_trigger_id=trigger.id

tests/sentry/workflow_engine/migration_helpers/test_migrate_alert_rule.py

+9-25
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
from sentry.integrations.pagerduty.client import PAGERDUTY_DEFAULT_SEVERITY
2222
from sentry.models.rulesnooze import RuleSnooze
2323
from sentry.notifications.models.notificationaction import ActionService, ActionTarget
24-
from sentry.silo.base import SiloMode
2524
from sentry.snuba.models import QuerySubscription
2625
from sentry.testutils.cases import APITestCase
27-
from sentry.testutils.silo import assume_test_silo_mode, assume_test_silo_mode_of
26+
from sentry.testutils.silo import assume_test_silo_mode_of
2827
from sentry.users.services.user.service import user_service
2928
from sentry.workflow_engine.migration_helpers.alert_rule import (
3029
PRIORITY_MAP,
@@ -268,8 +267,12 @@ class BaseMetricAlertMigrationTest(APITestCase, BaseWorkflowTest):
268267
def create_metric_alert_lookup_tables(
269268
self, alert_rule: AlertRule, detector: Detector, workflow: Workflow
270269
) -> tuple[AlertRuleDetector, AlertRuleWorkflow]:
271-
alert_rule_detector = self.create_alert_rule_detector(alert_rule, detector)
272-
alert_rule_workflow = self.create_alert_rule_workflow(alert_rule, workflow)
270+
alert_rule_detector = self.create_alert_rule_detector(
271+
alert_rule_id=alert_rule.id, detector=detector
272+
)
273+
alert_rule_workflow = self.create_alert_rule_workflow(
274+
alert_rule_id=alert_rule.id, workflow=workflow
275+
)
273276
return (
274277
alert_rule_detector,
275278
alert_rule_workflow,
@@ -411,31 +414,12 @@ def create_migrated_metric_alert_rule_action_objects(
411414
data_condition_group_action = self.create_data_condition_group_action(
412415
action, action_filter.condition_group
413416
)
414-
action_alert_rule_trigger_action = ActionAlertRuleTriggerAction.objects.create(
415-
action_id=action.id,
417+
action_alert_rule_trigger_action = self.create_action_alert_rule_trigger_action(
418+
action=action,
416419
alert_rule_trigger_action_id=alert_rule_trigger_action.id,
417420
)
418421
return action, data_condition_group_action, action_alert_rule_trigger_action
419422

420-
@staticmethod
421-
@assume_test_silo_mode(SiloMode.REGION)
422-
def create_alert_rule_detector(alert_rule: AlertRule, detector: Detector) -> AlertRuleDetector:
423-
return AlertRuleDetector.objects.create(alert_rule_id=alert_rule.id, detector=detector)
424-
425-
@staticmethod
426-
@assume_test_silo_mode(SiloMode.REGION)
427-
def create_alert_rule_workflow(alert_rule: AlertRule, workflow: Workflow) -> AlertRuleWorkflow:
428-
return AlertRuleWorkflow.objects.create(alert_rule_id=alert_rule.id, workflow=workflow)
429-
430-
@staticmethod
431-
@assume_test_silo_mode(SiloMode.REGION)
432-
def create_action_alert_rule_trigger_action(
433-
action: Action, alert_rule_trigger_action: AlertRuleTriggerAction
434-
) -> ActionAlertRuleTriggerAction:
435-
return ActionAlertRuleTriggerAction.objects.create(
436-
action=action, alert_rule_trigger_action_id=alert_rule_trigger_action.id
437-
)
438-
439423

440424
class DualWriteAlertRuleTest(APITestCase):
441425
def setUp(self):

0 commit comments

Comments
 (0)