|
9 | 9 | import components.http_server as http_server |
10 | 10 | import components.monitors_loader as monitors_loader |
11 | 11 | import databases as databases |
12 | | -from models import CodeModule, Monitor |
| 12 | +from models import Alert, AlertStatus, CodeModule, Monitor |
13 | 13 |
|
14 | 14 | pytestmark = pytest.mark.asyncio(loop_scope="session") |
15 | 15 |
|
@@ -37,10 +37,103 @@ async def test_list_monitors(clear_database, sample_monitor: Monitor): |
37 | 37 | "id": sample_monitor.id, |
38 | 38 | "name": sample_monitor.name, |
39 | 39 | "enabled": sample_monitor.enabled, |
| 40 | + "active_alerts": 0, |
40 | 41 | }, |
41 | 42 | ] |
42 | 43 |
|
43 | 44 |
|
| 45 | +@pytest.mark.parametrize("active_alerts", range(1, 5)) |
| 46 | +async def test_list_monitors_with_alerts(clear_database, sample_monitor: Monitor, active_alerts): |
| 47 | + """The 'monitor list' route should return a list of all monitors and the count of active alerts |
| 48 | + for them""" |
| 49 | + await Alert.create_batch( |
| 50 | + Alert( |
| 51 | + monitor_id=sample_monitor.id, |
| 52 | + priority=i, |
| 53 | + acknowledge_priority=5 - i, |
| 54 | + ) |
| 55 | + for i in range(active_alerts) |
| 56 | + ) |
| 57 | + await Alert.create( |
| 58 | + monitor_id=sample_monitor.id, |
| 59 | + status=AlertStatus.solved, |
| 60 | + priority=1, |
| 61 | + acknowledge_priority=1, |
| 62 | + ) |
| 63 | + |
| 64 | + url = BASE_URL + "/list" |
| 65 | + async with aiohttp.ClientSession() as session: |
| 66 | + async with session.get(url) as response: |
| 67 | + response_data = await response.json() |
| 68 | + |
| 69 | + assert response_data == [ |
| 70 | + { |
| 71 | + "id": sample_monitor.id, |
| 72 | + "name": sample_monitor.name, |
| 73 | + "enabled": sample_monitor.enabled, |
| 74 | + "active_alerts": active_alerts, |
| 75 | + }, |
| 76 | + ] |
| 77 | + |
| 78 | + |
| 79 | +async def test_list_monitors_not_enabled(clear_database, sample_monitor: Monitor): |
| 80 | + """The 'monitor list' route should return a list of all monitors and the count of active alerts |
| 81 | + for them""" |
| 82 | + await Alert.create( |
| 83 | + monitor_id=sample_monitor.id, |
| 84 | + priority=1, |
| 85 | + acknowledge_priority=1, |
| 86 | + ) |
| 87 | + sample_monitor.enabled = False |
| 88 | + await sample_monitor.save() |
| 89 | + |
| 90 | + url = BASE_URL + "/list" |
| 91 | + async with aiohttp.ClientSession() as session: |
| 92 | + async with session.get(url) as response: |
| 93 | + response_data = await response.json() |
| 94 | + |
| 95 | + assert response_data == [ |
| 96 | + { |
| 97 | + "id": sample_monitor.id, |
| 98 | + "name": sample_monitor.name, |
| 99 | + "enabled": sample_monitor.enabled, |
| 100 | + "active_alerts": 0, |
| 101 | + }, |
| 102 | + ] |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.parametrize("alerts_number", [1, 2]) |
| 106 | +async def test_list_monitor_active_alerts(clear_database, alerts_number, sample_monitor: Monitor): |
| 107 | + """The 'monitor active alerts' route should return a list of all active alerts for a monitor""" |
| 108 | + alerts = await Alert.create_batch( |
| 109 | + Alert( |
| 110 | + monitor_id=sample_monitor.id, |
| 111 | + priority=i, |
| 112 | + acknowledge_priority=5 - 1, |
| 113 | + ) |
| 114 | + for i in range(alerts_number) |
| 115 | + ) |
| 116 | + |
| 117 | + url = BASE_URL + f"/{sample_monitor.id}/alerts" |
| 118 | + async with aiohttp.ClientSession() as session: |
| 119 | + async with session.get(url) as response: |
| 120 | + response_data = await response.json() |
| 121 | + |
| 122 | + assert isinstance(response_data, list) |
| 123 | + assert len(response_data) == alerts_number |
| 124 | + for alert, response_alert in zip(alerts, response_data): |
| 125 | + assert alert.id == response_alert["id"] |
| 126 | + assert alert.status == response_alert["status"] |
| 127 | + assert alert.acknowledged == response_alert["acknowledged"] |
| 128 | + assert alert.locked == response_alert["locked"] |
| 129 | + assert alert.priority == response_alert["priority"] |
| 130 | + assert alert.acknowledge_priority == response_alert["acknowledge_priority"] |
| 131 | + assert alert.can_acknowledge == response_alert["can_acknowledge"] |
| 132 | + assert alert.can_lock == response_alert["can_lock"] |
| 133 | + assert alert.can_solve == response_alert["can_solve"] |
| 134 | + assert alert.created_at.strftime("%Y-%m-%d %H:%M:%S") == response_alert["created_at"] |
| 135 | + |
| 136 | + |
44 | 137 | async def test_get_monitor(clear_database, sample_monitor: Monitor): |
45 | 138 | """The 'monitor get' route should return the monitor attributes and code information""" |
46 | 139 | code_module = await CodeModule.get(CodeModule.monitor_id == sample_monitor.id) |
|
0 commit comments