forked from orus-io/json-schema-generate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonschemaparse_test.go
167 lines (145 loc) · 4.59 KB
/
jsonschemaparse_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
package generate
import (
"net/url"
"testing"
)
func TestThatAMissingSchemaKeyResultsInAnError(t *testing.T) {
invalid := `{
"title": "root"
}`
_, invaliderr := Parse(invalid, &url.URL{Scheme: "file", Path: "jsonschemaparse_test.go"})
valid := `{
"$schema": "http://json-schema.org/schema#",
"title": "root"
}`
_, validerr := Parse(valid, &url.URL{Scheme: "file", Path: "jsonschemaparse_test.go"})
if invaliderr == nil {
// it SHOULD be used in the root schema
// t.Error("When the $schema key is missing from the root, the JSON Schema is not valid")
}
if validerr != nil {
t.Error("It should be possible to parse a simple JSON schema if the $schema key is present")
}
}
func TestThatTheRootSchemaCanBeParsed(t *testing.T) {
s := `{
"$schema": "http://json-schema.org/schema#",
"title": "root"
}`
so, err := Parse(s, &url.URL{Scheme: "file", Path: "jsonschemaparse_test.go"})
if err != nil {
t.Fatal("It should be possible to unmarshal a simple schema, but received error:", err)
}
if so.Title != "root" {
t.Errorf("The title was not deserialised from the JSON schema, expected %s, but got %s", "root", so.Title)
}
}
func TestThatPropertiesCanBeParsed(t *testing.T) {
s := `{
"$schema": "http://json-schema.org/schema#",
"title": "root",
"properties": {
"name": {
"type": "string"
},
"address": {
"$ref": "#/definitions/address"
},
"status": {
"$ref": "#/definitions/status"
}
}
}`
so, err := Parse(s, &url.URL{Scheme: "file", Path: "jsonschemaparse_test.go"})
if err != nil {
t.Fatal("It was not possible to unmarshal the schema:", err)
}
nameType, nameMultiple := so.Properties["name"].Type()
if nameType != "string" || nameMultiple {
t.Errorf("expected property 'name' type to be 'string', but was '%v'", nameType)
}
addressType, _ := so.Properties["address"].Type()
if addressType != "" {
t.Errorf("expected property 'address' type to be '', but was '%v'", addressType)
}
if so.Properties["address"].Reference != "#/definitions/address" {
t.Errorf("expected property 'address' reference to be '#/definitions/address', but was '%v'", so.Properties["address"].Reference)
}
if so.Properties["status"].Reference != "#/definitions/status" {
t.Errorf("expected property 'status' reference to be '#/definitions/status', but was '%v'", so.Properties["status"].Reference)
}
}
func TestThatPropertiesCanHaveMultipleTypes(t *testing.T) {
s := `{
"$schema": "http://json-schema.org/schema#",
"title": "root",
"properties": {
"name": {
"type": [ "integer", "string" ]
}
}
}`
so, err := Parse(s, &url.URL{Scheme: "file", Path: "jsonschemaparse_test.go"})
if err != nil {
t.Fatal("It was not possible to unmarshal the schema:", err)
}
nameType, nameMultiple := so.Properties["name"].Type()
if nameType != "integer" {
t.Errorf("expected first value of property 'name' type to be 'integer', but was '%v'", nameType)
}
if !nameMultiple {
t.Errorf("expected multiple types, but only returned one")
}
}
func TestThatParsingInvalidValuesReturnsAnError(t *testing.T) {
s := `{ " }`
_, err := Parse(s, &url.URL{Scheme: "file", Path: "jsonschemaparse_test.go"})
if err == nil {
t.Fatal("Expected a parsing error, but got nil")
}
}
func TestThatDefaultsCanBeParsed(t *testing.T) {
s := `{
"$schema": "http://json-schema.org/schema#",
"title": "root",
"properties": {
"name": {
"type": [ "integer", "string" ],
"default":"Enrique"
}
}
}`
so, err := Parse(s, &url.URL{Scheme: "file", Path: "jsonschemaparse_test.go"})
if err != nil {
t.Fatal("It was not possible to unmarshal the schema:", err)
}
defaultValue := so.Properties["name"].Default
if defaultValue != "Enrique" {
t.Errorf("expected default value of property 'name' type to be 'Enrique', but was '%v'", defaultValue)
}
}
func TestReturnedSchemaId(t *testing.T) {
tests := []struct {
input *Schema
expected string
}{
{
input: &Schema{},
expected: "",
},
{
input: &Schema{ID06: "http://example.com/foo.json", ID04: "#foo"},
expected: "http://example.com/foo.json",
},
{
input: &Schema{ID04: "#foo"},
expected: "#foo",
},
}
for idx, test := range tests {
actual := test.input.ID()
if actual != test.expected {
t.Errorf("Test %d failed: For input \"%+v\", expected \"%s\", got \"%s\"", idx, test.input, test.expected, actual)
}
}
}