Skip to content
Snippets Groups Projects
Unverified Commit 6e60e886 authored by Jay Chiu's avatar Jay Chiu
Browse files

Testify gql and algo

parent 6445989f
No related branches found
No related tags found
No related merge requests found
...@@ -20,21 +20,18 @@ import ( ...@@ -20,21 +20,18 @@ import (
"testing" "testing"
"github.com/google/flatbuffers/go" "github.com/google/flatbuffers/go"
"github.com/stretchr/testify/require"
"github.com/dgraph-io/dgraph/task" "github.com/dgraph-io/dgraph/task"
) )
func listEqual(u, v *UIDList, t *testing.T) { func toArray(u *UIDList) []uint64 {
if u.Size() != v.Size() { n := u.Size()
t.Errorf("Size mismatch %d vs %d", u.Size(), v.Size()) out := make([]uint64, 0, n)
t.Fatal() for i := 0; i < n; i++ {
} out = append(out, u.Get(i))
for i := 0; i < u.Size(); i++ {
if u.Get(i) != v.Get(i) {
t.Errorf("Element mismatch at index %d: %d vs %d", i, u.Get(i), v.Get(i))
t.Fatal()
}
} }
return out
} }
func newListFromTask(a []uint64) *UIDList { func newListFromTask(a []uint64) *UIDList {
...@@ -58,15 +55,17 @@ func newListFromTask(a []uint64) *UIDList { ...@@ -58,15 +55,17 @@ func newListFromTask(a []uint64) *UIDList {
func TestEqual(t *testing.T) { func TestEqual(t *testing.T) {
input1 := newListFromTask([]uint64{5, 8, 13}) input1 := newListFromTask([]uint64{5, 8, 13})
input2 := NewUIDList([]uint64{5, 8, 13}) input2 := NewUIDList([]uint64{5, 8, 13})
listEqual(input1, input2, t) require.Equal(t, toArray(input1), toArray(input2))
input3 := NewUIDList([]uint64{5, 8, 12})
require.NotEqual(t, toArray(input1), toArray(input3))
} }
func TestMergeSorted1(t *testing.T) { func TestMergeSorted1(t *testing.T) {
input := []*UIDList{ input := []*UIDList{
NewUIDList([]uint64{55}), NewUIDList([]uint64{55}),
} }
expected := NewUIDList([]uint64{55}) require.Equal(t, toArray(MergeLists(input)), []uint64{55})
listEqual(MergeLists(input), expected, t)
} }
func TestMergeSorted2(t *testing.T) { func TestMergeSorted2(t *testing.T) {
...@@ -74,8 +73,8 @@ func TestMergeSorted2(t *testing.T) { ...@@ -74,8 +73,8 @@ func TestMergeSorted2(t *testing.T) {
NewUIDList([]uint64{1, 3, 6, 8, 10}), NewUIDList([]uint64{1, 3, 6, 8, 10}),
NewUIDList([]uint64{2, 4, 5, 7, 15}), NewUIDList([]uint64{2, 4, 5, 7, 15}),
} }
expected := NewUIDList([]uint64{1, 2, 3, 4, 5, 6, 7, 8, 10, 15}) require.Equal(t, toArray(MergeLists(input)),
listEqual(MergeLists(input), expected, t) []uint64{1, 2, 3, 4, 5, 6, 7, 8, 10, 15})
} }
func TestMergeSorted3(t *testing.T) { func TestMergeSorted3(t *testing.T) {
...@@ -83,8 +82,7 @@ func TestMergeSorted3(t *testing.T) { ...@@ -83,8 +82,7 @@ func TestMergeSorted3(t *testing.T) {
newListFromTask([]uint64{1, 3, 6, 8, 10}), newListFromTask([]uint64{1, 3, 6, 8, 10}),
NewUIDList([]uint64{}), NewUIDList([]uint64{}),
} }
expected := NewUIDList([]uint64{1, 3, 6, 8, 10}) require.Equal(t, toArray(MergeLists(input)), []uint64{1, 3, 6, 8, 10})
listEqual(MergeLists(input), expected, t)
} }
func TestMergeSorted4(t *testing.T) { func TestMergeSorted4(t *testing.T) {
...@@ -92,8 +90,7 @@ func TestMergeSorted4(t *testing.T) { ...@@ -92,8 +90,7 @@ func TestMergeSorted4(t *testing.T) {
newListFromTask([]uint64{}), newListFromTask([]uint64{}),
newListFromTask([]uint64{1, 3, 6, 8, 10}), newListFromTask([]uint64{1, 3, 6, 8, 10}),
} }
expected := NewUIDList([]uint64{1, 3, 6, 8, 10}) require.Equal(t, toArray(MergeLists(input)), []uint64{1, 3, 6, 8, 10})
listEqual(MergeLists(input), expected, t)
} }
func TestMergeSorted5(t *testing.T) { func TestMergeSorted5(t *testing.T) {
...@@ -101,8 +98,7 @@ func TestMergeSorted5(t *testing.T) { ...@@ -101,8 +98,7 @@ func TestMergeSorted5(t *testing.T) {
newListFromTask([]uint64{}), newListFromTask([]uint64{}),
newListFromTask([]uint64{}), newListFromTask([]uint64{}),
} }
expected := NewUIDList([]uint64{}) require.Empty(t, toArray(MergeLists(input)))
listEqual(MergeLists(input), expected, t)
} }
func TestMergeSorted6(t *testing.T) { func TestMergeSorted6(t *testing.T) {
...@@ -111,8 +107,8 @@ func TestMergeSorted6(t *testing.T) { ...@@ -111,8 +107,8 @@ func TestMergeSorted6(t *testing.T) {
NewUIDList([]uint64{12, 14, 15, 15, 16, 16, 17, 25}), NewUIDList([]uint64{12, 14, 15, 15, 16, 16, 17, 25}),
NewUIDList([]uint64{1, 2}), NewUIDList([]uint64{1, 2}),
} }
expected := NewUIDList([]uint64{1, 2, 11, 12, 13, 14, 15, 16, 17, 18, 20, 25}) require.Equal(t, toArray(MergeLists(input)),
listEqual(MergeLists(input), expected, t) []uint64{1, 2, 11, 12, 13, 14, 15, 16, 17, 18, 20, 25})
} }
func TestMergeSorted7(t *testing.T) { func TestMergeSorted7(t *testing.T) {
...@@ -122,22 +118,19 @@ func TestMergeSorted7(t *testing.T) { ...@@ -122,22 +118,19 @@ func TestMergeSorted7(t *testing.T) {
newListFromTask([]uint64{1, 2}), newListFromTask([]uint64{1, 2}),
NewUIDList([]uint64{}), NewUIDList([]uint64{}),
} }
expected := NewUIDList([]uint64{1, 2, 3, 4, 5, 6, 7}) require.Equal(t, toArray(MergeLists(input)), []uint64{1, 2, 3, 4, 5, 6, 7})
listEqual(MergeLists(input), expected, t)
} }
func TestMergeSorted8(t *testing.T) { func TestMergeSorted8(t *testing.T) {
input := []*UIDList{} input := []*UIDList{}
expected := NewUIDList([]uint64{}) require.Empty(t, toArray(MergeLists(input)))
listEqual(MergeLists(input), expected, t)
} }
func TestMergeSorted9(t *testing.T) { func TestMergeSorted9(t *testing.T) {
input := []*UIDList{ input := []*UIDList{
newListFromTask([]uint64{1, 1, 1}), newListFromTask([]uint64{1, 1, 1}),
} }
expected := NewUIDList([]uint64{1}) require.Equal(t, toArray(MergeLists(input)), []uint64{1})
listEqual(MergeLists(input), expected, t)
} }
func TestMergeSorted10(t *testing.T) { func TestMergeSorted10(t *testing.T) {
...@@ -145,8 +138,7 @@ func TestMergeSorted10(t *testing.T) { ...@@ -145,8 +138,7 @@ func TestMergeSorted10(t *testing.T) {
newListFromTask([]uint64{1, 2, 3, 3, 6}), newListFromTask([]uint64{1, 2, 3, 3, 6}),
newListFromTask([]uint64{4, 8, 9}), newListFromTask([]uint64{4, 8, 9}),
} }
expected := NewUIDList([]uint64{1, 2, 3, 4, 6, 8, 9}) require.Equal(t, toArray(MergeLists(input)), []uint64{1, 2, 3, 4, 6, 8, 9})
listEqual(MergeLists(input), expected, t)
} }
func TestIntersectSorted1(t *testing.T) { func TestIntersectSorted1(t *testing.T) {
...@@ -154,30 +146,26 @@ func TestIntersectSorted1(t *testing.T) { ...@@ -154,30 +146,26 @@ func TestIntersectSorted1(t *testing.T) {
newListFromTask([]uint64{1, 2, 3}), newListFromTask([]uint64{1, 2, 3}),
NewUIDList([]uint64{2, 3, 4, 5}), NewUIDList([]uint64{2, 3, 4, 5}),
} }
expected := NewUIDList([]uint64{2, 3}) require.Equal(t, toArray(IntersectLists(input)), []uint64{2, 3})
listEqual(IntersectLists(input), expected, t)
} }
func TestIntersectSorted2(t *testing.T) { func TestIntersectSorted2(t *testing.T) {
input := []*UIDList{ input := []*UIDList{
newListFromTask([]uint64{1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3}), newListFromTask([]uint64{1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3}),
} }
expected := NewUIDList([]uint64{1, 2, 3}) require.Equal(t, toArray(IntersectLists(input)), []uint64{1, 2, 3})
listEqual(IntersectLists(input), expected, t)
} }
func TestIntersectSorted3(t *testing.T) { func TestIntersectSorted3(t *testing.T) {
input := []*UIDList{} input := []*UIDList{}
expected := NewUIDList([]uint64{}) require.Empty(t, toArray(IntersectLists(input)))
listEqual(IntersectLists(input), expected, t)
} }
func TestIntersectSorted4(t *testing.T) { func TestIntersectSorted4(t *testing.T) {
input := []*UIDList{ input := []*UIDList{
NewUIDList([]uint64{100, 101}), NewUIDList([]uint64{100, 101}),
} }
expected := NewUIDList([]uint64{100, 101}) require.Equal(t, toArray(IntersectLists(input)), []uint64{100, 101})
listEqual(IntersectLists(input), expected, t)
} }
func TestIntersectSorted5(t *testing.T) { func TestIntersectSorted5(t *testing.T) {
...@@ -186,59 +174,52 @@ func TestIntersectSorted5(t *testing.T) { ...@@ -186,59 +174,52 @@ func TestIntersectSorted5(t *testing.T) {
newListFromTask([]uint64{2, 3, 4, 5}), newListFromTask([]uint64{2, 3, 4, 5}),
NewUIDList([]uint64{4, 5, 6}), NewUIDList([]uint64{4, 5, 6}),
} }
expected := NewUIDList([]uint64{}) require.Empty(t, toArray(IntersectLists(input)))
listEqual(IntersectLists(input), expected, t)
} }
func TestUIDListIntersect1(t *testing.T) { func TestUIDListIntersect1(t *testing.T) {
var u, v UIDList u := NewUIDList([]uint64{1, 2, 3})
u.FromUints([]uint64{1, 2, 3}) v := newListFromTask([]uint64{})
v.FromUints([]uint64{}) u.Intersect(v)
u.Intersect(&v) require.Empty(t, toArray(u))
listEqual(&u, NewUIDList([]uint64{}), t)
} }
func TestUIDListIntersect2(t *testing.T) { func TestUIDListIntersect2(t *testing.T) {
var u, v UIDList u := NewUIDList([]uint64{1, 2, 3})
u.FromUints([]uint64{1, 2, 3}) v := newListFromTask([]uint64{1, 2, 3, 4, 5})
v.FromUints([]uint64{1, 2, 3, 4, 5}) u.Intersect(v)
u.Intersect(&v) require.Equal(t, toArray(u), []uint64{1, 2, 3})
listEqual(&u, NewUIDList([]uint64{1, 2, 3}), t)
} }
func TestUIDListIntersect3(t *testing.T) { func TestUIDListIntersect3(t *testing.T) {
var u, v UIDList u := NewUIDList([]uint64{1, 2, 3})
u.FromUints([]uint64{1, 2, 3}) v := newListFromTask([]uint64{2})
v.FromUints([]uint64{2}) u.Intersect(v)
u.Intersect(&v) require.Equal(t, toArray(u), []uint64{2})
listEqual(&u, NewUIDList([]uint64{2}), t)
} }
func TestUIDListIntersect4(t *testing.T) { func TestUIDListIntersect4(t *testing.T) {
var u, v UIDList u := NewUIDList([]uint64{1, 2, 3})
u.FromUints([]uint64{1, 2, 3}) v := newListFromTask([]uint64{0, 5})
v.FromUints([]uint64{0, 5}) u.Intersect(v)
u.Intersect(&v) require.Empty(t, toArray(u))
listEqual(&u, NewUIDList([]uint64{}), t)
} }
func TestUIDListIntersect5(t *testing.T) { func TestUIDListIntersect5(t *testing.T) {
var u, v UIDList u := NewUIDList([]uint64{1, 2, 3})
u.FromUints([]uint64{1, 2, 3}) v := newListFromTask([]uint64{3, 5})
v.FromUints([]uint64{3, 5}) u.Intersect(v)
u.Intersect(&v) require.Equal(t, toArray(u), []uint64{3})
listEqual(&u, NewUIDList([]uint64{3}), t)
} }
func TestApplyFilterUint(t *testing.T) { func TestApplyFilterUint(t *testing.T) {
var u UIDList u := NewUIDList([]uint64{1, 2, 3, 4, 5})
u.FromUints([]uint64{1, 2, 3, 4, 5})
u.ApplyFilter(func(a uint64) bool { return (a % 2) == 1 }) u.ApplyFilter(func(a uint64) bool { return (a % 2) == 1 })
listEqual(&u, NewUIDList([]uint64{1, 3, 5}), t) require.Equal(t, toArray(u), []uint64{1, 3, 5})
} }
func TestApplyFilterList(t *testing.T) { func TestApplyFilterList(t *testing.T) {
u := newListFromTask([]uint64{1, 2, 3, 4, 5}) u := newListFromTask([]uint64{1, 2, 3, 4, 5})
u.ApplyFilter(func(a uint64) bool { return (a % 2) == 1 }) u.ApplyFilter(func(a uint64) bool { return (a % 2) == 1 })
listEqual(u, NewUIDList([]uint64{1, 3, 5}), t) require.Equal(t, toArray(u), []uint64{1, 3, 5})
} }
This diff is collapsed.
...@@ -19,6 +19,8 @@ package gql ...@@ -19,6 +19,8 @@ package gql
import ( import (
"testing" "testing"
"github.com/stretchr/testify/require"
"github.com/dgraph-io/dgraph/lex" "github.com/dgraph-io/dgraph/lex"
) )
...@@ -38,9 +40,7 @@ func TestNewLexer(t *testing.T) { ...@@ -38,9 +40,7 @@ func TestNewLexer(t *testing.T) {
l.Init(input) l.Init(input)
go run(l) go run(l)
for item := range l.Items { for item := range l.Items {
if item.Typ == lex.ItemError { require.NotEqual(t, item.Typ, lex.ItemError)
t.Error(item.String())
}
t.Log(item.String()) t.Log(item.String())
} }
} }
...@@ -66,9 +66,7 @@ func TestNewLexerMutation(t *testing.T) { ...@@ -66,9 +66,7 @@ func TestNewLexerMutation(t *testing.T) {
l.Init(input) l.Init(input)
go run(l) go run(l)
for item := range l.Items { for item := range l.Items {
if item.Typ == lex.ItemError { require.NotEqual(t, item.Typ, lex.ItemError)
t.Error(item.String())
}
t.Log(item.String()) t.Log(item.String())
} }
} }
...@@ -89,9 +87,7 @@ func TestAbruptMutation(t *testing.T) { ...@@ -89,9 +87,7 @@ func TestAbruptMutation(t *testing.T) {
t.Log(item.String()) t.Log(item.String())
typ = item.Typ typ = item.Typ
} }
if typ != lex.ItemError { require.Equal(t, typ, lex.ItemError)
t.Error("This should fail.")
}
} }
func TestVariables1(t *testing.T) { func TestVariables1(t *testing.T) {
...@@ -105,9 +101,7 @@ func TestVariables1(t *testing.T) { ...@@ -105,9 +101,7 @@ func TestVariables1(t *testing.T) {
l.Init(input) l.Init(input)
go run(l) go run(l)
for item := range l.Items { for item := range l.Items {
if item.Typ == lex.ItemError { require.NotEqual(t, item.Typ, lex.ItemError)
t.Error(item.String())
}
t.Log(item.String(), item.Typ) t.Log(item.String(), item.Typ)
} }
} }
...@@ -123,9 +117,7 @@ func TestVariables2(t *testing.T) { ...@@ -123,9 +117,7 @@ func TestVariables2(t *testing.T) {
l.Init(input) l.Init(input)
go run(l) go run(l)
for item := range l.Items { for item := range l.Items {
if item.Typ == lex.ItemError { require.NotEqual(t, item.Typ, lex.ItemError)
t.Error(item.String())
}
t.Log(item.String(), item.Typ) t.Log(item.String(), item.Typ)
} }
} }
...@@ -141,9 +133,7 @@ func TestVariablesDefault(t *testing.T) { ...@@ -141,9 +133,7 @@ func TestVariablesDefault(t *testing.T) {
l.Init(input) l.Init(input)
go run(l) go run(l)
for item := range l.Items { for item := range l.Items {
if item.Typ == lex.ItemError { require.NotEqual(t, item.Typ, lex.ItemError)
t.Error(item.String())
}
t.Log(item.String(), item.Typ) t.Log(item.String(), item.Typ)
} }
} }
...@@ -163,7 +153,5 @@ func TestVariablesError(t *testing.T) { ...@@ -163,7 +153,5 @@ func TestVariablesError(t *testing.T) {
t.Log(item.String()) t.Log(item.String())
typ = item.Typ typ = item.Typ
} }
if typ != lex.ItemError { require.Equal(t, typ, lex.ItemError)
t.Error("This should fail.")
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment