package main // 跨版本结构 diff 的纯函数单测(不触库)。 // // 守护三件事: // 1. 字段分类正确——机器事实与人工策展不能串味(串味 = 重摄取会冲掉人工编辑); // 2. 取值比较按归一化 JSON 判定(快照读回是通用 JSON 类型,直接比会误报「变了」); // 3. 增删改三类差异与汇总计数正确。 import "testing" func fact(kind, name string, machine, curated map[string]any, certified bool) schemaFact { if machine == nil { machine = map[string]any{} } if curated == nil { curated = map[string]any{} } return schemaFact{Kind: kind, Name: name, Machine: machine, Curated: curated, Certified: certified} } func TestDiffSchemaFactsClassifiesMachineAndCurated(t *testing.T) { old := []schemaFact{ fact(factKindObjectType, "合作企业", map[string]any{ "iri": "http://example.org/standard-ontology#合作企业", }, map[string]any{ "display_name": "合作企业", "description": "旧描述", }, false), } new := []schemaFact{ fact(factKindObjectType, "合作企业", map[string]any{ "iri": "http://example.org/standard-ontology#合作企业", }, map[string]any{ "display_name": "合作企业(新版)", "description": "新描述", }, false), } entries := diffSchemaFacts(old, new) if len(entries) != 1 { t.Fatalf("期望 1 条差异,实际 %d 条: %+v", len(entries), entries) } e := entries[0] if e.Change != "changed" { t.Errorf("change 期望 changed,实际 %q", e.Change) } got := map[string]string{} for _, f := range e.Fields { got[f.Field] = f.Class } if got["display_name"] != classCurated { t.Errorf("display_name 应归人工策展,实际 %q", got["display_name"]) } if got["description"] != classCurated { t.Errorf("description 应归人工策展,实际 %q", got["description"]) } if _, ok := got["iri"]; ok { t.Errorf("iri 未变,不该出现在差异里: %+v", e.Fields) } } func TestDiffSchemaFactsMachineFieldChange(t *testing.T) { old := []schemaFact{ fact(factKindObjectType, "T", map[string]any{"iri": "a"}, nil, false), } new := []schemaFact{ fact(factKindObjectType, "T", map[string]any{"iri": "b"}, nil, false), } entries := diffSchemaFacts(old, new) if len(entries) != 1 || len(entries[0].Fields) != 1 { t.Fatalf("期望 1 条 1 字段差异,实际 %+v", entries) } f := entries[0].Fields[0] if f.Field != "iri" || f.Class != classMachine { t.Errorf("期望 iri 归机器事实,实际 %+v", f) } if f.Old != "a" || f.New != "b" { t.Errorf("old/new 期望 a→b,实际 %v→%v", f.Old, f.New) } } func TestDiffSchemaFactsAddedRemoved(t *testing.T) { old := []schemaFact{ fact(factKindLinkType, "L1", map[string]any{"cardinality": "many_to_many"}, nil, false), } new := []schemaFact{ fact(factKindLinkType, "L2", map[string]any{"cardinality": "one_to_many"}, nil, false), } entries := diffSchemaFacts(old, new) if len(entries) != 2 { t.Fatalf("期望 2 条差异(增+删),实际 %d: %+v", len(entries), entries) } byName := map[string]schemaDiffEntry{} for _, e := range entries { byName[e.Name] = e } if byName["L1"].Change != "removed" { t.Errorf("L1 应为 removed,实际 %q", byName["L1"].Change) } if byName["L2"].Change != "added" { t.Errorf("L2 应为 added,实际 %q", byName["L2"].Change) } // 新增条目:字段值在 new 侧。 for _, f := range byName["L2"].Fields { if f.Old != nil { t.Errorf("added 条目的字段不应有 old 值: %+v", f) } } } func TestDiffSchemaFactsCertifiedMarker(t *testing.T) { old := []schemaFact{ fact(factKindObjectType, "T", nil, map[string]any{"description": "甲"}, true), } new := []schemaFact{ fact(factKindObjectType, "T", nil, map[string]any{"description": "乙"}, true), } entries := diffSchemaFacts(old, new) if len(entries) != 1 || !entries[0].Certified { t.Fatalf("已认证条目的变更应带 certified 标记: %+v", entries) } s := summarizeSchemaDiff(entries) if s.CertifiedTouched != 1 { t.Errorf("certified_entries_touched 期望 1,实际 %d", s.CertifiedTouched) } if s.CuratedFields != 1 { t.Errorf("curated_fields 期望 1,实际 %d", s.CuratedFields) } if s.MachineFactFields != 0 { t.Errorf("machine_fact_fields 期望 0,实际 %d", s.MachineFactFields) } } func TestCanonicalJSONNormalizesGoTypes(t *testing.T) { // 实时采集得到 []string,快照读回是 []any——归一化后必须判等。 if canonicalJSON([]string{"a", "b"}) != canonicalJSON([]any{"a", "b"}) { t.Errorf("[]string 与 []any 应判等") } if canonicalJSON(map[string]any{"a": 1}) != canonicalJSON(map[string]any{"a": float64(1)}) { t.Errorf("int 与 float64 编码后应一致") } if canonicalJSON(nil) != "null" { t.Errorf("nil 应归一为 null") } if canonicalJSON("x") == canonicalJSON("y") { t.Errorf("不同字符串不应判等") } } func TestParseSchemaVersionParam(t *testing.T) { cases := []struct { in string allowLive bool wantVer int64 wantLive bool wantErr bool }{ {"", false, 0, false, false}, {"baseline", false, 0, false, false}, {"基线", false, 0, false, false}, {"12", false, 12, false, false}, {"live", true, 0, true, false}, {"live", false, 0, false, true}, {"abc", true, 0, false, true}, } for _, c := range cases { ver, live, err := parseSchemaVersionParam(c.in, c.allowLive) if c.wantErr { if err == nil { t.Errorf("参数 %q 应报错", c.in) } continue } if err != nil { t.Errorf("参数 %q 不该报错: %v", c.in, err) continue } if ver != c.wantVer || live != c.wantLive { t.Errorf("参数 %q → (%d,%v),期望 (%d,%v)", c.in, ver, live, c.wantVer, c.wantLive) } } } func TestSummarizeSchemaDiff(t *testing.T) { entries := []schemaDiffEntry{ {Kind: factKindObjectType, Name: "A", Change: "added", Fields: []schemaFieldChange{{Field: "iri", Class: classMachine}}}, {Kind: factKindObjectType, Name: "B", Change: "removed", Fields: []schemaFieldChange{{Field: "description", Class: classCurated}}}, {Kind: factKindObjectType, Name: "C", Change: "changed", Fields: []schemaFieldChange{ {Field: "iri", Class: classMachine}, {Field: "description", Class: classCurated}, }}, } s := summarizeSchemaDiff(entries) if s.Added != 1 || s.Removed != 1 || s.Changed != 1 { t.Errorf("增删改计数错误: %+v", s) } if s.MachineFactFields != 2 { t.Errorf("machine_fact_fields 期望 2,实际 %d", s.MachineFactFields) } if s.CuratedFields != 2 { t.Errorf("curated_fields 期望 2,实际 %d", s.CuratedFields) } }