func TestMultipleConstraintParse(t *testing.T) { ts := memstore.NewTripleStore() 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)) } out, ok := it.Next() if !ok { t.Error("Got no results") } if out != ts.ValueOf("i") { t.Errorf("Got %d, expected %d", out, ts.ValueOf("i")) } _, ok = it.Next() if ok { t.Error("Too many results") } }
func makeTestSession(data []*graph.Triple) *Session { ts := memstore.NewTripleStore() for _, t := range data { ts.AddTriple(t) } return NewSession(ts, -1, false) }
func TestParseSexpWithMemstore(t *testing.T) { Convey("With a Memstore", t, func() { ts := memstore.NewTripleStore() Convey("It should parse an empty query", func() { it := BuildIteratorTreeForQuery(ts, "()") So(it.Type(), ShouldEqual, "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, "and") out, ok := it.Next() So(ok, ShouldBeTrue) So(out, ShouldEqual, ts.GetIdFor("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, "and") out, ok := it.Next() So(ok, ShouldBeTrue) So(out, ShouldEqual, ts.GetIdFor("i")) }) }) }
func init() { glog.SetToStderr(true) cfg := config.ParseConfigFromFile("cayley_appengine.cfg") ts := memstore.NewTripleStore() glog.Errorln(cfg) LoadTriplesFromFileInto(ts, cfg.DatabasePath, cfg.LoadSize) http.SetupRoutes(ts, cfg) }
func Open(cfg *config.Config) graph.TripleStore { glog.Infof("Opening database \"%s\" at %s", cfg.DatabaseType, cfg.DatabasePath) switch cfg.DatabaseType { case "mongo", "mongodb": return mongo.NewTripleStore(cfg.DatabasePath, cfg.DatabaseOptions) case "leveldb": return leveldb.NewTripleStore(cfg.DatabasePath, cfg.DatabaseOptions) case "mem": ts := memstore.NewTripleStore() Load(ts, cfg, cfg.DatabasePath, true) return ts } panic("Unsupported database backend " + cfg.DatabaseType) }
func TestTreeConstraintTagParse(t *testing.T) { ts := memstore.NewTripleStore() ts.AddTriple(&graph.Triple{"i", "like", "food", ""}) ts.AddTriple(&graph.Triple{"food", "is", "good", ""}) query := "(\"i\"\n" + "(:like\n" + "($a (:is :good))))" it := BuildIteratorTreeForQuery(ts, query) _, ok := it.Next() if !ok { t.Error("Got no results") } tags := make(map[string]graph.Value) it.TagResults(tags) if ts.NameOf(tags["$a"]) != "food" { t.Errorf("Got %s, expected food", ts.NameOf(tags["$a"])) } }
func TestTreeConstraintParse(t *testing.T) { ts := memstore.NewTripleStore() ts.AddTriple(&graph.Triple{"i", "like", "food", ""}) ts.AddTriple(&graph.Triple{"food", "is", "good", ""}) query := "(\"i\"\n" + "(:like\n" + "($a (:is :good))))" it := BuildIteratorTreeForQuery(ts, query) if it.Type() != graph.And { t.Error("Odd iterator tree. Got: %s", it.DebugString(0)) } out, ok := it.Next() if !ok { t.Error("Got no results") } if out != ts.ValueOf("i") { t.Errorf("Got %d, expected %d", out, 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 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\"))" it := BuildIteratorTreeForQuery(ts, query) if it.Type() != "and" { t.Error("Odd iterator tree. Got: %s", it.DebugString(0)) } out, ok := it.Next() if !ok { t.Error("Got no results") } if out != ts.GetIdFor("i") { t.Errorf("Got %d, expected %d", out, ts.GetIdFor("i")) } _, ok = it.Next() if ok { t.Error("Too many results") } }