forked from nytlabs/gojsonexplode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
explode_test.go
181 lines (168 loc) · 5.71 KB
/
explode_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
176
177
178
179
180
181
package gojsonexplode
import (
"testing"
)
func TestTrue(t *testing.T) {
input := `[true]`
output := `{"0":true}`
out, _ := Explodejsonstr(input, ".", -1)
if out != output {
t.Error("got", out)
}
}
func TestTruePassThrough(t *testing.T) {
input := `[true]`
output := input
out, _ := Explodejsonstr(input, ".", 0)
if out != output {
t.Error("got", out)
}
}
func TestNull(t *testing.T) {
input := `[null]`
output := `{"0":null}`
out, _ := Explodejsonstr(input, ".", -1)
if out != output {
t.Error("got", out)
}
}
func TestNesting(t *testing.T) {
input := `{"person":{"name":"Joe", "address":{"street":"123 Main St."}}}`
output := `{"person.address.street":"123 Main St.","person.name":"Joe"}`
out, _ := Explodejsonstr(input, ".", -1)
if out != output {
t.Error("got", out)
}
}
func TestNestingWithLimit(t *testing.T) {
input := `{"person":{"name":"Joe", "address":{"street":"123 Main St."}}}`
output := `{"person.address":{"street":"123 Main St."},"person.name":"Joe"}`
out, _ := Explodejsonstr(input, ".", 1)
if out != output {
t.Error("got", out)
}
}
func TestNestedPassthrough(t *testing.T) {
input := `{"person":{"name":"Joe", "address":{"street":"123 Main St."}}}`
output := input
out, _ := Explodejsonstr(input, ".", 0)
if out != output {
t.Error("got", out)
}
}
func TestNestingWithSlash(t *testing.T) {
input := `{"person":{"name":"Joe", "address":{"street":"123 Main St."}}}`
output := `{"person/address/street":"123 Main St.","person/name":"Joe"}`
out, _ := Explodejsonstr(input, "/", -1)
if out != output {
t.Error("got", out)
}
}
func TestItems(t *testing.T) {
input := `
[
{
"description": "a schema given for items",
"schema": {
"items": {"type": "integer"}
},
"tests": [
{
"description": "valid items",
"data": [ 1, 2, 3 ],
"valid": true
},
{
"description": "wrong type of items",
"data": [1, "x"],
"valid": false
},
{
"description": "ignores non-arrays",
"data": {"foo" : "bar"},
"valid": true
}
]
},
{
"description": "an array of schemas for items",
"schema": {
"items": [
{"type": "integer"},
{"type": "string"}
]
},
"tests": [
{
"description": "correct types",
"data": [ 1, "foo" ],
"valid": true
},
{
"description": "wrong types",
"data": [ "foo", 1 ],
"valid": false
}
]
}
]`
output := `{"0.description":"a schema given for items","0.schema.items.type":"integer","0.tests.0.data.0":1,"0.tests.0.data.1":2,"0.tests.0.data.2":3,"0.tests.0.description":"valid items","0.tests.0.valid":true,"0.tests.1.data.0":1,"0.tests.1.data.1":"x","0.tests.1.description":"wrong type of items","0.tests.1.valid":false,"0.tests.2.data.foo":"bar","0.tests.2.description":"ignores non-arrays","0.tests.2.valid":true,"1.description":"an array of schemas for items","1.schema.items.0.type":"integer","1.schema.items.1.type":"string","1.tests.0.data.0":1,"1.tests.0.data.1":"foo","1.tests.0.description":"correct types","1.tests.0.valid":true,"1.tests.1.data.0":"foo","1.tests.1.data.1":1,"1.tests.1.description":"wrong types","1.tests.1.valid":false}`
out, _ := Explodejsonstr(input, ".", -1)
if out != output {
t.Error("got", out)
}
}
func TestItemsWithLimit(t *testing.T) {
input := `
[
{
"description": "a schema given for items",
"schema": {
"items": {"type": "integer"}
},
"tests": [
{
"description": "valid items",
"data": [ 1, 2, 3 ],
"valid": true
},
{
"description": "wrong type of items",
"data": [1, "x"],
"valid": false
},
{
"description": "ignores non-arrays",
"data": {"foo" : "bar"},
"valid": true
}
]
},
{
"description": "an array of schemas for items",
"schema": {
"items": [
{"type": "integer"},
{"type": "string"}
]
},
"tests": [
{
"description": "correct types",
"data": [ 1, "foo" ],
"valid": true
},
{
"description": "wrong types",
"data": [ "foo", 1 ],
"valid": false
}
]
}
]`
output := `{"0.description":"a schema given for items","0.schema.items":{"type":"integer"},"0.tests.0":{"data":[1,2,3],"description":"valid items","valid":true},"0.tests.1":{"data":[1,"x"],"description":"wrong type of items","valid":false},"0.tests.2":{"data":{"foo":"bar"},"description":"ignores non-arrays","valid":true},"1.description":"an array of schemas for items","1.schema.items":[{"type":"integer"},{"type":"string"}],"1.tests.0":{"data":[1,"foo"],"description":"correct types","valid":true},"1.tests.1":{"data":["foo",1],"description":"wrong types","valid":false}}`
out, _ := Explodejsonstr(input, ".", 2)
if out != output {
t.Error("got", out)
}
}