11
11
import mock
12
12
13
13
14
-
15
14
@pytest .fixture
16
15
def usage_alerts_client (config ):
17
16
config .loadFromDict (
@@ -31,7 +30,6 @@ def usage_alerts_client(config):
31
30
return client
32
31
33
32
34
-
35
33
def test_create_usage_alert (usage_alerts_client ):
36
34
"""Test creating a usage alert"""
37
35
client = usage_alerts_client
@@ -49,7 +47,7 @@ def test_create_usage_alert(usage_alerts_client):
49
47
"created_at" : 1597937213 ,
50
48
"updated_at" : 1597937213
51
49
}
52
-
50
+
53
51
# Patch the client reference
54
52
with mock .patch .object (client .alerting ().usage , "_c" , client ):
55
53
alert = client .alerting ().usage .create (
@@ -58,7 +56,7 @@ def test_create_usage_alert(usage_alerts_client):
58
56
alert_at_percent = 85 ,
59
57
notifier_list_ids = ["n1" ]
60
58
)
61
-
59
+
62
60
# Verify _post was called with correct arguments
63
61
expected_body = {
64
62
"name" : "Test Alert" ,
@@ -68,8 +66,9 @@ def test_create_usage_alert(usage_alerts_client):
68
66
"notifier_list_ids" : ["n1" ],
69
67
"zone_names" : []
70
68
}
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
+
73
72
# Verify result
74
73
assert alert ["id" ] == "a1b2c3"
75
74
assert alert ["name" ] == "Test Alert"
@@ -78,12 +77,11 @@ def test_create_usage_alert(usage_alerts_client):
78
77
assert alert ["data" ]["alert_at_percent" ] == 85
79
78
80
79
81
-
82
80
def test_get_usage_alert (usage_alerts_client ):
83
81
"""Test retrieving a usage alert"""
84
82
client = usage_alerts_client
85
83
alert_id = "a1b2c3"
86
-
84
+
87
85
# Create a mock for the _get method
88
86
client ._get = mock .MagicMock ()
89
87
client ._get .return_value = {
@@ -95,26 +93,25 @@ def test_get_usage_alert(usage_alerts_client):
95
93
"notifier_list_ids" : ["n1" ],
96
94
"zone_names" : []
97
95
}
98
-
96
+
99
97
# Patch the client reference
100
98
with mock .patch .object (client .alerting ().usage , "_c" , client ):
101
99
alert = client .alerting ().usage .get (alert_id )
102
-
100
+
103
101
# Verify _get was called with correct URL
104
102
client ._get .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " )
105
-
103
+
106
104
# Verify result
107
105
assert alert ["id" ] == alert_id
108
106
assert alert ["name" ] == "Test Alert"
109
107
assert alert ["data" ]["alert_at_percent" ] == 85
110
108
111
109
112
-
113
110
def test_patch_usage_alert (usage_alerts_client ):
114
111
"""Test patching a usage alert - verify type/subtype are not sent"""
115
112
client = usage_alerts_client
116
113
alert_id = "a1b2c3"
117
-
114
+
118
115
# Create a mock for the _patch method
119
116
client ._patch = mock .MagicMock ()
120
117
client ._patch .return_value = {
@@ -126,55 +123,54 @@ def test_patch_usage_alert(usage_alerts_client):
126
123
"notifier_list_ids" : ["n1" ],
127
124
"zone_names" : []
128
125
}
129
-
126
+
130
127
# Patch the client reference
131
128
with mock .patch .object (client .alerting ().usage , "_c" , client ):
132
129
alert = client .alerting ().usage .patch (
133
130
alert_id ,
134
131
name = "Updated Alert" ,
135
132
alert_at_percent = 90
136
133
)
137
-
134
+
138
135
# Verify _patch was called with correct arguments
139
136
expected_body = {
140
137
"name" : "Updated Alert" ,
141
138
"data" : {"alert_at_percent" : 90 }
142
139
}
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
+
145
143
# Verify type/subtype are not in the arguments
146
144
call_args = client ._patch .call_args [1 ]["json" ]
147
145
assert "type" not in call_args
148
146
assert "subtype" not in call_args
149
-
147
+
150
148
# Verify result
151
149
assert alert ["id" ] == alert_id
152
150
assert alert ["name" ] == "Updated Alert"
153
151
assert alert ["data" ]["alert_at_percent" ] == 90
154
152
155
153
156
-
157
154
def test_delete_usage_alert (usage_alerts_client ):
158
155
"""Test deleting a usage alert"""
159
156
client = usage_alerts_client
160
157
alert_id = "a1b2c3"
161
-
158
+
162
159
# Create a mock for the _delete method
163
160
client ._delete = mock .MagicMock ()
164
-
161
+
165
162
# Patch the client reference
166
163
with mock .patch .object (client .alerting ().usage , "_c" , client ):
167
164
client .alerting ().usage .delete (alert_id )
168
-
165
+
169
166
# Verify _delete was called with correct URL
170
167
client ._delete .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " )
171
168
172
169
173
-
174
170
def test_list_usage_alerts (usage_alerts_client ):
175
171
"""Test listing usage alerts with pagination params"""
176
172
client = usage_alerts_client
177
-
173
+
178
174
# Create a mock for the _get method
179
175
client ._get = mock .MagicMock ()
180
176
client ._get .return_value = {
@@ -191,21 +187,22 @@ def test_list_usage_alerts(usage_alerts_client):
191
187
}
192
188
]
193
189
}
194
-
190
+
195
191
# Patch the client reference
196
192
with mock .patch .object (client .alerting ().usage , "_c" , client ):
197
193
response = client .alerting ().usage .list (
198
194
limit = 1 ,
199
195
order_descending = True
200
196
)
201
-
197
+
202
198
# Verify _get was called with correct URL and params
203
199
expected_params = {
204
200
"limit" : 1 ,
205
201
"order_descending" : "true"
206
202
}
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
+
209
206
# Verify result
210
207
assert "results" in response
211
208
assert "next" in response
@@ -215,11 +212,10 @@ def test_list_usage_alerts(usage_alerts_client):
215
212
assert response ["results" ][0 ]["id" ] == "a1"
216
213
217
214
218
-
219
215
def test_validation_threshold_bounds (usage_alerts_client ):
220
216
"""Test validation of alert_at_percent bounds"""
221
217
client = usage_alerts_client
222
-
218
+
223
219
# Test below minimum
224
220
with pytest .raises (ValueError ) as excinfo :
225
221
client .alerting ().usage .create (
@@ -228,7 +224,7 @@ def test_validation_threshold_bounds(usage_alerts_client):
228
224
alert_at_percent = 0
229
225
)
230
226
assert "alert_at_percent must be int in 1..100" in str (excinfo .value )
231
-
227
+
232
228
# Test above maximum
233
229
with pytest .raises (ValueError ) as excinfo :
234
230
client .alerting ().usage .create (
@@ -237,7 +233,7 @@ def test_validation_threshold_bounds(usage_alerts_client):
237
233
alert_at_percent = 101
238
234
)
239
235
assert "alert_at_percent must be int in 1..100" in str (excinfo .value )
240
-
236
+
241
237
# Test same validation in patch
242
238
with pytest .raises (ValueError ) as excinfo :
243
239
client .alerting ().usage .patch (
@@ -247,19 +243,18 @@ def test_validation_threshold_bounds(usage_alerts_client):
247
243
assert "alert_at_percent must be int in 1..100" in str (excinfo .value )
248
244
249
245
250
-
251
246
def test_validation_subtype ():
252
247
"""Test validation of subtype values"""
253
248
from ns1 .alerting import USAGE_SUBTYPES
254
249
from ns1 .alerting .usage_alerts import _validate
255
-
250
+
256
251
# Valid subtypes should pass validation
257
252
for subtype in USAGE_SUBTYPES :
258
253
try :
259
254
_validate ("Test Alert" , subtype , 85 )
260
255
except ValueError :
261
256
pytest .fail (f"Valid subtype '{ subtype } ' was rejected" )
262
-
257
+
263
258
# Invalid subtype should fail validation
264
259
with pytest .raises (ValueError ) as excinfo :
265
260
_validate ("Test Alert" , "invalid_subtype" , 85 )
0 commit comments