commit
c88a24467e
15 changed files with 788 additions and 751 deletions
|
|
@ -1,21 +1,16 @@
|
||||||
language: go
|
language: go
|
||||||
|
|
||||||
go:
|
go:
|
||||||
- 1.2
|
|
||||||
- 1.3
|
- 1.3
|
||||||
- tip
|
- tip
|
||||||
|
|
||||||
install:
|
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/badgerodon/peg
|
||||||
- go get github.com/barakmich/glog
|
- go get github.com/barakmich/glog
|
||||||
- go get github.com/julienschmidt/httprouter
|
- go get github.com/julienschmidt/httprouter
|
||||||
- go get github.com/petar/GoLLRB/llrb
|
- go get github.com/petar/GoLLRB/llrb
|
||||||
- go get github.com/robertkrimen/otto
|
- go get github.com/robertkrimen/otto
|
||||||
- go get github.com/russross/blackfriday
|
- 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
|
||||||
- go get github.com/syndtr/goleveldb/leveldb/cache
|
- go get github.com/syndtr/goleveldb/leveldb/cache
|
||||||
- go get github.com/syndtr/goleveldb/leveldb/iterator
|
- go get github.com/syndtr/goleveldb/leveldb/iterator
|
||||||
|
|
|
||||||
|
|
@ -21,13 +21,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLinksTo(t *testing.T) {
|
func TestLinksTo(t *testing.T) {
|
||||||
ts := new(TestTripleStore)
|
ts := &store{
|
||||||
tsFixed := newFixed()
|
data: []string{1: "cool"},
|
||||||
tsFixed.Add(2)
|
iter: newFixed(),
|
||||||
ts.On("ValueOf", "cool").Return(1)
|
}
|
||||||
ts.On("TripleIterator", graph.Object, 1).Return(tsFixed)
|
ts.iter.(*Fixed).Add(2)
|
||||||
fixed := newFixed()
|
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)
|
lto := NewLinksTo(ts, fixed, graph.Object)
|
||||||
val, ok := lto.Next()
|
val, ok := lto.Next()
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
||||||
|
|
@ -17,44 +17,58 @@ package iterator
|
||||||
// A quickly mocked version of the TripleStore interface, for use in tests.
|
// A quickly mocked version of the TripleStore interface, for use in tests.
|
||||||
// Can better used Mock.Called but will fill in as needed.
|
// Can better used Mock.Called but will fill in as needed.
|
||||||
|
|
||||||
import (
|
import "github.com/google/cayley/graph"
|
||||||
"github.com/stretchrcom/testify/mock"
|
|
||||||
|
|
||||||
"github.com/google/cayley/graph"
|
type store struct {
|
||||||
)
|
data []string
|
||||||
|
iter graph.Iterator
|
||||||
type TestTripleStore struct {
|
|
||||||
mock.Mock
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TestTripleStore) ValueOf(s string) graph.Value {
|
func (ts *store) ValueOf(s string) graph.Value {
|
||||||
args := ts.Mock.Called(s)
|
for i, v := range ts.data {
|
||||||
return args.Get(0)
|
if s == v {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
func (ts *TestTripleStore) AddTriple(*graph.Triple) {}
|
|
||||||
func (ts *TestTripleStore) AddTripleSet([]*graph.Triple) {}
|
func (ts *store) AddTriple(*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 {
|
func (ts *store) AddTripleSet([]*graph.Triple) {}
|
||||||
args := ts.Mock.Called(d, i)
|
|
||||||
return args.Get(0).(graph.Iterator)
|
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 *store) NodesAllIterator() graph.Iterator { return &Null{} }
|
||||||
func (ts *TestTripleStore) GetIteratorByString(string, string, string) 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)
|
func (ts *store) Size() int64 { return 0 }
|
||||||
return args.Get(0).(string)
|
|
||||||
}
|
func (ts *store) DebugPrint() {}
|
||||||
func (ts *TestTripleStore) Size() int64 { return 0 }
|
|
||||||
func (ts *TestTripleStore) DebugPrint() {}
|
func (ts *store) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||||
func (ts *TestTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
|
||||||
return &Null{}, false
|
return &Null{}, false
|
||||||
}
|
}
|
||||||
func (ts *TestTripleStore) FixedIterator() graph.FixedIterator {
|
|
||||||
|
func (ts *store) FixedIterator() graph.FixedIterator {
|
||||||
return NewFixedIteratorWithCompare(BasicEquality)
|
return NewFixedIteratorWithCompare(BasicEquality)
|
||||||
}
|
}
|
||||||
func (ts *TestTripleStore) Close() {}
|
|
||||||
func (ts *TestTripleStore) TripleDirection(graph.Value, graph.Direction) graph.Value { return 0 }
|
func (ts *store) Close() {}
|
||||||
func (ts *TestTripleStore) RemoveTriple(t *graph.Triple) {}
|
|
||||||
|
func (ts *store) TripleDirection(graph.Value, graph.Direction) graph.Value { return 0 }
|
||||||
|
|
||||||
|
func (ts *store) RemoveTriple(t *graph.Triple) {}
|
||||||
|
|
|
||||||
|
|
@ -15,131 +15,140 @@
|
||||||
package iterator
|
package iterator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
|
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
)
|
)
|
||||||
|
|
||||||
func extractNumbersFromIterator(it graph.Iterator) []int {
|
func iterated(it graph.Iterator) []int {
|
||||||
var outputNumbers []int
|
var res []int
|
||||||
for {
|
for {
|
||||||
val, ok := it.Next()
|
val, ok := it.Next()
|
||||||
if !ok {
|
if !ok {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
outputNumbers = append(outputNumbers, val.(int))
|
res = append(res, val.(int))
|
||||||
}
|
}
|
||||||
return outputNumbers
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOrIteratorBasics(t *testing.T) {
|
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() {
|
if v, _ := or.Size(); v != 7 {
|
||||||
orIt = NewOr()
|
t.Errorf("Unexpected iterator size, got:%d expected %d", v, 7)
|
||||||
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)
|
|
||||||
|
|
||||||
Convey("It should guess its size.", func() {
|
expect := []int{1, 2, 3, 3, 9, 20, 21}
|
||||||
v, _ := orIt.Size()
|
for i := 0; i < 2; i++ {
|
||||||
So(v, ShouldEqual, 7)
|
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() {
|
// Check that optimization works.
|
||||||
allNumbers := []int{1, 2, 3, 3, 9, 20, 21}
|
optOr, _ := or.Optimize()
|
||||||
So(extractNumbersFromIterator(orIt), ShouldResemble, allNumbers)
|
if got := iterated(optOr); !reflect.DeepEqual(got, expect) {
|
||||||
orIt.Reset()
|
t.Errorf("Failed to iterate optimized Or correctly, got:%v expect:%v", got, expect)
|
||||||
So(extractNumbersFromIterator(orIt), ShouldResemble, allNumbers)
|
}
|
||||||
// Optimization works
|
|
||||||
newOr, _ := orIt.Optimize()
|
|
||||||
So(extractNumbersFromIterator(newOr), ShouldResemble, allNumbers)
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("It should check that numbers in either iterator exist.", func() {
|
for _, v := range []int{2, 3, 21} {
|
||||||
So(orIt.Check(2), ShouldEqual, true)
|
if !or.Check(v) {
|
||||||
So(orIt.Check(3), ShouldEqual, true)
|
t.Errorf("Failed to correctly check %d as true", v)
|
||||||
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{22, 5, 0} {
|
||||||
|
if or.Check(v) {
|
||||||
|
t.Errorf("Failed to correctly check %d as false", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestShortCircuitingOrBasics(t *testing.T) {
|
func TestShortCircuitingOrBasics(t *testing.T) {
|
||||||
var orIt *Or
|
var or *Or
|
||||||
|
|
||||||
Convey("Given a short-circuiting Or of two fixed iterators", t, func() {
|
f1 := newFixed()
|
||||||
orIt = NewShortCircuitOr()
|
f1.Add(1)
|
||||||
fixed1 := newFixed()
|
f1.Add(2)
|
||||||
fixed1.Add(1)
|
f1.Add(3)
|
||||||
fixed1.Add(2)
|
f2 := newFixed()
|
||||||
fixed1.Add(3)
|
f2.Add(3)
|
||||||
fixed2 := newFixed()
|
f2.Add(9)
|
||||||
fixed2.Add(3)
|
f2.Add(20)
|
||||||
fixed2.Add(9)
|
f2.Add(21)
|
||||||
fixed2.Add(20)
|
|
||||||
fixed2.Add(21)
|
|
||||||
|
|
||||||
Convey("It should guess its size.", func() {
|
or = NewShortCircuitOr()
|
||||||
orIt.AddSubIterator(fixed1)
|
or.AddSubIterator(f1)
|
||||||
orIt.AddSubIterator(fixed2)
|
or.AddSubIterator(f2)
|
||||||
v, _ := orIt.Size()
|
v, exact := or.Size()
|
||||||
So(v, ShouldEqual, 4)
|
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() {
|
// It should extract the first iterators' numbers.
|
||||||
orIt.AddSubIterator(fixed1)
|
or = NewShortCircuitOr()
|
||||||
orIt.AddSubIterator(fixed2)
|
or.AddSubIterator(f1)
|
||||||
allNumbers := []int{1, 2, 3}
|
or.AddSubIterator(f2)
|
||||||
So(extractNumbersFromIterator(orIt), ShouldResemble, allNumbers)
|
expect := []int{1, 2, 3}
|
||||||
orIt.Reset()
|
for i := 0; i < 2; i++ {
|
||||||
So(extractNumbersFromIterator(orIt), ShouldResemble, allNumbers)
|
if got := iterated(or); !reflect.DeepEqual(got, expect) {
|
||||||
// Optimization works
|
t.Errorf("Failed to iterate Or correctly on repeat %d, got:%v expect:%v", i, got, expect)
|
||||||
newOr, _ := orIt.Optimize()
|
}
|
||||||
So(extractNumbersFromIterator(newOr), ShouldResemble, allNumbers)
|
or.Reset()
|
||||||
})
|
}
|
||||||
|
|
||||||
Convey("It should check that numbers in either iterator exist.", func() {
|
// Check optimization works.
|
||||||
orIt.AddSubIterator(fixed1)
|
optOr, _ := or.Optimize()
|
||||||
orIt.AddSubIterator(fixed2)
|
if got := iterated(optOr); !reflect.DeepEqual(got, expect) {
|
||||||
So(orIt.Check(2), ShouldEqual, true)
|
t.Errorf("Failed to iterate optimized Or correctly, got:%v expect:%v", got, expect)
|
||||||
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 that numbers in either iterator exist.
|
||||||
|
or = NewShortCircuitOr()
|
||||||
Convey("It should check that it pulls the second iterator's numbers if the first is empty.", func() {
|
or.AddSubIterator(f1)
|
||||||
orIt.AddSubIterator(newFixed())
|
or.AddSubIterator(f2)
|
||||||
orIt.AddSubIterator(fixed2)
|
for _, v := range []int{2, 3, 21} {
|
||||||
allNumbers := []int{3, 9, 20, 21}
|
if !or.Check(v) {
|
||||||
So(extractNumbersFromIterator(orIt), ShouldResemble, allNumbers)
|
t.Errorf("Failed to correctly check %d as true", v)
|
||||||
orIt.Reset()
|
}
|
||||||
So(extractNumbersFromIterator(orIt), ShouldResemble, allNumbers)
|
}
|
||||||
// Optimization works
|
for _, v := range []int{22, 5, 0} {
|
||||||
newOr, _ := orIt.Optimize()
|
if or.Check(v) {
|
||||||
So(extractNumbersFromIterator(newOr), ShouldResemble, allNumbers)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ type queryShape struct {
|
||||||
hasaDirs []graph.Direction
|
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{
|
qs := &queryShape{
|
||||||
ts: ts,
|
ts: ts,
|
||||||
nodeId: 1,
|
nodeId: 1,
|
||||||
|
|
@ -50,8 +50,8 @@ func OutputQueryShapeForIterator(it graph.Iterator, ts graph.TripleStore, output
|
||||||
|
|
||||||
node := qs.MakeNode(it.Clone())
|
node := qs.MakeNode(it.Clone())
|
||||||
qs.AddNode(node)
|
qs.AddNode(node)
|
||||||
(*outputMap)["nodes"] = qs.nodes
|
outputMap["nodes"] = qs.nodes
|
||||||
(*outputMap)["links"] = qs.links
|
outputMap["links"] = qs.links
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qs *queryShape) AddNode(n *Node) {
|
func (qs *queryShape) AddNode(n *Node) {
|
||||||
|
|
|
||||||
|
|
@ -15,112 +15,116 @@
|
||||||
package iterator
|
package iterator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
|
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
)
|
)
|
||||||
|
|
||||||
func buildHasaWithTag(ts graph.TripleStore, tag string, target string) *HasA {
|
func hasaWithTag(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)
|
|
||||||
and := NewAnd()
|
and := NewAnd()
|
||||||
and.AddSubIterator(lto1)
|
|
||||||
and.AddSubIterator(lto2)
|
obj := ts.FixedIterator()
|
||||||
hasa := NewHasA(ts, and, graph.Subject)
|
obj.Add(ts.ValueOf(target))
|
||||||
return hasa
|
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) {
|
func TestQueryShape(t *testing.T) {
|
||||||
var queryShape map[string]interface{}
|
ts := &store{
|
||||||
ts := new(TestTripleStore)
|
data: []string{
|
||||||
ts.On("ValueOf", "cool").Return(1)
|
1: "cool",
|
||||||
ts.On("NameOf", 1).Return("cool")
|
2: "status",
|
||||||
ts.On("ValueOf", "status").Return(2)
|
3: "fun",
|
||||||
ts.On("NameOf", 2).Return("status")
|
4: "name",
|
||||||
ts.On("ValueOf", "fun").Return(3)
|
},
|
||||||
ts.On("NameOf", 3).Return("fun")
|
}
|
||||||
ts.On("ValueOf", "name").Return(4)
|
|
||||||
ts.On("NameOf", 4).Return("name")
|
|
||||||
|
|
||||||
Convey("Given a single linkage iterator's shape", t, func() {
|
// Given a single linkage iterator's shape.
|
||||||
queryShape = make(map[string]interface{})
|
hasa := hasaWithTag(ts, "tag", "cool")
|
||||||
hasa := buildHasaWithTag(ts, "tag", "cool")
|
hasa.AddTag("top")
|
||||||
hasa.AddTag("top")
|
|
||||||
OutputQueryShapeForIterator(hasa, ts, &queryShape)
|
|
||||||
|
|
||||||
Convey("It should have three nodes and one link", func() {
|
shape := make(map[string]interface{})
|
||||||
nodes := queryShape["nodes"].([]Node)
|
OutputQueryShapeForIterator(hasa, ts, shape)
|
||||||
links := queryShape["links"].([]Link)
|
|
||||||
So(len(nodes), ShouldEqual, 3)
|
|
||||||
So(len(links), ShouldEqual, 1)
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("These nodes should be correctly tagged", func() {
|
nodes := shape["nodes"].([]Node)
|
||||||
nodes := queryShape["nodes"].([]Node)
|
if len(nodes) != 3 {
|
||||||
So(nodes[0].Tags, ShouldResemble, []string{"tag"})
|
t.Errorf("Failed to get correct number of nodes, got:%d expect:4", len(nodes))
|
||||||
So(nodes[1].IsLinkNode, ShouldEqual, true)
|
}
|
||||||
So(nodes[2].Tags, ShouldResemble, []string{"top"})
|
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() {
|
// Link should be correctly typed.
|
||||||
nodes := queryShape["nodes"].([]Node)
|
nodes = shape["nodes"].([]Node)
|
||||||
links := queryShape["links"].([]Link)
|
link := shape["links"].([]Link)[0]
|
||||||
So(links[0].Source, ShouldEqual, nodes[2].Id)
|
if link.Source != nodes[2].Id {
|
||||||
So(links[0].Target, ShouldEqual, nodes[0].Id)
|
t.Errorf("Failed to get correct link source, got:%v expect:%v", link.Source, nodes[2].Id)
|
||||||
So(links[0].LinkNode, ShouldEqual, nodes[1].Id)
|
}
|
||||||
So(links[0].Pred, ShouldEqual, 0)
|
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() {
|
hasa2 := hasaWithTag(ts, "tag2", "fun")
|
||||||
queryShape = make(map[string]interface{})
|
hasa2.AddTag("hasa2")
|
||||||
hasa1 := buildHasaWithTag(ts, "tag1", "cool")
|
andInternal.AddSubIterator(hasa2)
|
||||||
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)
|
|
||||||
|
|
||||||
Convey("It should have seven nodes and three links", func() {
|
pred := ts.FixedIterator()
|
||||||
nodes := queryShape["nodes"].([]Node)
|
pred.Add(ts.ValueOf("name"))
|
||||||
links := queryShape["links"].([]Link)
|
|
||||||
So(len(nodes), ShouldEqual, 7)
|
|
||||||
So(len(links), ShouldEqual, 3)
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Three of the nodes are link nodes, four aren't", func() {
|
and := NewAnd()
|
||||||
nodes := queryShape["nodes"].([]Node)
|
and.AddSubIterator(NewLinksTo(ts, andInternal, graph.Subject))
|
||||||
count := 0
|
and.AddSubIterator(NewLinksTo(ts, pred, graph.Predicate))
|
||||||
for _, node := range nodes {
|
|
||||||
if node.IsLinkNode {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
So(count, ShouldEqual, 3)
|
|
||||||
})
|
|
||||||
|
|
||||||
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,35 +20,14 @@ import (
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetupMockTripleStore(nameMap map[string]int) *TestTripleStore {
|
var simpleStore = &store{data: []string{"0", "1", "2", "3", "4", "5"}}
|
||||||
ts := new(TestTripleStore)
|
|
||||||
for k, v := range nameMap {
|
func simpleFixedIterator() *Fixed {
|
||||||
ts.On("ValueOf", k).Return(v)
|
f := newFixed()
|
||||||
ts.On("NameOf", v).Return(k)
|
for i := 0; i < 5; i++ {
|
||||||
|
f.Add(i)
|
||||||
}
|
}
|
||||||
return ts
|
return f
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkIteratorContains(ts graph.TripleStore, it graph.Iterator, expected []string, t *testing.T) {
|
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) {
|
func TestWorkingIntValueComparison(t *testing.T) {
|
||||||
ts := SimpleValueTripleStore()
|
ts := simpleStore
|
||||||
fixed := BuildFixedIterator()
|
fixed := simpleFixedIterator()
|
||||||
vc := NewComparison(fixed, kCompareLT, int64(3), ts)
|
vc := NewComparison(fixed, kCompareLT, int64(3), ts)
|
||||||
checkIteratorContains(ts, vc, []string{"0", "1", "2"}, t)
|
checkIteratorContains(ts, vc, []string{"0", "1", "2"}, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFailingIntValueComparison(t *testing.T) {
|
func TestFailingIntValueComparison(t *testing.T) {
|
||||||
ts := SimpleValueTripleStore()
|
ts := simpleStore
|
||||||
fixed := BuildFixedIterator()
|
fixed := simpleFixedIterator()
|
||||||
vc := NewComparison(fixed, kCompareLT, int64(0), ts)
|
vc := NewComparison(fixed, kCompareLT, int64(0), ts)
|
||||||
checkIteratorContains(ts, vc, []string{}, t)
|
checkIteratorContains(ts, vc, []string{}, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWorkingGT(t *testing.T) {
|
func TestWorkingGT(t *testing.T) {
|
||||||
ts := SimpleValueTripleStore()
|
ts := simpleStore
|
||||||
fixed := BuildFixedIterator()
|
fixed := simpleFixedIterator()
|
||||||
vc := NewComparison(fixed, kCompareGT, int64(2), ts)
|
vc := NewComparison(fixed, kCompareGT, int64(2), ts)
|
||||||
checkIteratorContains(ts, vc, []string{"3", "4"}, t)
|
checkIteratorContains(ts, vc, []string{"3", "4"}, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWorkingGTE(t *testing.T) {
|
func TestWorkingGTE(t *testing.T) {
|
||||||
ts := SimpleValueTripleStore()
|
ts := simpleStore
|
||||||
fixed := BuildFixedIterator()
|
fixed := simpleFixedIterator()
|
||||||
vc := NewComparison(fixed, kCompareGTE, int64(2), ts)
|
vc := NewComparison(fixed, kCompareGTE, int64(2), ts)
|
||||||
checkIteratorContains(ts, vc, []string{"2", "3", "4"}, t)
|
checkIteratorContains(ts, vc, []string{"2", "3", "4"}, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestVCICheck(t *testing.T) {
|
func TestVCICheck(t *testing.T) {
|
||||||
ts := SimpleValueTripleStore()
|
ts := simpleStore
|
||||||
fixed := BuildFixedIterator()
|
fixed := simpleFixedIterator()
|
||||||
vc := NewComparison(fixed, kCompareGTE, int64(2), ts)
|
vc := NewComparison(fixed, kCompareGTE, int64(2), ts)
|
||||||
if vc.Check(1) {
|
if vc.Check(1) {
|
||||||
t.Error("1 is less than 2, should be GTE")
|
t.Error("1 is less than 2, should be GTE")
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
@ -15,45 +15,104 @@
|
||||||
package memstore
|
package memstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
|
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
"github.com/google/cayley/graph/iterator"
|
"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) {
|
func TestMemstore(t *testing.T) {
|
||||||
Convey("With a simple memstore", t, func() {
|
ts, index := makeTestStore(simpleGraph)
|
||||||
ts := MakeTestingMemstore()
|
if size := ts.Size(); size != int64(len(simpleGraph)) {
|
||||||
Convey("It should have a reasonable size", func() {
|
t.Errorf("Triple store has unexpected size, got:%d expected %d", size, len(simpleGraph))
|
||||||
So(ts.Size(), ShouldEqual, 11)
|
}
|
||||||
})
|
for _, test := range index {
|
||||||
Convey("It should have an Id Space that makes sense", func() {
|
v := ts.ValueOf(test.query)
|
||||||
v := ts.ValueOf("C")
|
switch v := v.(type) {
|
||||||
So(v.(int64), ShouldEqual, 4)
|
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) {
|
func TestIteratorsAndNextResultOrderA(t *testing.T) {
|
||||||
ts := MakeTestingMemstore()
|
ts, _ := makeTestStore(simpleGraph)
|
||||||
|
|
||||||
fixed := ts.FixedIterator()
|
fixed := ts.FixedIterator()
|
||||||
fixed.Add(ts.ValueOf("C"))
|
fixed.Add(ts.ValueOf("C"))
|
||||||
all := ts.NodesAllIterator()
|
|
||||||
lto := iterator.NewLinksTo(ts, all, graph.Object)
|
|
||||||
innerAnd := iterator.NewAnd()
|
|
||||||
|
|
||||||
fixed2 := ts.FixedIterator()
|
fixed2 := ts.FixedIterator()
|
||||||
fixed2.Add(ts.ValueOf("follows"))
|
fixed2.Add(ts.ValueOf("follows"))
|
||||||
lto2 := iterator.NewLinksTo(ts, fixed2, graph.Predicate)
|
|
||||||
innerAnd.AddSubIterator(lto2)
|
all := ts.NodesAllIterator()
|
||||||
innerAnd.AddSubIterator(lto)
|
|
||||||
|
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)
|
hasa := iterator.NewHasA(ts, innerAnd, graph.Subject)
|
||||||
outerAnd := iterator.NewAnd()
|
outerAnd := iterator.NewAnd()
|
||||||
outerAnd.AddSubIterator(fixed)
|
outerAnd.AddSubIterator(fixed)
|
||||||
outerAnd.AddSubIterator(hasa)
|
outerAnd.AddSubIterator(hasa)
|
||||||
|
|
||||||
val, ok := outerAnd.Next()
|
val, ok := outerAnd.Next()
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Error("Expected one matching subtree")
|
t.Error("Expected one matching subtree")
|
||||||
|
|
@ -61,46 +120,38 @@ func TestIteratorsAndNextResultOrderA(t *testing.T) {
|
||||||
if ts.NameOf(val) != "C" {
|
if ts.NameOf(val) != "C" {
|
||||||
t.Errorf("Matching subtree should be %s, got %s", "barak", ts.NameOf(val))
|
t.Errorf("Matching subtree should be %s, got %s", "barak", ts.NameOf(val))
|
||||||
}
|
}
|
||||||
expected := make([]string, 2)
|
|
||||||
expected[0] = "B"
|
var (
|
||||||
expected[1] = "D"
|
got []string
|
||||||
actualOut := make([]string, 2)
|
expect = []string{"B", "D"}
|
||||||
actualOut[0] = ts.NameOf(all.Result())
|
)
|
||||||
nresultOk := outerAnd.NextResult()
|
for {
|
||||||
if !nresultOk {
|
got = append(got, ts.NameOf(all.Result()))
|
||||||
t.Error("Expected two results got one")
|
if !outerAnd.NextResult() {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
actualOut[1] = ts.NameOf(all.Result())
|
sort.Strings(got)
|
||||||
nresultOk = outerAnd.NextResult()
|
|
||||||
if nresultOk {
|
if !reflect.DeepEqual(got, expect) {
|
||||||
t.Error("Expected two results got three")
|
t.Errorf("Unexpected result, got:%q expect:%q", got, expect)
|
||||||
}
|
}
|
||||||
CompareStringSlices(t, expected, actualOut)
|
|
||||||
val, ok = outerAnd.Next()
|
val, ok = outerAnd.Next()
|
||||||
if ok {
|
if ok {
|
||||||
t.Error("More than one possible top level output?")
|
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) {
|
func TestLinksToOptimization(t *testing.T) {
|
||||||
ts := MakeTestingMemstore()
|
ts, _ := makeTestStore(simpleGraph)
|
||||||
|
|
||||||
fixed := ts.FixedIterator()
|
fixed := ts.FixedIterator()
|
||||||
fixed.Add(ts.ValueOf("cool"))
|
fixed.Add(ts.ValueOf("cool"))
|
||||||
|
|
||||||
lto := iterator.NewLinksTo(ts, fixed, graph.Object)
|
lto := iterator.NewLinksTo(ts, fixed, graph.Object)
|
||||||
lto.AddTag("foo")
|
lto.AddTag("foo")
|
||||||
|
|
||||||
newIt, changed := lto.Optimize()
|
newIt, changed := lto.Optimize()
|
||||||
if !changed {
|
if !changed {
|
||||||
t.Error("Iterator didn't change")
|
t.Error("Iterator didn't change")
|
||||||
|
|
@ -108,6 +159,7 @@ func TestLinksToOptimization(t *testing.T) {
|
||||||
if newIt.Type() != Type() {
|
if newIt.Type() != Type() {
|
||||||
t.Fatal("Didn't swap out to LLRB")
|
t.Fatal("Didn't swap out to LLRB")
|
||||||
}
|
}
|
||||||
|
|
||||||
v := newIt.(*Iterator)
|
v := newIt.(*Iterator)
|
||||||
v_clone := v.Clone()
|
v_clone := v.Clone()
|
||||||
if v_clone.DebugString(0) != v.DebugString(0) {
|
if v_clone.DebugString(0) != v.DebugString(0) {
|
||||||
|
|
@ -119,18 +171,22 @@ func TestLinksToOptimization(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoveTriple(t *testing.T) {
|
func TestRemoveTriple(t *testing.T) {
|
||||||
ts := MakeTestingMemstore()
|
ts, _ := makeTestStore(simpleGraph)
|
||||||
|
|
||||||
ts.RemoveTriple(&graph.Triple{"E", "follows", "F", ""})
|
ts.RemoveTriple(&graph.Triple{"E", "follows", "F", ""})
|
||||||
|
|
||||||
fixed := ts.FixedIterator()
|
fixed := ts.FixedIterator()
|
||||||
fixed.Add(ts.ValueOf("E"))
|
fixed.Add(ts.ValueOf("E"))
|
||||||
lto := iterator.NewLinksTo(ts, fixed, graph.Subject)
|
|
||||||
fixed2 := ts.FixedIterator()
|
fixed2 := ts.FixedIterator()
|
||||||
fixed2.Add(ts.ValueOf("follows"))
|
fixed2.Add(ts.ValueOf("follows"))
|
||||||
lto2 := iterator.NewLinksTo(ts, fixed2, graph.Predicate)
|
|
||||||
innerAnd := iterator.NewAnd()
|
innerAnd := iterator.NewAnd()
|
||||||
innerAnd.AddSubIterator(lto2)
|
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed, graph.Subject))
|
||||||
innerAnd.AddSubIterator(lto)
|
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed2, graph.Predicate))
|
||||||
|
|
||||||
hasa := iterator.NewHasA(ts, innerAnd, graph.Object)
|
hasa := iterator.NewHasA(ts, innerAnd, graph.Object)
|
||||||
|
|
||||||
newIt, _ := hasa.Optimize()
|
newIt, _ := hasa.Optimize()
|
||||||
_, ok := newIt.Next()
|
_, ok := newIt.Next()
|
||||||
if ok {
|
if ok {
|
||||||
|
|
|
||||||
|
|
@ -119,6 +119,7 @@ func (t *Triple) Equals(o *Triple) bool {
|
||||||
|
|
||||||
// Pretty-prints a triple.
|
// Pretty-prints a triple.
|
||||||
func (t *Triple) String() string {
|
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)
|
return fmt.Sprintf("%s -- %s -> %s\n", t.Subject, t.Predicate, t.Object)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,40 +15,59 @@
|
||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
"github.com/google/cayley/graph"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseJSONOkay(t *testing.T) {
|
var parseTests = []struct {
|
||||||
Convey("Parse JSON", t, func() {
|
message string
|
||||||
bytelist := []byte(`[
|
input string
|
||||||
|
expect []*graph.Triple
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
message: "parse correct JSON",
|
||||||
|
input: `[
|
||||||
{"subject": "foo", "predicate": "bar", "object": "baz"},
|
{"subject": "foo", "predicate": "bar", "object": "baz"},
|
||||||
{"subject": "foo", "predicate": "bar", "object": "baz", "provenance": "graph"}
|
{"subject": "foo", "predicate": "bar", "object": "baz", "provenance": "graph"}
|
||||||
]`)
|
]`,
|
||||||
x, err := ParseJsonToTripleList(bytelist)
|
expect: []*graph.Triple{
|
||||||
So(err, ShouldBeNil)
|
{"foo", "bar", "baz", ""},
|
||||||
So(len(x), ShouldEqual, 2)
|
{"foo", "bar", "baz", "graph"},
|
||||||
So(x[0].Subject, ShouldEqual, "foo")
|
},
|
||||||
So(x[0].Provenance, ShouldEqual, "")
|
err: nil,
|
||||||
So(x[1].Provenance, ShouldEqual, "graph")
|
},
|
||||||
})
|
{
|
||||||
|
message: "parse correct JSON with extra field",
|
||||||
Convey("Parse JSON extra field", t, func() {
|
input: `[
|
||||||
bytelist := []byte(`[
|
{"subject": "foo", "predicate": "bar", "object": "foo", "something_else": "extra data"}
|
||||||
{"subject": "foo", "predicate": "bar", "object": "foo", "something_else": "extra data"}
|
]`,
|
||||||
]`)
|
expect: []*graph.Triple{
|
||||||
_, err := ParseJsonToTripleList(bytelist)
|
{"foo", "bar", "foo", ""},
|
||||||
So(err, ShouldBeNil)
|
},
|
||||||
})
|
err: nil,
|
||||||
}
|
},
|
||||||
|
{
|
||||||
func TestParseJSONFail(t *testing.T) {
|
message: "reject incorrect JSON",
|
||||||
Convey("Parse JSON Fail", t, func() {
|
input: `[
|
||||||
bytelist := []byte(`[
|
|
||||||
{"subject": "foo", "predicate": "bar"}
|
{"subject": "foo", "predicate": "bar"}
|
||||||
]`)
|
]`,
|
||||||
_, err := ParseJsonToTripleList(bytelist)
|
expect: nil,
|
||||||
So(err, ShouldNotBeNil)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -239,7 +239,7 @@ func runIteratorWithCallback(it graph.Iterator, ses *Session, callback otto.Valu
|
||||||
|
|
||||||
func runIteratorOnSession(it graph.Iterator, ses *Session) {
|
func runIteratorOnSession(it graph.Iterator, ses *Session) {
|
||||||
if ses.lookingForQueryShape {
|
if ses.lookingForQueryShape {
|
||||||
iterator.OutputQueryShapeForIterator(it, ses.ts, &(ses.queryShape))
|
iterator.OutputQueryShapeForIterator(it, ses.ts, ses.queryShape)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
it, _ = it.Optimize()
|
it, _ = it.Optimize()
|
||||||
|
|
|
||||||
|
|
@ -98,73 +98,69 @@ var testQueries = []struct {
|
||||||
message: "show correct null semantics",
|
message: "show correct null semantics",
|
||||||
query: `[{"id": "cool", "status": null}]`,
|
query: `[{"id": "cool", "status": null}]`,
|
||||||
expect: `
|
expect: `
|
||||||
[{"id": "cool", "status": null}]
|
[
|
||||||
|
{"id": "cool", "status": null}
|
||||||
|
]
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: "get correct follows list",
|
message: "get correct follows list",
|
||||||
query: `[{"id": "C", "follows": []}]`,
|
query: `[{"id": "C", "follows": []}]`,
|
||||||
expect: `
|
expect: `
|
||||||
[{
|
[
|
||||||
"id": "C",
|
{"id": "C", "follows": ["B", "D"]}
|
||||||
"follows": ["B", "D"]
|
]
|
||||||
}]
|
`,
|
||||||
`,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: "get correct reverse follows list",
|
message: "get correct reverse follows list",
|
||||||
query: `[{"id": "F", "!follows": []}]`,
|
query: `[{"id": "F", "!follows": []}]`,
|
||||||
expect: `
|
expect: `
|
||||||
[{
|
[
|
||||||
"id": "F",
|
{"id": "F", "!follows": ["B", "E"]}
|
||||||
"!follows": ["B", "E"]
|
]
|
||||||
}]
|
`,
|
||||||
`,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: "get correct follows struct",
|
message: "get correct follows struct",
|
||||||
query: `[{"id": null, "follows": {"id": null, "status": "cool"}}]`,
|
query: `[{"id": null, "follows": {"id": null, "status": "cool"}}]`,
|
||||||
expect: `
|
expect: `
|
||||||
[
|
[
|
||||||
{"id": "A", "follows": {"id": "B", "status": "cool"}},
|
{"id": "A", "follows": {"id": "B", "status": "cool"}},
|
||||||
{"id": "C", "follows": {"id": "D", "status": "cool"}},
|
{"id": "C", "follows": {"id": "D", "status": "cool"}},
|
||||||
{"id": "D", "follows": {"id": "G", "status": "cool"}},
|
{"id": "D", "follows": {"id": "G", "status": "cool"}},
|
||||||
{"id": "F", "follows": {"id": "G", "status": "cool"}}
|
{"id": "F", "follows": {"id": "G", "status": "cool"}}
|
||||||
]
|
]
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: "get correct reverse follows struct",
|
message: "get correct reverse follows struct",
|
||||||
query: `[{"id": null, "!follows": [{"id": null, "status" : "cool"}]}]`,
|
query: `[{"id": null, "!follows": [{"id": null, "status" : "cool"}]}]`,
|
||||||
expect: `
|
expect: `
|
||||||
[
|
[
|
||||||
{"id": "F", "!follows": [{"id": "B", "status": "cool"}]},
|
{"id": "F", "!follows": [{"id": "B", "status": "cool"}]},
|
||||||
{"id": "B", "!follows": [{"id": "D", "status": "cool"}]},
|
{"id": "B", "!follows": [{"id": "D", "status": "cool"}]},
|
||||||
{"id": "G", "!follows": [{"id": "D", "status": "cool"}]}
|
{"id": "G", "!follows": [{"id": "D", "status": "cool"}]}
|
||||||
]
|
]
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: "get correct co-follows",
|
message: "get correct co-follows",
|
||||||
query: `[{"id": null, "@A:follows": "B", "@B:follows": "D"}]`,
|
query: `[{"id": null, "@A:follows": "B", "@B:follows": "D"}]`,
|
||||||
expect: `
|
expect: `
|
||||||
[{
|
[
|
||||||
"id": "C",
|
{"id": "C", "@A:follows": "B", "@B:follows": "D"}
|
||||||
"@A:follows": "B",
|
]
|
||||||
"@B:follows": "D"
|
|
||||||
}]
|
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: "get correct reverse co-follows",
|
message: "get correct reverse co-follows",
|
||||||
query: `[{"id": null, "!follows": {"id": "C"}, "@A:!follows": "D"}]`,
|
query: `[{"id": null, "!follows": {"id": "C"}, "@A:!follows": "D"}]`,
|
||||||
expect: `
|
expect: `
|
||||||
[{
|
[
|
||||||
"id": "B",
|
{"id": "B", "!follows": {"id": "C"}, "@A:!follows": "D"}
|
||||||
"!follows": {"id": "C"},
|
]
|
||||||
"@A:!follows": "D"
|
`,
|
||||||
}]
|
|
||||||
`,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ func (m *Session) GetQuery(input string, output_struct chan map[string]interface
|
||||||
m.currentQuery = NewQuery(m)
|
m.currentQuery = NewQuery(m)
|
||||||
m.currentQuery.BuildIteratorTree(mqlQuery)
|
m.currentQuery.BuildIteratorTree(mqlQuery)
|
||||||
output := make(map[string]interface{})
|
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)
|
nodes := output["nodes"].([]iterator.Node)
|
||||||
new_nodes := make([]iterator.Node, 0)
|
new_nodes := make([]iterator.Node, 0)
|
||||||
for _, n := range nodes {
|
for _, n := range nodes {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue