Newer
Older
Manish R Jain
committed
package posting
import (
"container/heap"
Manish R Jain
committed
"testing"
"time"
"github.com/Sirupsen/logrus"
"github.com/google/flatbuffers/go"
"github.com/dgraph-io/dgraph/store"
"github.com/dgraph-io/dgraph/task"
"github.com/dgraph-io/dgraph/x"
Manish R Jain
committed
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
)
func TestPush(t *testing.T) {
h := &elemHeap{}
heap.Init(h)
e := elem{Uid: 5}
heap.Push(h, e)
e.Uid = 3
heap.Push(h, e)
e.Uid = 4
heap.Push(h, e)
if h.Len() != 3 {
t.Errorf("Expected len 3. Found: %v", h.Len())
}
if (*h)[0].Uid != 3 {
t.Errorf("Expected min 3. Found: %+v", (*h)[0])
}
e.Uid = 10
(*h)[0] = e
heap.Fix(h, 0)
if (*h)[0].Uid != 4 {
t.Errorf("Expected min 4. Found: %+v", (*h)[0])
}
e.Uid = 11
(*h)[0] = e
heap.Fix(h, 0)
if (*h)[0].Uid != 5 {
t.Errorf("Expected min 5. Found: %+v", (*h)[0])
}
e = heap.Pop(h).(elem)
if e.Uid != 5 {
t.Errorf("Expected min 5. Found %+v", e)
}
e = heap.Pop(h).(elem)
if e.Uid != 10 {
t.Errorf("Expected min 10. Found: %+v", e)
}
e = heap.Pop(h).(elem)
if e.Uid != 11 {
t.Errorf("Expected min 11. Found: %+v", e)
}
if h.Len() != 0 {
t.Errorf("Expected len 0. Found: %v, values: %+v", h.Len(), h)
}
}
func addTriple(t *testing.T, triple x.Triple, l *List) {
if err := l.AddMutation(triple, Set); err != nil {
t.Error(err)
}
}
func TestProcessTask(t *testing.T) {
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
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)
Init(ps, ms)
triple := x.Triple{
ValueId: 23,
Source: "author0",
Timestamp: time.Now(),
}
addTriple(t, triple, Get(Key(10, "friend")))
addTriple(t, triple, Get(Key(11, "friend")))
addTriple(t, triple, Get(Key(12, "friend")))
triple.ValueId = 25
addTriple(t, triple, Get(Key(12, "friend")))
triple.ValueId = 26
addTriple(t, triple, Get(Key(12, "friend")))
triple.ValueId = 31
addTriple(t, triple, Get(Key(10, "friend")))
addTriple(t, triple, Get(Key(12, "friend")))
triple.Value = "photon"
addTriple(t, triple, Get(Key(12, "friend")))
query := NewQuery("friend", []uint64{10, 11, 12})
result, err := ProcessTask(query)
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
if err != nil {
t.Error(err)
}
ro := flatbuffers.GetUOffsetT(result)
r := new(task.Result)
r.Init(result, ro)
if r.UidsLength() != 4 {
t.Errorf("Expected 4. Got uids length: %v", r.UidsLength())
}
if r.Uids(0) != 23 {
t.Errorf("Expected 23. Got: %v", r.Uids(0))
}
if r.Uids(1) != 25 {
t.Errorf("Expected 25. Got: %v", r.Uids(0))
}
if r.Uids(2) != 26 {
t.Errorf("Expected 26. Got: %v", r.Uids(0))
}
if r.Uids(3) != 31 {
t.Errorf("Expected 31. Got: %v", r.Uids(0))
}
if r.ValuesLength() != 3 {
t.Errorf("Expected 3. Got values length: %v", r.ValuesLength())
}
var tval task.Value
if ok := r.Values(&tval, 0); !ok {
t.Errorf("Unable to retrieve value")
}
if tval.ValLength() != 1 ||
tval.ValBytes()[0] != 0x00 {
t.Errorf("Invalid byte value at index 0")
}
if ok := r.Values(&tval, 2); !ok {
t.Errorf("Unable to retrieve value")
}
var v string
if err := ParseValue(&v, tval.ValBytes()); err != nil {
t.Error(err)
}
if v != "photon" {
t.Errorf("Expected photon. Got: %q", v)
}
}