1111 import mock
1212
1313
14-
1514@pytest .fixture
1615def usage_alerts_client (config ):
1716 config .loadFromDict (
@@ -31,7 +30,6 @@ def usage_alerts_client(config):
3130 return client
3231
3332
34-
3533def test_create_usage_alert (usage_alerts_client ):
3634 """Test creating a usage alert"""
3735 client = usage_alerts_client
@@ -49,7 +47,7 @@ def test_create_usage_alert(usage_alerts_client):
4947 "created_at" : 1597937213 ,
5048 "updated_at" : 1597937213
5149 }
52-
50+
5351 # Patch the client reference
5452 with mock .patch .object (client .alerting ().usage , "_c" , client ):
5553 alert = client .alerting ().usage .create (
@@ -58,7 +56,7 @@ def test_create_usage_alert(usage_alerts_client):
5856 alert_at_percent = 85 ,
5957 notifier_list_ids = ["n1" ]
6058 )
61-
59+
6260 # Verify _post was called with correct arguments
6361 expected_body = {
6462 "name" : "Test Alert" ,
@@ -68,8 +66,9 @@ def test_create_usage_alert(usage_alerts_client):
6866 "notifier_list_ids" : ["n1" ],
6967 "zone_names" : []
7068 }
71- client ._post .assert_called_once_with ("/alerting/v1/alerts" , json = expected_body )
72-
69+ client ._post .assert_called_once_with (
70+ "/alerting/v1/alerts" , json = expected_body )
71+
7372 # Verify result
7473 assert alert ["id" ] == "a1b2c3"
7574 assert alert ["name" ] == "Test Alert"
@@ -78,12 +77,11 @@ def test_create_usage_alert(usage_alerts_client):
7877 assert alert ["data" ]["alert_at_percent" ] == 85
7978
8079
81-
8280def test_get_usage_alert (usage_alerts_client ):
8381 """Test retrieving a usage alert"""
8482 client = usage_alerts_client
8583 alert_id = "a1b2c3"
86-
84+
8785 # Create a mock for the _get method
8886 client ._get = mock .MagicMock ()
8987 client ._get .return_value = {
@@ -95,26 +93,25 @@ def test_get_usage_alert(usage_alerts_client):
9593 "notifier_list_ids" : ["n1" ],
9694 "zone_names" : []
9795 }
98-
96+
9997 # Patch the client reference
10098 with mock .patch .object (client .alerting ().usage , "_c" , client ):
10199 alert = client .alerting ().usage .get (alert_id )
102-
100+
103101 # Verify _get was called with correct URL
104102 client ._get .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " )
105-
103+
106104 # Verify result
107105 assert alert ["id" ] == alert_id
108106 assert alert ["name" ] == "Test Alert"
109107 assert alert ["data" ]["alert_at_percent" ] == 85
110108
111109
112-
113110def test_patch_usage_alert (usage_alerts_client ):
114111 """Test patching a usage alert - verify type/subtype are not sent"""
115112 client = usage_alerts_client
116113 alert_id = "a1b2c3"
117-
114+
118115 # Create a mock for the _patch method
119116 client ._patch = mock .MagicMock ()
120117 client ._patch .return_value = {
@@ -126,55 +123,54 @@ def test_patch_usage_alert(usage_alerts_client):
126123 "notifier_list_ids" : ["n1" ],
127124 "zone_names" : []
128125 }
129-
126+
130127 # Patch the client reference
131128 with mock .patch .object (client .alerting ().usage , "_c" , client ):
132129 alert = client .alerting ().usage .patch (
133130 alert_id ,
134131 name = "Updated Alert" ,
135132 alert_at_percent = 90
136133 )
137-
134+
138135 # Verify _patch was called with correct arguments
139136 expected_body = {
140137 "name" : "Updated Alert" ,
141138 "data" : {"alert_at_percent" : 90 }
142139 }
143- client ._patch .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " , json = expected_body )
144-
140+ client ._patch .assert_called_once_with (
141+ f"/alerting/v1/alerts/{ alert_id } " , json = expected_body )
142+
145143 # Verify type/subtype are not in the arguments
146144 call_args = client ._patch .call_args [1 ]["json" ]
147145 assert "type" not in call_args
148146 assert "subtype" not in call_args
149-
147+
150148 # Verify result
151149 assert alert ["id" ] == alert_id
152150 assert alert ["name" ] == "Updated Alert"
153151 assert alert ["data" ]["alert_at_percent" ] == 90
154152
155153
156-
157154def test_delete_usage_alert (usage_alerts_client ):
158155 """Test deleting a usage alert"""
159156 client = usage_alerts_client
160157 alert_id = "a1b2c3"
161-
158+
162159 # Create a mock for the _delete method
163160 client ._delete = mock .MagicMock ()
164-
161+
165162 # Patch the client reference
166163 with mock .patch .object (client .alerting ().usage , "_c" , client ):
167164 client .alerting ().usage .delete (alert_id )
168-
165+
169166 # Verify _delete was called with correct URL
170167 client ._delete .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " )
171168
172169
173-
174170def test_list_usage_alerts (usage_alerts_client ):
175171 """Test listing usage alerts with pagination params"""
176172 client = usage_alerts_client
177-
173+
178174 # Create a mock for the _get method
179175 client ._get = mock .MagicMock ()
180176 client ._get .return_value = {
@@ -191,21 +187,22 @@ def test_list_usage_alerts(usage_alerts_client):
191187 }
192188 ]
193189 }
194-
190+
195191 # Patch the client reference
196192 with mock .patch .object (client .alerting ().usage , "_c" , client ):
197193 response = client .alerting ().usage .list (
198194 limit = 1 ,
199195 order_descending = True
200196 )
201-
197+
202198 # Verify _get was called with correct URL and params
203199 expected_params = {
204200 "limit" : 1 ,
205201 "order_descending" : "true"
206202 }
207- client ._get .assert_called_once_with ("/alerting/v1/alerts" , params = expected_params )
208-
203+ client ._get .assert_called_once_with (
204+ "/alerting/v1/alerts" , params = expected_params )
205+
209206 # Verify result
210207 assert "results" in response
211208 assert "next" in response
@@ -215,11 +212,10 @@ def test_list_usage_alerts(usage_alerts_client):
215212 assert response ["results" ][0 ]["id" ] == "a1"
216213
217214
218-
219215def test_validation_threshold_bounds (usage_alerts_client ):
220216 """Test validation of alert_at_percent bounds"""
221217 client = usage_alerts_client
222-
218+
223219 # Test below minimum
224220 with pytest .raises (ValueError ) as excinfo :
225221 client .alerting ().usage .create (
@@ -228,7 +224,7 @@ def test_validation_threshold_bounds(usage_alerts_client):
228224 alert_at_percent = 0
229225 )
230226 assert "alert_at_percent must be int in 1..100" in str (excinfo .value )
231-
227+
232228 # Test above maximum
233229 with pytest .raises (ValueError ) as excinfo :
234230 client .alerting ().usage .create (
@@ -237,7 +233,7 @@ def test_validation_threshold_bounds(usage_alerts_client):
237233 alert_at_percent = 101
238234 )
239235 assert "alert_at_percent must be int in 1..100" in str (excinfo .value )
240-
236+
241237 # Test same validation in patch
242238 with pytest .raises (ValueError ) as excinfo :
243239 client .alerting ().usage .patch (
@@ -247,19 +243,18 @@ def test_validation_threshold_bounds(usage_alerts_client):
247243 assert "alert_at_percent must be int in 1..100" in str (excinfo .value )
248244
249245
250-
251246def test_validation_subtype ():
252247 """Test validation of subtype values"""
253248 from ns1 .alerting import USAGE_SUBTYPES
254249 from ns1 .alerting .usage_alerts import _validate
255-
250+
256251 # Valid subtypes should pass validation
257252 for subtype in USAGE_SUBTYPES :
258253 try :
259254 _validate ("Test Alert" , subtype , 85 )
260255 except ValueError :
261256 pytest .fail (f"Valid subtype '{ subtype } ' was rejected" )
262-
257+
263258 # Invalid subtype should fail validation
264259 with pytest .raises (ValueError ) as excinfo :
265260 _validate ("Test Alert" , "invalid_subtype" , 85 )
0 commit comments