Newer
Older
/*
* Copyright 2015 Manish R Jain <manishrjain@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package query
import (
Manish R Jain
committed
"fmt"
"io/ioutil"
"os"
"testing"
"time"
"github.com/dgraph-io/dgraph/commit"
"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/store"
"github.com/dgraph-io/dgraph/task"
"github.com/dgraph-io/dgraph/x"
Manish R Jain
committed
"github.com/google/flatbuffers/go"
)
func setErr(err *error, nerr error) {
if err != nil {
return
}
*err = nerr
}
func populateList(key []byte) error {
pl := posting.Get(key)
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
ValueId: 9,
Source: "query_test",
Timestamp: time.Now(),
}
var err error
setErr(&err, pl.AddMutation(t, posting.Set))
t.ValueId = 19
setErr(&err, pl.AddMutation(t, posting.Set))
t.ValueId = 29
setErr(&err, pl.AddMutation(t, posting.Set))
t.Value = "abracadabra"
setErr(&err, pl.AddMutation(t, posting.Set))
return err
}
func TestRun(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
pdir := NewStore(t)
defer os.RemoveAll(pdir)
ps := new(store.Store)
ps.Init(pdir)
mdir := NewStore(t)
defer os.RemoveAll(mdir)
ms := new(store.Store)
ms.Init(mdir)
posting.Init(ps, ms)
key := posting.Key(11, "testing")
if err := populateList(key); err != nil {
t.Error(err)
}
key = posting.Key(9, "name")
m := Message{Id: 11}
ma := Mattr{Attr: "testing"}
m.Attrs = append(m.Attrs, ma)
if err := Run(&m); err != nil {
t.Error(err)
}
ma = m.Attrs[0]
uids := result.GetRootAsUids(ma.ResultUids, 0)
if uids.UidLength() != 3 {
t.Errorf("Expected 3. Got: %v", uids.UidLength())
}
var v uint64
v = 9
for i := 0; i < uids.UidLength(); i++ {
if uids.Uid(i) == math.MaxUint64 {
t.Error("Value posting encountered at index:", i)
}
if v != uids.Uid(i) {
t.Errorf("Expected: %v. Got: %v", v, uids.Uid(i))
}
v += 10
}
log.Debugf("ResultUid buffer size: %v", len(ma.ResultUids))
var val string
if err := posting.ParseValue(&val, ma.ResultValue); err != nil {
t.Error(err)
}
if val != "abracadabra" {
t.Errorf("Expected abracadabra. Got: [%q]", val)
}
}
func addEdge(t *testing.T, edge x.DirectedEdge, l *posting.List) {
if err := l.AddMutation(edge, posting.Set); err != nil {
t.Error(err)
}
}
func checkName(t *testing.T, r *task.Result, idx int, expected string) {
var tv task.Value
if ok := r.Values(&tv, idx); !ok {
t.Error("Unable to retrieve value")
}
var iname interface{}
if err := posting.ParseValue(&iname, tv.ValBytes()); err != nil {
if name != expected {
t.Errorf("Expected: %v. Got: %v", expected, name)
}
}
func checkSingleValue(t *testing.T, child *SubGraph,
attr string, value string) {
if child.Attr != attr || len(child.result) == 0 {
t.Error("Expected attr name with some result")
}
uo := flatbuffers.GetUOffsetT(child.result)
r := new(task.Result)
r.Init(child.result, uo)
if r.ValuesLength() != 1 {
Manish R Jain
committed
t.Errorf("Expected value length 1. Got: %v", r.ValuesLength())
}
if r.UidmatrixLength() != 1 {
t.Errorf("Expected uidmatrix length 1. Got: %v", r.UidmatrixLength())
Manish R Jain
committed
var ul task.UidList
if ok := r.Uidmatrix(&ul, 0); !ok {
t.Errorf("While parsing uidlist")
}
if ul.UidsLength() != 0 {
t.Error("Expected uids length 0. Got: %v", ul.UidsLength())
}
checkName(t, r, 0, value)
}
Manish R Jain
committed
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
func TestNewGraph(t *testing.T) {
var ex uint64
ex = 101
sg, err := NewGraph(ex, "")
if err != nil {
t.Error(err)
}
uo := flatbuffers.GetUOffsetT(sg.result)
r := new(task.Result)
r.Init(sg.result, uo)
if r.UidmatrixLength() != 1 {
t.Errorf("Expected length 1. Got: %v", r.UidmatrixLength())
}
var ul task.UidList
if ok := r.Uidmatrix(&ul, 0); !ok {
t.Errorf("Unable to parse uidlist at index 0")
}
if ul.UidsLength() != 1 {
t.Errorf("Expected length 1. Got: %v", ul.UidsLength())
}
if ul.Uids(0) != ex {
t.Errorf("Expected uid: %v. Got: %v", ex, ul.Uids(0))
}
}
Manish R Jain
committed
func populateGraph(t *testing.T) string {
// logrus.SetLevel(logrus.DebugLevel)
dir, err := ioutil.TempDir("", "storetest_")
if err != nil {
t.Error(err)
Manish R Jain
committed
return ""
ps := new(store.Store)
clog := commit.NewLogger(dir, "mutations", 50<<20)
clog.Init()
posting.Init(ps, clog)
// So, user we're interested in has uid: 1.
// She has 4 friends: 23, 24, 25, 31, and 101
ValueId: 23,
Source: "testing",
Timestamp: time.Now(),
}
Manish R Jain
committed
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend")))
Manish R Jain
committed
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend")))
Manish R Jain
committed
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend")))
Manish R Jain
committed
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend")))
Manish R Jain
committed
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "friend")))
// Now let's add a few properties for the main user.
Manish R Jain
committed
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "name")))
Manish R Jain
committed
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "gender")))
Manish R Jain
committed
addEdge(t, edge, posting.GetOrCreate(posting.Key(1, "status")))
// Now let's add a name for each of the friends, except 101.
Manish R Jain
committed
addEdge(t, edge, posting.GetOrCreate(posting.Key(23, "name")))
Manish R Jain
committed
addEdge(t, edge, posting.GetOrCreate(posting.Key(24, "name")))
Manish R Jain
committed
addEdge(t, edge, posting.GetOrCreate(posting.Key(25, "name")))
Manish R Jain
committed
addEdge(t, edge, posting.GetOrCreate(posting.Key(31, "name")))
Manish R Jain
committed
return dir
Manish R Jain
committed
dir := populateGraph(t)
defer os.RemoveAll(dir)
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Alright. Now we have everything set up. Let's create the query.
sg, err := NewGraph(1, "")
if err != nil {
t.Error(err)
}
// Retrieve friends, and their names.
csg := new(SubGraph)
csg.Attr = "friend"
gsg := new(SubGraph)
gsg.Attr = "name"
csg.Children = append(csg.Children, gsg)
sg.Children = append(sg.Children, csg)
// Retireve profile information for uid:1.
csg = new(SubGraph)
csg.Attr = "name"
sg.Children = append(sg.Children, csg)
csg = new(SubGraph)
csg.Attr = "gender"
sg.Children = append(sg.Children, csg)
csg = new(SubGraph)
csg.Attr = "status"
sg.Children = append(sg.Children, csg)
ch := make(chan error)
go ProcessGraph(sg, ch)
err = <-ch
if err != nil {
t.Error(err)
}
if len(sg.Children) != 4 {
t.Errorf("Expected len 4. Got: %v", len(sg.Children))
}
child := sg.Children[0]
if child.Attr != "friend" {
t.Errorf("Expected attr friend. Got: %v", child.Attr)
}
if len(child.result) == 0 {
t.Errorf("Expected some result.")
Manish R Jain
committed
return
}
uo := flatbuffers.GetUOffsetT(child.result)
r := new(task.Result)
r.Init(child.result, uo)
Manish R Jain
committed
if r.UidmatrixLength() != 1 {
t.Errorf("Expected 1 matrix. Got: %v", r.UidmatrixLength())
}
var ul task.UidList
if ok := r.Uidmatrix(&ul, 0); !ok {
t.Errorf("While parsing uidlist")
}
if ul.UidsLength() != 5 {
t.Errorf("Expected 5 friends. Got: %v", ul.UidsLength())
Manish R Jain
committed
if ul.Uids(0) != 23 || ul.Uids(1) != 24 || ul.Uids(2) != 25 ||
ul.Uids(3) != 31 || ul.Uids(4) != 101 {
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
t.Errorf("Friend ids don't match")
}
if len(child.Children) != 1 || child.Children[0].Attr != "name" {
t.Errorf("Expected attr name")
}
child = child.Children[0]
uo = flatbuffers.GetUOffsetT(child.result)
r.Init(child.result, uo)
if r.ValuesLength() != 5 {
t.Errorf("Expected 5 names of 5 friends")
}
checkName(t, r, 0, "Rick Grimes")
checkName(t, r, 1, "Glenn Rhee")
checkName(t, r, 2, "Daryl Dixon")
checkName(t, r, 3, "Andrea")
{
var tv task.Value
if ok := r.Values(&tv, 4); !ok {
t.Error("Unable to retrieve value")
}
if tv.ValLength() != 1 || tv.ValBytes()[0] != 0x00 {
t.Error("Expected a null byte")
}
}
checkSingleValue(t, sg.Children[1], "name", "Michonne")
checkSingleValue(t, sg.Children[2], "gender", "female")
checkSingleValue(t, sg.Children[3], "status", "alive")
Manish R Jain
committed
dir := populateGraph(t)
defer os.RemoveAll(dir)
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Alright. Now we have everything set up. Let's create the query.
sg, err := NewGraph(1, "")
if err != nil {
t.Error(err)
}
// Retireve profile information for uid:1.
csg := new(SubGraph)
csg.Attr = "name"
sg.Children = append(sg.Children, csg)
csg = new(SubGraph)
csg.Attr = "gender"
sg.Children = append(sg.Children, csg)
csg = new(SubGraph)
csg.Attr = "status"
sg.Children = append(sg.Children, csg)
gsg := new(SubGraph)
gsg.Attr = "name"
csg = new(SubGraph)
csg.Attr = "friend"
csg.Children = append(csg.Children, gsg)
sg.Children = append(sg.Children, csg)
ch := make(chan error)
go ProcessGraph(sg, ch)
err = <-ch
if err != nil {
t.Error(err)
}
Manish R Jain
committed
Manish R Jain
committed
var l Latency
js, err := sg.ToJson(&l)
Manish R Jain
committed
if err != nil {
t.Error(err)
}
fmt.Printf(string(js))