-
Notifications
You must be signed in to change notification settings - Fork 98
/
output_test.go
145 lines (137 loc) · 3.38 KB
/
output_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
137
138
139
140
141
142
143
144
145
package jsonschema_test
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/santhosh-tekuri/jsonschema/v6"
)
func testOutputDir(t *testing.T, suite, dir string, draft *jsonschema.Draft) {
content := filepath.Join(suite, "output-tests", dir, "content")
if _, err := os.Stat(content); err != nil {
if os.IsNotExist(err) {
return
}
t.Fatal(err)
}
f, err := os.Open(filepath.Join(suite, "output-tests", dir, "output-schema.json"))
if err != nil {
t.Fatal(err)
}
defer f.Close()
outputSchema, err := jsonschema.UnmarshalJSON(f)
if err != nil {
t.Fatal(err)
}
outputSchemaURL := fmt.Sprintf("https://json-schema.org/draft/%s/output/schema", strings.TrimPrefix(dir, "draft"))
ee, err := os.ReadDir(content)
if err != nil {
t.Fatal(err)
}
for _, e := range ee {
if e.IsDir() {
continue
}
file := filepath.Join(content, e.Name())
t.Logf("FILE: %s", file)
var groups []struct {
Description string
Schema any
Tests []struct {
Description string
Data any
Output struct {
Basic any
Detailed any
}
}
}
f, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
defer f.Close()
dec := json.NewDecoder(f)
dec.UseNumber()
if err := dec.Decode(&groups); err != nil {
t.Fatal(err)
}
for _, group := range groups {
t.Logf(" %s", group.Description)
c := jsonschema.NewCompiler()
c.DefaultDraft(draft)
url := "http://output-tests/schema.json"
basicURL := "http://output-tests/basic.json"
detailedURL := "http://output-tests/detailed.json"
if err := c.AddResource(url, group.Schema); err != nil {
t.Fatal(err)
}
if err := c.AddResource(outputSchemaURL, outputSchema); err != nil {
t.Fatal(err)
}
sch, err := c.Compile(url)
if err != nil {
t.Fatal(err)
}
for _, test := range group.Tests {
t.Logf(" %s", test.Description)
verr := sch.Validate(test.Data)
if verr == nil {
t.Log("validation success")
continue
}
if test.Output.Basic != nil {
if err := c.AddResource(basicURL, test.Output.Basic); err != nil {
t.Fatal(err)
}
sch, err := c.Compile(basicURL)
if err != nil {
t.Fatal(err)
}
b, err := json.Marshal(verr.(*jsonschema.ValidationError).BasicOutput())
if err != nil {
t.Fatal(err)
}
v, err := jsonschema.UnmarshalJSON(bytes.NewReader(b))
if err != nil {
t.Fatal(err)
}
if err := sch.Validate(v); err != nil {
t.Fatal(err)
}
}
if test.Output.Detailed != nil {
if err := c.AddResource(detailedURL, test.Output.Detailed); err != nil {
t.Fatal(err)
}
sch, err := c.Compile(detailedURL)
if err != nil {
t.Fatal(err)
}
b, err := json.Marshal(verr.(*jsonschema.ValidationError).DetailedOutput())
if err != nil {
t.Fatal(err)
}
v, err := jsonschema.UnmarshalJSON(bytes.NewReader(b))
if err != nil {
t.Fatal(err)
}
if err := sch.Validate(v); err != nil {
t.Fatal(err)
}
}
}
}
}
}
func testOuputSuite(t *testing.T, suite string) {
testOutputDir(t, suite, "draft2019-09", jsonschema.Draft2019)
testOutputDir(t, suite, "draft2020-10", jsonschema.Draft2020)
}
func TestOutputSuites(t *testing.T) {
testOuputSuite(t, "./testdata/JSON-Schema-Test-Suite")
testOuputSuite(t, "./testdata/Extra-Test-Suite")
}