Skip to content

Commit 0d35793

Browse files
committed
Fixed get_alerts and improved tests - unstable due to server bug with user alerts
1 parent ad7bd24 commit 0d35793

File tree

2 files changed

+65
-24
lines changed

2 files changed

+65
-24
lines changed

simvue/client.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -990,17 +990,23 @@ def get_alerts(
990990
RuntimeError
991991
if there was a failure retrieving data from the server
992992
"""
993-
994993
if not run_id:
994+
if critical_only:
995+
raise RuntimeError(
996+
"critical_only is ambiguous when returning alerts with no run ID specified."
997+
)
995998
return [alert.name if names_only else alert for _, alert in Alert.get()] # type: ignore
996999

997-
return [
998-
alert.get("name")
999-
if names_only
1000-
else Alert(identifier=alert.get("id"), **alert)
1000+
_alerts = [
1001+
Alert(identifier=alert.get("id"), **alert)
10011002
for alert in Run(identifier=run_id).get_alert_details()
1002-
if not critical_only or alert["status"].get("current") == "critical"
1003-
] # type: ignore
1003+
]
1004+
1005+
return [
1006+
alert.name if names_only else alert
1007+
for alert in _alerts
1008+
if not critical_only or alert.get_status(run_id) == "critical"
1009+
]
10041010

10051011
@prettify_pydantic
10061012
@pydantic.validate_call

tests/functional/test_client.py

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from simvue.exception import ObjectNotFoundError
1313
import simvue.run as sv_run
1414
import simvue.api.objects as sv_api_obj
15-
15+
from simvue.api.objects.alert.base import AlertBase
1616

1717
@pytest.mark.dependency
1818
@pytest.mark.client
@@ -24,25 +24,60 @@ def test_get_events(create_test_run: tuple[sv_run.Run, dict]) -> None:
2424
@pytest.mark.dependency
2525
@pytest.mark.client
2626
@pytest.mark.parametrize(
27-
"from_run", (True, False)
27+
"from_run", (True, False), ids=("from_run", "all_runs")
2828
)
29-
def test_get_alerts(create_test_run: tuple[sv_run.Run, dict], from_run: bool) -> None:
30-
time.sleep(1.0)
29+
@pytest.mark.parametrize(
30+
"names_only", (True, False), ids=("names_only", "all_details")
31+
)
32+
@pytest.mark.parametrize(
33+
"critical_only", (True, False), ids=("critical_only", "all_states")
34+
)
35+
def test_get_alerts(create_plain_run: tuple[sv_run.Run, dict], from_run: bool, names_only: bool, critical_only: bool) -> None:
36+
run, run_data = create_plain_run
37+
run_id = run.id
38+
unique_id = f"{uuid.uuid4()}".split("-")[0]
39+
_id_1 = run.create_user_alert(
40+
name=f"user_alert_1_{unique_id}",
41+
)
42+
_id_2 = run.create_user_alert(
43+
name=f"user_alert_2_{unique_id}",
44+
)
45+
_id_3 = run.create_user_alert(
46+
name=f"user_alert_3_{unique_id}",
47+
attach_to_run=False
48+
)
49+
run.log_alert(identifier=_id_1, state="critical")
50+
time.sleep(2)
51+
run.close()
52+
3153
client = svc.Client()
32-
_, run_data = create_test_run
33-
if from_run:
34-
triggered_alerts_full = client.get_alerts(run_id=create_test_run[1]["run_id"], critical_only=False, names_only=False)
35-
assert len(triggered_alerts_full) == 7
36-
for alert in triggered_alerts_full:
37-
if alert.name == "value_above_1":
38-
assert alert["alert"]["status"]["current"] == "critical"
54+
55+
if critical_only and not from_run:
56+
with pytest.raises(RuntimeError) as e:
57+
_alerts = client.get_alerts(run_id=run_id if from_run else None, critical_only=critical_only, names_only=names_only)
58+
assert "critical_only is ambiguous when returning alerts with no run ID specified." in str(e.value)
3959
else:
40-
assert (triggered_alerts_full := client.get_alerts(names_only=True, critical_only=False))
41-
42-
for alert in run_data["created_alerts"]:
43-
assert alert in triggered_alerts_full, f"Alert '{alert}' was not triggered"
44-
45-
60+
_alerts = client.get_alerts(run_id=run_id if from_run else None, critical_only=critical_only, names_only=names_only)
61+
62+
if names_only:
63+
assert all(isinstance(item, str) for item in _alerts)
64+
else:
65+
assert all(isinstance(item, AlertBase) for item in _alerts)
66+
_alerts = [alert.name for alert in _alerts]
67+
68+
assert f"user_alert_1_{unique_id}" in _alerts
69+
70+
if not from_run:
71+
assert len(_alerts) > 2
72+
assert f"user_alert_3_{unique_id}" in _alerts
73+
else:
74+
assert f"user_alert_3_{unique_id}" not in _alerts
75+
if critical_only:
76+
assert len(_alerts) == 1
77+
else:
78+
assert len(_alerts) == 2
79+
assert f"user_alert_2_{unique_id}" in _alerts
80+
4681
@pytest.mark.dependency
4782
@pytest.mark.client
4883
def test_get_run_id_from_name(create_test_run: tuple[sv_run.Run, dict]) -> None:

0 commit comments

Comments
 (0)