Files
core/tests/conftest.py

51 lines
1.1 KiB
Python
Raw Normal View History

2025-02-09 22:26:50 +03:00
import asyncio
2025-05-29 12:37:39 +03:00
2025-02-09 22:26:50 +03:00
import pytest
2025-05-29 12:37:39 +03:00
2025-02-09 22:26:50 +03:00
from services.redis import redis
2025-05-16 09:11:39 +03:00
from tests.test_config import get_test_client
2025-02-09 22:26:50 +03:00
2025-02-11 12:00:35 +03:00
2025-02-09 22:26:50 +03:00
@pytest.fixture(scope="session")
def event_loop():
"""Create an instance of the default event loop for the test session."""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
2025-02-11 12:00:35 +03:00
2025-02-09 22:26:50 +03:00
@pytest.fixture(scope="session")
2025-05-16 09:11:39 +03:00
def test_app():
"""Create a test client and session factory."""
client, SessionLocal = get_test_client()
return client, SessionLocal
2025-02-09 22:26:50 +03:00
2025-02-11 12:00:35 +03:00
2025-02-09 22:26:50 +03:00
@pytest.fixture
2025-05-16 09:11:39 +03:00
def db_session(test_app):
2025-02-09 22:26:50 +03:00
"""Create a new database session for a test."""
2025-05-16 09:11:39 +03:00
_, SessionLocal = test_app
session = SessionLocal()
2025-02-11 12:00:35 +03:00
2025-02-09 22:26:50 +03:00
yield session
2025-02-11 12:00:35 +03:00
2025-05-16 09:11:39 +03:00
session.rollback()
2025-02-09 22:26:50 +03:00
session.close()
2025-05-16 09:11:39 +03:00
@pytest.fixture
def test_client(test_app):
"""Get the test client."""
client, _ = test_app
return client
2025-02-09 22:26:50 +03:00
2025-02-11 12:00:35 +03:00
2025-02-09 22:26:50 +03:00
@pytest.fixture
async def redis_client():
"""Create a test Redis client."""
await redis.connect()
2025-05-16 09:11:39 +03:00
await redis.flushall() # Очищаем Redis перед каждым тестом
2025-02-09 22:26:50 +03:00
yield redis
2025-05-16 09:11:39 +03:00
await redis.flushall() # Очищаем после теста
2025-02-09 22:26:50 +03:00
await redis.disconnect()