core/tests/conftest.py

61 lines
1.3 KiB
Python
Raw Permalink Normal View History

2025-02-09 19:26:50 +00:00
import asyncio
import os
2025-02-11 09:00:35 +00:00
2025-02-09 19:26:50 +00:00
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from starlette.testclient import TestClient
from main import app
from services.db import Base
from services.redis import redis
# Use SQLite for testing
TEST_DB_URL = "sqlite:///test.db"
2025-02-11 09:00:35 +00:00
2025-02-09 19:26:50 +00: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 09:00:35 +00:00
2025-02-09 19:26:50 +00:00
@pytest.fixture(scope="session")
def test_engine():
"""Create a test database engine."""
engine = create_engine(TEST_DB_URL)
Base.metadata.create_all(engine)
yield engine
Base.metadata.drop_all(engine)
os.remove("test.db")
2025-02-11 09:00:35 +00:00
2025-02-09 19:26:50 +00:00
@pytest.fixture
def db_session(test_engine):
"""Create a new database session for a test."""
connection = test_engine.connect()
transaction = connection.begin()
session = Session(bind=connection)
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-02-09 19:26:50 +00:00
session.close()
transaction.rollback()
connection.close()
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()
yield redis
await redis.disconnect()
2025-02-11 09:00:35 +00:00
2025-02-09 19:26:50 +00:00
@pytest.fixture
def test_client():
"""Create a TestClient instance."""
2025-02-11 09:00:35 +00:00
return TestClient(app)