-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_test.go
235 lines (202 loc) · 5.7 KB
/
query_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package protobufquery
import (
"fmt"
"strings"
"testing"
"github.com/antchfx/xpath"
"github.com/stretchr/testify/require"
)
func BenchmarkSelectorCache(b *testing.B) {
DisableSelectorCache = false
for i := 0; i < b.N; i++ {
_, _ = getQuery("/AAA/BBB/DDD/CCC/EEE/ancestor::*")
}
}
func BenchmarkDisableSelectorCache(b *testing.B) {
DisableSelectorCache = true
for i := 0; i < b.N; i++ {
_, _ = getQuery("/AAA/BBB/DDD/CCC/EEE/ancestor::*")
}
}
func TestNavigator(t *testing.T) {
msg := addressbookSample.ProtoReflect()
doc, err := Parse(msg)
require.NoError(t, err)
nav := CreateXPathNavigator(doc)
nav.MoveToRoot()
require.Equal(t, xpath.RootNode, nav.NodeType(), "node type is not RootNode")
expectedPeoples := []map[string]interface{}{
{
"name": "John Doe",
"id": "101",
"email": "[email protected]",
"age": "42",
},
{
"name": "Jane Doe",
"id": "102",
"age": "40",
},
}
// Find the people node
require.True(t, nav.MoveToChild())
for nav.Current().Name != "people" && nav.MoveToNext() {
// nothing to do
}
// Check the nodes against the expected values and check if we did not
// miss any field
for _, expected := range expectedPeoples {
require.Equal(t, "people", nav.Current().Name)
require.True(t, nav.MoveToChild())
seen := make(map[string]bool, len(expected))
for {
// Compare the node value
key := nav.Current().Name
require.NotEmpty(t, key)
expectedValue, found := expected[key]
require.Truef(t, found, "key %q not found", key)
require.Equal(t, expectedValue, nav.Value())
// Remember which fields we saw for later reverse check
seen[key] = true
if !nav.MoveToNext() {
break
}
}
// Check if we miss an expected field
for key := range expected {
require.Truef(t, seen[key], "key %q not found in nodes", key)
}
require.True(t, nav.MoveToParent())
require.True(t, nav.MoveToNext())
}
require.True(t, nav.MoveToParent())
require.Equal(t, nav.Current().Type, DocumentNode, "expected 'DocumentNode'")
}
func TestQueryNames(t *testing.T) {
msg := addressbookSample.ProtoReflect()
doc, err := Parse(msg)
require.NoError(t, err)
expected := []string{
"John Doe",
"Jane Doe",
"Jack Doe",
"Jack Buck",
"Janet Doe",
}
nodes, err := QueryAll(doc, "/descendant::*[name() = 'people']/name")
require.NoError(t, err)
require.Len(t, nodes, len(expected))
for i, name := range expected {
require.Equal(t, "name", nodes[i].Name)
require.EqualValues(t, name, nodes[i].Value())
}
nodes, err = QueryAll(doc, "//name")
require.NoError(t, err)
require.Len(t, nodes, len(expected))
for i, name := range expected {
require.Equal(t, "name", nodes[i].Name)
require.EqualValues(t, name, nodes[i].Value())
}
nodes, err = QueryAll(doc, "/people[3]/name")
require.NoError(t, err)
require.Len(t, nodes, 1)
require.Equal(t, "name", nodes[0].Name)
require.EqualValues(t, expected[2], nodes[0].Value())
}
func TestQueryPhoneNumberFirst(t *testing.T) {
msg := addressbookSample.ProtoReflect()
doc, err := Parse(msg)
require.NoError(t, err)
expected := []string{
"555-555-5555",
"555-555-0000",
"555-777-0000",
}
nodes, err := QueryAll(doc, "//phones[1]/number")
require.NoError(t, err)
require.Len(t, nodes, len(expected))
for i, name := range expected {
require.Equal(t, "number", nodes[i].Name)
require.EqualValues(t, name, nodes[i].Value())
}
}
func TestQueryPhoneNumberLast(t *testing.T) {
msg := addressbookSample.ProtoReflect()
doc, err := Parse(msg)
require.NoError(t, err)
expected := []string{
"555-555-5555",
"555-555-0002",
"555-777-0001",
}
nodes, err := QueryAll(doc, "//phones[last()]/number")
require.NoError(t, err)
require.Len(t, nodes, len(expected))
for i, name := range expected {
require.Equal(t, "number", nodes[i].Name)
require.EqualValues(t, name, nodes[i].Value())
}
}
func TestQueryPhoneNoEmail(t *testing.T) {
msg := addressbookSample.ProtoReflect()
doc, err := Parse(msg)
require.NoError(t, err)
nodes, err := QueryAll(doc, "/people[not(email)]/id")
require.NoError(t, err)
require.Len(t, nodes, 1)
require.Equal(t, "id", nodes[0].Name)
require.EqualValues(t, 102, nodes[0].Value())
}
func TestQueryPhoneAge(t *testing.T) {
msg := addressbookSample.ProtoReflect()
doc, err := Parse(msg)
require.NoError(t, err)
expected := []string{
"John Doe",
"Jane Doe",
"Jack Buck",
}
nodes, err := QueryAll(doc, "/people[age > 18]/name")
require.NoError(t, err)
require.Len(t, nodes, len(expected))
for i, name := range expected {
require.Equal(t, "name", nodes[i].Name)
require.EqualValues(t, name, nodes[i].Value())
}
}
func TestQueryJack(t *testing.T) {
msg := addressbookSample.ProtoReflect()
doc, err := Parse(msg)
require.NoError(t, err)
nodes, err := QueryAll(doc, "//people[contains(name, 'Jack')]/id")
require.NoError(t, err)
require.Len(t, nodes, 2)
require.Equal(t, "id", nodes[0].Name)
require.EqualValues(t, 201, nodes[0].Value())
require.Equal(t, "id", nodes[1].Name)
require.EqualValues(t, 301, nodes[1].Value())
}
func TestQueryExample(t *testing.T) {
msg := addressbookSample.ProtoReflect()
doc, err := Parse(msg)
require.NoError(t, err)
expected := []string{
"John Doe: ",
"Jane Doe: ",
"Jack Doe: 555-555-5555",
"Jack Buck: 555-555-0000,555-555-0001,555-555-0002",
"Janet Doe: 555-777-0000,555-777-0001",
}
nodes, err := QueryAll(doc, "//people")
require.NoError(t, err)
require.Len(t, nodes, len(expected))
for i, person := range nodes {
name := FindOne(person, "name").InnerText()
numbers := make([]string, 0)
for _, node := range Find(person, "phones/number") {
numbers = append(numbers, node.InnerText())
}
v := fmt.Sprintf("%s: %s", name, strings.Join(numbers, ","))
require.EqualValues(t, expected[i], v)
}
}