示例#1
0
func iterateResults(qs graph.QuadStore, it graph.Iterator) []string {
	var res []string
	for graph.Next(it) {
		v := it.Result()
		if t, ok := v.(*Token); ok && t.Kind == nodeKind {
			res = append(res, qs.NameOf(it.Result()))
		} else {
			res = append(res, qs.Quad(it.Result()).String())
		}
	}
	sort.Strings(res)
	it.Reset()
	return res
}
示例#2
0
func TestSetIterator(t *testing.T) {
	var ts *LevelDBTripleStore
	var tmpDir string

	Convey("Given a prepared database", t, func() {
		tmpDir, _ = ioutil.TempDir(os.TempDir(), "cayley_test")
		t.Log(tmpDir)
		defer os.RemoveAll(tmpDir)
		ok := CreateNewLevelDB(tmpDir)
		So(ok, ShouldBeTrue)
		ts = NewDefaultLevelDBTripleStore(tmpDir, nil)
		ts.AddTripleSet(makeTripleSet())
		var it graph.Iterator

		Convey("Can create a subject iterator", func() {
			it = ts.GetTripleIterator("s", ts.GetIdFor("C"))

			Convey("Containing the right things", func() {
				expected := []string{
					graph.MakeTriple("C", "follows", "B", "").ToString(),
					graph.MakeTriple("C", "follows", "D", "").ToString(),
				}
				actual := extractTripleFromIterator(ts, it)
				sort.Strings(actual)
				sort.Strings(expected)
				So(actual, ShouldResemble, expected)
			})

			Convey("And checkable", func() {
				and := graph.NewAndIterator()
				and.AddSubIterator(ts.GetTriplesAllIterator())
				and.AddSubIterator(it)

				expected := []string{
					graph.MakeTriple("C", "follows", "B", "").ToString(),
					graph.MakeTriple("C", "follows", "D", "").ToString(),
				}
				actual := extractTripleFromIterator(ts, and)
				sort.Strings(actual)
				sort.Strings(expected)
				So(actual, ShouldResemble, expected)
			})
			Reset(func() {
				it.Reset()
			})

		})

		Convey("Can create an object iterator", func() {
			it = ts.GetTripleIterator("o", ts.GetIdFor("F"))

			Convey("Containing the right things", func() {
				expected := []string{
					graph.MakeTriple("B", "follows", "F", "").ToString(),
					graph.MakeTriple("E", "follows", "F", "").ToString(),
				}
				actual := extractTripleFromIterator(ts, it)
				sort.Strings(actual)
				sort.Strings(expected)
				So(actual, ShouldResemble, expected)
			})

			Convey("Mutually and-checkable", func() {
				and := graph.NewAndIterator()
				and.AddSubIterator(ts.GetTripleIterator("s", ts.GetIdFor("B")))
				and.AddSubIterator(it)

				expected := []string{
					graph.MakeTriple("B", "follows", "F", "").ToString(),
				}
				actual := extractTripleFromIterator(ts, and)
				sort.Strings(actual)
				sort.Strings(expected)
				So(actual, ShouldResemble, expected)
			})

		})

		Convey("Can create a predicate iterator", func() {
			it = ts.GetTripleIterator("p", ts.GetIdFor("status"))

			Convey("Containing the right things", func() {
				expected := []string{
					graph.MakeTriple("B", "status", "cool", "status_graph").ToString(),
					graph.MakeTriple("D", "status", "cool", "status_graph").ToString(),
					graph.MakeTriple("G", "status", "cool", "status_graph").ToString(),
				}
				actual := extractTripleFromIterator(ts, it)
				sort.Strings(actual)
				sort.Strings(expected)
				So(actual, ShouldResemble, expected)
			})

		})

		Convey("Can create a provenance iterator", func() {
			it = ts.GetTripleIterator("c", ts.GetIdFor("status_graph"))

			Convey("Containing the right things", func() {
				expected := []string{
					graph.MakeTriple("B", "status", "cool", "status_graph").ToString(),
					graph.MakeTriple("D", "status", "cool", "status_graph").ToString(),
					graph.MakeTriple("G", "status", "cool", "status_graph").ToString(),
				}
				actual := extractTripleFromIterator(ts, it)
				sort.Strings(actual)
				sort.Strings(expected)
				So(actual, ShouldResemble, expected)
			})

			Convey("Can be cross-checked", func() {
				and := graph.NewAndIterator()
				// Order is important
				and.AddSubIterator(ts.GetTripleIterator("s", ts.GetIdFor("B")))
				and.AddSubIterator(it)

				expected := []string{
					graph.MakeTriple("B", "status", "cool", "status_graph").ToString(),
				}
				actual := extractTripleFromIterator(ts, and)
				So(actual, ShouldResemble, expected)
			})

			Convey("Can check against other iterators", func() {
				and := graph.NewAndIterator()
				// Order is important
				and.AddSubIterator(it)
				and.AddSubIterator(ts.GetTripleIterator("s", ts.GetIdFor("B")))

				expected := []string{
					graph.MakeTriple("B", "status", "cool", "status_graph").ToString(),
				}
				actual := extractTripleFromIterator(ts, and)
				So(actual, ShouldResemble, expected)
			})
			Reset(func() {
				it.Reset()
			})

		})

		Reset(func() {
			ts.Close()
		})

	})

}
示例#3
0
func TestIterator(t *testing.T) {
	var ts *LevelDBTripleStore

	Convey("Given a prepared database", t, func() {
		tmpDir, _ := ioutil.TempDir(os.TempDir(), "cayley_test")
		t.Log(tmpDir)
		defer os.RemoveAll(tmpDir)
		ok := CreateNewLevelDB(tmpDir)
		So(ok, ShouldBeTrue)
		ts = NewDefaultLevelDBTripleStore(tmpDir, nil)
		ts.AddTripleSet(makeTripleSet())
		var it graph.Iterator

		Convey("Can create an all iterator for nodes", func() {
			it = ts.GetNodesAllIterator()
			So(it, ShouldNotBeNil)

			Convey("Has basics", func() {
				size, accurate := it.Size()
				So(size, ShouldBeBetween, 0, 20)
				So(accurate, ShouldBeFalse)
				So(it.Type(), ShouldEqual, "all")
				re_it, ok := it.Optimize()
				So(ok, ShouldBeFalse)
				So(re_it, ShouldPointTo, it)
			})

			Convey("Iterates all nodes", func() {
				expected := []string{
					"A",
					"B",
					"C",
					"D",
					"E",
					"F",
					"G",
					"follows",
					"status",
					"cool",
					"status_graph",
				}
				sort.Strings(expected)
				actual := extractValuesFromIterator(ts, it)
				sort.Strings(actual)
				So(actual, ShouldResemble, expected)
				it.Reset()
				actual = extractValuesFromIterator(ts, it)
				sort.Strings(actual)
				So(actual, ShouldResemble, expected)

			})

			Convey("Contains a couple nodes", func() {
				So(it.Check(ts.GetIdFor("A")), ShouldBeTrue)
				So(it.Check(ts.GetIdFor("cool")), ShouldBeTrue)
				//So(it.Check(ts.GetIdFor("baller")), ShouldBeFalse)
			})

			Reset(func() {
				it.Reset()
			})
		})

		Convey("Can create an all iterator for edges", func() {
			it := ts.GetTriplesAllIterator()
			So(it, ShouldNotBeNil)
			Convey("Has basics", func() {
				size, accurate := it.Size()
				So(size, ShouldBeBetween, 0, 20)
				So(accurate, ShouldBeFalse)
				So(it.Type(), ShouldEqual, "all")
				re_it, ok := it.Optimize()
				So(ok, ShouldBeFalse)
				So(re_it, ShouldPointTo, it)
			})

			Convey("Iterates an edge", func() {
				edge_val, _ := it.Next()
				triple := ts.GetTriple(edge_val)
				set := makeTripleSet()
				var string_set []string
				for _, t := range set {
					string_set = append(string_set, t.ToString())
				}
				So(triple.ToString(), ShouldBeIn, string_set)
			})

			Reset(func() {
				ts.Close()
			})
		})
	})

}
示例#4
0
func TestIterator(t *testing.T) {
	tmpDir, err := ioutil.TempDir(os.TempDir(), "cayley_test")
	if err != nil {
		t.Fatalf("Could not create working directory: %v", err)
	}
	defer os.RemoveAll(tmpDir)
	t.Log(tmpDir)

	err = createNewLevelDB(tmpDir, nil)
	if err != nil {
		t.Fatal("Failed to create LevelDB database.")
	}

	qs, err := newTripleStore(tmpDir, nil)
	if qs == nil || err != nil {
		t.Error("Failed to create leveldb TripleStore.")
	}
	qs.AddTripleSet(makeTripleSet())
	var it graph.Iterator

	it = qs.NodesAllIterator()
	if it == nil {
		t.Fatal("Got nil iterator.")
	}

	size, exact := it.Size()
	if size <= 0 || size >= 20 {
		t.Errorf("Unexpected size, got:%d expect:(0, 20)", size)
	}
	if exact {
		t.Errorf("Got unexpected exact result.")
	}
	if typ := it.Type(); typ != graph.All {
		t.Errorf("Unexpected iterator type, got:%v expect:%v", typ, graph.All)
	}
	optIt, changed := it.Optimize()
	if changed || optIt != it {
		t.Errorf("Optimize unexpectedly changed iterator.")
	}

	expect := []string{
		"A",
		"B",
		"C",
		"D",
		"E",
		"F",
		"G",
		"follows",
		"status",
		"cool",
		"status_graph",
	}
	sort.Strings(expect)
	for i := 0; i < 2; i++ {
		got := iteratedNames(qs, it)
		sort.Strings(got)
		if !reflect.DeepEqual(got, expect) {
			t.Errorf("Unexpected iterated result on repeat %d, got:%v expect:%v", i, got, expect)
		}
		it.Reset()
	}

	for _, pq := range expect {
		if !it.Contains(qs.ValueOf(pq)) {
			t.Errorf("Failed to find and check %q correctly", pq)
		}
	}
	// FIXME(kortschak) Why does this fail?
	/*
		for _, pq := range []string{"baller"} {
			if it.Contains(qs.ValueOf(pq)) {
				t.Errorf("Failed to check %q correctly", pq)
			}
		}
	*/
	it.Reset()

	it = qs.TriplesAllIterator()
	edge, _ := graph.Next(it)
	triple := qs.Quad(edge)
	set := makeTripleSet()
	var ok bool
	for _, t := range set {
		if t.String() == triple.String() {
			ok = true
			break
		}
	}
	if !ok {
		t.Errorf("Failed to find %q during iteration, got:%q", triple, set)
	}

	qs.Close()
}
示例#5
0
文件: tests.go 项目: bmatsuo/cayley
// TestQuadStoreNodesAllIterator iterates the nodes in a fixture and asserts
// the result.
func TestQuadStoreNodesAllIterator(t *testing.T, ctx context.Context) {
	qs := ContextQuadStore(ctx)

	_, err := WriteFixtureQuadStore(qs, "simple")
	if err != nil {
		t.Errorf("Unexpected error writing fixures: %v", err)
	}

	var it graph.Iterator
	it = qs.NodesAllIterator()
	if it == nil {
		t.Fatal("Got nil iterator.")
	}
	defer it.Reset()

	size, _ := it.Size()
	if size <= 0 || size >= 20 {
		t.Errorf("Unexpected size, got:%d expect:(0, 20)", size)
	}
	if typ := it.Type(); typ != graph.All {
		t.Errorf("Unexpected iterator type, got:%v expect:%v", typ, graph.All)
	}
	optIt, changed := it.Optimize()
	if changed || optIt != it {
		t.Errorf("Optimize unexpectedly changed iterator.")
	}

	expect := []string{
		"A",
		"B",
		"C",
		"D",
		"E",
		"F",
		"G",
		"follows",
		"status",
		"cool",
		"status_graph",
	}
	sort.Strings(expect)
	for i := 0; i < 2; i++ {
		got := IterateNames(qs, it)
		sort.Strings(got)
		if !reflect.DeepEqual(got, expect) {
			t.Errorf("Unexpected iterated result on repeat %d, got:%v expect:%v", i, got, expect)
		}
		it.Reset()
	}

	for _, pq := range expect {
		if !it.Contains(qs.ValueOf(pq)) {
			t.Errorf("Failed to find and check %q correctly", pq)
		}
	}
	for _, pq := range []string{"baller"} {
		if it.Contains(qs.ValueOf(pq)) {
			t.Errorf("Failed to check %q correctly", pq)
		}
	}
}
示例#6
0
文件: tests.go 项目: bmatsuo/cayley
// TestQuadStoreQuadIteratorAnd tests the QuadIterator method of a
// graph.QuadStore by issuing several queries against a fixture.
func TestQuadStoreQuadIteratorAnd(t *testing.T, ctx context.Context) {
	qs := ContextQuadStore(ctx)

	_, err := WriteFixtureQuadStore(qs, "simple")
	if err != nil {
		t.Errorf("Unexpected error writing fixures: %v", err)
	}

	var tests = []struct {
		dir     quad.Direction
		name    string
		anddir  quad.Direction
		andname string
		expect  []quad.Quad
	}{
		{
			quad.Subject, "C",
			quad.Any, "",
			[]quad.Quad{
				{"C", "follows", "B", ""},
				{"C", "follows", "D", ""},
			},
		},
		{
			quad.Object, "F",
			quad.Subject, "B",
			[]quad.Quad{
				{"B", "follows", "F", ""},
			},
		},
		{
			quad.Predicate, "status",
			quad.Subject, "G",
			[]quad.Quad{
				{"G", "status", "cool", "status_graph"},
			},
		},
		{
			quad.Label, "status_graph",
			quad.Subject, "B",
			[]quad.Quad{
				{"B", "status", "cool", "status_graph"},
			},
		},
	}

	for i, test := range tests {
		func() {
			it := qs.QuadIterator(test.dir, qs.ValueOf(test.name))
			defer it.Reset()
			and := iterator.NewAnd(qs)
			var other graph.Iterator
			if test.anddir == quad.Any {
				other = qs.QuadsAllIterator()
			} else {
				other = qs.QuadIterator(test.anddir, qs.ValueOf(test.andname))
			}
			defer other.Reset()
			and.AddSubIterator(other)
			and.AddSubIterator(it)
			defer and.Reset()

			quads := IterateQuads(qs, and)
			if !reflect.DeepEqual(quads, test.expect) {
				t.Errorf("Test %d: Failed to get expected results, got:%q expect:%q", i, quads, test.expect)
			}
		}()
	}

}
示例#7
0
func TestIterator(t testing.TB, gen DatabaseFunc) {
	qs, opts, closer := gen(t)
	defer closer()

	MakeWriter(t, qs, opts, MakeQuadSet()...)

	var it graph.Iterator

	it = qs.NodesAllIterator()
	require.NotNil(t, it)

	size, _ := it.Size()
	require.True(t, size > 0 && size < 20, "Unexpected size")
	// TODO: leveldb had this test
	//if exact {
	//	t.Errorf("Got unexpected exact result.")
	//}
	require.Equal(t, graph.All, it.Type(), "Unexpected iterator type")

	optIt, changed := it.Optimize()
	require.True(t, !changed && optIt == it, "Optimize unexpectedly changed iterator: %v, %T", changed, optIt)

	expect := []string{
		"A",
		"B",
		"C",
		"D",
		"E",
		"F",
		"G",
		"follows",
		"status",
		"cool",
		"status_graph",
	}
	sort.Strings(expect)
	for i := 0; i < 2; i++ {
		got := IteratedRawStrings(t, qs, it)
		sort.Strings(got)
		require.Equal(t, expect, got, "Unexpected iterated result on repeat %d", i)
		it.Reset()
	}

	for _, pq := range expect {
		require.True(t, it.Contains(qs.ValueOf(quad.Raw(pq))), "Failed to find and check %q correctly", pq)

	}
	// FIXME(kortschak) Why does this fail?
	/*
		for _, pq := range []string{"baller"} {
			if it.Contains(qs.ValueOf(pq)) {
				t.Errorf("Failed to check %q correctly", pq)
			}
		}
	*/
	it.Reset()

	it = qs.QuadsAllIterator()
	optIt, changed = it.Optimize()
	require.True(t, !changed && optIt == it, "Optimize unexpectedly changed iterator: %v, %T", changed, optIt)

	require.True(t, graph.Next(it))

	q := qs.Quad(it.Result())
	require.Nil(t, it.Err())
	require.True(t, q.IsValid(), "Invalid quad returned: %q", q)
	set := MakeQuadSet()
	var ok bool
	for _, e := range set {
		if e.String() == q.String() {
			ok = true
			break
		}
	}
	require.True(t, ok, "Failed to find %q during iteration, got:%q", q, set)
}