|
1 | 1 | import datetime
|
2 |
| -from unittest.mock import ANY, MagicMock, patch |
| 2 | +from unittest.mock import MagicMock, patch |
3 | 3 |
|
4 | 4 | from django.utils import timezone
|
5 | 5 | from django.utils.dateparse import parse_datetime
|
|
19 | 19 | DetailedIncidentSerializerResponse,
|
20 | 20 | )
|
21 | 21 | from sentry.incidents.logic import CRITICAL_TRIGGER_LABEL
|
22 |
| -from sentry.incidents.models.incident import Incident |
| 22 | +from sentry.incidents.models.incident import Incident, IncidentActivityType, IncidentStatus |
23 | 23 | from sentry.incidents.typings.metric_detector import AlertContext, OpenPeriodContext
|
24 | 24 | from sentry.snuba.dataset import Dataset
|
25 | 25 | from sentry.testutils.cases import TestCase
|
@@ -128,30 +128,44 @@ def test_eap_alert(self, mock_client_get: MagicMock, mock_generate_chart: MagicM
|
128 | 128 |
|
129 | 129 | class FetchOpenPeriodsTest(TestCase):
|
130 | 130 | @freeze_time(frozen_time)
|
131 |
| - @patch("sentry.incidents.charts.client.get") |
| 131 | + @with_feature("organizations:incidents") |
132 | 132 | @with_feature("organizations:workflow-engine-single-process-metric-issues")
|
133 |
| - def test_get_incidents_from_detector(self, mock_client_get: MagicMock) -> None: |
| 133 | + def test_get_incidents_from_detector(self) -> None: |
134 | 134 | self.create_detector() # dummy so detector ID != alert rule ID
|
135 | 135 | detector = self.create_detector(project=self.project)
|
136 | 136 | alert_rule = self.create_alert_rule(organization=self.organization, projects=[self.project])
|
137 | 137 | self.create_alert_rule_detector(detector=detector, alert_rule_id=alert_rule.id)
|
138 |
| - incident = Incident( |
| 138 | + incident = self.create_incident( |
139 | 139 | date_started=must_parse_datetime("2022-05-16T18:55:00Z"),
|
140 |
| - date_closed=None, |
| 140 | + status=IncidentStatus.CRITICAL.value, |
141 | 141 | alert_rule=alert_rule,
|
142 | 142 | )
|
| 143 | + # create incident activity the same way we do in logic.py create_incident |
| 144 | + detected_activity = self.create_incident_activity( |
| 145 | + incident, |
| 146 | + IncidentActivityType.DETECTED.value, |
| 147 | + date_added=incident.date_started, |
| 148 | + ) |
| 149 | + created_activity = self.create_incident_activity( |
| 150 | + incident, |
| 151 | + IncidentActivityType.CREATED.value, |
| 152 | + ) |
| 153 | + |
143 | 154 | time_period = incident_date_range(60, incident.date_started, incident.date_closed)
|
144 | 155 |
|
145 |
| - fetch_metric_issue_open_periods(self.organization, detector.id, time_period) |
146 |
| - mock_client_get.assert_called_with( |
147 |
| - auth=ANY, |
148 |
| - user=ANY, |
149 |
| - path="/organizations/baz/incidents/", |
150 |
| - params={ |
151 |
| - "alertRule": alert_rule.id, |
152 |
| - "expand": "activities", |
153 |
| - "includeSnapshots": True, |
154 |
| - "project": -1, |
155 |
| - **time_period, |
156 |
| - }, |
157 |
| - ) |
| 156 | + chart_data = fetch_metric_issue_open_periods(self.organization, detector.id, time_period) |
| 157 | + assert chart_data[0]["alertRule"]["id"] == str(alert_rule.id) |
| 158 | + assert chart_data[0]["projects"] == [self.project.slug] |
| 159 | + assert chart_data[0]["dateStarted"] == incident.date_started |
| 160 | + |
| 161 | + assert len(chart_data[0]["activities"]) == 2 |
| 162 | + detected_activity_resp = chart_data[0]["activities"][0] |
| 163 | + created_activity_resp = chart_data[0]["activities"][1] |
| 164 | + |
| 165 | + assert detected_activity_resp["incidentIdentifier"] == str(incident.id) |
| 166 | + assert detected_activity_resp["type"] == IncidentActivityType.DETECTED.value |
| 167 | + assert detected_activity_resp["dateCreated"] == detected_activity.date_added |
| 168 | + |
| 169 | + assert created_activity_resp["incidentIdentifier"] == str(incident.id) |
| 170 | + assert created_activity_resp["type"] == IncidentActivityType.CREATED.value |
| 171 | + assert created_activity_resp["dateCreated"] == created_activity.date_added |
0 commit comments