Files
memory-gateway/tests/test_backend_integration.py

81 lines
2.3 KiB
Python

from __future__ import annotations
import os
from pathlib import Path
from uuid import uuid4
import httpx
import pytest
from core.api import create_app
from core.config import GatewayConfig
from core.backend_client import BackendClient
pytestmark = pytest.mark.integration
def _integration_enabled() -> bool:
return os.environ.get("RUN_BACKEND_INTEGRATION") == "1"
def _ingest_integration_enabled() -> bool:
return os.environ.get("RUN_BACKEND_INGEST_INTEGRATION") == "1"
def _backend_base_url() -> str:
return os.environ.get("MEMORY_GATEWAY_BACKEND_BASE_URL", "http://127.0.0.1:1995")
@pytest.mark.skipif(
not _integration_enabled(),
reason="set RUN_BACKEND_INTEGRATION=1 to run against an upstream memory service",
)
@pytest.mark.asyncio
async def test_real_backend_health_check() -> None:
client = BackendClient(_backend_base_url(), timeout=10)
health = await client.health_check()
assert isinstance(health, dict)
@pytest.mark.skipif(
not _ingest_integration_enabled(),
reason=(
"set RUN_BACKEND_INGEST_INTEGRATION=1 to run upstream add/flush ingestion"
),
)
@pytest.mark.asyncio
async def test_gateway_uploads_text_resource_to_real_backend(tmp_path: Path) -> None:
config = GatewayConfig(
backend_base_url=_backend_base_url(),
database_path=tmp_path / "gateway.sqlite3",
storage_dir=tmp_path / "storage",
backend_ingest_attempts=1,
backend_timeout_seconds=30,
)
app = create_app(config=config)
transport = httpx.ASGITransport(app=app)
user_id = f"it_{uuid4().hex}"
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
created_user = await client.post("/users", json={"user_id": user_id})
assert created_user.status_code == 200, created_user.text
user_key = created_user.json()["user_key"]
uploaded = await client.post(
"/resources",
data={"user_id": user_id, "user_key": user_key},
files={
"file": (
"integration.txt",
b"upstream memory service integration",
"text/plain",
)
},
)
assert uploaded.status_code == 200, uploaded.text
assert uploaded.json()["status"] == "extracted"