Purge goconvey and mock

These packages really impact on test readability with crazy action at a
distance. In addition to this removal of goconvey reduced the test run
time for leveldb on average by about 40-50%.
This commit is contained in:
kortschak 2014-07-05 20:43:49 +09:30
parent 3f6cfc98d5
commit 1c181429da
9 changed files with 602 additions and 587 deletions

View file

@ -21,13 +21,17 @@ import (
)
func TestLinksTo(t *testing.T) {
ts := new(TestTripleStore)
tsFixed := newFixed()
tsFixed.Add(2)
ts.On("ValueOf", "cool").Return(1)
ts.On("TripleIterator", graph.Object, 1).Return(tsFixed)
ts := &store{
data: []string{1: "cool"},
iter: newFixed(),
}
ts.iter.(*Fixed).Add(2)
fixed := newFixed()
fixed.Add(ts.ValueOf("cool"))
val := ts.ValueOf("cool")
if val != 1 {
t.Fatalf("Failed to return correct value, got:%v expect:1", val)
}
fixed.Add(val)
lto := NewLinksTo(ts, fixed, graph.Object)
val, ok := lto.Next()
if !ok {

View file

@ -17,44 +17,58 @@ package iterator
// A quickly mocked version of the TripleStore interface, for use in tests.
// Can better used Mock.Called but will fill in as needed.
import (
"github.com/stretchrcom/testify/mock"
import "github.com/google/cayley/graph"
"github.com/google/cayley/graph"
)
type TestTripleStore struct {
mock.Mock
type store struct {
data []string
iter graph.Iterator
}
func (ts *TestTripleStore) ValueOf(s string) graph.Value {
args := ts.Mock.Called(s)
return args.Get(0)
func (ts *store) ValueOf(s string) graph.Value {
for i, v := range ts.data {
if s == v {
return i
}
}
return nil
}
func (ts *TestTripleStore) AddTriple(*graph.Triple) {}
func (ts *TestTripleStore) AddTripleSet([]*graph.Triple) {}
func (ts *TestTripleStore) Triple(graph.Value) *graph.Triple { return &graph.Triple{} }
func (ts *TestTripleStore) TripleIterator(d graph.Direction, i graph.Value) graph.Iterator {
args := ts.Mock.Called(d, i)
return args.Get(0).(graph.Iterator)
func (ts *store) AddTriple(*graph.Triple) {}
func (ts *store) AddTripleSet([]*graph.Triple) {}
func (ts *store) Triple(graph.Value) *graph.Triple { return &graph.Triple{} }
func (ts *store) TripleIterator(d graph.Direction, i graph.Value) graph.Iterator {
return ts.iter
}
func (ts *TestTripleStore) NodesAllIterator() graph.Iterator { return &Null{} }
func (ts *TestTripleStore) TriplesAllIterator() graph.Iterator { return &Null{} }
func (ts *TestTripleStore) GetIteratorByString(string, string, string) graph.Iterator {
return &Null{}
func (ts *store) NodesAllIterator() graph.Iterator { return &Null{} }
func (ts *store) TriplesAllIterator() graph.Iterator { return &Null{} }
func (ts *store) NameOf(v graph.Value) string {
i := v.(int)
if i < 0 || i >= len(ts.data) {
return ""
}
return ts.data[i]
}
func (ts *TestTripleStore) NameOf(v graph.Value) string {
args := ts.Mock.Called(v)
return args.Get(0).(string)
}
func (ts *TestTripleStore) Size() int64 { return 0 }
func (ts *TestTripleStore) DebugPrint() {}
func (ts *TestTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
func (ts *store) Size() int64 { return 0 }
func (ts *store) DebugPrint() {}
func (ts *store) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
return &Null{}, false
}
func (ts *TestTripleStore) FixedIterator() graph.FixedIterator {
func (ts *store) FixedIterator() graph.FixedIterator {
return NewFixedIteratorWithCompare(BasicEquality)
}
func (ts *TestTripleStore) Close() {}
func (ts *TestTripleStore) TripleDirection(graph.Value, graph.Direction) graph.Value { return 0 }
func (ts *TestTripleStore) RemoveTriple(t *graph.Triple) {}
func (ts *store) Close() {}
func (ts *store) TripleDirection(graph.Value, graph.Direction) graph.Value { return 0 }
func (ts *store) RemoveTriple(t *graph.Triple) {}

View file

@ -15,131 +15,140 @@
package iterator
import (
"reflect"
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/google/cayley/graph"
)
func extractNumbersFromIterator(it graph.Iterator) []int {
var outputNumbers []int
func iterated(it graph.Iterator) []int {
var res []int
for {
val, ok := it.Next()
if !ok {
break
}
outputNumbers = append(outputNumbers, val.(int))
res = append(res, val.(int))
}
return outputNumbers
return res
}
func TestOrIteratorBasics(t *testing.T) {
var orIt *Or
or := NewOr()
f1 := newFixed()
f1.Add(1)
f1.Add(2)
f1.Add(3)
f2 := newFixed()
f2.Add(3)
f2.Add(9)
f2.Add(20)
f2.Add(21)
or.AddSubIterator(f1)
or.AddSubIterator(f2)
Convey("Given an Or Iterator of two fixed iterators", t, func() {
orIt = NewOr()
fixed1 := newFixed()
fixed1.Add(1)
fixed1.Add(2)
fixed1.Add(3)
fixed2 := newFixed()
fixed2.Add(3)
fixed2.Add(9)
fixed2.Add(20)
fixed2.Add(21)
orIt.AddSubIterator(fixed1)
orIt.AddSubIterator(fixed2)
if v, _ := or.Size(); v != 7 {
t.Errorf("Unexpected iterator size, got:%d expected %d", v, 7)
}
Convey("It should guess its size.", func() {
v, _ := orIt.Size()
So(v, ShouldEqual, 7)
})
expect := []int{1, 2, 3, 3, 9, 20, 21}
for i := 0; i < 2; i++ {
if got := iterated(or); !reflect.DeepEqual(got, expect) {
t.Errorf("Failed to iterate Or correctly on repeat %d, got:%v expect:%v", i, got, expect)
}
or.Reset()
}
Convey("It should extract all the numbers, potentially twice.", func() {
allNumbers := []int{1, 2, 3, 3, 9, 20, 21}
So(extractNumbersFromIterator(orIt), ShouldResemble, allNumbers)
orIt.Reset()
So(extractNumbersFromIterator(orIt), ShouldResemble, allNumbers)
// Optimization works
newOr, _ := orIt.Optimize()
So(extractNumbersFromIterator(newOr), ShouldResemble, allNumbers)
})
// Check that optimization works.
optOr, _ := or.Optimize()
if got := iterated(optOr); !reflect.DeepEqual(got, expect) {
t.Errorf("Failed to iterate optimized Or correctly, got:%v expect:%v", got, expect)
}
Convey("It should check that numbers in either iterator exist.", func() {
So(orIt.Check(2), ShouldEqual, true)
So(orIt.Check(3), ShouldEqual, true)
So(orIt.Check(21), ShouldEqual, true)
})
Convey("It should check that numbers not in either iterator are false.", func() {
So(orIt.Check(22), ShouldEqual, false)
So(orIt.Check(5), ShouldEqual, false)
So(orIt.Check(0), ShouldEqual, false)
})
})
for _, v := range []int{2, 3, 21} {
if !or.Check(v) {
t.Errorf("Failed to correctly check %d as true", v)
}
}
for _, v := range []int{22, 5, 0} {
if or.Check(v) {
t.Errorf("Failed to correctly check %d as false", v)
}
}
}
func TestShortCircuitingOrBasics(t *testing.T) {
var orIt *Or
var or *Or
Convey("Given a short-circuiting Or of two fixed iterators", t, func() {
orIt = NewShortCircuitOr()
fixed1 := newFixed()
fixed1.Add(1)
fixed1.Add(2)
fixed1.Add(3)
fixed2 := newFixed()
fixed2.Add(3)
fixed2.Add(9)
fixed2.Add(20)
fixed2.Add(21)
f1 := newFixed()
f1.Add(1)
f1.Add(2)
f1.Add(3)
f2 := newFixed()
f2.Add(3)
f2.Add(9)
f2.Add(20)
f2.Add(21)
Convey("It should guess its size.", func() {
orIt.AddSubIterator(fixed1)
orIt.AddSubIterator(fixed2)
v, _ := orIt.Size()
So(v, ShouldEqual, 4)
})
or = NewShortCircuitOr()
or.AddSubIterator(f1)
or.AddSubIterator(f2)
v, exact := or.Size()
if v != 4 {
t.Errorf("Unexpected iterator size, got:%d expected %d", v, 4)
}
if !exact {
t.Error("Size not exact.")
}
Convey("It should extract the first iterators' numbers.", func() {
orIt.AddSubIterator(fixed1)
orIt.AddSubIterator(fixed2)
allNumbers := []int{1, 2, 3}
So(extractNumbersFromIterator(orIt), ShouldResemble, allNumbers)
orIt.Reset()
So(extractNumbersFromIterator(orIt), ShouldResemble, allNumbers)
// Optimization works
newOr, _ := orIt.Optimize()
So(extractNumbersFromIterator(newOr), ShouldResemble, allNumbers)
})
// It should extract the first iterators' numbers.
or = NewShortCircuitOr()
or.AddSubIterator(f1)
or.AddSubIterator(f2)
expect := []int{1, 2, 3}
for i := 0; i < 2; i++ {
if got := iterated(or); !reflect.DeepEqual(got, expect) {
t.Errorf("Failed to iterate Or correctly on repeat %d, got:%v expect:%v", i, got, expect)
}
or.Reset()
}
Convey("It should check that numbers in either iterator exist.", func() {
orIt.AddSubIterator(fixed1)
orIt.AddSubIterator(fixed2)
So(orIt.Check(2), ShouldEqual, true)
So(orIt.Check(3), ShouldEqual, true)
So(orIt.Check(21), ShouldEqual, true)
So(orIt.Check(22), ShouldEqual, false)
So(orIt.Check(5), ShouldEqual, false)
So(orIt.Check(0), ShouldEqual, false)
// Check optimization works.
optOr, _ := or.Optimize()
if got := iterated(optOr); !reflect.DeepEqual(got, expect) {
t.Errorf("Failed to iterate optimized Or correctly, got:%v expect:%v", got, expect)
}
})
Convey("It should check that it pulls the second iterator's numbers if the first is empty.", func() {
orIt.AddSubIterator(newFixed())
orIt.AddSubIterator(fixed2)
allNumbers := []int{3, 9, 20, 21}
So(extractNumbersFromIterator(orIt), ShouldResemble, allNumbers)
orIt.Reset()
So(extractNumbersFromIterator(orIt), ShouldResemble, allNumbers)
// Optimization works
newOr, _ := orIt.Optimize()
So(extractNumbersFromIterator(newOr), ShouldResemble, allNumbers)
})
})
// Check that numbers in either iterator exist.
or = NewShortCircuitOr()
or.AddSubIterator(f1)
or.AddSubIterator(f2)
for _, v := range []int{2, 3, 21} {
if !or.Check(v) {
t.Errorf("Failed to correctly check %d as true", v)
}
}
for _, v := range []int{22, 5, 0} {
if or.Check(v) {
t.Errorf("Failed to correctly check %d as false", v)
}
}
// Check that it pulls the second iterator's numbers if the first is empty.
or = NewShortCircuitOr()
or.AddSubIterator(newFixed())
or.AddSubIterator(f2)
expect = []int{3, 9, 20, 21}
for i := 0; i < 2; i++ {
if got := iterated(or); !reflect.DeepEqual(got, expect) {
t.Errorf("Failed to iterate Or correctly on repeat %d, got:%v expect:%v", i, got, expect)
}
or.Reset()
}
// Check optimization works.
optOr, _ = or.Optimize()
if got := iterated(optOr); !reflect.DeepEqual(got, expect) {
t.Errorf("Failed to iterate optimized Or correctly, got:%v expect:%v", got, expect)
}
}

View file

@ -42,7 +42,7 @@ type queryShape struct {
hasaDirs []graph.Direction
}
func OutputQueryShapeForIterator(it graph.Iterator, ts graph.TripleStore, outputMap *map[string]interface{}) {
func OutputQueryShapeForIterator(it graph.Iterator, ts graph.TripleStore, outputMap map[string]interface{}) {
qs := &queryShape{
ts: ts,
nodeId: 1,
@ -50,8 +50,8 @@ func OutputQueryShapeForIterator(it graph.Iterator, ts graph.TripleStore, output
node := qs.MakeNode(it.Clone())
qs.AddNode(node)
(*outputMap)["nodes"] = qs.nodes
(*outputMap)["links"] = qs.links
outputMap["nodes"] = qs.nodes
outputMap["links"] = qs.links
}
func (qs *queryShape) AddNode(n *Node) {

View file

@ -15,112 +15,116 @@
package iterator
import (
"reflect"
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/google/cayley/graph"
)
func buildHasaWithTag(ts graph.TripleStore, tag string, target string) *HasA {
fixed_obj := ts.FixedIterator()
fixed_pred := ts.FixedIterator()
fixed_obj.Add(ts.ValueOf(target))
fixed_pred.Add(ts.ValueOf("status"))
fixed_obj.AddTag(tag)
lto1 := NewLinksTo(ts, fixed_obj, graph.Object)
lto2 := NewLinksTo(ts, fixed_pred, graph.Predicate)
func hasaWithTag(ts graph.TripleStore, tag string, target string) *HasA {
and := NewAnd()
and.AddSubIterator(lto1)
and.AddSubIterator(lto2)
hasa := NewHasA(ts, and, graph.Subject)
return hasa
obj := ts.FixedIterator()
obj.Add(ts.ValueOf(target))
obj.AddTag(tag)
and.AddSubIterator(NewLinksTo(ts, obj, graph.Object))
pred := ts.FixedIterator()
pred.Add(ts.ValueOf("status"))
and.AddSubIterator(NewLinksTo(ts, pred, graph.Predicate))
return NewHasA(ts, and, graph.Subject)
}
func TestQueryShape(t *testing.T) {
var queryShape map[string]interface{}
ts := new(TestTripleStore)
ts.On("ValueOf", "cool").Return(1)
ts.On("NameOf", 1).Return("cool")
ts.On("ValueOf", "status").Return(2)
ts.On("NameOf", 2).Return("status")
ts.On("ValueOf", "fun").Return(3)
ts.On("NameOf", 3).Return("fun")
ts.On("ValueOf", "name").Return(4)
ts.On("NameOf", 4).Return("name")
ts := &store{
data: []string{
1: "cool",
2: "status",
3: "fun",
4: "name",
},
}
Convey("Given a single linkage iterator's shape", t, func() {
queryShape = make(map[string]interface{})
hasa := buildHasaWithTag(ts, "tag", "cool")
hasa.AddTag("top")
OutputQueryShapeForIterator(hasa, ts, &queryShape)
// Given a single linkage iterator's shape.
hasa := hasaWithTag(ts, "tag", "cool")
hasa.AddTag("top")
Convey("It should have three nodes and one link", func() {
nodes := queryShape["nodes"].([]Node)
links := queryShape["links"].([]Link)
So(len(nodes), ShouldEqual, 3)
So(len(links), ShouldEqual, 1)
})
shape := make(map[string]interface{})
OutputQueryShapeForIterator(hasa, ts, shape)
Convey("These nodes should be correctly tagged", func() {
nodes := queryShape["nodes"].([]Node)
So(nodes[0].Tags, ShouldResemble, []string{"tag"})
So(nodes[1].IsLinkNode, ShouldEqual, true)
So(nodes[2].Tags, ShouldResemble, []string{"top"})
nodes := shape["nodes"].([]Node)
if len(nodes) != 3 {
t.Errorf("Failed to get correct number of nodes, got:%d expect:4", len(nodes))
}
links := shape["links"].([]Link)
if len(nodes) != 3 {
t.Errorf("Failed to get correct number of links, got:%d expect:1", len(links))
}
})
// Nodes should be correctly tagged.
nodes = shape["nodes"].([]Node)
for i, expect := range [][]string{{"tag"}, nil, {"top"}} {
if !reflect.DeepEqual(nodes[i].Tags, expect) {
t.Errorf("Failed to get correct tag for node[%d], got:%s expect:%s", i, nodes[i].Tags, expect)
}
}
if !nodes[1].IsLinkNode {
t.Error("Failed to get node[1] as link node")
}
Convey("The link should be correctly typed", func() {
nodes := queryShape["nodes"].([]Node)
links := queryShape["links"].([]Link)
So(links[0].Source, ShouldEqual, nodes[2].Id)
So(links[0].Target, ShouldEqual, nodes[0].Id)
So(links[0].LinkNode, ShouldEqual, nodes[1].Id)
So(links[0].Pred, ShouldEqual, 0)
// Link should be correctly typed.
nodes = shape["nodes"].([]Node)
link := shape["links"].([]Link)[0]
if link.Source != nodes[2].Id {
t.Errorf("Failed to get correct link source, got:%v expect:%v", link.Source, nodes[2].Id)
}
if link.Target != nodes[0].Id {
t.Errorf("Failed to get correct link target, got:%v expect:%v", link.Target, nodes[0].Id)
}
if link.LinkNode != nodes[1].Id {
t.Errorf("Failed to get correct link node, got:%v expect:%v", link.LinkNode, nodes[1].Id)
}
if link.Pred != 0 {
t.Errorf("Failed to get correct number of predecessors:%v expect:0", link.Pred)
}
})
// Given a name-of-an-and-iterator's shape.
andInternal := NewAnd()
})
hasa1 := hasaWithTag(ts, "tag1", "cool")
hasa1.AddTag("hasa1")
andInternal.AddSubIterator(hasa1)
Convey("Given a name-of-an-and-iterator's shape", t, func() {
queryShape = make(map[string]interface{})
hasa1 := buildHasaWithTag(ts, "tag1", "cool")
hasa1.AddTag("hasa1")
hasa2 := buildHasaWithTag(ts, "tag2", "fun")
hasa1.AddTag("hasa2")
andInternal := NewAnd()
andInternal.AddSubIterator(hasa1)
andInternal.AddSubIterator(hasa2)
fixed_pred := ts.FixedIterator()
fixed_pred.Add(ts.ValueOf("name"))
lto1 := NewLinksTo(ts, andInternal, graph.Subject)
lto2 := NewLinksTo(ts, fixed_pred, graph.Predicate)
and := NewAnd()
and.AddSubIterator(lto1)
and.AddSubIterator(lto2)
hasa := NewHasA(ts, and, graph.Object)
OutputQueryShapeForIterator(hasa, ts, &queryShape)
hasa2 := hasaWithTag(ts, "tag2", "fun")
hasa2.AddTag("hasa2")
andInternal.AddSubIterator(hasa2)
Convey("It should have seven nodes and three links", func() {
nodes := queryShape["nodes"].([]Node)
links := queryShape["links"].([]Link)
So(len(nodes), ShouldEqual, 7)
So(len(links), ShouldEqual, 3)
})
pred := ts.FixedIterator()
pred.Add(ts.ValueOf("name"))
Convey("Three of the nodes are link nodes, four aren't", func() {
nodes := queryShape["nodes"].([]Node)
count := 0
for _, node := range nodes {
if node.IsLinkNode {
count++
}
}
So(count, ShouldEqual, 3)
})
and := NewAnd()
and.AddSubIterator(NewLinksTo(ts, andInternal, graph.Subject))
and.AddSubIterator(NewLinksTo(ts, pred, graph.Predicate))
Convey("These nodes should be correctly tagged", nil)
})
shape = make(map[string]interface{})
OutputQueryShapeForIterator(NewHasA(ts, and, graph.Object), ts, shape)
links = shape["links"].([]Link)
if len(links) != 3 {
t.Errorf("Failed to find the correct number of links, got:%d expect:3", len(links))
}
nodes = shape["nodes"].([]Node)
if len(nodes) != 7 {
t.Errorf("Failed to find the correct number of nodes, got:%d expect:7", len(nodes))
}
var n int
for _, node := range nodes {
if node.IsLinkNode {
n++
}
}
if n != 3 {
t.Errorf("Failed to find the correct number of link nodes, got:%d expect:3", n)
}
}

View file

@ -20,35 +20,14 @@ import (
"github.com/google/cayley/graph"
)
func SetupMockTripleStore(nameMap map[string]int) *TestTripleStore {
ts := new(TestTripleStore)
for k, v := range nameMap {
ts.On("ValueOf", k).Return(v)
ts.On("NameOf", v).Return(k)
var simpleStore = &store{data: []string{"0", "1", "2", "3", "4", "5"}}
func simpleFixedIterator() *Fixed {
f := newFixed()
for i := 0; i < 5; i++ {
f.Add(i)
}
return ts
}
func SimpleValueTripleStore() *TestTripleStore {
ts := SetupMockTripleStore(map[string]int{
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
})
return ts
}
func BuildFixedIterator() *Fixed {
fixed := newFixed()
fixed.Add(0)
fixed.Add(1)
fixed.Add(2)
fixed.Add(3)
fixed.Add(4)
return fixed
return f
}
func checkIteratorContains(ts graph.TripleStore, it graph.Iterator, expected []string, t *testing.T) {
@ -82,36 +61,36 @@ func checkIteratorContains(ts graph.TripleStore, it graph.Iterator, expected []s
}
func TestWorkingIntValueComparison(t *testing.T) {
ts := SimpleValueTripleStore()
fixed := BuildFixedIterator()
ts := simpleStore
fixed := simpleFixedIterator()
vc := NewComparison(fixed, kCompareLT, int64(3), ts)
checkIteratorContains(ts, vc, []string{"0", "1", "2"}, t)
}
func TestFailingIntValueComparison(t *testing.T) {
ts := SimpleValueTripleStore()
fixed := BuildFixedIterator()
ts := simpleStore
fixed := simpleFixedIterator()
vc := NewComparison(fixed, kCompareLT, int64(0), ts)
checkIteratorContains(ts, vc, []string{}, t)
}
func TestWorkingGT(t *testing.T) {
ts := SimpleValueTripleStore()
fixed := BuildFixedIterator()
ts := simpleStore
fixed := simpleFixedIterator()
vc := NewComparison(fixed, kCompareGT, int64(2), ts)
checkIteratorContains(ts, vc, []string{"3", "4"}, t)
}
func TestWorkingGTE(t *testing.T) {
ts := SimpleValueTripleStore()
fixed := BuildFixedIterator()
ts := simpleStore
fixed := simpleFixedIterator()
vc := NewComparison(fixed, kCompareGTE, int64(2), ts)
checkIteratorContains(ts, vc, []string{"2", "3", "4"}, t)
}
func TestVCICheck(t *testing.T) {
ts := SimpleValueTripleStore()
fixed := BuildFixedIterator()
ts := simpleStore
fixed := simpleFixedIterator()
vc := NewComparison(fixed, kCompareGTE, int64(2), ts)
if vc.Check(1) {
t.Error("1 is less than 2, should be GTE")

File diff suppressed because it is too large Load diff

View file

@ -239,7 +239,7 @@ func runIteratorWithCallback(it graph.Iterator, ses *Session, callback otto.Valu
func runIteratorOnSession(it graph.Iterator, ses *Session) {
if ses.lookingForQueryShape {
iterator.OutputQueryShapeForIterator(it, ses.ts, &(ses.queryShape))
iterator.OutputQueryShapeForIterator(it, ses.ts, ses.queryShape)
return
}
it, _ = it.Optimize()

View file

@ -51,7 +51,7 @@ func (m *Session) GetQuery(input string, output_struct chan map[string]interface
m.currentQuery = NewQuery(m)
m.currentQuery.BuildIteratorTree(mqlQuery)
output := make(map[string]interface{})
iterator.OutputQueryShapeForIterator(m.currentQuery.it, m.ts, &output)
iterator.OutputQueryShapeForIterator(m.currentQuery.it, m.ts, output)
nodes := output["nodes"].([]iterator.Node)
new_nodes := make([]iterator.Node, 0)
for _, n := range nodes {