Skip to content

Commit 16c54b7

Browse files
fix linting
1 parent 605ad35 commit 16c54b7

File tree

3 files changed

+44
-49
lines changed

3 files changed

+44
-49
lines changed

examples/usage_alerts.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@
3131
client = NS1(config=c)
3232

3333

34-
3534
# Usage Alerts API Examples
3635
def usage_alerts_example():
3736
print("\n=== Usage Alerts Examples ===\n")
38-
37+
3938
# List all usage alerts
4039
print("Listing usage alerts:")
4140
try:
@@ -45,7 +44,7 @@ def usage_alerts_example():
4544
print(f" {i+1}. {alert.get('name')} (id: {alert.get('id')})")
4645
except Exception as e:
4746
print(f"Error listing alerts: {e}")
48-
47+
4948
# Create a usage alert
5049
print("\nCreating a usage alert:")
5150
try:
@@ -62,7 +61,7 @@ def usage_alerts_example():
6261
except Exception as e:
6362
print(f"Error creating alert: {e}")
6463
return
65-
64+
6665
# Update the alert
6766
print("\nUpdating the alert threshold to 90%:")
6867
try:
@@ -74,15 +73,15 @@ def usage_alerts_example():
7473
print(f"New threshold: {updated['data']['alert_at_percent']}%")
7574
except Exception as e:
7675
print(f"Error updating alert: {e}")
77-
76+
7877
# Get alert details
7978
print("\nGetting alert details:")
8079
try:
8180
details = client.alerting().usage.get(alert_id)
8281
print(f"Alert details: {json.dumps(details, indent=2)}")
8382
except Exception as e:
8483
print(f"Error getting alert: {e}")
85-
84+
8685
# Delete the alert
8786
print("\nDeleting the alert:")
8887
try:
@@ -92,11 +91,10 @@ def usage_alerts_example():
9291
print(f"Error deleting alert: {e}")
9392

9493

95-
9694
# Test validation failures
9795
def test_validation():
9896
print("\n=== Validation Tests ===\n")
99-
97+
10098
# Test invalid subtype
10199
print("Testing invalid subtype:")
102100
try:
@@ -107,7 +105,7 @@ def test_validation():
107105
)
108106
except ValueError as e:
109107
print(f"Validation error (expected): {e}")
110-
108+
111109
# Test threshold too low
112110
print("\nTesting threshold too low (0):")
113111
try:
@@ -118,7 +116,7 @@ def test_validation():
118116
)
119117
except ValueError as e:
120118
print(f"Validation error (expected): {e}")
121-
119+
122120
# Test threshold too high
123121
print("\nTesting threshold too high (101):")
124122
try:
@@ -131,13 +129,12 @@ def test_validation():
131129
print(f"Validation error (expected): {e}")
132130

133131

134-
135132
if __name__ == '__main__':
136133
print("Usage Alerts API Examples")
137134
print("-" * 30)
138135
print("Note: To run against the actual API, set the NS1_APIKEY environment variable")
139136
print("Otherwise, this will run against a mock API endpoint")
140-
137+
141138
# Run examples
142139
usage_alerts_example()
143140
test_validation()

ns1/alerting/usage_alerts.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ def _validate(name: str, subtype: str, alert_at_percent: int) -> None:
1111
raise ValueError("name required")
1212
if subtype not in USAGE_SUBTYPES:
1313
raise ValueError("invalid subtype")
14-
if not isinstance(alert_at_percent, int) or not (1 <= alert_at_percent <= 100):
14+
if not isinstance(alert_at_percent, int) or not (
15+
1 <= alert_at_percent <= 100):
1516
raise ValueError("data.alert_at_percent must be int in 1..100")
1617

1718

@@ -21,6 +22,7 @@ class UsageAlertsAPI:
2122
Server rules: type='account'; data.alert_at_percent in 1..100;
2223
PATCH must not include type/subtype; zone_names/notifier_list_ids may be [].
2324
"""
25+
2426
def __init__(self, client) -> None:
2527
self._c = client
2628

@@ -53,7 +55,8 @@ def patch(self, alert_id: str, *,
5355
if name is not None:
5456
patch["name"] = name
5557
if alert_at_percent is not None:
56-
if not isinstance(alert_at_percent, int) or not (1 <= alert_at_percent <= 100):
58+
if not isinstance(alert_at_percent, int) or not (
59+
1 <= alert_at_percent <= 100):
5760
raise ValueError("data.alert_at_percent must be int in 1..100")
5861
patch["data"] = {"alert_at_percent": int(alert_at_percent)}
5962
if notifier_list_ids is not None:

tests/unit/test_usage_alerts.py

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import mock
1212

1313

14-
1514
@pytest.fixture
1615
def usage_alerts_client(config):
1716
config.loadFromDict(
@@ -31,7 +30,6 @@ def usage_alerts_client(config):
3130
return client
3231

3332

34-
3533
def 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-
8280
def 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-
113110
def 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-
157154
def 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-
174170
def 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-
219215
def 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-
251246
def 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

Comments
 (0)