1
1
from datetime import datetime , timedelta , timezone
2
2
3
+ import pytest
4
+ from jsonschema import ValidationError
5
+
3
6
from sentry .rules .age import AgeComparisonType
4
7
from sentry .rules .filters .age_comparison import AgeComparisonFilter
5
8
from sentry .testutils .helpers .datetime import freeze_time
@@ -36,7 +39,7 @@ def setUp(self):
36
39
)
37
40
self .dc = self .create_data_condition (
38
41
type = self .condition ,
39
- comparison = {"comparison_type" : AgeComparisonType .OLDER , "value" : "10" , "time" : "hour" },
42
+ comparison = {"comparison_type" : AgeComparisonType .OLDER , "value" : 10 , "time" : "hour" },
40
43
condition_result = True ,
41
44
)
42
45
@@ -47,16 +50,30 @@ def test_dual_write(self):
47
50
assert dc .type == self .condition
48
51
assert dc .comparison == {
49
52
"comparison_type" : AgeComparisonType .OLDER ,
50
- "value" : "10" ,
53
+ "value" : 10 ,
51
54
"time" : "hour" ,
52
55
}
53
56
assert dc .condition_result is True
54
57
assert dc .condition_group == dcg
55
58
59
+ def test_json_schema (self ):
60
+ self .dc .comparison .update ({"time" : "asdf" })
61
+ with pytest .raises (ValidationError ):
62
+ self .dc .save ()
63
+
64
+ self .dc .comparison .update ({"value" : "bad_value" })
65
+ with pytest .raises (ValidationError ):
66
+ self .dc .save ()
67
+
68
+ self .dc .comparison .update ({"comparison_type" : "bad_value" })
69
+ with pytest .raises (ValidationError ):
70
+ self .dc .save ()
71
+
56
72
def test_older_applies_correctly (self ):
57
- self .dc .update (
58
- comparison = {"comparison_type" : AgeComparisonType .OLDER , "value" : "10" , "time" : "hour" }
73
+ self .dc .comparison . update (
74
+ {"comparison_type" : AgeComparisonType .OLDER , "value" : 10 , "time" : "hour" }
59
75
)
76
+ self .dc .save ()
60
77
61
78
self .group .update (first_seen = datetime .now (timezone .utc ) - timedelta (hours = 3 ))
62
79
self .assert_does_not_pass (self .dc , self .job )
@@ -67,22 +84,13 @@ def test_older_applies_correctly(self):
67
84
self .assert_passes (self .dc , self .job )
68
85
69
86
def test_newer_applies_correctly (self ):
70
- self .dc .update (
71
- comparison = {"comparison_type" : AgeComparisonType .NEWER , "value" : "10" , "time" : "hour" }
87
+ self .dc .comparison . update (
88
+ {"comparison_type" : AgeComparisonType .NEWER , "value" : 10 , "time" : "hour" }
72
89
)
90
+ self .dc .save ()
73
91
74
92
self .group .update (first_seen = datetime .now (timezone .utc ) - timedelta (hours = 3 ))
75
93
self .assert_passes (self .dc , self .job )
76
94
77
95
self .group .update (first_seen = datetime .now (timezone .utc ) - timedelta (hours = 10 ))
78
96
self .assert_does_not_pass (self .dc , self .job )
79
-
80
- def test_fails_on_insufficient_data (self ):
81
- self .dc .update (comparison = {"time" : "hour" })
82
- self .assert_does_not_pass (self .dc , self .job )
83
-
84
- self .dc .update (comparison = {"value" : "bad_value" })
85
- self .assert_does_not_pass (self .dc , self .job )
86
-
87
- self .dc .update (comparison = {"comparison_type" : "bad_value" })
88
- self .assert_does_not_pass (self .dc , self .job )
0 commit comments