2026-02-25 00:06:34 +02:00
|
|
|
import uuid
|
2026-02-24 22:48:40 +02:00
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
2026-02-24 18:19:56 +02:00
|
|
|
|
2026-02-24 22:48:40 +02:00
|
|
|
import pytest
|
2026-02-24 18:19:56 +02:00
|
|
|
import pytest_asyncio
|
|
|
|
|
from sqlalchemy import text
|
2026-02-24 22:48:40 +02:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
2026-02-24 18:19:56 +02:00
|
|
|
from sqlalchemy.pool import NullPool
|
|
|
|
|
|
2026-02-27 00:17:39 +05:30
|
|
|
from app.config import config as app_config
|
2026-02-26 03:05:20 +05:30
|
|
|
from app.db import (
|
|
|
|
|
Base,
|
|
|
|
|
DocumentType,
|
|
|
|
|
SearchSourceConnector,
|
|
|
|
|
SearchSourceConnectorType,
|
|
|
|
|
SearchSpace,
|
|
|
|
|
User,
|
|
|
|
|
)
|
2026-02-25 08:29:53 +02:00
|
|
|
from app.indexing_pipeline.connector_document import ConnectorDocument
|
2026-02-27 00:45:51 +05:30
|
|
|
from tests.conftest import TEST_DATABASE_URL
|
2026-02-24 18:19:56 +02:00
|
|
|
|
2026-02-27 00:17:39 +05:30
|
|
|
_EMBEDDING_DIM = app_config.embedding_model_instance.dimension
|
2026-02-24 22:48:40 +02:00
|
|
|
|
2026-02-24 18:19:56 +02:00
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="session")
|
|
|
|
|
async def async_engine():
|
2026-02-25 00:06:34 +02:00
|
|
|
engine = create_async_engine(
|
|
|
|
|
TEST_DATABASE_URL,
|
|
|
|
|
poolclass=NullPool,
|
|
|
|
|
echo=False,
|
|
|
|
|
# Required for asyncpg + savepoints: disables prepared statement cache
|
|
|
|
|
# to prevent "another operation is in progress" errors during savepoint rollbacks.
|
|
|
|
|
connect_args={"prepared_statement_cache_size": 0},
|
|
|
|
|
)
|
2026-02-24 18:19:56 +02:00
|
|
|
|
|
|
|
|
async with engine.begin() as conn:
|
|
|
|
|
await conn.execute(text("CREATE EXTENSION IF NOT EXISTS vector"))
|
|
|
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
|
|
|
|
|
|
yield engine
|
|
|
|
|
|
2026-02-25 00:06:34 +02:00
|
|
|
# drop_all fails on circular FKs (new_chat_threads ↔ public_chat_snapshots).
|
|
|
|
|
# DROP SCHEMA CASCADE handles this without needing topological sort.
|
2026-02-24 18:19:56 +02:00
|
|
|
async with engine.begin() as conn:
|
2026-02-25 00:06:34 +02:00
|
|
|
await conn.execute(text("DROP SCHEMA public CASCADE"))
|
|
|
|
|
await conn.execute(text("CREATE SCHEMA public"))
|
2026-02-24 18:19:56 +02:00
|
|
|
|
|
|
|
|
await engine.dispose()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
|
|
|
async def db_session(async_engine) -> AsyncSession:
|
|
|
|
|
# Bind the session to a connection that holds an outer transaction.
|
|
|
|
|
# join_transaction_mode="create_savepoint" makes session.commit() release
|
|
|
|
|
# a SAVEPOINT instead of committing the outer transaction, so the final
|
|
|
|
|
# transaction.rollback() undoes everything — including commits made by the
|
|
|
|
|
# service under test — leaving the DB clean for the next test.
|
|
|
|
|
async with async_engine.connect() as conn:
|
|
|
|
|
transaction = await conn.begin()
|
|
|
|
|
async with AsyncSession(
|
|
|
|
|
bind=conn,
|
|
|
|
|
expire_on_commit=False,
|
|
|
|
|
join_transaction_mode="create_savepoint",
|
|
|
|
|
) as session:
|
|
|
|
|
yield session
|
|
|
|
|
await transaction.rollback()
|
2026-02-24 22:48:40 +02:00
|
|
|
|
|
|
|
|
|
2026-02-25 00:06:34 +02:00
|
|
|
@pytest_asyncio.fixture
|
|
|
|
|
async def db_user(db_session: AsyncSession) -> User:
|
|
|
|
|
user = User(
|
|
|
|
|
id=uuid.uuid4(),
|
|
|
|
|
email="test@surfsense.net",
|
|
|
|
|
hashed_password="hashed",
|
|
|
|
|
is_active=True,
|
|
|
|
|
is_superuser=False,
|
|
|
|
|
is_verified=True,
|
|
|
|
|
)
|
|
|
|
|
db_session.add(user)
|
|
|
|
|
await db_session.flush()
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
2026-02-25 08:29:53 +02:00
|
|
|
@pytest_asyncio.fixture
|
2026-02-26 03:05:20 +05:30
|
|
|
async def db_connector(
|
|
|
|
|
db_session: AsyncSession, db_user: User, db_search_space: "SearchSpace"
|
|
|
|
|
) -> SearchSourceConnector:
|
2026-02-25 08:29:53 +02:00
|
|
|
connector = SearchSourceConnector(
|
|
|
|
|
name="Test Connector",
|
|
|
|
|
connector_type=SearchSourceConnectorType.CLICKUP_CONNECTOR,
|
|
|
|
|
config={},
|
|
|
|
|
search_space_id=db_search_space.id,
|
|
|
|
|
user_id=db_user.id,
|
|
|
|
|
)
|
|
|
|
|
db_session.add(connector)
|
|
|
|
|
await db_session.flush()
|
|
|
|
|
return connector
|
|
|
|
|
|
|
|
|
|
|
2026-02-25 00:06:34 +02:00
|
|
|
@pytest_asyncio.fixture
|
|
|
|
|
async def db_search_space(db_session: AsyncSession, db_user: User) -> SearchSpace:
|
|
|
|
|
space = SearchSpace(
|
|
|
|
|
name="Test Space",
|
|
|
|
|
user_id=db_user.id,
|
|
|
|
|
)
|
|
|
|
|
db_session.add(space)
|
|
|
|
|
await db_session.flush()
|
|
|
|
|
return space
|
|
|
|
|
|
|
|
|
|
|
2026-02-24 22:48:40 +02:00
|
|
|
@pytest.fixture
|
2026-02-25 01:40:30 +02:00
|
|
|
def patched_summarize(monkeypatch) -> AsyncMock:
|
|
|
|
|
mock = AsyncMock(return_value="Mocked summary.")
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
"app.indexing_pipeline.indexing_pipeline_service.summarize_document",
|
|
|
|
|
mock,
|
|
|
|
|
)
|
|
|
|
|
return mock
|
2026-02-24 22:48:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2026-02-25 01:40:30 +02:00
|
|
|
def patched_summarize_raises(monkeypatch) -> AsyncMock:
|
|
|
|
|
mock = AsyncMock(side_effect=RuntimeError("LLM unavailable"))
|
2026-02-25 00:30:11 +02:00
|
|
|
monkeypatch.setattr(
|
2026-02-25 01:40:30 +02:00
|
|
|
"app.indexing_pipeline.indexing_pipeline_service.summarize_document",
|
2026-02-25 00:30:11 +02:00
|
|
|
mock,
|
|
|
|
|
)
|
|
|
|
|
return mock
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2026-02-25 01:40:30 +02:00
|
|
|
def patched_embed_text(monkeypatch) -> MagicMock:
|
|
|
|
|
mock = MagicMock(return_value=[0.1] * _EMBEDDING_DIM)
|
2026-02-25 00:30:11 +02:00
|
|
|
monkeypatch.setattr(
|
2026-02-25 01:40:30 +02:00
|
|
|
"app.indexing_pipeline.indexing_pipeline_service.embed_text",
|
2026-02-25 00:30:11 +02:00
|
|
|
mock,
|
2026-02-24 22:48:40 +02:00
|
|
|
)
|
2026-02-25 00:30:11 +02:00
|
|
|
return mock
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2026-02-25 01:40:30 +02:00
|
|
|
def patched_chunk_text(monkeypatch) -> MagicMock:
|
|
|
|
|
mock = MagicMock(return_value=["Test chunk content."])
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
"app.indexing_pipeline.indexing_pipeline_service.chunk_text",
|
|
|
|
|
mock,
|
|
|
|
|
)
|
|
|
|
|
return mock
|
|
|
|
|
|
2026-02-25 00:30:11 +02:00
|
|
|
|
2026-02-25 08:29:53 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def make_connector_document(db_connector, db_user):
|
|
|
|
|
"""Integration-scoped override: uses real DB connector and user IDs."""
|
2026-02-26 03:05:20 +05:30
|
|
|
|
2026-02-25 08:29:53 +02:00
|
|
|
def _make(**overrides):
|
|
|
|
|
defaults = {
|
|
|
|
|
"title": "Test Document",
|
|
|
|
|
"source_markdown": "## Heading\n\nSome content.",
|
|
|
|
|
"unique_id": "test-id-001",
|
|
|
|
|
"document_type": DocumentType.CLICKUP_CONNECTOR,
|
|
|
|
|
"search_space_id": db_connector.search_space_id,
|
|
|
|
|
"connector_id": db_connector.id,
|
|
|
|
|
"created_by_id": str(db_user.id),
|
|
|
|
|
}
|
|
|
|
|
defaults.update(overrides)
|
|
|
|
|
return ConnectorDocument(**defaults)
|
|
|
|
|
|
2026-02-26 03:05:20 +05:30
|
|
|
return _make
|