|
| 1 | +from dataclasses import dataclass |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from sentry.incidents.grouptype import MetricAlertFire |
| 5 | +from sentry.issues.grouptype import ( |
| 6 | + GroupCategory, |
| 7 | + GroupType, |
| 8 | + GroupTypeRegistry, |
| 9 | + MonitorIncidentType, |
| 10 | + PerformanceSlowDBQueryGroupType, |
| 11 | + UptimeDomainCheckFailure, |
| 12 | +) |
| 13 | +from sentry.testutils.cases import APITestCase |
| 14 | +from sentry.testutils.silo import region_silo_test |
| 15 | +from sentry.workflow_engine.handlers.detector import DetectorEvaluationResult, DetectorHandler |
| 16 | +from sentry.workflow_engine.models import DataPacket |
| 17 | +from sentry.workflow_engine.types import DetectorGroupKey, DetectorPriorityLevel |
| 18 | + |
| 19 | + |
| 20 | +@region_silo_test |
| 21 | +class OrganizationDetectorTypesAPITestCase(APITestCase): |
| 22 | + endpoint = "sentry-api-0-organization-detector-type-index" |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + super().setUp() |
| 26 | + self.login_as(user=self.user) |
| 27 | + |
| 28 | + self.registry_patcher = patch( |
| 29 | + "sentry.workflow_engine.endpoints.organization_detector_types.grouptype.registry", |
| 30 | + new=GroupTypeRegistry(), |
| 31 | + ) |
| 32 | + self.registry_patcher.start() |
| 33 | + |
| 34 | + class MockDetectorHandler(DetectorHandler[dict]): |
| 35 | + def evaluate( |
| 36 | + self, data_packet: DataPacket[dict] |
| 37 | + ) -> dict[DetectorGroupKey, DetectorEvaluationResult]: |
| 38 | + return {None: DetectorEvaluationResult(None, True, DetectorPriorityLevel.HIGH)} |
| 39 | + |
| 40 | + @dataclass(frozen=True) |
| 41 | + class TestMetricGroupType(GroupType): |
| 42 | + type_id = 1 |
| 43 | + slug = MetricAlertFire.slug |
| 44 | + description = "Metric alert" |
| 45 | + category = GroupCategory.METRIC_ALERT.value |
| 46 | + detector_handler = MockDetectorHandler |
| 47 | + released = True |
| 48 | + |
| 49 | + @dataclass(frozen=True) |
| 50 | + class TestCronsGroupType(GroupType): |
| 51 | + type_id = 2 |
| 52 | + slug = MonitorIncidentType.slug |
| 53 | + description = "Crons" |
| 54 | + category = GroupCategory.CRON.value |
| 55 | + detector_handler = MockDetectorHandler |
| 56 | + released = True |
| 57 | + |
| 58 | + @dataclass(frozen=True) |
| 59 | + class TestUptimeGroupType(GroupType): |
| 60 | + type_id = 3 |
| 61 | + slug = UptimeDomainCheckFailure.slug |
| 62 | + description = "Uptime" |
| 63 | + category = GroupCategory.UPTIME.value |
| 64 | + detector_handler = MockDetectorHandler |
| 65 | + released = True |
| 66 | + |
| 67 | + # Should not be included in the response |
| 68 | + @dataclass(frozen=True) |
| 69 | + class TestPerformanceGroupType(GroupType): |
| 70 | + type_id = 4 |
| 71 | + slug = PerformanceSlowDBQueryGroupType.slug |
| 72 | + description = "Performance" |
| 73 | + category = GroupCategory.PERFORMANCE.value |
| 74 | + released = True |
| 75 | + |
| 76 | + def tearDown(self): |
| 77 | + super().tearDown() |
| 78 | + self.registry_patcher.stop() |
| 79 | + |
| 80 | + def test_simple(self): |
| 81 | + response = self.get_success_response(self.organization.slug, status_code=200) |
| 82 | + assert len(response.data) == 3 |
| 83 | + assert response.data == [ |
| 84 | + MetricAlertFire.slug, |
| 85 | + MonitorIncidentType.slug, |
| 86 | + UptimeDomainCheckFailure.slug, |
| 87 | + ] |
0 commit comments