ForcePilot/test/api/test_unified_graph_router.py
Wenjie Zhang 3d63418adf feat: 实现统一图谱适配器及前端组件重构
重构图谱系统架构,引入适配器模式支持多种图数据库类型。主要变更包括:

1. 新增 GraphAdapter 基类及 LightRAG/Upload 适配器实现
2. 重构前端组件结构,新增 GraphDetailPanel 等可复用组件
3. 实现 useGraph 组合式函数统一管理图谱状态
4. 优化 Neo4j 节点/边数据标准化处理
5. 新增统一图谱 API 接口及对应测试用例
6. 改进前端交互体验,增加节点详情展示等功能

前端组件库重构为模块化结构,提升代码复用性。适配器模式使系统可灵活支持不同图数据源,为后续扩展奠定基础。

Next:上传图谱文件的时候,支持属性解析
2025-12-15 23:25:56 +08:00

139 lines
4.4 KiB
Python

"""
Integration tests for the unified graph router endpoints.
"""
from __future__ import annotations
import pytest
pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
async def test_get_graphs_list(test_client, admin_headers):
"""Test retrieving the list of all graphs."""
response = await test_client.get("/api/graph/list", headers=admin_headers)
assert response.status_code == 200
payload = response.json()
assert payload["success"] is True
graphs = payload["data"]
assert isinstance(graphs, list)
# Check for Neo4j default graph
neo4j_graph = next((g for g in graphs if g["id"] == "neo4j"), None)
assert neo4j_graph is not None
assert neo4j_graph["type"] == "neo4j"
# Note: LightRAG graphs might be empty if none created, but we check structure
async def test_get_subgraph_neo4j(test_client, admin_headers):
"""Test unified subgraph query for Neo4j."""
# Query with a wildcard or a known node. Using "*" to get a sample.
response = await test_client.get(
"/api/graph/subgraph",
params={"db_id": "neo4j", "node_label": "*", "max_nodes": 10},
headers=admin_headers
)
assert response.status_code == 200
payload = response.json()
assert payload["success"] is True
data = payload["data"]
assert "nodes" in data
assert "edges" in data
assert isinstance(data["nodes"], list)
async def test_get_subgraph_lightrag(test_client, admin_headers, knowledge_database):
"""Test unified subgraph query for LightRAG."""
db_id = knowledge_database["db_id"]
response = await test_client.get(
"/api/graph/subgraph",
params={"db_id": db_id, "node_label": "*", "max_nodes": 10},
headers=admin_headers
)
assert response.status_code == 200
payload = response.json()
assert payload["success"] is True
data = payload["data"]
assert "nodes" in data
assert "edges" in data
async def test_get_stats_neo4j(test_client, admin_headers):
"""Test stats endpoint for Neo4j."""
response = await test_client.get(
"/api/graph/stats",
params={"db_id": "neo4j"},
headers=admin_headers
)
assert response.status_code == 200
payload = response.json()
assert payload["success"] is True
data = payload["data"]
assert "total_nodes" in data
assert "total_edges" in data
assert "entity_types" in data
async def test_get_stats_lightrag(test_client, admin_headers, knowledge_database):
"""Test stats endpoint for LightRAG."""
db_id = knowledge_database["db_id"]
response = await test_client.get(
"/api/graph/stats",
params={"db_id": db_id},
headers=admin_headers
)
assert response.status_code == 200
payload = response.json()
assert payload["success"] is True
data = payload["data"]
assert "total_nodes" in data
assert "total_edges" in data
assert "entity_types" in data
async def test_get_labels_neo4j(test_client, admin_headers):
"""Test labels endpoint for Neo4j."""
response = await test_client.get(
"/api/graph/labels",
params={"db_id": "neo4j"},
headers=admin_headers
)
assert response.status_code == 200
payload = response.json()
assert payload["success"] is True
data = payload["data"]
assert "labels" in data
assert isinstance(data["labels"], list)
async def test_deprecated_neo4j_endpoints(test_client, admin_headers):
"""Verify deprecated endpoints still work and return correct structure."""
# /neo4j/nodes
response = await test_client.get(
"/api/graph/neo4j/nodes",
params={"kgdb_name": "neo4j", "num": 5},
headers=admin_headers
)
assert response.status_code == 200
payload = response.json()
# Check compatibility structure
assert payload["success"] is True
assert "result" in payload
assert payload["message"] == "success"
assert "nodes" in payload["result"]
# /neo4j/node
# This might return empty if "NonExistentEntity" doesn't exist, but structure should be valid
response = await test_client.get(
"/api/graph/neo4j/node",
params={"entity_name": "NonExistentEntity"},
headers=admin_headers
)
assert response.status_code == 200
payload = response.json()
assert payload["success"] is True
assert "result" in payload
assert payload["message"] == "success"