Prepare shared glossary envelopes and combined input
This commit is contained in:
@@ -1,68 +1,13 @@
|
||||
package glossary
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
import "testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestValidation(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, input string
|
||||
partial, valid bool
|
||||
}{
|
||||
{"empty", "party: {entries: []}", false, true},
|
||||
{"null aliases", "party: {entries: [{id: a, name: A, category: person, aliases: null}]}", false, true},
|
||||
{"invalid aliases", "party: {entries: [{id: a, name: A, category: person, aliases: wrong}]}", false, false},
|
||||
{"invalid alias item", "party: {entries: [{id: a, name: A, category: person, aliases: [null]}]}", false, false},
|
||||
{"null description", "party: {entries: [{id: a, name: A, category: person, description: null}]}", false, false},
|
||||
{"null instruction", "party: {instruction: null, entries: []}", false, false},
|
||||
{"missing entries", "party: {instruction: hi}", false, false},
|
||||
{"instruction only", "instruction: hi", true, true},
|
||||
{"optional and extensions", "party: {entries: [{id: a, name: A, category: person, aliases: [Alias], plural: People, description: text, extra: {rank: 2}}]}", false, true},
|
||||
{"missing category", "party: {entries: [{id: a, name: A}]}", false, false},
|
||||
{"duplicate within collection", "party: {entries: [{id: a, name: A, category: person}, {id: a, name: B, category: person}]}", false, false},
|
||||
{"same id across collections", "party: {entries: [{id: a, name: A, category: person}]}\nother: {entries: [{id: a, name: B, category: person}]}", false, true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var value any
|
||||
if err := yaml.Unmarshal([]byte(tc.input), &value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var err error
|
||||
if tc.partial {
|
||||
err = ValidatePartial(value)
|
||||
} else {
|
||||
err = ValidateDocument(value)
|
||||
}
|
||||
if (err == nil) != tc.valid {
|
||||
t.Fatalf("valid=%v: %v", tc.valid, err)
|
||||
}
|
||||
if tc.name == "duplicate within collection" && !strings.Contains(err.Error(), "party") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypedCollectionRetainsExtensionsAndEmptyInstruction(t *testing.T) {
|
||||
var collection Collection
|
||||
if err := yaml.Unmarshal([]byte("instruction: ''\nentries: [{id: a, name: A, category: person, source_note: test}]\n"), &collection); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if collection.Instruction == nil || *collection.Instruction != "" || collection.Entries[0].Extra["source_note"] != "test" {
|
||||
t.Fatalf("lost data: %+v", collection)
|
||||
}
|
||||
b, err := json.Marshal(map[string]Collection{"party": collection})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(b), `"source_note":"test"`) || !strings.Contains(string(b), `"instruction":""`) {
|
||||
t.Fatalf("lost data: %s", b)
|
||||
}
|
||||
if err := ValidateDocument(map[string]Collection{"party": collection}); err != nil {
|
||||
t.Fatal(err)
|
||||
func TestValidateDocumentUsesSharedEnvelope(t *testing.T) {
|
||||
valid := map[string]any{"schema_version": glossaryMarker, "collections": map[string]any{"party": map[string]any{"entries": []any{}}}}
|
||||
if err := ValidateDocument(valid); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ValidateDocument(map[string]any{"party": map[string]any{"entries": []any{}}}); err == nil {
|
||||
t.Fatal("wrapperless glossary accepted")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user