4
4
5
5
from sentry .issues .grouptype import MonitorIncidentType
6
6
from sentry .monitors .types import DATA_SOURCE_CRON_MONITOR
7
- from sentry .monitors .utils import ensure_cron_detector
7
+ from sentry .monitors .utils import ensure_cron_detector , get_detector_for_monitor
8
8
from sentry .testutils .cases import TestCase
9
- from sentry .workflow_engine .models import DataSource , DataSourceDetector , Detector
9
+ from sentry .workflow_engine .models import DataSource , Detector
10
10
11
11
12
12
class EnsureCronDetectorTest (TestCase ):
@@ -15,66 +15,24 @@ def setUp(self):
15
15
self .monitor = self .create_monitor (owner_user_id = None )
16
16
17
17
def test_creates_data_source_and_detector_for_new_monitor (self ):
18
- assert not DataSource .objects .filter (
19
- type = DATA_SOURCE_CRON_MONITOR ,
20
- organization_id = self .monitor .organization_id ,
21
- source_id = str (self .monitor .id ),
22
- ).exists ()
23
-
18
+ assert not get_detector_for_monitor (self .monitor )
24
19
ensure_cron_detector (self .monitor )
25
- data_source = DataSource .objects .get (
26
- type = DATA_SOURCE_CRON_MONITOR ,
27
- organization_id = self .monitor .organization_id ,
28
- source_id = str (self .monitor .id ),
29
- )
30
- assert data_source is not None
31
- detector = Detector .objects .get (
32
- type = MonitorIncidentType .slug ,
33
- project_id = self .monitor .project_id ,
34
- name = self .monitor .name ,
35
- )
20
+ detector = get_detector_for_monitor (self .monitor )
36
21
assert detector is not None
22
+ assert detector .type == "monitor_check_in_failure"
23
+ assert detector .project_id == self .monitor .project_id
24
+ assert detector .name == self .monitor .name
37
25
assert detector .owner_user_id == self .monitor .owner_user_id
38
26
assert detector .owner_team_id == self .monitor .owner_team_id
39
- assert DataSourceDetector .objects .filter (
40
- data_source = data_source ,
41
- detector = detector ,
42
- ).exists ()
43
27
44
28
def test_idempotent_for_existing_data_source (self ):
45
29
ensure_cron_detector (self .monitor )
46
- data_source = DataSource .objects .get (
47
- type = DATA_SOURCE_CRON_MONITOR ,
48
- organization_id = self .monitor .organization_id ,
49
- source_id = str (self .monitor .id ),
50
- )
51
- detector = Detector .objects .get (
52
- type = MonitorIncidentType .slug ,
53
- project_id = self .monitor .project_id ,
54
- name = self .monitor .name ,
55
- )
56
- link = DataSourceDetector .objects .get (
57
- data_source = data_source ,
58
- detector = detector ,
59
- )
30
+ detector = get_detector_for_monitor (self .monitor )
31
+ assert detector
60
32
ensure_cron_detector (self .monitor )
61
- data_source_after = DataSource .objects .get (
62
- type = DATA_SOURCE_CRON_MONITOR ,
63
- organization_id = self .monitor .organization_id ,
64
- source_id = str (self .monitor .id ),
65
- )
66
- detector_after = Detector .objects .get (
67
- type = MonitorIncidentType .slug ,
68
- project_id = self .monitor .project_id ,
69
- name = self .monitor .name ,
70
- )
71
- link_after = DataSourceDetector .objects .get (
72
- data_source = data_source ,
73
- detector = detector ,
74
- )
75
- assert data_source .id == data_source_after .id
33
+ detector_after = get_detector_for_monitor (self .monitor )
34
+ assert detector_after is not None
76
35
assert detector .id == detector_after .id
77
- assert link .id == link_after .id
78
36
79
37
def test_with_owner_user (self ):
80
38
self .monitor .owner_user_id = self .user .id
@@ -118,3 +76,38 @@ def test_atomic_transaction_rollback(self):
118
76
assert not DataSource .objects .filter (
119
77
type = DATA_SOURCE_CRON_MONITOR , source_id = str (self .monitor .id )
120
78
).exists ()
79
+
80
+
81
+ class GetDetectorForMonitorTest (TestCase ):
82
+ def setUp (self ):
83
+ super ().setUp ()
84
+ self .monitor = self .create_monitor ()
85
+
86
+ def test_returns_none_when_no_detector_exists (self ):
87
+ detector = get_detector_for_monitor (self .monitor )
88
+ assert detector is None
89
+
90
+ def test_returns_detector_when_exists (self ):
91
+ ensure_cron_detector (self .monitor )
92
+
93
+ detector = get_detector_for_monitor (self .monitor )
94
+ assert detector is not None
95
+ assert detector .type == "monitor_check_in_failure"
96
+ assert detector .project_id == self .monitor .project_id
97
+ assert detector .name == self .monitor .name
98
+
99
+ def test_returns_correct_detector_for_specific_monitor (self ):
100
+ monitor1 = self .monitor
101
+ monitor2 = self .create_monitor (name = "Monitor 2" )
102
+
103
+ ensure_cron_detector (monitor1 )
104
+ ensure_cron_detector (monitor2 )
105
+
106
+ detector1 = get_detector_for_monitor (monitor1 )
107
+ detector2 = get_detector_for_monitor (monitor2 )
108
+
109
+ assert detector1 is not None
110
+ assert detector2 is not None
111
+ assert detector1 .id != detector2 .id
112
+ assert detector1 .name == monitor1 .name
113
+ assert detector2 .name == monitor2 .name
0 commit comments