homelab_automation/tests/backend/test_schemas.py
Bruno Charest ecefbc8611
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
Clean up test files and debug artifacts, add node_modules to gitignore, export DashboardManager for testing, and enhance pytest configuration with comprehensive test markers and settings
2025-12-15 08:15:49 -05:00

211 lines
5.3 KiB
Python

"""
Tests pour les schémas Pydantic.
Couvre:
- Validation des données
- Sérialisation/désérialisation
- Valeurs par défaut
"""
import pytest
from datetime import datetime, timezone
from pydantic import ValidationError
pytestmark = pytest.mark.unit
class TestHostSchemas:
"""Tests pour les schémas Host."""
def test_host_create_valid(self):
"""Création valide."""
from app.schemas.host import HostCreate
host = HostCreate(
name="test-host.local",
ip_address="192.168.1.100"
)
assert host.name == "test-host.local"
assert host.ip_address == "192.168.1.100"
def test_host_out(self):
"""Sortie host."""
from app.schemas.host import HostOut
host = HostOut(
id="host-1",
name="test.local",
ip_address="10.0.0.1",
status="online",
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc)
)
assert host.id == "host-1"
class TestScheduleSchemas:
"""Tests pour les schémas Schedule."""
def test_schedule_create_valid(self):
"""Création valide."""
from app.schemas.schedule import ScheduleCreate
schedule = ScheduleCreate(
name="Daily Backup",
playbook="backup.yml",
target="all",
schedule_type="recurring"
)
assert schedule.name == "Daily Backup"
def test_schedule_out(self):
"""Sortie schedule."""
from app.schemas.schedule import ScheduleOut
schedule = ScheduleOut(
id="sched-1",
name="Test",
playbook="test.yml",
target="all",
schedule_type="recurring",
enabled=True,
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc)
)
assert schedule.enabled is True
class TestAuthSchemas:
"""Tests pour les schémas Auth."""
def test_login_request_valid(self):
"""Login valide."""
from app.schemas.auth import LoginRequest
login = LoginRequest(
username="testuser",
password="password123"
)
assert login.username == "testuser"
def test_token_response(self):
"""Réponse token."""
from app.schemas.auth import Token
token = Token(
access_token="jwt.token.here",
token_type="bearer",
expires_in=3600
)
assert token.token_type == "bearer"
assert token.expires_in == 3600
def test_user_out(self):
"""Sortie utilisateur."""
from app.schemas.auth import UserOut
user = UserOut(
id=1,
username="testuser",
email="test@example.com",
role="admin",
is_active=True,
is_superuser=False,
created_at=datetime.now(timezone.utc)
)
assert user.role == "admin"
def test_token_data(self):
"""Données de token."""
from app.schemas.auth import TokenData
data = TokenData(
username="testuser",
user_id=1,
role="admin"
)
assert data.username == "testuser"
class TestNotificationSchemas:
"""Tests pour les schémas Notification."""
def test_ntfy_config_defaults(self):
"""Configuration par défaut."""
from app.schemas.notification import NtfyConfig
config = NtfyConfig()
assert config.base_url is not None
def test_notification_request_minimal(self):
"""Requête minimale."""
from app.schemas.notification import NotificationRequest
req = NotificationRequest(message="Test message")
assert req.message == "Test message"
def test_notification_request_with_priority(self):
"""Requête avec priorité."""
from app.schemas.notification import NotificationRequest
req = NotificationRequest(message="Test", priority=5)
assert req.priority == 5
class TestHealthSchemas:
"""Tests pour les schémas Health."""
def test_health_check(self):
"""Health check."""
from app.schemas.health import HealthCheck
check = HealthCheck(
host="test-host",
ssh_ok=True,
ansible_ok=True,
sudo_ok=True,
reachable=True
)
assert check.reachable is True
class TestCommonSchemas:
"""Tests pour les schémas communs."""
def test_log_entry(self):
"""Entrée de log."""
from app.schemas.common import LogEntry
entry = LogEntry(
timestamp=datetime.now(timezone.utc),
level="INFO",
message="Test message"
)
assert entry.level == "INFO"
def test_system_metrics(self):
"""Métriques système."""
from app.schemas.common import SystemMetrics
metrics = SystemMetrics(
online_hosts=5,
total_tasks=100,
success_rate=95.5,
cpu_usage=25.5
)
assert metrics.online_hosts == 5
assert metrics.cpu_usage == 25.5