-
Notifications
You must be signed in to change notification settings - Fork 3
/
helperfx_test.go
142 lines (128 loc) · 3.11 KB
/
helperfx_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
package ndgo_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/ppp225/ndgo/v5"
"github.com/stretchr/testify/require"
)
func TestFlattenRespToObject(t *testing.T) {
var testData = []struct {
in []byte
out []byte
panic bool
}{
{
in: []byte(`{"f":[{"testName":"first"}]}`),
out: []byte(`{"testName":"first"}`),
},
{
in: []byte(`{"s":[{"s":"s"}]}`),
out: []byte(`{"s":"s"}`),
},
{
in: []byte(`{"e":[]}`),
out: []byte(`{}`),
},
{
in: []byte(`{"f":[{"testName":"first"},{"testName":"second"}]}`),
out: []byte(`{"testName":"first"},{"testName":"second"}`),
},
{
in: []byte(`{"toolongname":[{"testName":"first"}]}`),
out: []byte(``),
panic: true,
},
}
for i, tt := range testData {
if tt.panic {
require.Panics(t, func() { ndgo.Unsafe{}.FlattenRespToObject(tt.in) }, "Should have panicked")
} else {
require.Exactly(t, tt.out, ndgo.Unsafe{}.FlattenRespToObject(tt.in), "Test i=%d", i)
}
}
}
func TestFlattenRespToArray(t *testing.T) {
var testData = []struct {
in []byte
out []byte
panic bool
}{
{
in: []byte(`{"f":[{"testName":"first"}]}`),
out: []byte(`[{"testName":"first"}]`),
},
{
in: []byte(`{"s":[{"s":"s"}]}`),
out: []byte(`[{"s":"s"}]`),
},
{
in: []byte(`{"e":[]}`),
out: []byte(`[]`),
},
{
in: []byte(`{"f":[{"testName":"first"},{"testName":"second"}]}`),
out: []byte(`[{"testName":"first"},{"testName":"second"}]`),
},
{
in: []byte(`{"toolongname":[{"testName":"first"}]}`),
out: []byte(``),
panic: true,
},
}
for i, tt := range testData {
if tt.panic {
require.Panics(t, func() { ndgo.Unsafe{}.FlattenRespToArray(tt.in) }, "Should have panicked")
} else {
require.Exactly(t, tt.out, ndgo.Unsafe{}.FlattenRespToArray(tt.in), "Test i=%d", i)
}
}
}
func BenchmarkFlattenRespToObject(b *testing.B) {
data := []byte(`{"f":[{"testName":"first"}]}`)
for n := 0; n < b.N; n++ {
_ = ndgo.Unsafe{}.FlattenRespToObject(data)
}
}
func BenchmarkFlattenRespToObjectEmpty(b *testing.B) {
data := []byte(`{"f":[]}`)
for n := 0; n < b.N; n++ {
_ = ndgo.Unsafe{}.FlattenRespToObject(data)
}
}
func TestFlattenRespToObjectWithDgraph(t *testing.T) {
dg := dgNewClient()
defer setupTeardown(dg)()
// insert data and commit, so indexing works on queries
txn := ndgo.NewTxnWithoutContext(dg.NewTxn())
defer txn.Discard()
populateDBComplex(txn, t)
txn.Commit()
// pre
txn = ndgo.NewTxnWithoutContext(dg.NewTxn())
defer txn.Discard()
// check one result
q := fmt.Sprintf(`
{
f(func: eq(`+predicateName+`, "%[1]s")) { testName }
}
`, firstName)
resp, err := txn.Query(q)
require.NoError(t, err)
var decode testObject
err = json.Unmarshal(ndgo.Unsafe{}.FlattenRespToObject(resp.GetJson()), &decode)
require.NoError(t, err)
require.Equal(t, firstName, decode.Name)
// check empty
q2 := fmt.Sprintf(`
{
f(func: has(` + predicateName + `))
}
`)
resp, err = txn.Query(q2)
require.NoError(t, err)
var decode2 testObject
err = json.Unmarshal(ndgo.Unsafe{}.FlattenRespToObject(resp.GetJson()), &decode2)
require.NoError(t, err)
require.Equal(t, "", decode2.Name)
}