|
1 | | -from datetime import datetime |
| 1 | +from datetime import UTC, datetime |
2 | 2 | from uuid import uuid4 |
3 | 3 |
|
4 | 4 | from sentry.eventstore.models import Event, GroupEvent |
5 | | -from sentry.issues.grouptype import ErrorGroupType |
| 5 | +from sentry.incidents.grouptype import MetricAlertFire |
| 6 | +from sentry.incidents.utils.types import QuerySubscriptionUpdate |
6 | 7 | from sentry.models.group import Group |
7 | 8 | from sentry.snuba.models import SnubaQuery |
8 | 9 | from sentry.testutils.cases import TestCase |
9 | 10 | from sentry.testutils.factories import EventType |
10 | 11 | from sentry.workflow_engine.models import ( |
11 | 12 | Action, |
12 | 13 | DataConditionGroup, |
| 14 | + DataPacket, |
| 15 | + DataSource, |
13 | 16 | Detector, |
14 | 17 | DetectorWorkflow, |
15 | 18 | Workflow, |
16 | 19 | ) |
17 | 20 | from sentry.workflow_engine.models.data_condition import Condition |
| 21 | +from sentry.workflow_engine.types import DetectorPriorityLevel |
18 | 22 | from tests.sentry.issues.test_utils import OccurrenceTestMixin |
19 | 23 |
|
20 | 24 |
|
@@ -66,9 +70,13 @@ def create_detector_and_workflow( |
66 | 70 | self, |
67 | 71 | name_prefix="test", |
68 | 72 | workflow_triggers: DataConditionGroup | None = None, |
69 | | - detector_type: str = ErrorGroupType.slug, |
| 73 | + detector_type: str = MetricAlertFire.slug, |
70 | 74 | **kwargs, |
71 | 75 | ) -> tuple[Workflow, Detector, DetectorWorkflow, DataConditionGroup]: |
| 76 | + """ |
| 77 | + Create a Worfkllow, Detector, DetectorWorkflow, and DataConditionGroup for testing. |
| 78 | + These models are configured to work together to test the workflow engine. |
| 79 | + """ |
72 | 80 | workflow_triggers = workflow_triggers or self.create_data_condition_group() |
73 | 81 |
|
74 | 82 | if not workflow_triggers.conditions.exists(): |
@@ -100,6 +108,46 @@ def create_detector_and_workflow( |
100 | 108 |
|
101 | 109 | return workflow, detector, detector_workflow, workflow_triggers |
102 | 110 |
|
| 111 | + def create_test_query_data_source(self, detector) -> tuple[DataSource, DataPacket]: |
| 112 | + """ |
| 113 | + Create a DataSource and DataPacket for testing; this will create a fake QuerySubscriptionUpdate and link it to a data_source. |
| 114 | +
|
| 115 | + A detector is required to create this test data, so we can ensure that the detector |
| 116 | + has a condition to evaluate for the data_packet that evalutes to true. |
| 117 | + """ |
| 118 | + subscription_update: QuerySubscriptionUpdate = { |
| 119 | + "subscription_id": "123", |
| 120 | + "values": {"foo": 1}, |
| 121 | + "timestamp": datetime.now(UTC), |
| 122 | + "entity": "test-entity", |
| 123 | + } |
| 124 | + |
| 125 | + data_source = self.create_data_source( |
| 126 | + query_id=subscription_update["subscription_id"], |
| 127 | + organization=self.organization, |
| 128 | + ) |
| 129 | + |
| 130 | + data_source.detectors.add(detector) |
| 131 | + |
| 132 | + if detector.workflow_condition_group is None: |
| 133 | + detector.workflow_condition_group = self.create_data_condition_group(logic_type="any") |
| 134 | + detector.save() |
| 135 | + |
| 136 | + self.create_data_condition( |
| 137 | + condition_group=detector.workflow_condition_group, |
| 138 | + type=Condition.EQUAL, |
| 139 | + condition_result=DetectorPriorityLevel.HIGH, |
| 140 | + comparison=1, |
| 141 | + ) |
| 142 | + |
| 143 | + # Create a data_packet from the update for testing |
| 144 | + data_packet = DataPacket[QuerySubscriptionUpdate]( |
| 145 | + query_id=subscription_update["subscription_id"], |
| 146 | + packet=subscription_update, |
| 147 | + ) |
| 148 | + |
| 149 | + return data_source, data_packet |
| 150 | + |
103 | 151 | def create_workflow_action( |
104 | 152 | self, |
105 | 153 | workflow: Workflow, |
|
0 commit comments