-
Notifications
You must be signed in to change notification settings - Fork 2
/
general_test.go
175 lines (140 loc) · 3.37 KB
/
general_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package bench_test
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// nolint:gochecknoinits
func init() {
go func() {
err := http.ListenAndServe(":1234", http.FileServer(http.Dir("./spec/JSON-Schema-Test-Suite/remotes/")))
if err != nil {
log.Fatal(err)
}
}()
}
type testCase struct {
Description string `json:"description"`
Schema json.RawMessage `json:"schema"`
Tests []test `json:"tests"`
}
type test struct {
Description string `json:"description"`
Data json.RawMessage `json:"data"`
Valid bool `json:"valid"`
}
type validator interface {
LoadSchema([]byte) error
ValidJSON([]byte) bool
ValidValue(interface{}) bool
}
func testDir(t *testing.T, dir string, v validator) {
t.Helper()
files, err := ioutil.ReadDir(dir)
require.NoError(t, err)
for _, f := range files {
if f.IsDir() {
continue
}
j, err := ioutil.ReadFile(dir + f.Name()) // nolint:gosec
require.NoError(t, err)
var testCases []testCase
require.NoError(t, json.Unmarshal(j, &testCases))
for _, testCase := range testCases {
testCase := testCase
assert.NotPanics(t, func() {
err = v.LoadSchema(testCase.Schema)
})
if !assert.NoError(t, err) {
continue
}
for _, test := range testCase.Tests {
test := test
t.Run(f.Name()+":"+testCase.Description+":"+test.Description, func(t *testing.T) {
assert.NotPanics(t, func() {
if test.Valid != v.ValidJSON(test.Data) {
t.Fail()
}
})
})
}
}
}
}
// nolint:gocognit
func benchDir(b *testing.B, dir string, v validator, validateValue bool) {
b.Helper()
files, err := ioutil.ReadDir(dir)
require.NoError(b, err)
for _, f := range files {
j, err := ioutil.ReadFile(dir + f.Name()) // nolint:gosec
require.NoError(b, err)
var testCases []testCase
require.NoError(b, json.Unmarshal(j, &testCases))
for _, testCase := range testCases {
err := v.LoadSchema(testCase.Schema)
assert.NoError(b, err)
for _, test := range testCase.Tests {
test := test
b.Run(f.Name()+":"+testCase.Description+":"+test.Description, func(b *testing.B) {
if validateValue {
var val interface{}
err := json.Unmarshal(test.Data, &val)
if err != nil {
b.FailNow()
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if test.Valid != v.ValidValue(val) {
b.FailNow()
}
}
} else {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if test.Valid != v.ValidJSON(test.Data) {
b.FailNow()
}
}
}
})
}
}
}
}
func validatorFromEnv() validator {
var v validator
switch os.Getenv("VALIDATOR") {
case "santhosh":
v = &santhoshValidator{}
case "qri":
v = &qriValidator{}
case "xeipuuv":
v = &xeipuuvValidator{}
default:
panic("unknown or missing VALIDATOR env var")
}
return v
}
const (
ajvPath = "spec/ajv/spec/tests/schemas/"
)
func BenchmarkAjv(b *testing.B) {
dir := ajvPath
benchDir(b, dir, validatorFromEnv(), false)
}
func BenchmarkAjvVal(b *testing.B) {
dir := ajvPath
benchDir(b, dir, validatorFromEnv(), true)
}
func BenchmarkDraft7(b *testing.B) {
dir := "spec/JSON-Schema-Test-Suite/tests/draft7/"
benchDir(b, dir, validatorFromEnv(), false)
}