first commit
This commit is contained in:
131
backend/tests/test_models.py
Normal file
131
backend/tests/test_models.py
Normal file
@@ -0,0 +1,131 @@
|
||||
"""Unit tests for models"""
|
||||
|
||||
import pytest
|
||||
from datetime import datetime
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from app.models.user import User
|
||||
from app.models.alert import Alert, AlertSeverity, AlertStatus
|
||||
from app.models.task import CollectionTask
|
||||
|
||||
|
||||
class TestUserModel:
|
||||
"""Tests for User model"""
|
||||
|
||||
def test_user_creation(self):
|
||||
"""Test user model creation"""
|
||||
user = User(
|
||||
id=1,
|
||||
username="testuser",
|
||||
email="test@example.com",
|
||||
password_hash="hashed_password",
|
||||
role="admin",
|
||||
is_active=True,
|
||||
)
|
||||
assert user.id == 1
|
||||
assert user.username == "testuser"
|
||||
assert user.email == "test@example.com"
|
||||
assert user.is_active is True
|
||||
|
||||
def test_user_role_assignment(self):
|
||||
"""Test user role assignment"""
|
||||
user = User(
|
||||
username="testuser",
|
||||
email="test@example.com",
|
||||
password_hash="hashed",
|
||||
role="admin",
|
||||
)
|
||||
assert user.role == "admin"
|
||||
|
||||
def test_user_password_hash(self):
|
||||
"""Test user password hash attribute"""
|
||||
user = User(
|
||||
username="testuser",
|
||||
email="test@example.com",
|
||||
password_hash="hashed_password",
|
||||
)
|
||||
assert user.password_hash == "hashed_password"
|
||||
|
||||
|
||||
class TestAlertModel:
|
||||
"""Tests for Alert model"""
|
||||
|
||||
def test_alert_creation(self):
|
||||
"""Test alert model creation"""
|
||||
alert = Alert(
|
||||
id=1,
|
||||
severity=AlertSeverity.WARNING,
|
||||
status=AlertStatus.ACTIVE,
|
||||
message="Test alert message",
|
||||
datasource_id=1,
|
||||
datasource_name="Test Source",
|
||||
)
|
||||
assert alert.id == 1
|
||||
assert alert.severity == AlertSeverity.WARNING
|
||||
assert alert.status == AlertStatus.ACTIVE
|
||||
assert alert.message == "Test alert message"
|
||||
|
||||
def test_alert_to_dict(self):
|
||||
"""Test alert to_dict method"""
|
||||
alert = Alert(
|
||||
id=1,
|
||||
severity=AlertSeverity.CRITICAL,
|
||||
status=AlertStatus.ACTIVE,
|
||||
message="Critical alert",
|
||||
datasource_id=2,
|
||||
datasource_name="Test Source",
|
||||
created_at=datetime(2024, 1, 1, 12, 0, 0),
|
||||
)
|
||||
result = alert.to_dict()
|
||||
assert result["id"] == 1
|
||||
assert result["severity"] == "critical"
|
||||
assert result["status"] == "active"
|
||||
assert result["message"] == "Critical alert"
|
||||
assert result["created_at"] == "2024-01-01T12:00:00"
|
||||
|
||||
def test_alert_severity_enum(self):
|
||||
"""Test alert severity enum values"""
|
||||
assert AlertSeverity.CRITICAL.value == "critical"
|
||||
assert AlertSeverity.WARNING.value == "warning"
|
||||
assert AlertSeverity.INFO.value == "info"
|
||||
|
||||
def test_alert_status_enum(self):
|
||||
"""Test alert status enum values"""
|
||||
assert AlertStatus.ACTIVE.value == "active"
|
||||
assert AlertStatus.ACKNOWLEDGED.value == "acknowledged"
|
||||
assert AlertStatus.RESOLVED.value == "resolved"
|
||||
|
||||
|
||||
class TestCollectionTaskModel:
|
||||
"""Tests for CollectionTask model"""
|
||||
|
||||
def test_task_creation(self):
|
||||
"""Test collection task creation"""
|
||||
task = CollectionTask(
|
||||
id=1,
|
||||
datasource_id=1,
|
||||
status="running",
|
||||
records_processed=0,
|
||||
started_at=datetime.utcnow(),
|
||||
)
|
||||
assert task.id == 1
|
||||
assert task.datasource_id == 1
|
||||
assert task.status == "running"
|
||||
|
||||
def test_task_with_records(self):
|
||||
"""Test collection task with records processed"""
|
||||
task = CollectionTask(
|
||||
datasource_id=1,
|
||||
status="success",
|
||||
records_processed=100,
|
||||
)
|
||||
assert task.records_processed == 100
|
||||
|
||||
def test_task_error_message(self):
|
||||
"""Test collection task with error message"""
|
||||
task = CollectionTask(
|
||||
datasource_id=1,
|
||||
status="failed",
|
||||
error_message="Connection timeout",
|
||||
)
|
||||
assert task.error_message == "Connection timeout"
|
||||
Reference in New Issue
Block a user