Update test in light of graph iterator types

This commit is contained in:
Barak Michener 2014-07-03 15:52:00 -04:00
commit 62d8ebec8a
5 changed files with 356 additions and 425 deletions

View file

@ -17,8 +17,6 @@ package sexp
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/google/cayley/graph"
"github.com/google/cayley/graph/memstore"
)
@ -26,41 +24,55 @@ import (
func TestBadParse(t *testing.T) {
str := ParseString("()")
if str != "" {
t.Errorf("It parsed! Got \"%s\"", str)
t.Errorf("Unexpected parse result, got:%q", str)
}
}
func TestParseSexpWithMemstore(t *testing.T) {
Convey("With a Memstore", t, func() {
ts := memstore.NewTripleStore()
var testQueries = []struct {
message string
add *graph.Triple
query string
typ graph.Type
expect string
}{
{
message: "get a single triple linkage",
add: &graph.Triple{"i", "can", "win", ""},
query: "($a (:can \"win\"))",
typ: graph.And,
expect: "i",
},
{
message: "get a single triple linkage",
add: &graph.Triple{"i", "can", "win", ""},
query: "(\"i\" (:can $a))",
typ: graph.And,
expect: "i",
},
}
Convey("It should parse an empty query", func() {
it := BuildIteratorTreeForQuery(ts, "()")
So(it.Type(), ShouldEqual, graph.Null)
})
Convey("It should get a single triple linkage", func() {
ts.AddTriple(&graph.Triple{"i", "can", "win", ""})
query := "($a (:can \"win\"))"
So(len(query), ShouldEqual, 17)
it := BuildIteratorTreeForQuery(ts, query)
So(it.Type(), ShouldEqual, graph.And)
out, ok := it.Next()
So(ok, ShouldBeTrue)
So(out, ShouldEqual, ts.ValueOf("i"))
})
Convey("It can get an internal linkage", func() {
ts.AddTriple(&graph.Triple{"i", "can", "win", ""})
query := "(\"i\" (:can $a))"
it := BuildIteratorTreeForQuery(ts, query)
So(it.Type(), ShouldEqual, graph.And)
out, ok := it.Next()
So(ok, ShouldBeTrue)
So(out, ShouldEqual, ts.ValueOf("i"))
})
})
func TestMemstoreBackedSexp(t *testing.T) {
ts := memstore.NewTripleStore()
it := BuildIteratorTreeForQuery(ts, "()")
if it.Type() != graph.Null {
t.Errorf(`Incorrect type for empty query, got:%q expect: "null"`, it.Type())
}
for _, test := range testQueries {
if test.add != nil {
ts.AddTriple(test.add)
}
it := BuildIteratorTreeForQuery(ts, test.query)
if it.Type() != test.typ {
t.Errorf("Incorrect type for %s, got:%q expect %q", test.message, it.Type(), test.expect)
}
got, ok := it.Next()
if !ok {
t.Errorf("Failed to %s", test.message)
}
if expect := ts.ValueOf(test.expect); got != expect {
t.Errorf("Incorrect result for %s, got:%v expect %v", test.message, got, expect)
}
}
}
func TestTreeConstraintParse(t *testing.T) {
@ -105,12 +117,18 @@ func TestTreeConstraintTagParse(t *testing.T) {
func TestMultipleConstraintParse(t *testing.T) {
ts := memstore.NewTripleStore()
ts.AddTriple(&graph.Triple{"i", "like", "food", ""})
ts.AddTriple(&graph.Triple{"i", "like", "beer", ""})
ts.AddTriple(&graph.Triple{"you", "like", "beer", ""})
query := "($a \n" +
"(:like :beer)\n" +
"(:like \"food\"))"
for _, tv := range []*graph.Triple{
{"i", "like", "food", ""},
{"i", "like", "beer", ""},
{"you", "like", "beer", ""},
} {
ts.AddTriple(tv)
}
query := `(
$a
(:like :beer)
(:like "food")
)`
it := BuildIteratorTreeForQuery(ts, query)
if it.Type() != graph.And {
t.Error("Odd iterator tree. Got: %s", it.DebugString(0))