@@ -35,7 +35,7 @@ def test_create_usage_alert(usage_alerts_client):
3535 """Test creating a usage alert"""
3636 client = usage_alerts_client
3737
38- # Create a mock for the _post method in alerting().usage
38+ # Create a mock for the _post method
3939 client ._post = mock .MagicMock ()
4040 client ._post .return_value = {
4141 "id" : "a1b2c3" ,
@@ -49,9 +49,12 @@ def test_create_usage_alert(usage_alerts_client):
4949 "updated_at" : 1597937213 ,
5050 }
5151
52- # Patch the client reference
53- with mock .patch .object (client .alerting ().usage , "_c" , client ):
54- alert = client .alerting ().usage .create (
52+ # Get the usage API and directly set its client
53+ usage_api = client .alerts ().usage
54+ usage_api ._c = client
55+
56+ # Make the API call
57+ alert = usage_api .create (
5558 name = "Test Alert" ,
5659 subtype = "query_usage" ,
5760 alert_at_percent = 85 ,
@@ -96,9 +99,12 @@ def test_get_usage_alert(usage_alerts_client):
9699 "zone_names" : [],
97100 }
98101
99- # Patch the client reference
100- with mock .patch .object (client .alerting ().usage , "_c" , client ):
101- alert = client .alerting ().usage .get (alert_id )
102+ # Get the usage API and directly set its client
103+ usage_api = client .alerts ().usage
104+ usage_api ._c = client
105+
106+ # Make the API call
107+ alert = usage_api .get (alert_id )
102108
103109 # Verify _get was called with correct URL
104110 client ._get .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " )
@@ -126,11 +132,14 @@ def test_patch_usage_alert(usage_alerts_client):
126132 "zone_names" : [],
127133 }
128134
129- # Patch the client reference
130- with mock .patch .object (client .alerting ().usage , "_c" , client ):
131- alert = client .alerting ().usage .patch (
132- alert_id , name = "Updated Alert" , alert_at_percent = 90
133- )
135+ # Get the usage API and directly set its client
136+ usage_api = client .alerts ().usage
137+ usage_api ._c = client
138+
139+ # Make the API call
140+ alert = usage_api .patch (
141+ alert_id , name = "Updated Alert" , alert_at_percent = 90
142+ )
134143
135144 # Verify _patch was called with correct arguments
136145 expected_body = {"name" : "Updated Alert" , "data" : {"alert_at_percent" : 90 }}
@@ -157,9 +166,12 @@ def test_delete_usage_alert(usage_alerts_client):
157166 # Create a mock for the _delete method
158167 client ._delete = mock .MagicMock ()
159168
160- # Patch the client reference
161- with mock .patch .object (client .alerting ().usage , "_c" , client ):
162- client .alerting ().usage .delete (alert_id )
169+ # Get the usage API and directly set its client
170+ usage_api = client .alerts ().usage
171+ usage_api ._c = client
172+
173+ # Make the API call
174+ usage_api .delete (alert_id )
163175
164176 # Verify _delete was called with correct URL
165177 client ._delete .assert_called_once_with (f"/alerting/v1/alerts/{ alert_id } " )
@@ -186,9 +198,12 @@ def test_list_usage_alerts(usage_alerts_client):
186198 ],
187199 }
188200
189- # Patch the client reference
190- with mock .patch .object (client .alerting ().usage , "_c" , client ):
191- response = client .alerting ().usage .list (limit = 1 , order_descending = True )
201+ # Get the usage API and directly set its client
202+ usage_api = client .alerts ().usage
203+ usage_api ._c = client
204+
205+ # Make the API call
206+ response = usage_api .list (limit = 1 , order_descending = True )
192207
193208 # Verify _get was called with correct URL and params
194209 expected_params = {"limit" : 1 , "order_descending" : "true" }
@@ -211,21 +226,21 @@ def test_validation_threshold_bounds(usage_alerts_client):
211226
212227 # Test below minimum
213228 with pytest .raises (ValueError ) as excinfo :
214- client .alerting ().usage .create (
229+ client .alerts ().usage .create (
215230 name = "Test Alert" , subtype = "query_usage" , alert_at_percent = 0
216231 )
217232 assert "alert_at_percent must be int in 1..100" in str (excinfo .value )
218233
219234 # Test above maximum
220235 with pytest .raises (ValueError ) as excinfo :
221- client .alerting ().usage .create (
236+ client .alerts ().usage .create (
222237 name = "Test Alert" , subtype = "query_usage" , alert_at_percent = 101
223238 )
224239 assert "alert_at_percent must be int in 1..100" in str (excinfo .value )
225240
226241 # Test same validation in patch
227242 with pytest .raises (ValueError ) as excinfo :
228- client .alerting ().usage .patch ("a1" , alert_at_percent = 101 )
243+ client .alerts ().usage .patch ("a1" , alert_at_percent = 101 )
229244 assert "alert_at_percent must be int in 1..100" in str (excinfo .value )
230245
231246
0 commit comments