-
Notifications
You must be signed in to change notification settings - Fork 3
/
jsonpath_test.go
219 lines (209 loc) · 7.76 KB
/
jsonpath_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package jsonpath_test
import (
"encoding/json"
"testing"
"time"
. "github.com/cthulhu/jsonpath"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gmeasure"
)
var _ = Describe("Jpath", func() {
Context("Simple key value", func() {
It("generates json", func() {
in := map[string]string{"key": "value"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`{"key":"value"}`)))
})
})
Context("Simple embeddens key value", func() {
It("generates json", func() {
in := map[string]string{"price.value": "100.00"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`{"price":{"value":"100.00"}}`)))
})
})
Context("Panic atack with number and dot", func() {
It("generates json skipping the wrong keys - first hashes", func() {
in := map[string]string{"one": "1233", "2. subcategory": "booooom", "two": "2"}
_, err := Marshal(in)
Expect(err).To(HaveOccurred())
})
It("generates json skipping the wrong keys - first arrays", func() {
in := map[string]string{"2. subcategory": "booooom", "one": "1233", "two": "2"}
_, err := Marshal(in)
Expect(err).To(HaveOccurred())
})
})
Context("Long embeddens key value", func() {
It("generates json", func() {
in := map[string]string{"price.value1.value2": "100.00"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`{"price":{"value1":{"value2":"100.00"}}}`)))
})
})
Context("Simple embeddens few key values", func() {
It("generates json", func() {
in := map[string]string{"price.value": "100.00", "price.currency": "EU"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`{"price":{"currency":"EU","value":"100.00"}}`)))
})
})
Context("Simple embeddens few key values different levels", func() {
It("generates json", func() {
in := map[string]string{"price.value": "100.00", "price.currency": "EU", "shipping.value": "99.00", "shipping.currency": "UA"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`{"price":{"currency":"EU","value":"100.00"},"shipping":{"currency":"UA","value":"99.00"}}`)))
})
})
Context("Simple embeddens few key values and array with one value", func() {
It("generates json", func() {
in := map[string]string{"prices.0": "100.00"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`{"prices":["100.00"]}`)))
})
})
Context("Simple embeddens few key values and array with two values", func() {
It("generates json", func() {
in := map[string]string{"prices.1": "100.00", "prices.0": "10.00"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`{"prices":["10.00","100.00"]}`)))
})
})
Context("Simple embeddens few key values and array with three values", func() {
It("generates json", func() {
in := map[string]string{"prices.2": "100.00", "prices.0": "10.00"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`{"prices":["10.00",null,"100.00"]}`)))
})
})
Context("Simple embeddens few key values and array with shipping", func() {
It("generates json", func() {
in := map[string]string{"price.value": "100.00", "price.currency": "EU", "shipping.0.country": "GB", "shipping.0.service": "Standart shipping", "shipping.0.price.value": "33", "shipping.0.price.curency": "GBP"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`{"price":{"currency":"EU","value":"100.00"},"shipping":[{"country":"GB","price":{"curency":"GBP","value":"33"},"service":"Standart shipping"}]}`)))
})
})
Context("Arrays", func() {
Context("key value", func() {
It("generates json", func() {
in := map[string]string{"0.value": "100.00"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`[{"value":"100.00"}]`)))
})
})
Context("array key", func() {
It("generates array", func() {
in := map[string]string{"0.value.[]": "1,2,3,4"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`[{"value":["1","2","3","4"]}]`)))
})
})
Context("key value with num", func() {
It("generates json", func() {
in := map[string]string{"0.value.num()": "100.00"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`[{"value":100}]`)))
})
})
Context("key value with float", func() {
It("generates json", func() {
in := map[string]string{"0.value.num()": "100.12"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`[{"value":100.12}]`)))
})
})
Context("key value with bool", func() {
It("generates json for true", func() {
in := map[string]string{"0.value.bool()": "true"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`[{"value":true}]`)))
})
It("generates json for false", func() {
in := map[string]string{"0.value.bool()": "false"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`[{"value":false}]`)))
})
It("generates json for empty", func() {
in := map[string]string{"0.value.bool()": ""}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`[{"value":false}]`)))
})
It("generates json for not bool value", func() {
in := map[string]string{"0.value.bool()": "1234"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`[{"value":false}]`)))
})
})
})
It("should do something hard efficiently", func() {
experiment := gmeasure.NewExperiment("end-to-end web-server performance")
AddReportEntry(experiment.Name, experiment)
experiment.MeasureDuration("runtime", func() {
in := map[string]string{"price.value": "100.00", "price.currency": "EU", "shipping.0.country": "GB", "shipping.0.service": "Standart shipping", "shipping.0.price.value": "33", "shipping.0.price.curency": "GBP"}
actual, err := Marshal(in)
Expect(err).NotTo(HaveOccurred())
Expect(actual).To(Equal([]byte(`{"price":{"currency":"EU","value":"100.00"},"shipping":[{"country":"GB","price":{"curency":"GBP","value":"33"},"service":"Standart shipping"}]}`)))
})
Expect(experiment.GetStats("runtime").DurationFor(gmeasure.StatMedian)).To(BeNumerically("<", 1*time.Millisecond))
})
})
func BenchmarkComplexJSONPathArray(b *testing.B) {
in := map[string]string{"price.value": "100.00", "price.currency": "EU", "shipping.0.country": "GB", "shipping.0.service": "Standart shipping", "shipping.0.price.value": "33", "shipping.0.price.curency": "GBP"}
for n := 0; n < b.N; n++ {
Marshal(in)
}
}
func BenchmarkSimpleJSONPathArrayWithNum(b *testing.B) {
in := map[string]string{"0.value.num()": "100.12"}
for n := 0; n < b.N; n++ {
Marshal(in)
}
}
func BenchmarkSimpleJSONPathArrayWithBool(b *testing.B) {
in := map[string]string{"0.value.bool()": "true"}
for n := 0; n < b.N; n++ {
Marshal(in)
}
}
func BenchmarkSimpleJSONPathArrayInsideArray(b *testing.B) {
in := map[string]string{"0.value.[]": "1,2,3,4,5,6"}
for n := 0; n < b.N; n++ {
Marshal(in)
}
}
func BenchmarkSimpleJSONPathArrays(b *testing.B) {
in := map[string]string{"value.[]": "1,2,3,4,5,6"}
for n := 0; n < b.N; n++ {
Marshal(in)
}
}
func BenchmarkSimpleJSONPathSimple(b *testing.B) {
in := map[string]string{"value": "100.12"}
for n := 0; n < b.N; n++ {
Marshal(in)
}
}
func BenchmarkJSONNative(b *testing.B) {
in := map[string]string{"0.value.num()": "100.12"}
for n := 0; n < b.N; n++ {
json.Marshal(in)
}
}