64 lines
2.2 KiB
Go
64 lines
2.2 KiB
Go
package glossary
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"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},
|
|
{"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)
|
|
}
|
|
}
|