core/tests/conftest.py

41 lines
924 B
Python
Raw Normal View History

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