Some checks failed
Tests / Backend Tests (Python) (3.10) (push) Has been cancelled
Tests / Backend Tests (Python) (3.11) (push) Has been cancelled
Tests / Backend Tests (Python) (3.12) (push) Has been cancelled
Tests / Frontend Tests (JS) (push) Has been cancelled
Tests / Integration Tests (push) Has been cancelled
Tests / All Tests Passed (push) Has been cancelled
225 lines
8.6 KiB
Python
225 lines
8.6 KiB
Python
"""
|
|
Tests pour HostMetricsRepository.
|
|
"""
|
|
|
|
import pytest
|
|
from datetime import datetime, timezone, timedelta
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
|
|
class TestHostMetricsRepository:
|
|
"""Tests pour HostMetricsRepository."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_metrics(self, db_session: AsyncSession, host_factory):
|
|
"""Création de métriques."""
|
|
from app.crud.host_metrics import HostMetricsRepository
|
|
|
|
host = await host_factory.create(db_session, name="metrics-host")
|
|
|
|
repo = HostMetricsRepository(db_session)
|
|
metrics = await repo.create(
|
|
host_id=host.id,
|
|
metric_type="system_info",
|
|
cpu_usage_percent=45.5,
|
|
memory_usage_percent=60.0,
|
|
disk_root_usage_percent=75.0
|
|
)
|
|
await db_session.commit()
|
|
|
|
assert metrics.id is not None
|
|
assert metrics.host_id == host.id
|
|
assert metrics.cpu_usage_percent == 45.5
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_metrics_by_id(self, db_session: AsyncSession, host_factory):
|
|
"""Récupération par ID."""
|
|
from app.crud.host_metrics import HostMetricsRepository
|
|
|
|
host = await host_factory.create(db_session, name="metrics-host-2")
|
|
|
|
repo = HostMetricsRepository(db_session)
|
|
metrics = await repo.create(
|
|
host_id=host.id,
|
|
metric_type="system_info",
|
|
cpu_usage_percent=50.0
|
|
)
|
|
await db_session.commit()
|
|
|
|
found = await repo.get(metrics.id)
|
|
|
|
assert found is not None
|
|
assert found.cpu_usage_percent == 50.0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_latest_for_host(self, db_session: AsyncSession, host_factory):
|
|
"""Récupération des dernières métriques pour un hôte."""
|
|
from app.crud.host_metrics import HostMetricsRepository
|
|
|
|
host = await host_factory.create(db_session, name="metrics-host-3")
|
|
|
|
repo = HostMetricsRepository(db_session)
|
|
# Create older metrics
|
|
await repo.create(
|
|
host_id=host.id,
|
|
metric_type="system_info",
|
|
cpu_usage_percent=30.0
|
|
)
|
|
# Create newer metrics
|
|
await repo.create(
|
|
host_id=host.id,
|
|
metric_type="system_info",
|
|
cpu_usage_percent=60.0
|
|
)
|
|
await db_session.commit()
|
|
|
|
latest = await repo.get_latest_for_host(host.id)
|
|
|
|
assert latest is not None
|
|
# Latest is ordered by collected_at desc, both have same timestamp so order may vary
|
|
assert latest.cpu_usage_percent in [30.0, 60.0]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_latest_for_host_with_type_filter(self, db_session: AsyncSession, host_factory):
|
|
"""Récupération des dernières métriques avec filtre de type."""
|
|
from app.crud.host_metrics import HostMetricsRepository
|
|
|
|
host = await host_factory.create(db_session, name="metrics-host-4")
|
|
|
|
repo = HostMetricsRepository(db_session)
|
|
await repo.create(
|
|
host_id=host.id,
|
|
metric_type="system_info",
|
|
cpu_usage_percent=50.0
|
|
)
|
|
await repo.create(
|
|
host_id=host.id,
|
|
metric_type="network",
|
|
cpu_usage_percent=0.0
|
|
)
|
|
await db_session.commit()
|
|
|
|
latest = await repo.get_latest_for_host(host.id, metric_type="system_info")
|
|
|
|
assert latest is not None
|
|
assert latest.metric_type == "system_info"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_for_host(self, db_session: AsyncSession, host_factory):
|
|
"""Liste des métriques pour un hôte."""
|
|
from app.crud.host_metrics import HostMetricsRepository
|
|
|
|
host = await host_factory.create(db_session, name="metrics-host-5")
|
|
|
|
repo = HostMetricsRepository(db_session)
|
|
await repo.create(host_id=host.id, metric_type="system_info", cpu_usage_percent=40.0)
|
|
await repo.create(host_id=host.id, metric_type="system_info", cpu_usage_percent=50.0)
|
|
await repo.create(host_id=host.id, metric_type="system_info", cpu_usage_percent=60.0)
|
|
await db_session.commit()
|
|
|
|
metrics_list = await repo.list_for_host(host.id)
|
|
|
|
assert len(metrics_list) == 3
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_for_host_with_pagination(self, db_session: AsyncSession, host_factory):
|
|
"""Liste des métriques avec pagination."""
|
|
from app.crud.host_metrics import HostMetricsRepository
|
|
|
|
host = await host_factory.create(db_session, name="metrics-host-6")
|
|
|
|
repo = HostMetricsRepository(db_session)
|
|
for i in range(5):
|
|
await repo.create(host_id=host.id, metric_type="system_info", cpu_usage_percent=float(i * 10))
|
|
await db_session.commit()
|
|
|
|
page1 = await repo.list_for_host(host.id, limit=2, offset=0)
|
|
page2 = await repo.list_for_host(host.id, limit=2, offset=2)
|
|
|
|
assert len(page1) == 2
|
|
assert len(page2) == 2
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_count_for_host(self, db_session: AsyncSession, host_factory):
|
|
"""Comptage des métriques pour un hôte."""
|
|
from app.crud.host_metrics import HostMetricsRepository
|
|
|
|
host = await host_factory.create(db_session, name="metrics-host-7")
|
|
|
|
repo = HostMetricsRepository(db_session)
|
|
await repo.create(host_id=host.id, metric_type="system_info", cpu_usage_percent=40.0)
|
|
await repo.create(host_id=host.id, metric_type="system_info", cpu_usage_percent=50.0)
|
|
await db_session.commit()
|
|
|
|
count = await repo.count_for_host(host.id)
|
|
|
|
assert count == 2
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_count_for_host_empty(self, db_session: AsyncSession):
|
|
"""Comptage pour un hôte sans métriques."""
|
|
from app.crud.host_metrics import HostMetricsRepository
|
|
|
|
repo = HostMetricsRepository(db_session)
|
|
count = await repo.count_for_host("nonexistent-host")
|
|
|
|
assert count == 0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_all_latest(self, db_session: AsyncSession, host_factory):
|
|
"""Récupération des dernières métriques pour tous les hôtes."""
|
|
from app.crud.host_metrics import HostMetricsRepository
|
|
|
|
host1 = await host_factory.create(db_session, name="metrics-host-8a")
|
|
host2 = await host_factory.create(db_session, name="metrics-host-8b")
|
|
|
|
repo = HostMetricsRepository(db_session)
|
|
await repo.create(host_id=host1.id, metric_type="system_info", cpu_usage_percent=40.0)
|
|
await repo.create(host_id=host1.id, metric_type="system_info", cpu_usage_percent=50.0)
|
|
await repo.create(host_id=host2.id, metric_type="system_info", cpu_usage_percent=60.0)
|
|
await db_session.commit()
|
|
|
|
all_latest = await repo.get_all_latest(metric_type="system_info")
|
|
|
|
assert len(all_latest) == 2
|
|
assert host1.id in all_latest
|
|
assert host2.id in all_latest
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cleanup_old_metrics(self, db_session: AsyncSession, host_factory):
|
|
"""Nettoyage des anciennes métriques."""
|
|
from app.crud.host_metrics import HostMetricsRepository
|
|
from app.models.host_metrics import HostMetrics
|
|
|
|
host = await host_factory.create(db_session, name="metrics-host-9")
|
|
|
|
repo = HostMetricsRepository(db_session)
|
|
# Create recent metrics
|
|
await repo.create(host_id=host.id, metric_type="system_info", cpu_usage_percent=50.0)
|
|
await db_session.commit()
|
|
|
|
# Cleanup with 30 days retention (should not delete recent)
|
|
deleted = await repo.cleanup_old_metrics(days_to_keep=30)
|
|
|
|
# Recent metrics should still exist
|
|
count = await repo.count_for_host(host.id)
|
|
assert count >= 1
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_metrics_history(self, db_session: AsyncSession, host_factory):
|
|
"""Récupération de l'historique des métriques."""
|
|
from app.crud.host_metrics import HostMetricsRepository
|
|
|
|
host = await host_factory.create(db_session, name="metrics-host-10")
|
|
|
|
repo = HostMetricsRepository(db_session)
|
|
await repo.create(host_id=host.id, metric_type="system_info", cpu_usage_percent=40.0)
|
|
await repo.create(host_id=host.id, metric_type="system_info", cpu_usage_percent=50.0)
|
|
await db_session.commit()
|
|
|
|
history = await repo.get_metrics_history(host.id, metric_type="system_info", hours=24)
|
|
|
|
assert len(history) >= 2
|