Bundle and validate the shared glossary contract
This commit is contained in:
81
internal/appconfig/sharedglossary/schema_test.go
Normal file
81
internal/appconfig/sharedglossary/schema_test.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package sharedglossary
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.yaml.in/yaml/v4"
|
||||
)
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
validator, err := NewValidator()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name, document, location string
|
||||
valid bool
|
||||
}{
|
||||
{"ordinary", `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{"entries":[{"id":"one","name":"One","category":"number"}]}}}`, "", true},
|
||||
{"empty collections", `{"schema_version":"maximumdirect.glossary.v1","collections":{}}`, "", true},
|
||||
{"nullable and extensions", `{"schema_version":"maximumdirect.glossary.v1","extra":true,"collections":{"terms":{"instruction":42,"provenance":{"extra":true},"entries":[{"id":"one","name":"One","category":"number","aliases":null,"plural":null,"extra":{"n":1}}]}}}`, "", true},
|
||||
{"missing marker", `{"collections":{}}`, "schema_version", false},
|
||||
{"wrong marker", `{"schema_version":"wrong","collections":{}}`, "schema_version", false},
|
||||
{"missing entries", `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{}}}`, "entries", false},
|
||||
{"invalid name", `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{"entries":[{"id":"one","name":3,"category":"number"}]}}}`, "name", false},
|
||||
{"invalid aliases", `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{"entries":[{"id":"one","name":"One","category":"number","aliases":3}]}}}`, "aliases", false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var document, before map[string]any
|
||||
if err := json.Unmarshal([]byte(tc.document), &document); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal([]byte(tc.document), &before); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := validator.Validate(document, "example.yml")
|
||||
if (err == nil) != tc.valid {
|
||||
t.Fatalf("Validate() = %v", err)
|
||||
}
|
||||
if !tc.valid && (!strings.Contains(err.Error(), "example.yml") || !strings.Contains(err.Error(), tc.location)) {
|
||||
t.Fatalf("error lacks source or field location: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(document, before) {
|
||||
t.Fatal("validation modified the document")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBundledExamples(t *testing.T) {
|
||||
validator, err := NewValidator()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"glossary.yml", "sports.yml", "weather.yml", "weather.json"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
path := filepath.Join("assets", "glossary", "v0.2.0", "examples", name)
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var document map[string]any
|
||||
if err := yaml.Unmarshal(data, &document); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := validator.Validate(document, path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOfflineLoader(t *testing.T) {
|
||||
if _, err := (offlineLoader{}).Load("https://example.invalid/schema.json"); err == nil {
|
||||
t.Fatal("unregistered resource was accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user