-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery_test.go
231 lines (188 loc) · 5.35 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
package quirk
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"github.com/dgraph-io/dgo/v2"
. "github.com/franela/goblin"
)
func BenchmarkCreateQuery(b *testing.B) {
var builder strings.Builder
for i := 0; i < b.N; i++ {
_ = createQuery(&builder, testPredValCorrect)
}
}
func BenchmarkExecuteQuery(b *testing.B) {
var (
ctx = context.Background()
decoder = make(queryDecode)
dgraph = dgo.NewDgraphClient(&testDgraphClient{
queryResponse: testValidJSONOutput,
})
)
for i := 0; i < b.N; i++ {
_ = executeQuery(ctx, dgraph.NewTxn(),
&testBuilder{}, &DupleNode{}, &decoder)
}
}
func BenchmarkFindDecodedUID(b *testing.B) {
uid := "0x1"
emptyDecode := make(queryDecode)
emptyDecode["a"] = []struct{ UID *string }{
{UID: &uid},
}
for i := 0; i < b.N; i++ {
_, _ = findDecodedUID(emptyDecode)
}
}
func TestQueryUID(t *testing.T) {
g := Goblin(t)
g.Describe("createUID", func() {
ctx := context.Background()
dgraph := dgo.NewDgraphClient(&testDgraphClient{
queryResponse: testValidJSONOutput,
})
g.It("Should not error with nil parameters", func() {
s, err := queryUID(ctx, dgraph.NewTxn(),
&testBuilder{}, &DupleNode{})
g.Assert(s).Equal("0x1")
g.Assert(err).Equal(nil)
})
g.It("Should error when executeQuery errors", func() {
s, err := queryUID(ctx, dgraph.NewTxn(),
&testBuilder{failOn: 1}, &DupleNode{})
g.Assert(s).Equal("")
g.Assert(err).Equal(errors.New("WRITE_ERROR"))
})
})
}
func TestFindDecodeUID(t *testing.T) {
g := Goblin(t)
g.Describe("findDecodeUID", func() {
emptyDecode := make(queryDecode)
g.It("Should not error with empty queryDecode", func() {
s, err := findDecodedUID(emptyDecode)
g.Assert(s).Equal("")
g.Assert(err).Equal(error(nil))
})
g.It("Should error with empty struct slice", func() {
emptyDecode["a"] = []struct{ UID *string }{
{UID: new(string)},
{UID: new(string)},
}
s, err := findDecodedUID(emptyDecode)
g.Assert(s).Equal("")
g.Assert(err).Equal(&QueryError{
Msg: msgTooManyResponses, Function: "findDecodedUID"})
})
g.It("Should error with nil UID", func() {
emptyDecode["a"] = []struct{ UID *string }{
{UID: nil},
}
s, err := findDecodedUID(emptyDecode)
g.Assert(s).Equal("")
g.Assert(err).Equal(&QueryError{
Msg: msgNilUID, Function: "findDecodedUID"})
})
g.It("Should return valid UID", func() {
uid := "0x1"
emptyDecode["a"] = []struct{ UID *string }{
{UID: &uid},
}
s, err := findDecodedUID(emptyDecode)
g.Assert(s).Equal(uid)
g.Assert(err).Equal(error(nil))
})
})
}
func TestExecuteQuery(t *testing.T) {
g := Goblin(t)
g.Describe("executeQuery", func() {
ctx := context.Background()
emptyDecode := make(queryDecode)
dgraph := dgo.NewDgraphClient(&testDgraphClient{
queryResponse: testValidJSONOutput,
})
g.It("Should not error with valid parameters", func() {
g.Assert(executeQuery(ctx, dgraph.NewTxn(),
&testBuilder{}, &DupleNode{}, &emptyDecode)).
Equal(error(nil))
})
g.It("Should error when builder fails first use", func() {
g.Assert(executeQuery(ctx, dgraph.NewTxn(),
&testBuilder{failOn: 1}, &DupleNode{}, &emptyDecode)).
Equal(errors.New("WRITE_ERROR"))
})
g.It("Should not error when builder returns empty query", func() {
g.Assert(executeQuery(ctx, dgraph.NewTxn(),
&testBuilder{stringOutput: emptyQuery}, &DupleNode{}, &emptyDecode)).
Equal(error(nil))
})
g.It("Should error when txn fails", func() {
faildgraph := dgo.NewDgraphClient(&testDgraphClient{
failQueryOn: 1,
shouldAbort: true,
})
g.Assert(executeQuery(ctx, faildgraph.NewTxn(),
&testBuilder{}, &DupleNode{}, &emptyDecode)).
Equal(&QueryError{
Function: "executeQuery",
Query: "",
ExtErr: errors.New("QUERY_ERROR"),
})
})
g.It("Should error when txn fails", func() {
faildgraph := dgo.NewDgraphClient(&testDgraphClient{
shouldAbort: true,
})
err := executeQuery(ctx, faildgraph.NewTxn(),
&testBuilder{}, &DupleNode{}, &emptyDecode)
// Can't test the value of the json error.
// Instead just test if the function returned an error at all.
if err == nil {
g.Fail(err)
}
})
})
}
func TestCreateQuery(t *testing.T) {
g := Goblin(t)
g.Describe("createQuery", func() {
g.It("Should not return an error", func(done Done) {
go func() {
g.Assert(createQuery(&strings.Builder{}, testPredValCorrect)).
Equal(error(nil))
done()
}()
})
g.It("Should error when builder fails on first use", func(done Done) {
go func() {
g.Assert(createQuery(&testBuilder{failOn: 1}, &DupleNode{})).
Equal(errors.New("WRITE_ERROR"))
done()
}()
})
g.It("Should error when builder fails when adding unique predicates", func(done Done) {
go func() {
g.Assert(createQuery(&testBuilder{failOn: 2}, testPredValCorrect)).
Equal(&QueryError{
ExtErr: errors.New("WRITE_ERROR"),
Msg: fmt.Sprintf(msgBuilderWriting, "username", "damienstamates"),
Function: "createQuery",
})
done()
}()
})
g.It("Should error when builder fails on last use", func(done Done) {
go func() {
// When putting in empty predValPairs it will skip the for loop.
// This is why the last use is set to 2.
g.Assert(createQuery(&testBuilder{failOn: 2}, &DupleNode{})).
Equal(errors.New("WRITE_ERROR"))
done()
}()
})
})
}