bring the path test in line with the gremlin test, add Back()

This commit is contained in:
Barak Michener 2015-10-28 19:15:57 -04:00
parent bcbdb1f82a
commit b91b7ab824
4 changed files with 130 additions and 63 deletions

View file

@ -15,12 +15,15 @@
package path
import (
"io"
"os"
"reflect"
"sort"
"testing"
"github.com/google/cayley/graph"
"github.com/google/cayley/quad"
"github.com/google/cayley/quad/cquads"
_ "github.com/google/cayley/graph/memstore"
_ "github.com/google/cayley/writer"
@ -28,38 +31,43 @@ import (
// This is a simple test graph.
//
// +---+ +---+
// | A |------- ->| F |<--
// +---+ \------>+---+-/ +---+ \--+---+
// ------>|#B#| | | E |
// +---+-------/ >+---+ | +---+
// | C | / v
// +---+ -/ +---+
// ---- +---+/ |#G#|
// \-->|#D#|------------->+---+
// +---+
//
// +-------+ +------+
// | alice |----- ->| fred |<--
// +-------+ \---->+-------+-/ +------+ \-+-------+
// ----->| #bob# | | | emily |
// +---------+--/ --->+-------+ | +-------+
// | charlie | / v
// +---------+ / +--------+
// \--- +--------+ | #greg# |
// \-->| #dani# |------------>+--------+
// +--------+
var simpleGraph = []quad.Quad{
{"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"},
{"predicates", "are", "follows", ""},
{"predicates", "are", "status", ""},
func loadGraph(path string, t testing.TB) []quad.Quad {
var r io.Reader
var simpleGraph []quad.Quad
f, err := os.Open(path)
if err != nil {
t.Fatalf("Failed to open %q: %v", path, err)
}
defer f.Close()
r = f
dec := cquads.NewDecoder(r)
q1, err := dec.Unmarshal()
if err != nil {
t.Fatalf("Failed to Unmarshal: %v", err)
}
for ; err == nil; q1, err = dec.Unmarshal() {
simpleGraph = append(simpleGraph, q1)
}
return simpleGraph
}
func makeTestStore(data []quad.Quad) graph.QuadStore {
func makeTestStore(t testing.TB) graph.QuadStore {
simpleGraph := loadGraph("../../data/testdata.nq", t)
qs, _ := graph.NewQuadStore("memstore", "", nil)
w, _ := graph.NewQuadWriter("single", qs, nil)
for _, t := range data {
for _, t := range simpleGraph {
w.AddQuad(t)
}
return qs
@ -104,89 +112,100 @@ func testSet(qs graph.QuadStore) []test {
return []test{
{
message: "use out",
path: StartPath(qs, "A").Out("follows"),
expect: []string{"B"},
path: StartPath(qs, "alice").Out("follows"),
expect: []string{"bob"},
},
{
message: "use in",
path: StartPath(qs, "B").In("follows"),
expect: []string{"A", "C", "D"},
path: StartPath(qs, "bob").In("follows"),
expect: []string{"alice", "charlie", "dani"},
},
{
message: "use path Out",
path: StartPath(qs, "B").Out(StartPath(qs, "predicates").Out("are")),
expect: []string{"F", "cool"},
path: StartPath(qs, "bob").Out(StartPath(qs, "predicates").Out("are")),
expect: []string{"fred", "cool_person"},
},
{
message: "use And",
path: StartPath(qs, "D").Out("follows").And(
StartPath(qs, "C").Out("follows")),
expect: []string{"B"},
path: StartPath(qs, "dani").Out("follows").And(
StartPath(qs, "charlie").Out("follows")),
expect: []string{"bob"},
},
{
message: "use Or",
path: StartPath(qs, "F").Out("follows").Or(
StartPath(qs, "A").Out("follows")),
expect: []string{"B", "G"},
path: StartPath(qs, "fred").Out("follows").Or(
StartPath(qs, "alice").Out("follows")),
expect: []string{"bob", "greg"},
},
{
message: "implicit All",
path: StartPath(qs),
expect: []string{"A", "B", "C", "D", "E", "F", "G", "follows", "status", "cool", "status_graph", "predicates", "are"},
expect: []string{"alice", "bob", "charlie", "dani", "emily", "fred", "greg", "follows", "status", "cool_person", "predicates", "are"},
},
{
message: "follow",
path: StartPath(qs, "C").Follow(StartMorphism().Out("follows").Out("follows")),
expect: []string{"B", "F", "G"},
path: StartPath(qs, "charlie").Follow(StartMorphism().Out("follows").Out("follows")),
expect: []string{"bob", "fred", "greg"},
},
{
message: "followR",
path: StartPath(qs, "F").FollowReverse(StartMorphism().Out("follows").Out("follows")),
expect: []string{"A", "C", "D"},
path: StartPath(qs, "fred").FollowReverse(StartMorphism().Out("follows").Out("follows")),
expect: []string{"alice", "charlie", "dani"},
},
{
message: "is, tag, instead of FollowR",
path: StartPath(qs).Tag("first").Follow(StartMorphism().Out("follows").Out("follows")).Is("F"),
expect: []string{"A", "C", "D"},
path: StartPath(qs).Tag("first").Follow(StartMorphism().Out("follows").Out("follows")).Is("fred"),
expect: []string{"alice", "charlie", "dani"},
tag: "first",
},
{
message: "use Except to filter out a single vertex",
path: StartPath(qs, "A", "B").Except(StartPath(qs, "A")),
expect: []string{"B"},
path: StartPath(qs, "alice", "bob").Except(StartPath(qs, "alice")),
expect: []string{"bob"},
},
{
message: "use chained Except",
path: StartPath(qs, "A", "B", "C").Except(StartPath(qs, "B")).Except(StartPath(qs, "A")),
expect: []string{"C"},
path: StartPath(qs, "alice", "bob", "charlie").Except(StartPath(qs, "bob")).Except(StartPath(qs, "alice")),
expect: []string{"charlie"},
},
{
message: "show a simple save",
path: StartPath(qs).Save("status", "somecool"),
tag: "somecool",
expect: []string{"cool", "cool", "cool"},
expect: []string{"cool_person", "cool_person", "cool_person"},
},
{
message: "show a simple saveR",
path: StartPath(qs, "cool").SaveReverse("status", "who"),
path: StartPath(qs, "cool_person").SaveReverse("status", "who"),
tag: "who",
expect: []string{"G", "D", "B"},
expect: []string{"greg", "dani", "bob"},
},
{
message: "show a simple Has",
path: StartPath(qs).Has("status", "cool"),
expect: []string{"G", "D", "B"},
path: StartPath(qs).Has("status", "cool_person"),
expect: []string{"greg", "dani", "bob"},
},
{
message: "show a double Has",
path: StartPath(qs).Has("status", "cool").Has("follows", "F"),
expect: []string{"B"},
path: StartPath(qs).Has("status", "cool_person").Has("follows", "fred"),
expect: []string{"bob"},
},
{
message: "use .Tag()-.Is()-.Back()",
path: StartPath(qs, "bob").In("follows").Tag("foo").Out("status").Is("cool_person").Back("foo"),
expect: []string{"dani"},
},
{
message: "do multiple .Back()s",
path: StartPath(qs, "emily").Out("follows").As("f").Out("follows").Out("status").Is("cool_person").Back("f").In("follows").In("follows").As("acd").Out("status").Is("cool_person").Back("f"),
tag: "acd",
expect: []string{"dani"},
},
}
}
func TestMorphisms(t *testing.T) {
qs := makeTestStore(simpleGraph)
qs := makeTestStore(t)
for _, test := range testSet(qs) {
var got []string
if test.tag == "" {