Merge pull request #52 from kortschak/testing

Simplify testing code
This commit is contained in:
Barak Michener 2014-07-05 21:55:46 -04:00
commit c88a24467e
15 changed files with 788 additions and 751 deletions

View file

@ -1,21 +1,16 @@
language: go
go:
- 1.2
- 1.3
- tip
install:
- go get github.com/jacobsa/ogletest
- go get github.com/jacobsa/oglematchers
- go get github.com/smartystreets/goconvey
- go get github.com/badgerodon/peg
- go get github.com/barakmich/glog
- go get github.com/julienschmidt/httprouter
- go get github.com/petar/GoLLRB/llrb
- go get github.com/robertkrimen/otto
- go get github.com/russross/blackfriday
- go get github.com/stretchrcom/testify/mock
- go get github.com/syndtr/goleveldb/leveldb
- go get github.com/syndtr/goleveldb/leveldb/cache
- go get github.com/syndtr/goleveldb/leveldb/iterator

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

@ -1,45 +0,0 @@
// Copyright 2014 The Cayley Authors. All rights reserved.
//
// 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 memstore
import "github.com/google/cayley/graph"
// +---+ +---+
// | A |------- ->| F |<--
// +---+ \------>+---+-/ +---+ \--+---+
// ------>|#B#| | | E |
// +---+-------/ >+---+ | +---+
// | C | / v
// +---+ -/ +---+
// ---- +---+/ |#G#|
// \-->|#D#|------------->+---+
// +---+
//
func MakeTestingMemstore() *TripleStore {
ts := NewTripleStore()
ts.AddTriple(&graph.Triple{"A", "follows", "B", ""})
ts.AddTriple(&graph.Triple{"C", "follows", "B", ""})
ts.AddTriple(&graph.Triple{"C", "follows", "D", ""})
ts.AddTriple(&graph.Triple{"D", "follows", "B", ""})
ts.AddTriple(&graph.Triple{"B", "follows", "F", ""})
ts.AddTriple(&graph.Triple{"F", "follows", "G", ""})
ts.AddTriple(&graph.Triple{"D", "follows", "G", ""})
ts.AddTriple(&graph.Triple{"E", "follows", "F", ""})
ts.AddTriple(&graph.Triple{"B", "status", "cool", "status_graph"})
ts.AddTriple(&graph.Triple{"D", "status", "cool", "status_graph"})
ts.AddTriple(&graph.Triple{"G", "status", "cool", "status_graph"})
return ts
}

View file

