from __future__ import annotations import pytest from core.config.settings import QdrantSettings from services.base.qdrant import QdrantService class _FakeCollection: def __init__(self, name: str) -> None: self.name = name class _FakeCollections: def __init__(self) -> None: self.collections = [_FakeCollection("default")] class _FakeQdrantClient: def get_collections(self) -> _FakeCollections: return _FakeCollections() @pytest.mark.asyncio async def test_initialize_success(monkeypatch: pytest.MonkeyPatch) -> None: service = QdrantService(settings=QdrantSettings(host="localhost", port=6333)) def _build_client(_: QdrantService) -> _FakeQdrantClient: return _FakeQdrantClient() monkeypatch.setattr(QdrantService, "_build_client", _build_client) result = await service.initialize() assert result is True assert service.is_initialized is True health = await service.health_check() assert health["status"] == "healthy" @pytest.mark.asyncio async def test_initialize_failure(monkeypatch: pytest.MonkeyPatch) -> None: service = QdrantService(settings=QdrantSettings(host="localhost", port=6333)) def _build_client(_: QdrantService) -> _FakeQdrantClient: raise RuntimeError("boom") monkeypatch.setattr(QdrantService, "_build_client", _build_client) result = await service.initialize() assert result is False assert service.is_initialized is False @pytest.mark.asyncio async def test_health_check_returns_unhealthy_when_not_initialized() -> None: service = QdrantService(settings=QdrantSettings(host="localhost", port=6333)) health = await service.health_check() assert health["status"] == "unhealthy" @pytest.mark.asyncio async def test_close_is_idempotent() -> None: service = QdrantService(settings=QdrantSettings(host="localhost", port=6333)) assert await service.close() is True assert service.is_initialized is False def test_get_client_raises_before_init() -> None: service = QdrantService(settings=QdrantSettings(host="localhost", port=6333)) with pytest.raises(RuntimeError): service.get_client()