diff --git a/backend/requirements.txt b/backend/requirements.txt index 04b9295..4b0f368 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -11,6 +11,8 @@ pytest>=7.4.0 pytest-cov>=4.1.0 pytest-timeout>=2.1.0 pytest-xdist>=3.3.1 +pytest-mock>=3.10.0 httpx>=0.24.1 redis>=4.6.0 +aiogram>=3.4.0 python-telegram-bot>=20.4 diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index c61a14c..6ee4be7 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -1,68 +1,94 @@ """Test configuration""" import os import pytest -from sqlalchemy import create_engine, text -from sqlalchemy.orm import sessionmaker, scoped_session from fastapi.testclient import TestClient -from typing import Generator, Any +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from sqlalchemy.pool import StaticPool -# Устанавливаем переменную окружения для тестов -os.environ["TESTING"] = "1" - -from app.database import Base +from app.core.config import settings +from app.db.base import Base from app.main import app -from app.core.test_config import test_settings -from app.dependencies import get_db -from .test_fixtures import * # импортируем все фикстуры +from app.deps import get_db +from app.models.employee import Employee +from app.utils.security import get_password_hash -# Создаем тестовый движок базы данных -DATABASE_URL = os.getenv("DATABASE_URL", test_settings.get_database_url()) -engine = create_engine( - DATABASE_URL, - pool_size=5, - max_overflow=10, - pool_timeout=30, - pool_pre_ping=True -) +# Mock Telegram notifications +@pytest.fixture(autouse=True) +def mock_telegram_bot(mocker): + """Mock Telegram Bot""" + mock_bot = mocker.patch('app.utils.telegram.Bot') + return mock_bot -# Создаем тестовую фабрику сессий -TestingSessionLocal = scoped_session( - sessionmaker(autocommit=False, autoflush=False, bind=engine) -) +@pytest.fixture(autouse=True) +def mock_telegram_notify(mocker): + """Mock Telegram notifications""" + mocker.patch('app.utils.telegram.notify_new_request', return_value=None) -@pytest.fixture(scope="session", autouse=True) -def setup_test_db() -> Generator[None, Any, None]: - """Setup test database""" - # Создаем все таблицы - Base.metadata.drop_all(bind=engine) +# Database fixtures +@pytest.fixture(scope="session") +def engine(): + """Create test database engine""" + engine = create_engine( + "sqlite:///:memory:", + connect_args={"check_same_thread": False}, + poolclass=StaticPool, + ) Base.metadata.create_all(bind=engine) - yield - # Удаляем все таблицы и закрываем соединения - TestingSessionLocal.remove() - Base.metadata.drop_all(bind=engine) - engine.dispose() + return engine @pytest.fixture(scope="function") -def db_session() -> Generator[Any, Any, None]: - """Get database session""" - session = TestingSessionLocal() +def db_session(engine): + """Create test database session""" + Session = sessionmaker(bind=engine) + session = Session() try: yield session finally: session.rollback() session.close() - TestingSessionLocal.remove() -@pytest.fixture(scope="function") -def client(db_session: Any) -> Generator[TestClient, Any, None]: - """Get test client""" - def override_get_db() -> Generator[Any, Any, None]: +@pytest.fixture +def client(db_session): + """Create test client""" + def override_get_db(): try: yield db_session finally: pass - app.dependency_overrides[get_db] = override_get_db - with TestClient(app) as test_client: - yield test_client - app.dependency_overrides.clear() \ No newline at end of file + return TestClient(app) + +@pytest.fixture +def test_employee(db_session): + """Create test employee""" + employee = Employee( + first_name="Test", + last_name="User", + department="IT", + office="Main", + hashed_password=get_password_hash("testpass123"), + is_active=True, + is_admin=False + ) + db_session.add(employee) + db_session.commit() + db_session.refresh(employee) + return employee + +@pytest.fixture +def test_admin(db_session): + """Create test admin""" + admin = Employee( + first_name="Admin", + last_name="User", + department="IT", + office="Main", + hashed_password=get_password_hash("adminpass123"), + is_active=True, + is_admin=True + ) + db_session.add(admin) + db_session.commit() + db_session.refresh(admin) + return admin \ No newline at end of file