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