Skip to content

Commit 5902268

Browse files
smyrmanrs
authored andcommitted
jsonschema: Test cleanup and bugfix (#64)
Testing: - Replaces encoder_test.go (encoding package) with example_test.go (jsonschema package). - Replaces remaining tests in jsonschema_test.go with tests that test the pulbic interface. The new tests are placed in {typeName}_test.go or all_test.go. - Remove go 1.6 testing support (no longer run by CI). Code: - Solves Issue #63 (found by test change) where errors from validatorToJSONSchema and serializeField where never propagated. - Encodes schema.Fields in alphabetical order to allow example test. - Order type methods to appear right below type definition.
1 parent f7e9c99 commit 5902268

13 files changed

+231
-200
lines changed

schema/encoding/encoder_test.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

schema/encoding/jsonschema/all_go16_test.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

schema/encoding/jsonschema/all_go17_test.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

schema/encoding/jsonschema/all_test.go

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build go1.7
2+
13
package jsonschema_test
24

35
import (
@@ -10,6 +12,24 @@ import (
1012
"github.com/stretchr/testify/assert"
1113
)
1214

15+
type dummyValidator struct{}
16+
17+
func (v dummyValidator) Validate(value interface{}) (interface{}, error) {
18+
return value, nil
19+
}
20+
21+
func TestErrNotImplemented(t *testing.T) {
22+
s := schema.Schema{
23+
Fields: schema.Fields{
24+
"i": {
25+
Validator: &dummyValidator{},
26+
},
27+
},
28+
}
29+
enc := jsonschema.NewEncoder(new(bytes.Buffer))
30+
assert.Equal(t, jsonschema.ErrNotImplemented, enc.Encode(&s))
31+
}
32+
1333
// encoderTestCase is used to test the Encoder.Encode() function.
1434
type encoderTestCase struct {
1535
name string
@@ -18,23 +38,20 @@ type encoderTestCase struct {
1838
customValidate encoderValidator
1939
}
2040

21-
// Run runs the testCase according to your Go version. For Go >= 1.7, test cases are run in parallel using the Go 1.7
22-
// sub-test feature. For older versions of Go, the testCase name is simply logged before tests are run sequentially.
2341
func (tc *encoderTestCase) Run(t *testing.T) {
24-
tc.run(t)
25-
}
42+
t.Run(tc.name, func(t *testing.T) {
43+
t.Parallel()
2644

27-
// test performs the actual test and is used by both implementations of run.
28-
func (tc *encoderTestCase) test(t *testing.T) {
29-
b := new(bytes.Buffer)
30-
enc := jsonschema.NewEncoder(b)
31-
assert.NoError(t, enc.Encode(&tc.schema))
45+
b := new(bytes.Buffer)
46+
enc := jsonschema.NewEncoder(b)
47+
assert.NoError(t, enc.Encode(&tc.schema))
3248

33-
if tc.customValidate == nil {
34-
assert.JSONEq(t, tc.expect, b.String())
35-
} else {
36-
tc.customValidate(t, b.Bytes())
37-
}
49+
if tc.customValidate == nil {
50+
assert.JSONEq(t, tc.expect, b.String())
51+
} else {
52+
tc.customValidate(t, b.Bytes())
53+
}
54+
})
3855
}
3956

4057
// encoderValidator can be used to validate encoded data.
@@ -63,7 +80,7 @@ func TestEncoder(t *testing.T) {
6380
name: "ReadOnly=true",
6481
schema: schema.Schema{
6582
Fields: schema.Fields{
66-
"name": schema.Field{
83+
"name": {
6784
ReadOnly: true,
6885
Validator: &schema.String{},
6986
},
@@ -84,7 +101,7 @@ func TestEncoder(t *testing.T) {
84101
name: `type(Default)=string`,
85102
schema: schema.Schema{
86103
Fields: schema.Fields{
87-
"name": schema.Field{
104+
"name": {
88105
Validator: &schema.String{},
89106
Default: "Luke",
90107
},
@@ -110,7 +127,7 @@ func TestEncoder(t *testing.T) {
110127
name: `type(Default)=int`,
111128
schema: schema.Schema{
112129
Fields: schema.Fields{
113-
"name": schema.Field{
130+
"name": {
114131
Validator: &schema.String{},
115132
Default: 24,
116133
},
@@ -135,10 +152,10 @@ func TestEncoder(t *testing.T) {
135152
Validator: &schema.Object{
136153
Schema: &schema.Schema{
137154
Fields: schema.Fields{
138-
"name": schema.Field{
155+
"name": {
139156
Validator: &schema.String{},
140157
},
141-
"age": schema.Field{
158+
"age": {
142159
Validator: &schema.Integer{},
143160
},
144161
},
@@ -178,13 +195,13 @@ func TestEncoder(t *testing.T) {
178195
name: "MinLen=2",
179196
schema: schema.Schema{
180197
Fields: schema.Fields{
181-
"foo": schema.Field{
198+
"foo": {
182199
Validator: &schema.Bool{},
183200
},
184-
"bar": schema.Field{
201+
"bar": {
185202
Validator: &schema.Bool{},
186203
},
187-
"baz": schema.Field{
204+
"baz": {
188205
Validator: &schema.Bool{},
189206
},
190207
},
@@ -205,13 +222,13 @@ func TestEncoder(t *testing.T) {
205222
name: "MaxLen=2",
206223
schema: schema.Schema{
207224
Fields: schema.Fields{
208-
"foo": schema.Field{
225+
"foo": {
209226
Validator: &schema.Bool{},
210227
},
211-
"bar": schema.Field{
228+
"bar": {
212229
Validator: &schema.Bool{},
213230
},
214-
"baz": schema.Field{
231+
"baz": {
215232
Validator: &schema.Bool{},
216233
},
217234
},
@@ -232,11 +249,11 @@ func TestEncoder(t *testing.T) {
232249
name: "Required=true(1/2)",
233250
schema: schema.Schema{
234251
Fields: schema.Fields{
235-
"name": schema.Field{
252+
"name": {
236253
Required: true,
237254
Validator: &schema.String{},
238255
},
239-
"age": schema.Field{
256+
"age": {
240257
Validator: &schema.Integer{},
241258
},
242259
},
@@ -259,15 +276,15 @@ func TestEncoder(t *testing.T) {
259276
name: "Required=true(2/3)",
260277
schema: schema.Schema{
261278
Fields: schema.Fields{
262-
"name": schema.Field{
279+
"name": {
263280
Required: true,
264281
Validator: &schema.String{},
265282
},
266-
"age": schema.Field{
283+
"age": {
267284
Required: true,
268285
Validator: &schema.Integer{},
269286
},
270-
"class": schema.Field{
287+
"class": {
271288
Validator: &schema.String{},
272289
},
273290
},
@@ -304,7 +321,7 @@ func TestEncoder(t *testing.T) {
304321
name: "Validator=Array,ValuesValidator=Object{Schema:nil}",
305322
schema: schema.Schema{
306323
Fields: schema.Fields{
307-
"students": schema.Field{
324+
"students": {
308325
Validator: &schema.Array{
309326
ValuesValidator: &schema.Object{},
310327
},
@@ -327,14 +344,14 @@ func TestEncoder(t *testing.T) {
327344
schema: schema.Schema{
328345
Description: "Object with array of students",
329346
Fields: schema.Fields{
330-
"students": schema.Field{
347+
"students": {
331348
Description: "Array of students",
332349
Validator: &schema.Array{
333350
ValuesValidator: &schema.Object{
334351
Schema: &schema.Schema{
335352
Description: "Student and class",
336353
Fields: schema.Fields{
337-
"student": schema.Field{
354+
"student": {
338355
Description: "The student name",
339356
Required: true,
340357
Default: "Unknown",
@@ -343,7 +360,7 @@ func TestEncoder(t *testing.T) {
343360
MaxLen: 10,
344361
},
345362
},
346-
"class": schema.Field{
363+
"class": {
347364
Description: "The class name",
348365
Default: "Unassigned",
349366
Validator: &schema.String{
@@ -395,11 +412,11 @@ func TestEncoder(t *testing.T) {
395412
name: `Validator=Object,Fields["location"].Validator=Object`,
396413
schema: schema.Schema{
397414
Fields: schema.Fields{
398-
"location": schema.Field{
415+
"location": {
399416
Validator: &schema.Object{
400417
Schema: &schema.Schema{
401418
Fields: schema.Fields{
402-
"country": schema.Field{
419+
"country": {
403420
Validator: &schema.String{},
404421
},
405422
},
@@ -428,7 +445,7 @@ func TestEncoder(t *testing.T) {
428445
name: `Incorrectly configured field`,
429446
schema: schema.Schema{
430447
Fields: schema.Fields{
431-
"location": schema.Field{
448+
"location": {
432449
Description: "location of your stuff",
433450
},
434451
},

schema/encoding/jsonschema/array_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build go1.7
2+
13
package jsonschema_test
24

35
import (
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// +build go1.7
2+
3+
package jsonschema_test
4+
5+
import (
6+
"testing"
7+
8+
"github.com/rs/rest-layer/schema"
9+
)
10+
11+
func TestBoolValidatorEncode(t *testing.T) {
12+
testCase := encoderTestCase{
13+
name: ``,
14+
schema: schema.Schema{
15+
Fields: schema.Fields{
16+
"b": {
17+
Validator: &schema.Bool{},
18+
},
19+
},
20+
},
21+
customValidate: fieldValidator("b", `{"type": "boolean"}`),
22+
}
23+
testCase.Run(t)
24+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package jsonschema_test
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"math"
8+
9+
"github.com/rs/rest-layer/schema"
10+
"github.com/rs/rest-layer/schema/encoding/jsonschema"
11+
)
12+
13+
func ExampleEncoder() {
14+
s := schema.Schema{
15+
Fields: schema.Fields{
16+
"foo": schema.Field{
17+
Required: true,
18+
// NOTE: Min is currently encoded as '0E+00', not '0'.
19+
Validator: &schema.Float{Boundaries: &schema.Boundaries{Min: 0, Max: math.Inf(1)}},
20+
},
21+
"bar": schema.Field{
22+
Validator: &schema.Integer{},
23+
},
24+
"baz": schema.Field{
25+
ReadOnly: true,
26+
Validator: &schema.String{MaxLen: 42},
27+
},
28+
"foobar": schema.Field{},
29+
},
30+
}
31+
b := new(bytes.Buffer)
32+
enc := jsonschema.NewEncoder(b)
33+
enc.Encode(&s)
34+
b2 := new(bytes.Buffer)
35+
json.Indent(b2, b.Bytes(), "", "| ")
36+
fmt.Println(b2)
37+
// Output: {
38+
// | "type": "object",
39+
// | "additionalProperties": false,
40+
// | "properties": {
41+
// | | "bar": {
42+
// | | | "type": "integer"
43+
// | | },
44+
// | | "baz": {
45+
// | | | "readOnly": true,
46+
// | | | "type": "string",
47+
// | | | "maxLength": 42
48+
// | | },
49+
// | | "foo": {
50+
// | | | "type": "number",
51+
// | | | "minimum": 0E+00
52+
// | | },
53+
// | | "foobar": {}
54+
// | },
55+
// | "required": [
56+
// | | "foo"
57+
// | ]
58+
// }
59+
}

schema/encoding/jsonschema/float_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build go1.7
2+
13
package jsonschema_test
24

35
import (
@@ -31,7 +33,7 @@ func TestFloatValidatorEncode(t *testing.T) {
3133
},
3234
},
3335
},
34-
customValidate: fieldValidator("f", `{"type": "number", "enum": [0,0.5,100]}`),
36+
customValidate: fieldValidator("f", `{"type": "number", "enum": [0, 0.5, 100]}`),
3537
},
3638
{
3739
name: "Boundaries={Min:0,Max:100}",

0 commit comments

Comments
 (0)