背景:v1.2.0 加了 object_types.classification_property/_levels/_default 三列,
但只暴露了 GET /classification/config,没有配对的 PUT/POST。
后果:
- 前端编辑对象类型分级保存按钮看似无反应(POST 到不存在的路由)
- 587 个打了 data_level 标签的实例是「裸属性」,分级引擎不接管
- 23 个类型 enabled 全是 false
改动:
- 新增 REST PUT /api/v1/ontology/object-types/:name/classification
(handler: setObjectTypeClassification)
- 校验:levels 非空有序、default ∈ levels、property 必须是已存在属性
- 写入后调 invalidateClassCache() 让下一次 GET config 立刻反映
- 「关闭分级」入口:property=空 允许 levels=[] / default=''
- 新增 MCP 工具 set_object_type_classification(manager 必填)
- 24 个测试全绿(11 纯函数 + 13 集成)
199 lines
7.2 KiB
Go
199 lines
7.2 KiB
Go
//go:build integration
|
||
|
||
package main
|
||
|
||
import (
|
||
"context"
|
||
"strconv"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// TestIntegration_SnapshotRouter 验证请求级 search_path 切库:
|
||
// 1) 改前建快照 → 改后读快照:可见改前状态,不可见改后新增/改动
|
||
// 2) 不带快照头读:见活库现状(含改动)
|
||
// 3) 写路径带快照头:直接 403(不改活库、不污染事务)
|
||
// 4) 同事务内 search_path 不影响 information_schema(catalog 表独立)
|
||
func TestIntegration_SnapshotRouter(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
|
||
// 1) 建一个测试类型 + 一个实例(作为「快照前」状态)
|
||
env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{
|
||
"api_name": env.typeKey, "display_name": "snapshot-test",
|
||
}, h)
|
||
env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": env.typeKey, "pk": "before",
|
||
"properties": map[string]any{"v": "1"}, "via": "test",
|
||
}, h)
|
||
|
||
// 2) 建快照
|
||
_, body := env.httpDo(t, "POST", "/api/v1/backups",
|
||
map[string]any{"name": uniqSnapshotName(t), "notes": "snapshot-routing-test"}, h)
|
||
snapID := int64(body["id"].(float64))
|
||
if snapID == 0 {
|
||
t.Fatal("create snapshot returned id=0")
|
||
}
|
||
t.Cleanup(func() {
|
||
env.httpDo(t, "DELETE", "/api/v1/backups/"+strconv.FormatInt(snapID, 10), nil, h)
|
||
})
|
||
|
||
// 3) 改活库:加 after、改 before 的 v
|
||
env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": env.typeKey, "pk": "after",
|
||
"properties": map[string]any{"v": "1"}, "via": "test",
|
||
}, h)
|
||
env.httpDo(t, "PUT", "/api/v1/objects/"+env.typeKey+"/before", map[string]any{
|
||
"properties": map[string]any{"v": "2"}, "via": "test",
|
||
}, h)
|
||
|
||
// 4) 活库读:应见 before=v=2 与 after=v=1
|
||
_, liveBody := env.httpDo(t, "GET", "/api/v1/connector/objects/"+env.typeKey+"?limit=50", nil, h)
|
||
liveItems, _ := liveBody["items"].([]any)
|
||
liveSet := map[string]string{}
|
||
for _, it := range liveItems {
|
||
m := it.(map[string]any)
|
||
if pk, _ := m["pk"].(string); pk == "before" || pk == "after" {
|
||
liveSet[pk] = m["properties"].(map[string]any)["v"].(string)
|
||
}
|
||
}
|
||
if liveSet["before"] != "2" || liveSet["after"] != "1" {
|
||
t.Errorf("活库应见 before=2 + after=1, got %+v", liveSet)
|
||
}
|
||
|
||
// 5) 快照头读:应见 before=v=1、无 after(快照取的是改前那一刻)
|
||
_, snapBody := env.httpDo(t, "GET", "/api/v1/connector/objects/"+env.typeKey+"?limit=50",
|
||
nil, h,
|
||
map[string]string{"X-Principal": "manager", "X-Snapshot-Id": strconv.FormatInt(snapID, 10)})
|
||
snapItems, _ := snapBody["items"].([]any)
|
||
snapSet := map[string]string{}
|
||
for _, it := range snapItems {
|
||
m := it.(map[string]any)
|
||
if pk, _ := m["pk"].(string); pk == "before" || pk == "after" {
|
||
snapSet[pk] = m["properties"].(map[string]any)["v"].(string)
|
||
}
|
||
}
|
||
if snapSet["before"] != "1" {
|
||
t.Errorf("快照应见 before=v=1, got %+v", snapSet)
|
||
}
|
||
if _, ok := snapSet["after"]; ok {
|
||
t.Errorf("快照不应见改后新增的 after, got %+v", snapSet)
|
||
}
|
||
|
||
// 6) 写路径带快照头:必须 403,且不污染活库
|
||
env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": env.typeKey, "pk": "forbidden",
|
||
"properties": map[string]any{"v": "x"}, "via": "test",
|
||
}, h,
|
||
map[string]string{"X-Snapshot-Id": strconv.FormatInt(snapID, 10)})
|
||
|
||
// 验证活库没有 forbidden
|
||
_, postBody := env.httpDo(t, "GET", "/api/v1/connector/object/"+env.typeKey+"/forbidden", nil, h)
|
||
if postBody != nil && postBody["pk"] == "forbidden" {
|
||
t.Errorf("写带快照头不应写入活库, got %v", postBody)
|
||
}
|
||
}
|
||
|
||
// TestIntegration_SnapshotSearchPathPersist 验证 search_path 不污染下一个请求:
|
||
// - 跑完一个 snapshot 请求后,下一个活库请求仍走 public
|
||
func TestIntegration_SnapshotSearchPathPersist(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
|
||
env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{
|
||
"api_name": env.typeKey, "display_name": "persist-test",
|
||
}, h)
|
||
_, body := env.httpDo(t, "POST", "/api/v1/backups",
|
||
map[string]any{"name": uniqSnapshotName(t)}, h)
|
||
snapID := int64(body["id"].(float64))
|
||
t.Cleanup(func() {
|
||
env.httpDo(t, "DELETE", "/api/v1/backups/"+strconv.FormatInt(snapID, 10), nil, h)
|
||
})
|
||
|
||
// 第一次:带快照头读
|
||
_, snapBody := env.httpDo(t, "GET", "/api/v1/connector/object-types",
|
||
nil, h,
|
||
map[string]string{"X-Snapshot-Id": strconv.FormatInt(snapID, 10)})
|
||
if types, _ := snapBody["object_types"].([]any); len(types) == 0 {
|
||
t.Errorf("快照头读 object-types 应非空")
|
||
}
|
||
|
||
// 第二次:不带头读活库,应同样非空(不被快照库的 search_path 残留污染)
|
||
_, liveBody := env.httpDo(t, "GET", "/api/v1/connector/object-types", nil, h)
|
||
if types, _ := liveBody["object_types"].([]any); len(types) == 0 {
|
||
t.Errorf("活库读 object-types 应非空(search_path 不应残留)")
|
||
}
|
||
}
|
||
|
||
// TestIntegration_SnapshotByTimestamp 验证 X-Snapshot-Ts 也能切库
|
||
func TestIntegration_SnapshotByTimestamp(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
|
||
_, body := env.httpDo(t, "POST", "/api/v1/backups",
|
||
map[string]any{"name": uniqSnapshotName(t)}, h)
|
||
snapID := int64(body["id"].(float64))
|
||
t.Cleanup(func() {
|
||
env.httpDo(t, "DELETE", "/api/v1/backups/"+strconv.FormatInt(snapID, 10), nil, h)
|
||
})
|
||
|
||
// 取快照的 created_at unix 时间戳(+1 防毫秒向下取整丢失)
|
||
pool, _ := initDB(context.Background(), testDSN())
|
||
defer pool.Close()
|
||
var createdUnix int64
|
||
_ = pool.QueryRow(context.Background(),
|
||
`SELECT EXTRACT(EPOCH FROM created_at)::bigint FROM backups.snapshot_runs WHERE id=$1`,
|
||
snapID).Scan(&createdUnix)
|
||
|
||
// 用 ts 头读:应见快照库
|
||
code, _ := env.httpDo(t, "GET", "/api/v1/connector/object-types",
|
||
nil, h,
|
||
map[string]string{"X-Snapshot-Ts": strconv.FormatInt(createdUnix+1, 10)})
|
||
if code != 200 {
|
||
t.Errorf("X-Snapshot-Ts 应能切库, got %d", code)
|
||
}
|
||
}
|
||
|
||
// TestIntegration_SnapshotDeleteCleansSchema 验证 delete 真清掉对应 schema
|
||
func TestIntegration_SnapshotDeleteCleansSchema(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
|
||
_, body := env.httpDo(t, "POST", "/api/v1/backups",
|
||
map[string]any{"name": uniqSnapshotName(t)}, h)
|
||
snapID := int64(body["id"].(float64))
|
||
schemaName := body["schema_name"].(string)
|
||
|
||
pool, _ := initDB(context.Background(), testDSN())
|
||
defer pool.Close()
|
||
|
||
// schema 应已建
|
||
var n int
|
||
_ = pool.QueryRow(context.Background(),
|
||
`SELECT count(*) FROM information_schema.tables WHERE table_schema=$1`, schemaName).Scan(&n)
|
||
if n == 0 {
|
||
t.Fatalf("schema %s 应有表", schemaName)
|
||
}
|
||
|
||
// delete
|
||
code, _ := env.httpDo(t, "DELETE", "/api/v1/backups/"+strconv.FormatInt(snapID, 10), nil, h)
|
||
if code != 200 {
|
||
t.Errorf("delete 应 200, got %d", code)
|
||
}
|
||
|
||
// schema 应被 DROP
|
||
_ = pool.QueryRow(context.Background(),
|
||
`SELECT count(*) FROM information_schema.schemata WHERE schema_name=$1`, schemaName).Scan(&n)
|
||
if n != 0 {
|
||
t.Errorf("schema %s 应被删, got %d rows", schemaName, n)
|
||
}
|
||
|
||
// 404 再调
|
||
code, _ = env.httpDo(t, "GET", "/api/v1/backups/"+strconv.FormatInt(snapID, 10), nil, h)
|
||
if code != 404 {
|
||
t.Errorf("delete 后 inspect 应 404, got %d", code)
|
||
}
|
||
}
|
||
|
||
// 注释:unify sub-test run consistency
|
||
var _ = strings.Contains |