@ -15,45 +15,104 @@
package memstore
import (
"reflect"
"sort"
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/google/cayley/graph"
"github.com/google/cayley/graph/iterator"
)
// This is a simple test graph.
//
// +---+ +---+
// | A |------- ->| F |<--
// +---+ \------>+---+-/ +---+ \--+---+
// ------>|#B#| | | E |
// +---+-------/ >+---+ | +---+
// | C | / v
// +---+ -/ +---+
// ---- +---+/ |#G#|
// \-->|#D#|------------->+---+
// +---+
//
var simpleGraph = []*graph.Triple{
{"A", "follows", "B", ""},
{"C", "follows", "B", ""},
{"C", "follows", "D", ""},
{"D", "follows", "B", ""},
{"B", "follows", "F", ""},
{"F", "follows", "G", ""},
{"D", "follows", "G", ""},
{"E", "follows", "F", ""},
{"B", "status", "cool", "status_graph"},
{"D", "status", "cool", "status_graph"},
{"G", "status", "cool", "status_graph"},
}
func makeTestStore(data []*graph.Triple) (*TripleStore, []pair) {
seen := make(map[string]struct{})
ts := NewTripleStore()
var (
val int64
ind []pair
)
for _, t := range data {
for _, qp := range []string{t.Subject, t.Predicate, t.Object, t.Provenance} {
if _, ok := seen[qp]; !ok && qp != "" {
val++
ind = append(ind, pair{qp, val})
seen[qp] = struct{}{}
}
}
ts.AddTriple(t)
}
return ts, ind
}
type pair struct {
query string
value int64
}
func TestMemstore(t *testing.T) {
Convey("With a simple memstore", t, func() {
ts := MakeTestingMemstore()
Convey("It should have a reasonable size", func() {
So(ts.Size(), ShouldEqual, 11)
})
Convey("It should have an Id Space that makes sense", func() {
v := ts.ValueOf("C")
So(v.(int64), ShouldEqual, 4)
})
})
ts, index := makeTestStore(simpleGraph)
if size := ts.Size(); size != int64(len(simpleGraph)) {
t.Errorf("Triple store has unexpected size, got:%d expected %d", size, len(simpleGraph))
}
for _, test := range index {
v := ts.ValueOf(test.query)
switch v := v.(type) {
default:
t.Errorf("ValueOf(%q) returned unexpected type, got:%T expected int64", test.query, v)
case int64:
if v != test.value {
t.Errorf("ValueOf(%q) returned unexpected value, got:%d expected:%d", test.query, v, test.value)
}
}
}
}
func TestIteratorsAndNextResultOrderA(t *testing.T) {
ts := MakeTestingMemstore()
ts, _ := makeTestStore(simpleGraph)
fixed := ts.FixedIterator()
fixed.Add(ts.ValueOf("C"))
all := ts.NodesAllIterator()
lto := iterator.NewLinksTo(ts, all, graph.Object)
innerAnd := iterator.NewAnd()
fixed2 := ts.FixedIterator()
fixed2.Add(ts.ValueOf("follows"))
lto2 := iterator.NewLinksTo(ts, fixed2, graph.Predicate)
innerAnd.AddSubIterator(lto2)
innerAnd.AddSubIterator(lto)
all := ts.NodesAllIterator()
innerAnd := iterator.NewAnd()
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed2, graph.Predicate))
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, all, graph.Object))
hasa := iterator.NewHasA(ts, innerAnd, graph.Subject)
outerAnd := iterator.NewAnd()
outerAnd.AddSubIterator(fixed)
outerAnd.AddSubIterator(hasa)
val, ok := outerAnd.Next()
if !ok {
t.Error("Expected one matching subtree")
@ -61,46 +120,38 @@ func TestIteratorsAndNextResultOrderA(t *testing.T) {
if ts.NameOf(val) != "C" {
t.Errorf("Matching subtree should be %s, got %s", "barak", ts.NameOf(val))
}
expected := make([]string, 2)
expected[0] = "B"
expected[1] = "D"
actualOut := make([]string, 2)
actualOut[0] = ts.NameOf(all.Result())
nresultOk := outerAnd.NextResult()
if !nresultOk {
t.Error("Expected two results got one")
var (
got []string
expect = []string{"B", "D"}
)
for {
got = append(got, ts.NameOf(all.Result()))
if !outerAnd.NextResult() {
break
}
}
actualOut[1] = ts.NameOf(all.Result())
nresultOk = outerAnd.NextResult()
if nresultOk {
t.Error("Expected two results got three")
sort.Strings(got)
if !reflect.DeepEqual(got, expect) {
t.Errorf("Unexpected result, got:%q expect:%q", got, expect)
}
CompareStringSlices(t, expected, actualOut)
val, ok = outerAnd.Next()
if ok {
t.Error("More than one possible top level output?")
}
}
func CompareStringSlices(t *testing.T, expected []string, actual []string) {
if len(expected) != len(actual) {
t.Error("String slices are not the same length")
}
sort.Strings(expected)
sort.Strings(actual)
for i := 0; i < len(expected); i++ {
if expected[i] != actual[i] {
t.Errorf("At index %d, expected \"%s\" and got \"%s\"", i, expected[i], actual[i])
}
}
}
func TestLinksToOptimization(t *testing.T) {
ts := MakeTestingMemstore()
ts, _ := makeTestStore(simpleGraph)
fixed := ts.FixedIterator()
fixed.Add(ts.ValueOf("cool"))
lto := iterator.NewLinksTo(ts, fixed, graph.Object)
lto.AddTag("foo")
newIt, changed := lto.Optimize()
if !changed {
t.Error("Iterator didn't change")
@ -108,6 +159,7 @@ func TestLinksToOptimization(t *testing.T) {
if newIt.Type() != Type() {
t.Fatal("Didn't swap out to LLRB")
}
v := newIt.(*Iterator)
v_clone := v.Clone()
if v_clone.DebugString(0) != v.DebugString(0) {
@ -119,18 +171,22 @@ func TestLinksToOptimization(t *testing.T) {
}
func TestRemoveTriple(t *testing.T) {
ts := MakeTestingMemstore()
ts, _ := makeTestStore(simpleGraph)
ts.RemoveTriple(&graph.Triple{"E", "follows", "F", ""})
fixed := ts.FixedIterator()
fixed.Add(ts.ValueOf("E"))
lto := iterator.NewLinksTo(ts, fixed, graph.Subject)
fixed2 := ts.FixedIterator()
fixed2.Add(ts.ValueOf("follows"))
lto2 := iterator.NewLinksTo(ts, fixed2, graph.Predicate)
innerAnd := iterator.NewAnd()
innerAnd.AddSubIterator(lto2)
innerAnd.AddSubIterator(lto)
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed, graph.Subject))
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed2, graph.Predicate))
hasa := iterator.NewHasA(ts, innerAnd, graph.Object)
newIt, _ := hasa.Optimize()
_, ok := newIt.Next()
if ok {

View file

@ -119,6 +119,7 @@ func (t *Triple) Equals(o *Triple) bool {
// Pretty-prints a triple.
func (t *Triple) String() string {
// TODO(kortschak) String methods should generally not terminate in '\n'.
return fmt.Sprintf("%s -- %s -> %s\n", t.Subject, t.Predicate, t.Object)
}

View file

@ -15,40 +15,59 @@
package http
import (
"fmt"
"reflect"
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/google/cayley/graph"
)
func TestParseJSONOkay(t *testing.T) {
Convey("Parse JSON", t, func() {
bytelist := []byte(`[
var parseTests = []struct {
message string
input string
expect []*graph.Triple
err error
}{
{
message: "parse correct JSON",
input: `[
{"subject": "foo", "predicate": "bar", "object": "baz"},
{"subject": "foo", "predicate": "bar", "object": "baz", "provenance": "graph"}
]`)
x, err := ParseJsonToTripleList(bytelist)
So(err, ShouldBeNil)
So(len(x), ShouldEqual, 2)
So(x[0].Subject, ShouldEqual, "foo")
So(x[0].Provenance, ShouldEqual, "")
So(x[1].Provenance, ShouldEqual, "graph")
})
Convey("Parse JSON extra field", t, func() {
bytelist := []byte(`[
{"subject": "foo", "predicate": "bar", "object": "foo", "something_else": "extra data"}
]`)
_, err := ParseJsonToTripleList(bytelist)
So(err, ShouldBeNil)
})
}
func TestParseJSONFail(t *testing.T) {
Convey("Parse JSON Fail", t, func() {
bytelist := []byte(`[
]`,
expect: []*graph.Triple{
{"foo", "bar", "baz", ""},
{"foo", "bar", "baz", "graph"},
},
err: nil,
},
{
message: "parse correct JSON with extra field",
input: `[
{"subject": "foo", "predicate": "bar", "object": "foo", "something_else": "extra data"}
]`,
expect: []*graph.Triple{
{"foo", "bar", "foo", ""},
},
err: nil,
},
{
message: "reject incorrect JSON",
input: `[
{"subject": "foo", "predicate": "bar"}
]`)
_, err := ParseJsonToTripleList(bytelist)
So(err, ShouldNotBeNil)
})
]`,
expect: nil,
err: fmt.Errorf("Invalid triple at index %d. %v", 0, &graph.Triple{"foo", "bar", "", ""}),
},
}
func TestParseJSON(t *testing.T) {
for _, test := range parseTests {
got, err := ParseJsonToTripleList([]byte(test.input))
if fmt.Sprint(err) != fmt.Sprint(test.err) {
t.Errorf("Failed to %v with unexpected error, got:%v expected %v", test.message, err, test.err)
}
if !reflect.DeepEqual(got, test.expect) {
t.Errorf("Failed to %v, got:%v expect:%v", test.message, got, test.expect)
}
}
}

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

@ -98,73 +98,69 @@ var testQueries = []struct {
message: "show correct null semantics",
query: `[{"id": "cool", "status": null}]`,
expect: `
[{"id": "cool", "status": null}]
[
{"id": "cool", "status": null}
]
`,
},
{
message: "get correct follows list",
query: `[{"id": "C", "follows": []}]`,
expect: `
[{
"id": "C",
"follows": ["B", "D"]
}]
`,
[
{"id": "C", "follows": ["B", "D"]}
]
`,
},
{
message: "get correct reverse follows list",
query: `[{"id": "F", "!follows": []}]`,
expect: `
[{
"id": "F",
"!follows": ["B", "E"]
}]
`,
[
{"id": "F", "!follows": ["B", "E"]}
]
`,
},
{
message: "get correct follows struct",
query: `[{"id": null, "follows": {"id": null, "status": "cool"}}]`,
expect: `
[
{"id": "A", "follows": {"id": "B", "status": "cool"}},
{"id": "C", "follows": {"id": "D", "status": "cool"}},
{"id": "D", "follows": {"id": "G", "status": "cool"}},
{"id": "F", "follows": {"id": "G", "status": "cool"}}
]
`,
[
{"id": "A", "follows": {"id": "B", "status": "cool"}},
{"id": "C", "follows": {"id": "D", "status": "cool"}},
{"id": "D", "follows": {"id": "G", "status": "cool"}},
{"id": "F", "follows": {"id": "G", "status": "cool"}}
]
`,
},
{
message: "get correct reverse follows struct",
query: `[{"id": null, "!follows": [{"id": null, "status" : "cool"}]}]`,
expect: `
[
{"id": "F", "!follows": [{"id": "B", "status": "cool"}]},
{"id": "B", "!follows": [{"id": "D", "status": "cool"}]},
{"id": "G", "!follows": [{"id": "D", "status": "cool"}]}
]
[
{"id": "F", "!follows": [{"id": "B", "status": "cool"}]},
{"id": "B", "!follows": [{"id": "D", "status": "cool"}]},
{"id": "G", "!follows": [{"id": "D", "status": "cool"}]}
]
`,
},
{
message: "get correct co-follows",
query: `[{"id": null, "@A:follows": "B", "@B:follows": "D"}]`,
expect: `
[{
"id": "C",
"@A:follows": "B",
"@B:follows": "D"
}]
[
{"id": "C", "@A:follows": "B", "@B:follows": "D"}
]
`,
},
{
message: "get correct reverse co-follows",
query: `[{"id": null, "!follows": {"id": "C"}, "@A:!follows": "D"}]`,
expect: `
[{
"id": "B",
"!follows": {"id": "C"},
"@A:!follows": "D"
}]
`,
[
{"id": "B", "!follows": {"id": "C"}, "@A:!follows": "D"}
]
`,
},
}

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 {