Files
ssw-ontology/server/display_name_test.go
T
admin ae9a1a69e2 test: 加 16 个回归测试 (11 纯函数 + 5 集成) + docs/TESTING.md
- 纯函数单测(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] 加 测试 与 文档 段落
2026-09-07 10:11:39 +08:00

71 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import "testing"
// ---------- propString ----------
func TestPropString(t *testing.T) {
cases := []struct {
in interface{}
want string
}{
{nil, ""},
{"", ""},
{" hello ", "hello"},
{"张 三", "张 三"},
{float64(42), "42"},
{true, "true"},
{false, "false"},
{[]interface{}{"a", "b"}, "[a b]"}, // 数组 → fmt.Sprintf 字面量(propString 注释说仅 <nil> / [] / map[] 判空)
{map[string]interface{}{"k": 1}, "map[k:1]"}, // map → 同上
}
for _, c := range cases {
if got := propString(c.in); got != c.want {
t.Errorf("propString(%v) = %q, want %q", c.in, got, c.want)
}
}
}
// ---------- resolveDisplayName ----------
func TestResolveDisplayName(t *testing.T) {
props := map[string]interface{}{
"name": " 张三 ",
"label": "Label",
"title": "Title",
"phone": "13800000000",
"nested": map[string]interface{}{"k": 1},
}
// 1. 显式 title_property 优先
if got := resolveDisplayName("phone", props); got != "13800000000" {
t.Errorf("title_property=phone → %q, want 13800000000", got)
}
// 2. title_property 未命中 → 走回退链
if got := resolveDisplayName("does_not_exist", props); got != "张三" {
t.Errorf("title_property 缺失 → %q, want name 回退值", got)
}
// 3. 回退链顺序:name → title → real_name → display_name → label
onlyLabel := map[string]interface{}{"label": " L "}
if got := resolveDisplayName("", onlyLabel); got != "L" {
t.Errorf("仅 label → %q, want L", got)
}
noName := map[string]interface{}{"real_name": "R", "title": "T"}
if got := resolveDisplayName("", noName); got != "T" {
t.Errorf("无 name + title+real_name → %q, want T (title 优先于 real_name)", got)
}
// 4. 全部为空 → 返回 ""
if got := resolveDisplayName("", map[string]interface{}{}); got != "" {
t.Errorf("空 props → %q, want ''", got)
}
// 5. title_property 指向空值 → 回退链
missing := map[string]interface{}{"title": "from-title"}
if got := resolveDisplayName("phone", missing); got != "from-title" {
t.Errorf("title_property=phone(phone 为空) → %q, want from-title 回退", got)
}
}