11from datetime import datetime , timedelta , timezone
22
3+ import pytest
4+ from jsonschema import ValidationError
5+
36from sentry .rules .age import AgeComparisonType
47from sentry .rules .filters .age_comparison import AgeComparisonFilter
58from sentry .testutils .helpers .datetime import freeze_time
@@ -36,7 +39,7 @@ def setUp(self):
3639 )
3740 self .dc = self .create_data_condition (
3841 type = self .condition ,
39- comparison = {"comparison_type" : AgeComparisonType .OLDER , "value" : "10" , "time" : "hour" },
42+ comparison = {"comparison_type" : AgeComparisonType .OLDER , "value" : 10 , "time" : "hour" },
4043 condition_result = True ,
4144 )
4245
@@ -47,16 +50,30 @@ def test_dual_write(self):
4750 assert dc .type == self .condition
4851 assert dc .comparison == {
4952 "comparison_type" : AgeComparisonType .OLDER ,
50- "value" : "10" ,
53+ "value" : 10 ,
5154 "time" : "hour" ,
5255 }
5356 assert dc .condition_result is True
5457 assert dc .condition_group == dcg
5558
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+
5672 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" }
5975 )
76+ self .dc .save ()
6077
6178 self .group .update (first_seen = datetime .now (timezone .utc ) - timedelta (hours = 3 ))
6279 self .assert_does_not_pass (self .dc , self .job )
@@ -67,22 +84,13 @@ def test_older_applies_correctly(self):
6784 self .assert_passes (self .dc , self .job )
6885
6986 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" }
7289 )
90+ self .dc .save ()
7391
7492 self .group .update (first_seen = datetime .now (timezone .utc ) - timedelta (hours = 3 ))
7593 self .assert_passes (self .dc , self .job )
7694
7795 self .group .update (first_seen = datetime .now (timezone .utc ) - timedelta (hours = 10 ))
7896 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