Files
ssw-ontology/server/ontology_export_test.go
T

161 lines
5.3 KiB
Go

package main
import (
"encoding/json"
"strings"
"testing"
)
func TestTurtleRange(t *testing.T) {
want := map[string]string{
"string": "xsd:string", "text": "xsd:string", "enum": "xsd:string", "json": "xsd:string",
"integer": "xsd:integer", "number": "xsd:decimal",
"boolean": "xsd:boolean", "date": "xsd:date", "datetime": "xsd:dateTime",
"未知类型": "xsd:string", // 兜底
}
for dt, w := range want {
if got := turtleRange(dt); got != w {
t.Errorf("turtleRange(%q)=%q want %q", dt, got, w)
}
}
}
func TestBuildTurtle(t *testing.T) {
s := schemaSnapshot{
BaseIRI: "http://example.org/standard-ontology#",
Types: []schemaTypeExport{{
IRI: "http://example.org/standard-ontology#Person", APIName: "Person",
DisplayName: "人物", Description: `带"引号` + "\n换行",
Properties: []schemaPropExport{{
IRI: "http://example.org/standard-ontology#name", APIName: "name",
DisplayName: "姓名", DataType: "string",
}},
}},
Links: []schemaLinkExport{{
IRI: "http://example.org/standard-ontology#knows", APIName: "knows",
DisplayName: "认识", SourceIRI: "http://example.org/standard-ontology#Person",
TargetIRI: "http://example.org/standard-ontology#Person",
}},
}
out := buildTurtle(s)
for _, frag := range []string{
"@prefix owl:", "@prefix rdfs:", "@prefix xsd:",
"<http://example.org/standard-ontology#Person> a owl:Class",
`rdfs:label "人物"`,
`a owl:DatatypeProperty`,
`rdfs:range xsd:string`,
`<http://example.org/standard-ontology#knows> a owl:ObjectProperty`,
`rdfs:domain <http://example.org/standard-ontology#Person>`,
} {
if !strings.Contains(out, frag) {
t.Errorf("输出缺少片段 %q\n---\n%s", frag, out)
}
}
// 引号与换行必须转义,不能产生裸断行
if strings.Contains(out, `"带"引号`) || strings.Contains(out, "引号\n换行\"") {
t.Errorf("字面量未转义:\n%s", out)
}
}
func TestBuildTurtle_Axioms(t *testing.T) {
s := schemaSnapshot{
BaseIRI: "http://example.org/standard-ontology#",
Types: []schemaTypeExport{
{IRI: "http://example.org/standard-ontology#A", APIName: "A", DisplayName: "A",
Axioms: typeAxioms{
DisjointWith: []string{"B"},
EquivalentTo: []string{"C"},
FunctionalLinks: []string{"knows"},
InverseOf: map[string]string{"knows": "knows_rev"},
}},
{IRI: "http://example.org/standard-ontology#B", APIName: "B", DisplayName: "B"},
{IRI: "http://example.org/standard-ontology#C", APIName: "C", DisplayName: "C"},
},
Links: []schemaLinkExport{
{IRI: "http://example.org/standard-ontology#knows", APIName: "knows"},
{IRI: "http://example.org/standard-ontology#knows_rev", APIName: "knows_rev"},
},
}
out := buildTurtle(s)
for _, frag := range []string{
"<http://example.org/standard-ontology#A> owl:disjointWith <http://example.org/standard-ontology#B>",
"<http://example.org/standard-ontology#A> owl:equivalentClass <http://example.org/standard-ontology#C>",
"<http://example.org/standard-ontology#knows> a owl:FunctionalProperty",
"<http://example.org/standard-ontology#knows> owl:inverseOf <http://example.org/standard-ontology#knows_rev>",
} {
if !strings.Contains(out, frag) {
t.Errorf("输出缺片段 %q\n---\n%s", frag, out)
}
}
}
func TestTurtleLiteralEscaping(t *testing.T) {
cases := []struct{ in, want string }{
{`plain`, `"plain"`},
{`带"引号\反斜杠`, `"带\"引号\\反斜杠"`},
{"tab\t换行\n回车\r", `"tab\t换行\n回车\r"`},
// \a(0x07)/\v(0x0b) 不在 Turtle 转义表 → \uXXXX(Go %q 会错产 \a \v)
{"\x07\x0b\x1f", `"\u0007\u000B\u001F"`},
{"中文 café 保留", `"中文 café 保留"`},
}
for _, c := range cases {
if got := turtleLiteral(c.in); got != c.want {
t.Errorf("turtleLiteral(%q)=%q want %q", c.in, got, c.want)
}
}
}
func TestTurtleLabels(t *testing.T) {
cases := []struct{ name, lang, fb, want string }{
{"empty_fallback", "", "Person", `rdfs:label "Person"`},
{"single_lang", `{"zh":"人物"}`, "Person", `rdfs:label "人物"@zh`},
{"multi_lang_sorted", `{"zh":"人物","en":"Person"}`, "Person", `rdfs:label "Person"@en ;` + "\n " + `rdfs:label "人物"@zh`},
}
for _, c := range cases {
var m map[string]string
if c.lang != "" {
_ = json.Unmarshal([]byte(c.lang), &m)
}
got := turtleLabels(m, c.fb)
if got != c.want {
t.Errorf("%s: got %q want %q", c.name, got, c.want)
}
}
}
func TestValidLangTag(t *testing.T) {
ok := []string{"zh", "en", "zh-CN", "zh-Hans", "en-US", "fr-FR"}
bad := []string{"", "z", "zh-cn", "zh_CN", "1zh", "zh-", "zh-12", "zh-cn1"}
for _, s := range ok {
if !validLangTag(s) {
t.Errorf("validLangTag(%q)=false want true", s)
}
}
for _, s := range bad {
if validLangTag(s) {
t.Errorf("validLangTag(%q)=true want false", s)
}
}
}
func TestTurtleRangeURI(t *testing.T) {
if got := turtleRange("uri"); got != "xsd:anyURI" {
t.Errorf("turtleRange(uri)=%q want xsd:anyURI", got)
}
}
func TestIsAbsURI(t *testing.T) {
ok := []string{"https://example.org/x", "http://x.cn/a?b=c", "mailto:[email protected]", "urn:iso:std:48000.3"}
bad := []string{"", "not a uri", "example.org/x", "/local", "://empty", "ht tp://x"}
for _, s := range ok {
if !isAbsURI(s) {
t.Errorf("isAbsURI(%q)=false want true", s)
}
}
for _, s := range bad {
if isAbsURI(s) {
t.Errorf("isAbsURI(%q)=true want false", s)
}
}
}