"""Integration tests for the targets API.""" from pathlib import Path import pytest from agenteval.web.app import app from agenteval.web.deps import get_db from fastapi.testclient import TestClient from sqlmodel import Session, SQLModel, create_engine @pytest.fixture() def targets_client(tmp_path: Path): from agenteval.storage.db import EvalTargetDB # noqa: F401 engine = create_engine( f"sqlite:///{tmp_path / 'targets_api.db'}", connect_args={"check_same_thread": False}, ) SQLModel.metadata.create_all(engine) session = Session(engine) def override_get_db(): yield session app.dependency_overrides[get_db] = override_get_db with TestClient(app) as client: yield client, session app.dependency_overrides.clear() session.close() engine.dispose() def test_create_and_get_target(targets_client): client, _ = targets_client resp = client.post("/api/targets", json={ "name": "测试目标", "description": "描述", "platform": "ai_digital_employee", "channel_type": "tutu-api", "channel_config": {"base_url": "https://example.com"}, }) assert resp.status_code == 200 target_id = resp.json()["id"] resp = client.get(f"/api/targets/{target_id}") assert resp.status_code == 200 assert resp.json()["name"] == "测试目标" def test_list_targets(targets_client): client, _ = targets_client client.post("/api/targets", json={"name": "目标1"}) client.post("/api/targets", json={"name": "目标2"}) resp = client.get("/api/targets") assert resp.status_code == 200 assert len(resp.json()) == 2 def test_update_target(targets_client): client, _ = targets_client resp = client.post("/api/targets", json={"name": "原名"}) target_id = resp.json()["id"] resp = client.put(f"/api/targets/{target_id}", json={"name": "新名"}) assert resp.status_code == 200 assert resp.json()["name"] == "新名" def test_delete_target(targets_client): client, _ = targets_client resp = client.post("/api/targets", json={"name": "待删除"}) target_id = resp.json()["id"] resp = client.delete(f"/api/targets/{target_id}") assert resp.status_code == 200 resp = client.get(f"/api/targets/{target_id}") assert resp.status_code == 404 def test_get_nonexistent_target_returns_404(targets_client): client, _ = targets_client resp = client.get("/api/targets/non-existent") assert resp.status_code == 404 def test_update_nonexistent_target_returns_404(targets_client): client, _ = targets_client resp = client.put("/api/targets/non-existent", json={"name": "新名"}) assert resp.status_code == 404 def test_delete_nonexistent_target_returns_404(targets_client): client, _ = targets_client resp = client.delete("/api/targets/non-existent") assert resp.status_code == 404