- 纯函数单测(go test -short)覆盖: classification (levelIndex/maxLevel/materializeClassification/principalMaxIndex/deniedLevelsFor)、 display_name (propString/resolveDisplayName)、 governance (diffProps/mergeProps/parseStringArray/similarity) - 集成测试(-tags=integration,跑真实 PG)覆盖 issue #4 #5 端到端: enum_violation 拦截 + 友好错误;分级过滤 manager vs analyst(L0/L1) 越级 404; properties.sensitivity 越级 redact 为 ***;删→恢复关系边 (links_snapshot+edges_created=1); recompute dry_run 不落库 / apply=true 落库修正错误分级 - 集成测试踩坑:X-Principal 与 principal_clearances.principal 必须一致;每次新主体必须清 authzCache(sync.Map 进程级),否则历史空 Authz 会让分级过滤假阴性 - docs/TESTING.md:测试分层 / 跑测命令 / 编写新测试的约定 - CHANGELOG [1.2.0] 加 测试 与 文档 段落
151 lines
4.1 KiB
Go
151 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"reflect"
|
|
"sort"
|
|
"testing"
|
|
)
|
|
|
|
// ---------- diffProps ----------
|
|
|
|
func TestDiffProps(t *testing.T) {
|
|
// 全空 → 全空
|
|
d := diffProps(nil, nil)
|
|
if !d.empty() {
|
|
t.Errorf("nil vs nil 应为空, got %+v", d)
|
|
}
|
|
|
|
// 新增
|
|
d = diffProps(map[string]interface{}{}, map[string]interface{}{"a": "1", "b": "2"})
|
|
if !reflect.DeepEqual(d.Added, []string{"a", "b"}) {
|
|
t.Errorf("Added = %v, want [a b]", d.Added)
|
|
}
|
|
if len(d.Changed) != 0 || len(d.Removed) != 0 {
|
|
t.Errorf("纯新增不应有 Changed/Removed, got %+v", d)
|
|
}
|
|
|
|
// 删除
|
|
d = diffProps(map[string]interface{}{"a": "1", "b": "2"}, map[string]interface{}{})
|
|
if !reflect.DeepEqual(d.Removed, []string{"a", "b"}) {
|
|
t.Errorf("Removed = %v, want [a b]", d.Removed)
|
|
}
|
|
|
|
// 修改
|
|
d = diffProps(
|
|
map[string]interface{}{"a": "1", "b": "2"},
|
|
map[string]interface{}{"a": "1", "b": "3"})
|
|
if len(d.Added) != 0 || len(d.Removed) != 0 {
|
|
t.Errorf("纯修改不应有 Added/Removed, got %+v", d)
|
|
}
|
|
if !reflect.DeepEqual(d.Changed["b"], map[string]interface{}{"from": "2", "to": "3"}) {
|
|
t.Errorf("Changed[b] = %v, want from=2/to=3", d.Changed["b"])
|
|
}
|
|
if _, ok := d.Changed["a"]; ok {
|
|
t.Errorf("a 值未变不应出现在 Changed")
|
|
}
|
|
|
|
// 复合
|
|
d = diffProps(
|
|
map[string]interface{}{"x": 1, "y": 2},
|
|
map[string]interface{}{"x": 1, "y": 3, "z": 4})
|
|
wantAdded := []string{"z"}
|
|
// 注意:diffProps 仅在确实有删除时才 push 进 Removed,没删除时返回 nil
|
|
if !reflect.DeepEqual(d.Added, wantAdded) {
|
|
t.Errorf("Added = %v, want %v", d.Added, wantAdded)
|
|
}
|
|
if len(d.Removed) != 0 {
|
|
t.Errorf("无删除时 Removed 应为空: %v", d.Removed)
|
|
}
|
|
if !reflect.DeepEqual(d.Changed["y"], map[string]interface{}{"from": 2, "to": 3}) {
|
|
t.Errorf("Changed[y] = %v", d.Changed["y"])
|
|
}
|
|
}
|
|
|
|
// ---------- mergeProps ----------
|
|
|
|
func TestMergeProps(t *testing.T) {
|
|
base := map[string]interface{}{"a": "1", "b": "2", "c": "3"}
|
|
|
|
// 普通合并:新增 + 覆盖
|
|
out, removed := mergeProps(base, map[string]interface{}{"a": "1'", "d": "4"})
|
|
if !reflect.DeepEqual(out["a"], "1'") || out["d"] != "4" || out["b"] != "2" {
|
|
t.Errorf("合并错: out=%v", out)
|
|
}
|
|
if len(removed) != 0 {
|
|
t.Errorf("无 null 不应有 removed: %v", removed)
|
|
}
|
|
|
|
// patch 里 null = 删除
|
|
out, removed = mergeProps(base, map[string]interface{}{"a": nil, "b": nil})
|
|
if _, ok := out["a"]; ok {
|
|
t.Errorf("null 应删除 a")
|
|
}
|
|
if _, ok := out["b"]; ok {
|
|
t.Errorf("null 应删除 b")
|
|
}
|
|
sort.Strings(removed)
|
|
if !reflect.DeepEqual(removed, []string{"a", "b"}) {
|
|
t.Errorf("removed = %v, want [a b]", removed)
|
|
}
|
|
|
|
// 删除不存在的字段 → 不报错,removed 不包含
|
|
out, removed = mergeProps(base, map[string]interface{}{"not_exists": nil})
|
|
if len(removed) != 0 {
|
|
t.Errorf("删除不存在的字段不应记 removed: %v", removed)
|
|
}
|
|
}
|
|
|
|
// ---------- parseStringArray ----------
|
|
|
|
func TestParseStringArray(t *testing.T) {
|
|
cases := []struct {
|
|
in []byte
|
|
want []string
|
|
}{
|
|
{nil, nil},
|
|
{[]byte(""), nil},
|
|
{[]byte(`["L0","L1","L2"]`), []string{"L0", "L1", "L2"}},
|
|
{[]byte(`[]`), []string{}},
|
|
{[]byte(`not-json`), nil},
|
|
}
|
|
for _, c := range cases {
|
|
got := parseStringArray(c.in)
|
|
if len(got) == 0 && len(c.want) == 0 {
|
|
continue // nil/empty 视为等价
|
|
}
|
|
if !reflect.DeepEqual(got, c.want) {
|
|
t.Errorf("parseStringArray(%q) = %v, want %v", c.in, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------- similarity / normalizeName ----------
|
|
|
|
func TestSimilarityAndNormalize(t *testing.T) {
|
|
// 完全相同 → 1
|
|
if got := similarity("张三", "张三"); got != 1 {
|
|
t.Errorf("完全相同 similarity = %f, want 1", got)
|
|
}
|
|
|
|
// 大小写 + 空格归一化后相同 → 1
|
|
if got := similarity(" Zhang San ", "zhang san"); got != 1 {
|
|
t.Errorf("规范化后相同 similarity = %f, want 1", got)
|
|
}
|
|
|
|
// 子串包含 → ≥ 0.7
|
|
got := similarity("张三丰", "张三")
|
|
if got < 0.7 {
|
|
t.Errorf("子串包含 similarity = %f, want ≥ 0.7", got)
|
|
}
|
|
|
|
// 空串 → 0
|
|
if got := similarity("", "张三"); got != 0 {
|
|
t.Errorf("空串 similarity = %f, want 0", got)
|
|
}
|
|
|
|
// 完全无关 → < 0.7
|
|
if got := similarity("张三", "李四"); got >= 0.7 {
|
|
t.Errorf("无关 similarity = %f, want < 0.7", got)
|
|
}
|
|
}
|