64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
"""数据中台穿透 API 集成测试(需 PostgreSQL)。
|
|
|
|
通过 TestClient 调用 /datahub/penetrate,验证统一穿透查询服务端到端可用。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.datahub.graph_repo import add_relationship, upsert_entity
|
|
from app.datahub.ontology import EntityType, RelationshipType
|
|
from app.db import get_session
|
|
from app.main import app
|
|
|
|
|
|
@pytest.fixture()
|
|
def client(session):
|
|
# 用集成测试的事务化 session 覆盖应用依赖,保证测试数据回滚
|
|
app.dependency_overrides[get_session] = lambda: session
|
|
try:
|
|
yield TestClient(app)
|
|
finally:
|
|
app.dependency_overrides.pop(get_session, None)
|
|
|
|
|
|
def test_penetrate_endpoint_detects_related(client, session):
|
|
suffix = uuid.uuid4().hex[:8]
|
|
controller = upsert_entity(session, EntityType.LEGAL_PERSON, f"CTRL-{suffix}", "实控人")
|
|
cust = upsert_entity(session, EntityType.CUSTOMER, f"CUST-{suffix}", "政企客户")
|
|
rep = upsert_entity(session, EntityType.LEGAL_PERSON, f"REP-{suffix}", "法人")
|
|
add_relationship(session, RelationshipType.LEGAL_REP_OF, rep, cust)
|
|
add_relationship(session, RelationshipType.RELATED_TO, rep, controller)
|
|
session.flush()
|
|
|
|
resp = client.post(
|
|
"/datahub/penetrate",
|
|
json={"start_entity_id": str(controller.id), "max_depth": 3},
|
|
)
|
|
assert resp.status_code == 200
|
|
body = resp.json()
|
|
related_ids = {r["entity"]["id"] for r in body["related"]}
|
|
assert str(cust.id) in related_ids
|
|
assert body["related_count"] >= 2
|
|
|
|
|
|
def test_penetrate_unknown_entity_404(client):
|
|
resp = client.post(
|
|
"/datahub/penetrate",
|
|
json={"start_entity_id": str(uuid.uuid4()), "max_depth": 2},
|
|
)
|
|
assert resp.status_code == 404
|
|
|
|
|
|
def test_get_entity_endpoint(client, session):
|
|
suffix = uuid.uuid4().hex[:8]
|
|
e = upsert_entity(session, EntityType.SUPPLIER, f"SUP-{suffix}", "供应商甲")
|
|
session.flush()
|
|
resp = client.get(f"/datahub/entities/{e.id}")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["business_key"] == f"SUP-{suffix}"
|