forked from alecthomas/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 3
/
reflect_test.go
85 lines (72 loc) · 2.81 KB
/
reflect_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
package jsonschema_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"path/filepath"
"strings"
"testing"
"github.com/discovery-digital/jsonschema"
"github.com/discovery-digital/jsonschema/internal/testmodels"
)
type testSet struct {
reflector *jsonschema.Reflector
fixture string
actual interface{}
}
var schemaGenerationTests = []testSet{
{&jsonschema.Reflector{}, "fixtures/defaults.json", testmodels.TestUser{}},
{&jsonschema.Reflector{AllowAdditionalProperties: true}, "fixtures/allow_additional_props.json", testmodels.TestUser{}},
{&jsonschema.Reflector{RequiredFromJSONSchemaTags: true}, "fixtures/required_from_jsontags.json", testmodels.TestUser{}},
{&jsonschema.Reflector{ExpandedStruct: true}, "fixtures/defaults_expanded_toplevel.json", testmodels.TestUser{}},
{&jsonschema.Reflector{}, "fixtures/test_one_of_default.json", testmodels.TestUserOneOf{}},
{&jsonschema.Reflector{}, "fixtures/test_versioned_packages.json", testmodels.TestVersionedPackages{}},
{&jsonschema.Reflector{}, "fixtures/if_then_else.json", testmodels.Application{}},
{&jsonschema.Reflector{}, "fixtures/case.json", testmodels.ExampleCase{}},
{&jsonschema.Reflector{}, "fixtures/test_min_max_items.json", testmodels.SliceTestType{}},
{&jsonschema.Reflector{}, "fixtures/test_recursion.json", testmodels.TestFamilyMember{}},
{&jsonschema.Reflector{}, "fixtures/duplicate_embedded_fields.json", testmodels.Root{}},
{&jsonschema.Reflector{}, "fixtures/arrays.json", testmodels.Arrays{}},
}
func TestSchemaGeneration(t *testing.T) {
for _, tt := range schemaGenerationTests {
runTests(t, tt)
}
}
func TestOverrides(t *testing.T) {
override := jsonschema.GetSchemaTagOverride()
override.Set(testmodels.Hardware{}, "Brand", "enum=microsoft|apple|lenovo|dell")
test := testSet{
reflector: &jsonschema.Reflector{Overrides: override},
fixture: "fixtures/override_jsonschema_tag.json",
actual: testmodels.TestUserOneOf{},
}
runTests(t, test)
}
func runTests(t *testing.T, tt testSet) {
name := strings.TrimSuffix(filepath.Base(tt.fixture), ".json")
t.Run(name, func(t *testing.T) {
f, err := ioutil.ReadFile(tt.fixture)
if err != nil {
t.Errorf("ioutil.ReadAll(%s): %s", tt.fixture, err)
return
}
actualSchema := tt.reflector.Reflect(tt.actual)
actualJSON, err := json.Marshal(actualSchema)
if err != nil {
t.Errorf("json.MarshalIndent(%v, \"\", \" \"): %v", actualJSON, err)
return
}
actualJSON = sanitizeExpectedJson(actualJSON)
cleanExpectedJSON := sanitizeExpectedJson(f)
if !bytes.Equal(cleanExpectedJSON, actualJSON) {
t.Errorf("reflector %+v wanted schema %s, got %s", tt.reflector, cleanExpectedJSON, actualJSON)
}
})
}
func sanitizeExpectedJson(expectedJSON []byte) []byte {
var js interface{}
json.Unmarshal(expectedJSON, &js)
clean, _ := json.MarshalIndent(js, "", " ")
return clean
}