-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_schema_test.go
More file actions
112 lines (102 loc) · 2.97 KB
/
json_schema_test.go
File metadata and controls
112 lines (102 loc) · 2.97 KB
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
package main
import (
"encoding/json"
"github.com/dameleon/structr/fixtures"
"github.com/xeipuuv/gojsonpointer"
"testing"
)
func TestJsonSchema_GetItemList_ByObject(t *testing.T) {
var schema JsonSchema
itemsByObject := `{ "items": { "type": "string" } }`
json.Unmarshal([]byte(itemsByObject), &schema)
items := schema.GetItemList()
if len(items) != 1 {
t.Error("mismatched item length. LEN: %d", len(items))
}
if items[0].Type != JsonSchemaTypeString {
t.Error("mismatched item type. TYPE: %s", items[0].Type)
}
}
func TestJsonSchema_GetItemList_ByArray(t *testing.T) {
var schema JsonSchema
itemsByArray := `{ "items": [{ "type": "string" }, { "type": "integer" }] }`
json.Unmarshal([]byte(itemsByArray), &schema)
items := schema.GetItemList()
if len(items) != 2 {
t.Error("mismatched item length. LEN: %d", len(items))
}
if items[0].Type != JsonSchemaTypeString {
t.Error("mismatched item type. TYPE: %s", items[0].Type)
}
if items[1].Type != JsonSchemaTypeInteger {
t.Error("mismatched item type. TYPE: %s", items[0].Type)
}
}
func TestJsonSchema_IsRequired(t *testing.T) {
var schema JsonSchema
json.Unmarshal([]byte(`{ "required": ["hoge", "fuga"] }`), &schema)
if schema.IsRequired("hoge") != true {
t.Error("key 'hoge' should be required")
}
if schema.IsRequired("piyo") != false {
t.Error("key 'piyo' should be not required")
}
}
func TestJsonSchema_GetRefSchema(t *testing.T) {
var schema JsonSchema
json.Unmarshal([]byte(`{ "definitions": { "hoge": { "type": "string" } } }`), &schema)
p, _ := gojsonpointer.NewJsonPointer("/definitions/hoge")
refSchema := schema.GetRefSchema(&p)
if refSchema.Type != JsonSchemaTypeString {
t.Error("ref schema should be type of string")
}
}
func TestJsonSchema_GetRefList(t *testing.T) {
schema, _ := NewJsonSchemaLoader().Load(fixtures.InternalReferenceSchemaRef)
refList := schema.GetRefList()
if len(refList) != 2 {
t.Errorf("ref list count should be 2. LEN: %d", len(refList))
}
}
func TestJsonSchema_HasReference(t *testing.T) {
{
var schema JsonSchema
json.Unmarshal([]byte(`{ "$ref": "#/definitions/hoge" }`), &schema)
if schema.HasReference() != true {
t.Error("has reference should be true")
}
}
{
var schema JsonSchema
json.Unmarshal([]byte(`{ "$ref": "" }`), &schema)
if schema.HasReference() != false {
t.Error("has reference should be false")
}
}
}
func TestJsonSchema_HasStructure(t *testing.T) {
// object
{
var schema JsonSchema
json.Unmarshal([]byte(`{ "type": "object" }`), &schema)
if schema.HasStructure() != true {
t.Error("has structure should be true")
}
}
// object with array
{
var schema JsonSchema
json.Unmarshal([]byte(`{ "type": "array", "items": { "type": "object" } }`), &schema)
if schema.HasStructure() != true {
t.Error("has structure should be true")
}
}
// other
{
var schema JsonSchema
json.Unmarshal([]byte(`{ "type": "string" }`), &schema)
if schema.HasStructure() != false {
t.Error("has structure should be false")
}
}
}