-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfields_array_test.go
More file actions
76 lines (71 loc) · 1.7 KB
/
Copy pathfields_array_test.go
File metadata and controls
76 lines (71 loc) · 1.7 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
package jseek
import (
"strconv"
"testing"
)
func TestEachArrayFields(t *testing.T) {
var b []byte
b = append(b, `{"users":[`...)
for i := 0; i < 20; i++ {
if i > 0 {
b = append(b, ',')
}
id := strconv.Itoa(i)
b = append(b, `{"id":`...)
b = append(b, id...)
b = append(b, `,"username":"u`...)
b = append(b, id...)
b = append(b, `","followers":`...)
b = append(b, strconv.Itoa(i*7)...)
b = append(b, '}')
}
b = append(b, `]}`...)
type hit struct {
elem, key int
val string
}
var hits []hit
err := EachArrayFields(b, []string{"users"}, []string{"username", "followers", "missing"},
func(elem, key int, value []byte, vt ValueType, e error) bool {
if key == 2 {
if e != ErrKeyPathNotFound {
t.Fatalf("missing key err: %v", e)
}
return true
}
if e != nil {
t.Fatalf("elem=%d key=%d: %v", elem, key, e)
}
hits = append(hits, hit{elem, key, string(value)})
return true
})
if err != nil {
t.Fatal(err)
}
if len(hits) != 40 {
t.Fatalf("hits=%d want 40", len(hits))
}
if hits[0].val != "u0" || hits[1].val != "0" {
t.Fatalf("first row %v", hits[:2])
}
if hits[38].val != "u19" || hits[39].val != "133" {
t.Fatalf("last row %v", hits[38:])
}
for i := 0; i < 20; i++ {
u, _, _, err := Get(b, "users", "["+strconv.Itoa(i)+"]", "username")
if err != nil || string(u) != "u"+strconv.Itoa(i) {
t.Fatalf("Get cross-check i=%d: %s %v", i, u, err)
}
}
}
func TestEachArrayFieldsEarlyStop(t *testing.T) {
data := []byte(`[{"a":1},{"a":2},{"a":3}]`)
n := 0
_ = EachArrayFields(data, nil, []string{"a"}, func(elem, key int, value []byte, vt ValueType, err error) bool {
n++
return false
})
if n != 1 {
t.Fatalf("n=%d want 1", n)
}
}