Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/sentry/testutils/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,9 +1697,9 @@ def create_incident(

@staticmethod
@assume_test_silo_mode(SiloMode.REGION)
def create_incident_activity(incident, type, comment=None, user_id=None):
def create_incident_activity(incident, type, comment=None, user_id=None, **kwargs):
return IncidentActivity.objects.create(
incident=incident, type=type, comment=comment, user_id=user_id
incident=incident, type=type, comment=comment, user_id=user_id, **kwargs
)

@staticmethod
Expand Down
52 changes: 33 additions & 19 deletions tests/sentry/incidents/test_charts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
from unittest.mock import ANY, MagicMock, patch
from unittest.mock import MagicMock, patch

from django.utils import timezone
from django.utils.dateparse import parse_datetime
Expand All @@ -19,7 +19,7 @@
DetailedIncidentSerializerResponse,
)
from sentry.incidents.logic import CRITICAL_TRIGGER_LABEL
from sentry.incidents.models.incident import Incident
from sentry.incidents.models.incident import Incident, IncidentActivityType, IncidentStatus
from sentry.incidents.typings.metric_detector import AlertContext, OpenPeriodContext
from sentry.snuba.dataset import Dataset
from sentry.testutils.cases import TestCase
Expand Down Expand Up @@ -128,30 +128,44 @@ def test_eap_alert(self, mock_client_get: MagicMock, mock_generate_chart: MagicM

class FetchOpenPeriodsTest(TestCase):
@freeze_time(frozen_time)
@patch("sentry.incidents.charts.client.get")
@with_feature("organizations:incidents")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this we were actually hitting a 404 hitting the incidents endpoint, but the test didn't care to check

@with_feature("organizations:workflow-engine-single-process-metric-issues")
def test_get_incidents_from_detector(self, mock_client_get: MagicMock) -> None:
def test_get_incidents_from_detector(self) -> None:
self.create_detector() # dummy so detector ID != alert rule ID
detector = self.create_detector(project=self.project)
alert_rule = self.create_alert_rule(organization=self.organization, projects=[self.project])
self.create_alert_rule_detector(detector=detector, alert_rule_id=alert_rule.id)
incident = Incident(
incident = self.create_incident(
date_started=must_parse_datetime("2022-05-16T18:55:00Z"),
date_closed=None,
status=IncidentStatus.CRITICAL.value,
alert_rule=alert_rule,
)
# create incident activity the same way we do in logic.py create_incident
detected_activity = self.create_incident_activity(
incident,
IncidentActivityType.DETECTED.value,
date_added=incident.date_started,
)
created_activity = self.create_incident_activity(
incident,
IncidentActivityType.CREATED.value,
)

time_period = incident_date_range(60, incident.date_started, incident.date_closed)

fetch_metric_issue_open_periods(self.organization, detector.id, time_period)
mock_client_get.assert_called_with(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recognize that I'm changing what's being tested here, but I'm not sure the previous test was adding much value. I can keep it around and add this as an additional test if necessary.

auth=ANY,
user=ANY,
path="/organizations/baz/incidents/",
params={
"alertRule": alert_rule.id,
"expand": "activities",
"includeSnapshots": True,
"project": -1,
**time_period,
},
)
chart_data = fetch_metric_issue_open_periods(self.organization, detector.id, time_period)
assert chart_data[0]["alertRule"]["id"] == str(alert_rule.id)
assert chart_data[0]["projects"] == [self.project.slug]
assert chart_data[0]["dateStarted"] == incident.date_started

assert len(chart_data[0]["activities"]) == 2
detected_activity_resp = chart_data[0]["activities"][0]
created_activity_resp = chart_data[0]["activities"][1]

assert detected_activity_resp["incidentIdentifier"] == str(incident.identifier)
assert detected_activity_resp["type"] == IncidentActivityType.DETECTED.value
assert detected_activity_resp["dateCreated"] == detected_activity.date_added

assert created_activity_resp["incidentIdentifier"] == str(incident.identifier)
assert created_activity_resp["type"] == IncidentActivityType.CREATED.value
assert created_activity_resp["dateCreated"] == created_activity.date_added
Loading