Update test in light of graph iterator types
This commit is contained in:
commit
62d8ebec8a
5 changed files with 356 additions and 425 deletions
|
|
@ -17,8 +17,6 @@ package sexp
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
|
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
"github.com/google/cayley/graph/memstore"
|
"github.com/google/cayley/graph/memstore"
|
||||||
)
|
)
|
||||||
|
|
@ -26,41 +24,55 @@ import (
|
||||||
func TestBadParse(t *testing.T) {
|
func TestBadParse(t *testing.T) {
|
||||||
str := ParseString("()")
|
str := ParseString("()")
|
||||||
if str != "" {
|
if str != "" {
|
||||||
t.Errorf("It parsed! Got \"%s\"", str)
|
t.Errorf("Unexpected parse result, got:%q", str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseSexpWithMemstore(t *testing.T) {
|
var testQueries = []struct {
|
||||||
Convey("With a Memstore", t, func() {
|
message string
|
||||||
ts := memstore.NewTripleStore()
|
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() {
|
func TestMemstoreBackedSexp(t *testing.T) {
|
||||||
it := BuildIteratorTreeForQuery(ts, "()")
|
ts := memstore.NewTripleStore()
|
||||||
So(it.Type(), ShouldEqual, graph.Null)
|
it := BuildIteratorTreeForQuery(ts, "()")
|
||||||
})
|
if it.Type() != graph.Null {
|
||||||
|
t.Errorf(`Incorrect type for empty query, got:%q expect: "null"`, it.Type())
|
||||||
Convey("It should get a single triple linkage", func() {
|
}
|
||||||
ts.AddTriple(&graph.Triple{"i", "can", "win", ""})
|
for _, test := range testQueries {
|
||||||
query := "($a (:can \"win\"))"
|
if test.add != nil {
|
||||||
So(len(query), ShouldEqual, 17)
|
ts.AddTriple(test.add)
|
||||||
it := BuildIteratorTreeForQuery(ts, query)
|
}
|
||||||
So(it.Type(), ShouldEqual, graph.And)
|
it := BuildIteratorTreeForQuery(ts, test.query)
|
||||||
out, ok := it.Next()
|
if it.Type() != test.typ {
|
||||||
So(ok, ShouldBeTrue)
|
t.Errorf("Incorrect type for %s, got:%q expect %q", test.message, it.Type(), test.expect)
|
||||||
So(out, ShouldEqual, ts.ValueOf("i"))
|
}
|
||||||
})
|
got, ok := it.Next()
|
||||||
|
if !ok {
|
||||||
Convey("It can get an internal linkage", func() {
|
t.Errorf("Failed to %s", test.message)
|
||||||
ts.AddTriple(&graph.Triple{"i", "can", "win", ""})
|
}
|
||||||
query := "(\"i\" (:can $a))"
|
if expect := ts.ValueOf(test.expect); got != expect {
|
||||||
it := BuildIteratorTreeForQuery(ts, query)
|
t.Errorf("Incorrect result for %s, got:%v expect %v", test.message, got, expect)
|
||||||
So(it.Type(), ShouldEqual, graph.And)
|
}
|
||||||
out, ok := it.Next()
|
}
|
||||||
So(ok, ShouldBeTrue)
|
|
||||||
So(out, ShouldEqual, ts.ValueOf("i"))
|
|
||||||
})
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTreeConstraintParse(t *testing.T) {
|
func TestTreeConstraintParse(t *testing.T) {
|
||||||
|
|
@ -105,12 +117,18 @@ func TestTreeConstraintTagParse(t *testing.T) {
|
||||||
|
|
||||||
func TestMultipleConstraintParse(t *testing.T) {
|
func TestMultipleConstraintParse(t *testing.T) {
|
||||||
ts := memstore.NewTripleStore()
|
ts := memstore.NewTripleStore()
|
||||||
ts.AddTriple(&graph.Triple{"i", "like", "food", ""})
|
for _, tv := range []*graph.Triple{
|
||||||
ts.AddTriple(&graph.Triple{"i", "like", "beer", ""})
|
{"i", "like", "food", ""},
|
||||||
ts.AddTriple(&graph.Triple{"you", "like", "beer", ""})
|
{"i", "like", "beer", ""},
|
||||||
query := "($a \n" +
|
{"you", "like", "beer", ""},
|
||||||
"(:like :beer)\n" +
|
} {
|
||||||
"(:like \"food\"))"
|
ts.AddTriple(tv)
|
||||||
|
}
|
||||||
|
query := `(
|
||||||
|
$a
|
||||||
|
(:like :beer)
|
||||||
|
(:like "food")
|
||||||
|
)`
|
||||||
it := BuildIteratorTreeForQuery(ts, query)
|
it := BuildIteratorTreeForQuery(ts, query)
|
||||||
if it.Type() != graph.And {
|
if it.Type() != graph.And {
|
||||||
t.Error("Odd iterator tree. Got: %s", it.DebugString(0))
|
t.Error("Odd iterator tree. Got: %s", it.DebugString(0))
|
||||||
|
|
|
||||||
|
|
@ -15,117 +15,112 @@
|
||||||
package nquads
|
package nquads
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
|
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParsingNTriples(t *testing.T) {
|
var testNTriples = []struct {
|
||||||
Convey("When parsing", t, func() {
|
message string
|
||||||
Convey("It should not parse invalid triples", func() {
|
input string
|
||||||
x := Parse("invalid")
|
expect *graph.Triple
|
||||||
So(x, ShouldBeNil)
|
}{
|
||||||
})
|
// NTriple tests.
|
||||||
Convey("It should not parse comments", func() {
|
{
|
||||||
x := Parse("# nominally valid triple .")
|
message: "not parse invalid triples",
|
||||||
So(x, ShouldBeNil)
|
input: "invalid",
|
||||||
})
|
expect: nil,
|
||||||
Convey("It should parse simple triples", func() {
|
},
|
||||||
x := Parse("this is valid .")
|
{
|
||||||
So(x, ShouldNotBeNil)
|
message: "not parse comments",
|
||||||
So(x.Subject, ShouldEqual, "this")
|
input: "# nominally valid triple .",
|
||||||
})
|
expect: nil,
|
||||||
Convey("It should parse quoted triples", func() {
|
},
|
||||||
x := Parse("this is \"valid too\" .")
|
{
|
||||||
So(x, ShouldNotBeNil)
|
message: "parse simple triples",
|
||||||
So(x.Object, ShouldEqual, "valid too")
|
input: "this is valid .",
|
||||||
So(x.Provenance, ShouldEqual, "")
|
expect: &graph.Triple{"this", "is", "valid", ""},
|
||||||
})
|
},
|
||||||
Convey("It should parse escaped quoted triples", func() {
|
{
|
||||||
x := Parse("he said \"\\\"That's all folks\\\"\" .")
|
message: "parse quoted triples",
|
||||||
So(x, ShouldNotBeNil)
|
input: `this is "valid too" .`,
|
||||||
So(x.Object, ShouldEqual, "\"That's all folks\"")
|
expect: &graph.Triple{"this", "is", "valid too", ""},
|
||||||
So(x.Provenance, ShouldEqual, "")
|
},
|
||||||
})
|
{
|
||||||
|
message: "parse escaped quoted triples",
|
||||||
|
input: `he said "\"That's all folks\"" .`,
|
||||||
|
expect: &graph.Triple{"he", "said", `"That's all folks"`, ""},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "parse an example real triple",
|
||||||
|
input: `":/guid/9202a8c04000641f80000000010c843c" "name" "George Morris" .`,
|
||||||
|
expect: &graph.Triple{":/guid/9202a8c04000641f80000000010c843c", "name", "George Morris", ""},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "parse a pathologically spaced triple",
|
||||||
|
input: "foo is \"\\tA big tough\\r\\nDeal\\\\\" .",
|
||||||
|
expect: &graph.Triple{"foo", "is", "\tA big tough\r\nDeal\\", ""},
|
||||||
|
},
|
||||||
|
|
||||||
Convey("It should parse an example real triple", func() {
|
// NQuad tests.
|
||||||
x := Parse("\":/guid/9202a8c04000641f80000000010c843c\" \"name\" \"George Morris\" .")
|
{
|
||||||
So(x, ShouldNotBeNil)
|
message: "parse a simple quad",
|
||||||
So(x.Object, ShouldEqual, "George Morris")
|
input: "this is valid quad .",
|
||||||
So(x.Provenance, ShouldEqual, "")
|
expect: &graph.Triple{"this", "is", "valid", "quad"},
|
||||||
})
|
},
|
||||||
|
{
|
||||||
|
message: "parse a quoted quad",
|
||||||
|
input: `this is valid "quad thing" .`,
|
||||||
|
expect: &graph.Triple{"this", "is", "valid", "quad thing"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "parse crazy escaped quads",
|
||||||
|
input: `"\"this" "\"is" "\"valid" "\"quad thing".`,
|
||||||
|
expect: &graph.Triple{`"this`, `"is`, `"valid`, `"quad thing`},
|
||||||
|
},
|
||||||
|
|
||||||
Convey("It should parse a pathologically spaced triple", func() {
|
// NTriple official tests.
|
||||||
x := Parse("foo is \"\\tA big tough\\r\\nDeal\\\\\" .")
|
{
|
||||||
So(x, ShouldNotBeNil)
|
message: "handle simple case with comments",
|
||||||
So(x.Object, ShouldEqual, "\tA big tough\r\nDeal\\")
|
input: "<http://example/s> <http://example/p> <http://example/o> . # comment",
|
||||||
So(x.Provenance, ShouldEqual, "")
|
expect: &graph.Triple{"http://example/s", "http://example/p", "http://example/o", ""},
|
||||||
})
|
},
|
||||||
|
{
|
||||||
Convey("It should parse a simple quad", func() {
|
message: "handle simple case with comments",
|
||||||
x := Parse("this is valid quad .")
|
input: "<http://example/s> <http://example/p> _:o . # comment",
|
||||||
So(x, ShouldNotBeNil)
|
expect: &graph.Triple{"http://example/s", "http://example/p", "_:o", ""},
|
||||||
So(x.Object, ShouldEqual, "valid")
|
},
|
||||||
So(x.Provenance, ShouldEqual, "quad")
|
{
|
||||||
})
|
message: "handle simple case with comments",
|
||||||
|
input: "<http://example/s> <http://example/p> \"o\" . # comment",
|
||||||
Convey("It should parse a quoted quad", func() {
|
expect: &graph.Triple{"http://example/s", "http://example/p", "o", ""},
|
||||||
x := Parse("this is valid \"quad thing\" .")
|
},
|
||||||
So(x, ShouldNotBeNil)
|
{
|
||||||
So(x.Object, ShouldEqual, "valid")
|
message: "handle simple case with comments",
|
||||||
So(x.Provenance, ShouldEqual, "quad thing")
|
input: "<http://example/s> <http://example/p> \"o\"^^<http://example/dt> . # comment",
|
||||||
})
|
expect: &graph.Triple{"http://example/s", "http://example/p", "o", ""},
|
||||||
|
},
|
||||||
Convey("It should parse crazy escaped quads", func() {
|
{
|
||||||
x := Parse("\"\\\"this\" \"\\\"is\" \"\\\"valid\" \"\\\"quad thing\".")
|
message: "handle simple case with comments",
|
||||||
So(x, ShouldNotBeNil)
|
input: "<http://example/s> <http://example/p> \"o\"@en . # comment",
|
||||||
So(x.Subject, ShouldEqual, "\"this")
|
expect: &graph.Triple{"http://example/s", "http://example/p", "o", ""},
|
||||||
So(x.Predicate, ShouldEqual, "\"is")
|
},
|
||||||
So(x.Object, ShouldEqual, "\"valid")
|
|
||||||
So(x.Provenance, ShouldEqual, "\"quad thing")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParsingNTriplesOfficial(t *testing.T) {
|
func TestParse(t *testing.T) {
|
||||||
Convey("When using some public test cases...", t, func() {
|
for _, test := range testNTriples {
|
||||||
Convey("It should handle some simple cases with comments", func() {
|
got := Parse(test.input)
|
||||||
var x *graph.Triple
|
if !reflect.DeepEqual(got, test.expect) {
|
||||||
x = Parse("<http://example/s> <http://example/p> <http://example/o> . # comment")
|
t.Errorf("Failed to %s, %q, got:%q expect:%q", test.message, test.input, got, test.expect)
|
||||||
So(x, ShouldNotBeNil)
|
|
||||||
So(x.Subject, ShouldEqual, "http://example/s")
|
|
||||||
So(x.Predicate, ShouldEqual, "http://example/p")
|
|
||||||
So(x.Object, ShouldEqual, "http://example/o")
|
|
||||||
So(x.Provenance, ShouldEqual, "")
|
|
||||||
x = Parse("<http://example/s> <http://example/p> _:o . # comment")
|
|
||||||
So(x, ShouldNotBeNil)
|
|
||||||
So(x.Subject, ShouldEqual, "http://example/s")
|
|
||||||
So(x.Predicate, ShouldEqual, "http://example/p")
|
|
||||||
So(x.Object, ShouldEqual, "_:o")
|
|
||||||
So(x.Provenance, ShouldEqual, "")
|
|
||||||
x = Parse("<http://example/s> <http://example/p> \"o\" . # comment")
|
|
||||||
So(x, ShouldNotBeNil)
|
|
||||||
So(x.Object, ShouldEqual, "o")
|
|
||||||
So(x.Provenance, ShouldEqual, "")
|
|
||||||
x = Parse("<http://example/s> <http://example/p> \"o\"^^<http://example/dt> . # comment")
|
|
||||||
So(x, ShouldNotBeNil)
|
|
||||||
So(x.Object, ShouldEqual, "o")
|
|
||||||
So(x.Provenance, ShouldEqual, "")
|
|
||||||
x = Parse("<http://example/s> <http://example/p> \"o\"@en . # comment")
|
|
||||||
So(x, ShouldNotBeNil)
|
|
||||||
So(x.Object, ShouldEqual, "o")
|
|
||||||
So(x.Provenance, ShouldEqual, "")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkParser(b *testing.B) {
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
x := Parse("<http://example/s> <http://example/p> \"object of some real\\tlength\"@en . # comment")
|
|
||||||
if x.Object != "object of some real\tlength" {
|
|
||||||
b.Fail()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var result *graph.Triple
|
||||||
|
|
||||||
|
func BenchmarkParser(b *testing.B) {
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
result = Parse("<http://example/s> <http://example/p> \"object of some real\\tlength\"@en . # comment")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -119,7 +119,7 @@ var testQueries = []struct {
|
||||||
|
|
||||||
// Morphism tests.
|
// Morphism tests.
|
||||||
{
|
{
|
||||||
message: "show simple morphism works",
|
message: "show simple morphism",
|
||||||
query: `
|
query: `
|
||||||
grandfollows = g.M().Out("follows").Out("follows")
|
grandfollows = g.M().Out("follows").Out("follows")
|
||||||
g.V("C").Follow(grandfollows).All()
|
g.V("C").Follow(grandfollows).All()
|
||||||
|
|
@ -127,7 +127,7 @@ var testQueries = []struct {
|
||||||
expect: []string{"G", "F", "B"},
|
expect: []string{"G", "F", "B"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: "show reverse morphism works",
|
message: "show reverse morphism",
|
||||||
query: `
|
query: `
|
||||||
grandfollows = g.M().Out("follows").Out("follows")
|
grandfollows = g.M().Out("follows").Out("follows")
|
||||||
g.V("F").FollowR(grandfollows).All()
|
g.V("F").FollowR(grandfollows).All()
|
||||||
|
|
@ -137,7 +137,7 @@ var testQueries = []struct {
|
||||||
|
|
||||||
// Intersection tests.
|
// Intersection tests.
|
||||||
{
|
{
|
||||||
message: "Simple intersection",
|
message: "show simple intersection",
|
||||||
query: `
|
query: `
|
||||||
function follows(x) { return g.V(x).Out("follows") }
|
function follows(x) { return g.V(x).Out("follows") }
|
||||||
follows("D").And(follows("C")).All()
|
follows("D").And(follows("C")).All()
|
||||||
|
|
@ -145,7 +145,7 @@ var testQueries = []struct {
|
||||||
expect: []string{"B"},
|
expect: []string{"B"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: "Simple Morphism Intersection",
|
message: "show simple morphism intersection",
|
||||||
query: `
|
query: `
|
||||||
grandfollows = g.M().Out("follows").Out("follows")
|
grandfollows = g.M().Out("follows").Out("follows")
|
||||||
function gfollows(x) { return g.V(x).Follow(grandfollows) }
|
function gfollows(x) { return g.V(x).Follow(grandfollows) }
|
||||||
|
|
@ -154,7 +154,7 @@ var testQueries = []struct {
|
||||||
expect: []string{"F"},
|
expect: []string{"F"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: "Double Morphism Intersection",
|
message: "show double morphism intersection",
|
||||||
query: `
|
query: `
|
||||||
grandfollows = g.M().Out("follows").Out("follows")
|
grandfollows = g.M().Out("follows").Out("follows")
|
||||||
function gfollows(x) { return g.V(x).Follow(grandfollows) }
|
function gfollows(x) { return g.V(x).Follow(grandfollows) }
|
||||||
|
|
@ -163,16 +163,15 @@ var testQueries = []struct {
|
||||||
expect: []string{"G"},
|
expect: []string{"G"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: "Reverse Intersection",
|
message: "show reverse intersection",
|
||||||
query: `
|
query: `
|
||||||
grandfollows = g.M().Out("follows").Out("follows")
|
grandfollows = g.M().Out("follows").Out("follows")
|
||||||
g.V("G").FollowR(grandfollows).Intersect(g.V("F").FollowR(grandfollows)).All()
|
g.V("G").FollowR(grandfollows).Intersect(g.V("F").FollowR(grandfollows)).All()
|
||||||
`,
|
`,
|
||||||
expect: []string{"C"},
|
expect: []string{"C"},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
message: "Standard sort of morphism intersection, continue follow",
|
message: "show standard sort of morphism intersection, continue follow",
|
||||||
query: `gfollowers = g.M().In("follows").In("follows")
|
query: `gfollowers = g.M().In("follows").In("follows")
|
||||||
function cool(x) { return g.V(x).As("a").Out("status").Is("cool").Back("a") }
|
function cool(x) { return g.V(x).As("a").Out("status").Is("cool").Back("a") }
|
||||||
cool("G").Follow(gfollowers).Intersect(cool("B").Follow(gfollowers)).All()
|
cool("G").Follow(gfollowers).Intersect(cool("B").Follow(gfollowers)).All()
|
||||||
|
|
|
||||||
|
|
@ -1,276 +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 mql
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
|
|
||||||
"github.com/google/cayley/graph/memstore"
|
|
||||||
)
|
|
||||||
|
|
||||||
// +---+ +---+
|
|
||||||
// | A |------- ->| F |<--
|
|
||||||
// +---+ \------>+---+-/ +---+ \--+---+
|
|
||||||
// ------>|#B#| | | E |
|
|
||||||
// +---+-------/ >+---+ | +---+
|
|
||||||
// | C | / v
|
|
||||||
// +---+ -/ +---+
|
|
||||||
// ---- +---+/ |#G#|
|
|
||||||
// \-->|#D#|------------->+---+
|
|
||||||
// +---+
|
|
||||||
//
|
|
||||||
|
|
||||||
func buildTripleStore() *Session {
|
|
||||||
ts := memstore.MakeTestingMemstore()
|
|
||||||
return NewSession(ts)
|
|
||||||
}
|
|
||||||
|
|
||||||
func compareJsonInterfaces(actual interface{}, expected interface{}, path Path, t *testing.T) {
|
|
||||||
isError := false
|
|
||||||
switch ex := expected.(type) {
|
|
||||||
case bool:
|
|
||||||
switch ac := actual.(type) {
|
|
||||||
case bool:
|
|
||||||
if ac != ex {
|
|
||||||
isError = true
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
t.Log("Mismatched type")
|
|
||||||
isError = true
|
|
||||||
}
|
|
||||||
case float64:
|
|
||||||
switch ac := actual.(type) {
|
|
||||||
case float64:
|
|
||||||
if ac != ex {
|
|
||||||
isError = true
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
t.Log("Mismatched type")
|
|
||||||
isError = true
|
|
||||||
}
|
|
||||||
case string:
|
|
||||||
switch ac := actual.(type) {
|
|
||||||
case string:
|
|
||||||
if ac != ex {
|
|
||||||
isError = true
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
isError = true
|
|
||||||
}
|
|
||||||
case []interface{}:
|
|
||||||
switch ac := actual.(type) {
|
|
||||||
case []interface{}:
|
|
||||||
if len(ac) != len(ex) {
|
|
||||||
t.Log("Different lengths")
|
|
||||||
isError = true
|
|
||||||
} else {
|
|
||||||
for i, elem := range ex {
|
|
||||||
compareJsonInterfaces(ac[i], elem, path.Follow(string(i)), t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
t.Log("Mismatched type")
|
|
||||||
isError = true
|
|
||||||
}
|
|
||||||
case map[string]interface{}:
|
|
||||||
switch ac := actual.(type) {
|
|
||||||
case map[string]interface{}:
|
|
||||||
for k, v := range ex {
|
|
||||||
actual_value, ok := ac[k]
|
|
||||||
if !ok {
|
|
||||||
t.Log("Key", k, "not in actual output.")
|
|
||||||
isError = true
|
|
||||||
} else {
|
|
||||||
compareJsonInterfaces(actual_value, v, path.Follow(string(k)), t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
t.Log("Mismatched type")
|
|
||||||
isError = true
|
|
||||||
}
|
|
||||||
case nil:
|
|
||||||
switch ac := actual.(type) {
|
|
||||||
case nil:
|
|
||||||
if ac != ex {
|
|
||||||
isError = true
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
t.Log("Mismatched type")
|
|
||||||
isError = true
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
t.Error("Unknown JSON type?", expected)
|
|
||||||
}
|
|
||||||
|
|
||||||
if isError {
|
|
||||||
actual_bytes, _ := json.MarshalIndent(actual, "", " ")
|
|
||||||
expected_bytes, _ := json.MarshalIndent(expected, "", " ")
|
|
||||||
t.Error(path.DisplayString(), ":\n", string(actual_bytes), "\nexpected", string(expected_bytes))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func runAndTestQuery(query string, expected string, t *testing.T) {
|
|
||||||
ses := buildTripleStore()
|
|
||||||
c := make(chan interface{}, 5)
|
|
||||||
go ses.ExecInput(query, c, -1)
|
|
||||||
for result := range c {
|
|
||||||
ses.BuildJson(result)
|
|
||||||
}
|
|
||||||
actual_struct, _ := ses.GetJson()
|
|
||||||
var expected_struct interface{}
|
|
||||||
json.Unmarshal([]byte(expected), &expected_struct)
|
|
||||||
compareJsonInterfaces(actual_struct, expected_struct, NewPath(), t)
|
|
||||||
ses.ClearJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetAllIds(t *testing.T) {
|
|
||||||
Convey("Should get all IDs in the database", t, func() {
|
|
||||||
query := `
|
|
||||||
[{"id": null}]
|
|
||||||
`
|
|
||||||
expected := `
|
|
||||||
[
|
|
||||||
{"id": "A"},
|
|
||||||
{"id": "follows"},
|
|
||||||
{"id": "B"},
|
|
||||||
{"id": "C"},
|
|
||||||
{"id": "D"},
|
|
||||||
{"id": "F"},
|
|
||||||
{"id": "G"},
|
|
||||||
{"id": "E"},
|
|
||||||
{"id": "status"},
|
|
||||||
{"id": "cool"},
|
|
||||||
{"id": "status_graph"}
|
|
||||||
]
|
|
||||||
`
|
|
||||||
runAndTestQuery(query, expected, t)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetCool(t *testing.T) {
|
|
||||||
query := `
|
|
||||||
[{"id": null, "status": "cool"}]
|
|
||||||
`
|
|
||||||
expected := `
|
|
||||||
[
|
|
||||||
{"id": "B", "status": "cool"},
|
|
||||||
{"id": "D", "status": "cool"},
|
|
||||||
{"id": "G", "status": "cool"}
|
|
||||||
]
|
|
||||||
`
|
|
||||||
runAndTestQuery(query, expected, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNullSemantics(t *testing.T) {
|
|
||||||
query := `
|
|
||||||
[{"id": "cool", "status": null}]
|
|
||||||
`
|
|
||||||
expected := `
|
|
||||||
[
|
|
||||||
{"id": "cool", "status": null}
|
|
||||||
]
|
|
||||||
`
|
|
||||||
runAndTestQuery(query, expected, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetFollowsList(t *testing.T) {
|
|
||||||
query := `
|
|
||||||
[{"id": "C", "follows": []}]
|
|
||||||
`
|
|
||||||
expected := `
|
|
||||||
[{
|
|
||||||
"id": "C",
|
|
||||||
"follows": [
|
|
||||||
"B", "D"
|
|
||||||
]
|
|
||||||
}]
|
|
||||||
`
|
|
||||||
runAndTestQuery(query, expected, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetFollowsStruct(t *testing.T) {
|
|
||||||
query := `
|
|
||||||
[{"id": null, "follows": {"id": null, "status": "cool"}}]
|
|
||||||
`
|
|
||||||
expected := `
|
|
||||||
[
|
|
||||||
{"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"}}
|
|
||||||
]
|
|
||||||
`
|
|
||||||
runAndTestQuery(query, expected, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetFollowsReverseStructList(t *testing.T) {
|
|
||||||
query := `
|
|
||||||
[{"id": null, "!follows": [{"id": null, "status" : "cool"}]}]
|
|
||||||
`
|
|
||||||
expected := `
|
|
||||||
[
|
|
||||||
{"id": "F", "!follows": [{"id": "B", "status": "cool"}]},
|
|
||||||
{"id": "B", "!follows": [{"id": "D", "status": "cool"}]},
|
|
||||||
{"id": "G", "!follows": [{"id": "D", "status": "cool"}]}
|
|
||||||
]
|
|
||||||
`
|
|
||||||
runAndTestQuery(query, expected, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetRevFollowsList(t *testing.T) {
|
|
||||||
query := `
|
|
||||||
[{"id": "F", "!follows": []}]
|
|
||||||
`
|
|
||||||
expected := `
|
|
||||||
[{
|
|
||||||
"id": "F",
|
|
||||||
"!follows": [
|
|
||||||
"B", "E"
|
|
||||||
]
|
|
||||||
}]
|
|
||||||
`
|
|
||||||
runAndTestQuery(query, expected, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCoFollows(t *testing.T) {
|
|
||||||
query := `
|
|
||||||
[{"id": null, "@A:follows": "B", "@B:follows": "D"}]
|
|
||||||
`
|
|
||||||
expected := `
|
|
||||||
[{
|
|
||||||
"id": "C",
|
|
||||||
"@A:follows": "B",
|
|
||||||
"@B:follows": "D"
|
|
||||||
}]
|
|
||||||
`
|
|
||||||
runAndTestQuery(query, expected, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRevCoFollows(t *testing.T) {
|
|
||||||
query := `
|
|
||||||
[{"id": null, "!follows": {"id": "C"}, "@a:!follows": "D"}]
|
|
||||||
`
|
|
||||||
expected := `
|
|
||||||
[{
|
|
||||||
"id": "B",
|
|
||||||
"!follows": {"id": "C"},
|
|
||||||
"@a:!follows": "D"
|
|
||||||
}]
|
|
||||||
`
|
|
||||||
runAndTestQuery(query, expected, t)
|
|
||||||
}
|
|
||||||
195
query/mql/mql_test.go
Normal file
195
query/mql/mql_test.go
Normal file
|
|
@ -0,0 +1,195 @@
|
||||||
|
// 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 mql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/cayley/graph"
|
||||||
|
"github.com/google/cayley/graph/memstore"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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 makeTestSession(data []*graph.Triple) *Session {
|
||||||
|
ts := memstore.NewTripleStore()
|
||||||
|
for _, t := range data {
|
||||||
|
ts.AddTriple(t)
|
||||||
|
}
|
||||||
|
return NewSession(ts)
|
||||||
|
}
|
||||||
|
|
||||||
|
var testQueries = []struct {
|
||||||
|
message string
|
||||||
|
query string
|
||||||
|
tag string
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
message: "get all IDs in the database",
|
||||||
|
query: `[{"id": null}]`,
|
||||||
|
expect: `
|
||||||
|
[
|
||||||
|
{"id": "A"},
|
||||||
|
{"id": "follows"},
|
||||||
|
{"id": "B"},
|
||||||
|
{"id": "C"},
|
||||||
|
{"id": "D"},
|
||||||
|
{"id": "F"},
|
||||||
|
{"id": "G"},
|
||||||
|
{"id": "E"},
|
||||||
|
{"id": "status"},
|
||||||
|
{"id": "cool"},
|
||||||
|
{"id": "status_graph"}
|
||||||
|
]
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "get nodes by status",
|
||||||
|
query: `[{"id": null, "status": "cool"}]`,
|
||||||
|
expect: `
|
||||||
|
[
|
||||||
|
{"id": "B", "status": "cool"},
|
||||||
|
{"id": "D", "status": "cool"},
|
||||||
|
{"id": "G", "status": "cool"}
|
||||||
|
]
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "show correct null semantics",
|
||||||
|
query: `[{"id": "cool", "status": null}]`,
|
||||||
|
expect: `
|
||||||
|
[{"id": "cool", "status": null}]
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "get correct follows list",
|
||||||
|
query: `[{"id": "C", "follows": []}]`,
|
||||||
|
expect: `
|
||||||
|
[{
|
||||||
|
"id": "C",
|
||||||
|
"follows": ["B", "D"]
|
||||||
|
}]
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "get correct reverse follows list",
|
||||||
|
query: `[{"id": "F", "!follows": []}]`,
|
||||||
|
expect: `
|
||||||
|
[{
|
||||||
|
"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"}}
|
||||||
|
]
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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"}]}
|
||||||
|
]
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "get correct co-follows",
|
||||||
|
query: `[{"id": null, "@A:follows": "B", "@B:follows": "D"}]`,
|
||||||
|
expect: `
|
||||||
|
[{
|
||||||
|
"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"
|
||||||
|
}]
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func runQuery(g []*graph.Triple, query string) interface{} {
|
||||||
|
s := makeTestSession(g)
|
||||||
|
c := make(chan interface{}, 5)
|
||||||
|
go s.ExecInput(query, c, -1)
|
||||||
|
for result := range c {
|
||||||
|
s.BuildJson(result)
|
||||||
|
}
|
||||||
|
result, _ := s.GetJson()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMQL(t *testing.T) {
|
||||||
|
for _, test := range testQueries {
|
||||||
|
got := runQuery(simpleGraph, test.query)
|
||||||
|
var expect interface{}
|
||||||
|
json.Unmarshal([]byte(test.expect), &expect)
|
||||||
|
if !reflect.DeepEqual(got, expect) {
|
||||||
|
b, err := json.MarshalIndent(got, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected JSON marshal error", err)
|
||||||
|
}
|
||||||
|
t.Errorf("Failed to %s, got: %s expected: %s", test.message, b, test.expect)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue