refactor: align backend layout and supabase infra
Consolidate backend modules/tests under the backend package while syncing Supabase compose/env config and related plans.
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
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()
|
||||
@@ -0,0 +1,98 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from core.config.settings import RedisSettings
|
||||
from services.base.redis import RedisService
|
||||
|
||||
|
||||
class _FakeRedisClient:
|
||||
def __init__(self) -> None:
|
||||
self.closed = False
|
||||
|
||||
async def ping(self) -> bool:
|
||||
return True
|
||||
|
||||
async def info(self) -> dict[str, object]:
|
||||
return {
|
||||
"redis_version": "7.2",
|
||||
"connected_clients": 1,
|
||||
"used_memory_human": "1M",
|
||||
"uptime_in_seconds": 10,
|
||||
}
|
||||
|
||||
async def aclose(self) -> None:
|
||||
self.closed = True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_initialize_success(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
service = RedisService(settings=RedisSettings(host="localhost", port=6379))
|
||||
|
||||
def _build_client(_: RedisService) -> _FakeRedisClient:
|
||||
return _FakeRedisClient()
|
||||
|
||||
monkeypatch.setattr(RedisService, "_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 = RedisService(settings=RedisSettings(host="localhost", port=6379))
|
||||
|
||||
def _build_client(_: RedisService) -> _FakeRedisClient:
|
||||
raise RuntimeError("boom")
|
||||
|
||||
monkeypatch.setattr(RedisService, "_build_client", _build_client)
|
||||
|
||||
result = await service.initialize()
|
||||
|
||||
assert result is False
|
||||
assert service.is_initialized is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_close_is_idempotent() -> None:
|
||||
service = RedisService(settings=RedisSettings(host="localhost", port=6379))
|
||||
|
||||
assert await service.close() is True
|
||||
assert service.is_initialized is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_health_check_uninitialized() -> None:
|
||||
service = RedisService(settings=RedisSettings(host="localhost", port=6379))
|
||||
|
||||
health = await service.health_check()
|
||||
|
||||
assert health["status"] == "unhealthy"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_close_closes_client(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
service = RedisService(settings=RedisSettings(host="localhost", port=6379))
|
||||
client = _FakeRedisClient()
|
||||
|
||||
def _build_client(_: RedisService) -> _FakeRedisClient:
|
||||
return client
|
||||
|
||||
monkeypatch.setattr(RedisService, "_build_client", _build_client)
|
||||
|
||||
assert await service.initialize() is True
|
||||
assert await service.close() is True
|
||||
assert client.closed is True
|
||||
assert service.is_initialized is False
|
||||
|
||||
|
||||
def test_get_client_raises_before_init() -> None:
|
||||
service = RedisService(settings=RedisSettings(host="localhost", port=6379))
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
service.get_client()
|
||||
@@ -0,0 +1,49 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from services.base.service_interface import (
|
||||
BaseServiceProvider,
|
||||
ServiceRegistry,
|
||||
register_service,
|
||||
register_service_instance,
|
||||
)
|
||||
|
||||
|
||||
class _DummyService(BaseServiceProvider):
|
||||
def __init__(self, name: str = "dummy") -> None:
|
||||
super().__init__(name)
|
||||
|
||||
async def initialize(self, **_: object) -> bool:
|
||||
self._set_initialized(True)
|
||||
return True
|
||||
|
||||
async def close(self) -> bool:
|
||||
self._set_initialized(False)
|
||||
return True
|
||||
|
||||
async def health_check(self) -> dict[str, object]:
|
||||
return {"status": "healthy", "details": {}}
|
||||
|
||||
|
||||
def test_register_service_and_create_service() -> None:
|
||||
@register_service("dummy-service")
|
||||
class _RegisteredService(_DummyService):
|
||||
pass
|
||||
|
||||
created = ServiceRegistry.create_service("dummy-service")
|
||||
|
||||
assert created is not None
|
||||
assert created.get_service_info()["name"] == "dummy"
|
||||
|
||||
|
||||
def test_register_service_instance_returns_same_instance() -> None:
|
||||
instance = _DummyService("singleton")
|
||||
|
||||
returned = register_service_instance("dummy-singleton", instance)
|
||||
created = ServiceRegistry.create_service("dummy-singleton")
|
||||
|
||||
assert returned is instance
|
||||
assert created is instance
|
||||
|
||||
|
||||
def test_create_service_returns_none_for_missing() -> None:
|
||||
assert ServiceRegistry.create_service("missing-service") is None
|
||||
Reference in New Issue
Block a user