10
10
except ImportError :
11
11
import mock
12
12
13
- import json
14
13
15
14
16
15
@pytest .fixture
@@ -32,13 +31,14 @@ def usage_alerts_client(config):
32
31
return client
33
32
34
33
34
+
35
35
def test_create_usage_alert (usage_alerts_client ):
36
36
"""Test creating a usage alert"""
37
37
client = usage_alerts_client
38
-
38
+
39
39
# Create a mock for the _post method in alerting().usage
40
- client .alerting (). _c . _post = mock .MagicMock ()
41
- client .alerting (). _c . _post .return_value = {
40
+ client ._post = mock .MagicMock ()
41
+ client ._post .return_value = {
42
42
"id" : "a1b2c3" ,
43
43
"name" : "Test Alert" ,
44
44
"type" : "account" ,
@@ -50,12 +50,14 @@ def test_create_usage_alert(usage_alerts_client):
50
50
"updated_at" : 1597937213
51
51
}
52
52
53
- alert = client .alerting ().usage .create (
54
- name = "Test Alert" ,
55
- subtype = "query_usage" ,
56
- alert_at_percent = 85 ,
57
- notifier_list_ids = ["n1" ]
58
- )
53
+ # Patch the client reference
54
+ with mock .patch .object (client .alerting ().usage , "_c" , client ):
55
+ alert = client .alerting ().usage .create (
56
+ name = "Test Alert" ,
57
+ subtype = "query_usage" ,
58
+ alert_at_percent = 85 ,
59
+ notifier_list_ids = ["n1" ]
60
+ )
59
61
60
62
# Verify _post was called with correct arguments
61
63
expected_body = {
@@ -66,7 +68,7 @@ def test_create_usage_alert(usage_alerts_client):
66
68
"notifier_list_ids" : ["n1" ],
67
69
"zone_names" : []
68
70
}
69
- client .alerting (). _c . _post .assert_called_once_with ("/alerting/v1/alerts" , json = expected_body )
71
+ client ._post .assert_called_once_with ("/alerting/v1/alerts" , json = expected_body )
70
72
71
73
# Verify result
72
74
assert alert ["id" ] == "a1b2c3"
@@ -76,14 +78,15 @@ def test_create_usage_alert(usage_alerts_client):
76
78
assert alert ["data" ]["alert_at_percent" ] == 85
77
79
78
80
81
+
79
82
def test_get_usage_alert (usage_alerts_client ):
80
83
"""Test retrieving a usage alert"""
81
84
client = usage_alerts_client
82
85
alert_id = "a1b2c3"
83
86
84
87
# Create a mock for the _get method
85
- client .alerting (). _c . _get = mock .MagicMock ()
86
- client .alerting (). _c . _get .return_value = {
88
+ client ._get = mock .MagicMock ()
89
+ client ._get .return_value = {
87
90
"id" : alert_id ,
88
91
"name" : "Test Alert" ,
89
92
"type" : "account" ,
@@ -93,25 +96,28 @@ def test_get_usage_alert(usage_alerts_client):
93
96
"zone_names" : []
94
97
}
95
98
96
- alert = client .alerting ().usage .get (alert_id )
99
+ # Patch the client reference
100
+ with mock .patch .object (client .alerting ().usage , "_c" , client ):
101
+ alert = client .alerting ().usage .get (alert_id )
97
102
98
103
# Verify _get was called with correct URL
99
- client .alerting (). _c . _get .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " )
104
+ client ._get .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " )
100
105
101
106
# Verify result
102
107
assert alert ["id" ] == alert_id
103
108
assert alert ["name" ] == "Test Alert"
104
109
assert alert ["data" ]["alert_at_percent" ] == 85
105
110
106
111
112
+
107
113
def test_patch_usage_alert (usage_alerts_client ):
108
114
"""Test patching a usage alert - verify type/subtype are not sent"""
109
115
client = usage_alerts_client
110
116
alert_id = "a1b2c3"
111
117
112
118
# Create a mock for the _patch method
113
- client .alerting (). _c . _patch = mock .MagicMock ()
114
- client .alerting (). _c . _patch .return_value = {
119
+ client ._patch = mock .MagicMock ()
120
+ client ._patch .return_value = {
115
121
"id" : alert_id ,
116
122
"name" : "Updated Alert" ,
117
123
"type" : "account" ,
@@ -121,21 +127,23 @@ def test_patch_usage_alert(usage_alerts_client):
121
127
"zone_names" : []
122
128
}
123
129
124
- alert = client .alerting ().usage .patch (
125
- alert_id ,
126
- name = "Updated Alert" ,
127
- alert_at_percent = 90
128
- )
130
+ # Patch the client reference
131
+ with mock .patch .object (client .alerting ().usage , "_c" , client ):
132
+ alert = client .alerting ().usage .patch (
133
+ alert_id ,
134
+ name = "Updated Alert" ,
135
+ alert_at_percent = 90
136
+ )
129
137
130
138
# Verify _patch was called with correct arguments
131
139
expected_body = {
132
140
"name" : "Updated Alert" ,
133
141
"data" : {"alert_at_percent" : 90 }
134
142
}
135
- client .alerting (). _c . _patch .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " , json = expected_body )
143
+ client ._patch .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " , json = expected_body )
136
144
137
145
# Verify type/subtype are not in the arguments
138
- call_args = client .alerting (). _c . _patch .call_args [1 ]["json" ]
146
+ call_args = client ._patch .call_args [1 ]["json" ]
139
147
assert "type" not in call_args
140
148
assert "subtype" not in call_args
141
149
@@ -145,27 +153,31 @@ def test_patch_usage_alert(usage_alerts_client):
145
153
assert alert ["data" ]["alert_at_percent" ] == 90
146
154
147
155
156
+
148
157
def test_delete_usage_alert (usage_alerts_client ):
149
158
"""Test deleting a usage alert"""
150
159
client = usage_alerts_client
151
160
alert_id = "a1b2c3"
152
161
153
162
# Create a mock for the _delete method
154
- client .alerting (). _c . _delete = mock .MagicMock ()
163
+ client ._delete = mock .MagicMock ()
155
164
156
- client .alerting ().usage .delete (alert_id )
165
+ # Patch the client reference
166
+ with mock .patch .object (client .alerting ().usage , "_c" , client ):
167
+ client .alerting ().usage .delete (alert_id )
157
168
158
169
# Verify _delete was called with correct URL
159
- client .alerting ()._c ._delete .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " )
170
+ client ._delete .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " )
171
+
160
172
161
173
162
174
def test_list_usage_alerts (usage_alerts_client ):
163
175
"""Test listing usage alerts with pagination params"""
164
176
client = usage_alerts_client
165
177
166
178
# Create a mock for the _get method
167
- client .alerting (). _c . _get = mock .MagicMock ()
168
- client .alerting (). _c . _get .return_value = {
179
+ client ._get = mock .MagicMock ()
180
+ client ._get .return_value = {
169
181
"limit" : 1 ,
170
182
"next" : "next_token" ,
171
183
"total_results" : 2 ,
@@ -180,17 +192,19 @@ def test_list_usage_alerts(usage_alerts_client):
180
192
]
181
193
}
182
194
183
- response = client .alerting ().usage .list (
184
- limit = 1 ,
185
- order_descending = True
186
- )
195
+ # Patch the client reference
196
+ with mock .patch .object (client .alerting ().usage , "_c" , client ):
197
+ response = client .alerting ().usage .list (
198
+ limit = 1 ,
199
+ order_descending = True
200
+ )
187
201
188
202
# Verify _get was called with correct URL and params
189
203
expected_params = {
190
204
"limit" : 1 ,
191
205
"order_descending" : "true"
192
206
}
193
- client .alerting (). _c . _get .assert_called_once_with ("/alerting/v1/alerts" , params = expected_params )
207
+ client ._get .assert_called_once_with ("/alerting/v1/alerts" , params = expected_params )
194
208
195
209
# Verify result
196
210
assert "results" in response
@@ -201,6 +215,7 @@ def test_list_usage_alerts(usage_alerts_client):
201
215
assert response ["results" ][0 ]["id" ] == "a1"
202
216
203
217
218
+
204
219
def test_validation_threshold_bounds (usage_alerts_client ):
205
220
"""Test validation of alert_at_percent bounds"""
206
221
client = usage_alerts_client
@@ -232,6 +247,7 @@ def test_validation_threshold_bounds(usage_alerts_client):
232
247
assert "alert_at_percent must be int in 1..100" in str (excinfo .value )
233
248
234
249
250
+
235
251
def test_validation_subtype ():
236
252
"""Test validation of subtype values"""
237
253
from ns1 .alerting import USAGE_SUBTYPES
0 commit comments