This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalidation_test.go
136 lines (109 loc) · 3.37 KB
/
validation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package metadata_schemas_test
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/eclipse-pass/metadata-schemas/lib/jsonschema"
"github.com/xeipuuv/gojsonschema"
)
const (
jhuSchemaDir = "schemas/jhu"
harvardSchemaDir = "schemas/harvard"
)
// Merged schemas between institutions may conflict, so we need to do them separately
func TestJHUSchemas(t *testing.T) {
jhuSchemas := loadSchemas(t, jhuSchemaDir, true)
cases := map[string]bool{}
addCases(t, cases, "examples/jhu", true)
addCases(t, cases, "testdata/valid/jhu", true)
addCases(t, cases, "testdata/invalid/jhu", false)
validateSchemas(t, jhuSchemas, cases)
}
func TestHarvardSchemas(t *testing.T) {
harvardSchemas := loadSchemas(t, harvardSchemaDir, true)
cases := map[string]bool{}
addCases(t, cases, "examples/harvard", true)
addCases(t, cases, "testdata/valid/harvard", true)
addCases(t, cases, "testdata/invalid/harvard", false)
validateSchemas(t, harvardSchemas, cases)
}
func validateSchemas(t *testing.T, schemas jsonschema.Map, cases map[string]bool) {
for filename, shouldBeValid := range cases {
filename := filename
shouldBeValid := shouldBeValid
t.Run(filename, func(t *testing.T) {
var hasExpectedFailure bool
for id, schema := range schemas {
toTest := gojsonschema.NewGoLoader(schema)
result, err := gojsonschema.Validate(toTest, loadTestSchema(t, filename))
if err != nil {
t.Fatalf("Error validating against schema %s: %+v", id, err)
}
if shouldBeValid && !result.Valid() {
for _, err := range result.Errors() {
t.Logf("- %s\n", err)
}
t.Fatalf("Schema validation for %s failed!", id)
} else if !shouldBeValid && !result.Valid() {
hasExpectedFailure = true
}
}
if !shouldBeValid && !hasExpectedFailure {
t.Fatalf("schema passed validation, but should have failed")
}
})
}
}
func loadTestSchema(t *testing.T, filename string) gojsonschema.JSONLoader {
testdataFile, err := os.Open(filename)
if err != nil {
t.Fatalf("Could not open example data")
}
defer testdataFile.Close()
body, _ := ioutil.ReadAll(testdataFile)
return gojsonschema.NewBytesLoader(body)
}
func loadSchemas(t *testing.T, dir string, merge bool) jsonschema.Map {
schemaFiles := findJSONDocs(t, dir)
schemaMap, err := jsonschema.Load(schemaFiles)
if err != nil {
t.Fatalf("Error loading schemas: %+v", err)
}
var schemas []jsonschema.Instance
for _, v := range schemaMap {
schemas = append(schemas, v)
}
err = jsonschema.Dereference(schemaMap, schemas...)
if err != nil {
t.Fatalf("Error dereferencing schemas %+v", err)
}
if merge {
// Finally, add a union schema that merges them all (making sure that validations are expected against that too)
merged, err := jsonschema.Merge(schemas)
if err != nil {
t.Fatalf("Error merging schemas: %+v", err)
}
schemaMap["merged"] = merged
}
return schemaMap
}
func addCases(t *testing.T, cases map[string]bool, fromDir string, shouldValidate bool) {
for _, path := range findJSONDocs(t, fromDir) {
cases[path] = shouldValidate
}
}
func findJSONDocs(t *testing.T, dir string) []string {
var paths []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, "json") {
paths = append(paths, path)
}
return nil
})
if err != nil {
t.Fatalf("Walk for schemas failed: %+v", err)
}
return paths
}