Example #1
0
func populateBenchmarkStore(b *testing.B) storage.Store {
	s, ctx := memory.NewStore(), context.Background()
	g, err := s.NewGraph(ctx, "?test")
	if err != nil {
		b.Fatalf("memory.NewGraph failed to create \"?test\" with error %v", err)
	}
	buf := bytes.NewBufferString(testTriples)
	if _, err := io.ReadIntoGraph(ctx, g, buf, literal.DefaultBuilder()); err != nil {
		b.Fatalf("io.ReadIntoGraph failed to read test graph with error %v", err)
	}
	trpls := make(chan *triple.Triple)
	go func() {
		if err := g.Triples(ctx, trpls); err != nil {
			b.Fatal(err)
		}
	}()
	cnt := 0
	for _ = range trpls {
		cnt++
	}
	if got, want := cnt, len(strings.Split(testTriples, "\n"))-1; got != want {
		b.Fatalf("Failed to import all test triples; got %v, want %v", got, want)
	}
	return s
}
Example #2
0
// Registers the available drivers.
func registerDrivers() {
	registeredDrivers = map[string]common.StoreGenerator{
		// Memory only storage driver.
		"VOLATILE": func() (storage.Store, error) {
			return memory.NewStore(), nil
		},
	}
}
Example #3
0
func TestSerializationContents(t *testing.T) {
	var buffer bytes.Buffer
	ts, ctx := getTestTriples(t), context.Background()

	g, err := memory.NewStore().NewGraph(ctx, "test")
	if err != nil {
		t.Fatalf("memory.NewStore().NewGraph should have never failed to create a graph")
	}
	if err := g.AddTriples(ctx, ts); err != nil {
		t.Errorf("storage.AddTriples should have not fail to add triples %v with error %v", ts, err)
	}
	// Serialize to a buffer.
	cnt, err := WriteGraph(ctx, &buffer, g)
	if err != nil {
		t.Errorf("io.WriteGraph failed to read %s with error %v", buffer.String(), err)
	}
	if cnt != 6 {
		t.Errorf("io.WriteGraph should have been able to write 6 triples not %d", cnt)
	}
	// Deserialize from a buffer.
	g2, err := memory.DefaultStore.NewGraph(ctx, "test2")
	if err != nil {
		t.Fatalf("memory.DefaultStore.NewGraph should have never failed to create a graph")
	}
	cnt2, err := ReadIntoGraph(ctx, g2, &buffer, literal.DefaultBuilder())
	if err != nil {
		t.Errorf("io.readIntoGraph failed to read %s with error %v", buffer.String(), err)
	}
	if cnt2 != 6 {
		t.Errorf("io.readIntoGraph should have been able to read 6 triples not %d", cnt2)
	}
	// Check the graphs are equal
	m := make(map[string]bool)
	gs := 0
	gtpls, err := g.Triples(ctx)
	if err != nil {
		t.Errorf("g.Triples failed to retrieve triples with error %v", err)
	}
	for trpl := range gtpls {
		m[trpl.GUID()] = true
		gs++
	}
	gos := 0
	g2tpls, err := g2.Triples(ctx)
	if err != nil {
		t.Errorf("g2.Triples failed to retrieve triples with error %v", err)
	}
	for trpl := range g2tpls {
		if _, ok := m[trpl.GUID()]; !ok {
			t.Errorf("Failed to unmarshal marshaled triple; could not find triple %s", trpl.String())
		}
		gos++
	}
	if gs != gos || gs != 6 || gos != 6 {
		t.Errorf("Failed to unmarshal marshaled the right number of triples, %d != %d != 6", gs, gos)
	}
}
Example #4
0
func populateTestStore(t *testing.T) storage.Store {
	s := memory.NewStore()
	g, err := s.NewGraph("?test_graph")
	if err != nil {
		t.Fatalf("memory.NewGraph failed to create \"?test_graph\" with error %v", err)
	}
	b := bytes.NewBufferString(testTriples)
	if _, err := io.ReadIntoGraph(g, b, literal.DefaultBuilder()); err != nil {
		t.Fatalf("io.ReadIntoGraph failed to read test graph with error %v", err)
	}
	return s
}
Example #5
0
func getTestStore(t *testing.T, trpls []string) storage.Store {
	ts, ctx := getTestTriples(t, trpls), context.Background()
	s := memory.NewStore()
	g, err := s.NewGraph(ctx, "?test")
	if err != nil {
		t.Fatal(err)
	}
	if err := g.AddTriples(ctx, ts); err != nil {
		t.Fatalf("g.AddTriples(_) failed failed to add test triples with error %v", err)
	}
	return s
}
Example #6
0
func getTestStore(t *testing.T) storage.Store {
	ts := getTestTriples(t)
	s := memory.NewStore()
	g, err := s.NewGraph("?test")
	if err != nil {
		t.Fatal(err)
	}
	if err := g.AddTriples(ts); err != nil {
		t.Fatalf("g.AddTriples(_) failed failed to add test triples with error %v", err)
	}
	return s
}
Example #7
0
// assertCommand runs all the BQL statements available in the file.
func assertCommand(cmd *Command, args []string) int {
	if len(args) < 3 {
		fmt.Fprintf(os.Stderr, "Missing required folder path. ")
		cmd.Usage()
		return 2
	}
	// Open the folder.
	folder := strings.TrimSpace(args[2])
	f, err := os.Open(folder)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to open folder %s\n\n\t%v\n\n", folder, err)
		return 2
	}
	fis, err := f.Readdir(0)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to read folder %s\n\n\t%v\n\n", folder, err)
		return 2
	}
	fmt.Printf("Processing folder %q...\n\n", folder)
	for _, fi := range fis {
		if !strings.Contains(fi.Name(), "json") {
			continue
		}
		fmt.Println("-------------------------------------------------------------")
		fmt.Printf("Processing file %q...\n\n", fi.Name())
		lns, err := readLines(path.Join(folder, fi.Name()))
		if err != nil {
			fmt.Fprintf(os.Stderr, "\n\n\tFailed to read file content with error %v\n\n", err)
			return 2
		}
		rawStory := strings.Join(lns, "\n")
		s := &compliance.Story{}
		if err := s.Unmarshal(rawStory); err != nil {
			fmt.Fprintf(os.Stderr, "\n\n\tFailed to unmarshal story with error %v\n\n", err)
			return 2
		}
		m, err := s.Run(memory.NewStore(), literal.DefaultBuilder())
		if err != nil {
			fmt.Fprintf(os.Stderr, "\n\n\tFailed to run story %q with error %v\n\n", s.Name, err)
			return 2
		}
		for aid, aido := range m {
			if aido.Equal {
				fmt.Printf("%s [TRUE]\n", aid)
			} else {
				fmt.Printf("%s [FALSE]\n\nGot:\n\n%s\nWant:\n\n%s\n", aid, aido.Got, aido.Want)
			}
		}
	}
	fmt.Println("-------------------------------------------------------------")
	fmt.Println("\ndone")
	return 0
}
Example #8
0
func TestTreeTraversalToRoot(t *testing.T) {
	// Graph traversal data.
	traversalTriples := `/person<Gavin Belson>  "born in"@[]    /city<Springfield>
		/person<Gavin Belson>  "parent of"@[]  /person<Peter Belson>
		/person<Gavin Belson>  "parent of"@[]  /person<Mary Belson>
		/person<Mary Belson>   "parent of"@[]  /person<Amy Schumer>
		/person<Mary Belson>   "parent of"@[]  /person<Joe Schumer>`

	traversalQuery := `SELECT ?grandparent
		                 FROM ?test
										 WHERE {
										   ?s "parent of"@[] /person<Amy Schumer> .
											 ?grandparent "parent of"@[] ?s
										 };`

	// Load traversing data
	s, ctx := memory.NewStore(), context.Background()
	g, gErr := s.NewGraph(ctx, "?test")
	if gErr != nil {
		t.Fatalf("memory.NewGraph failed to create \"?test\" with error %v", gErr)
	}
	b := bytes.NewBufferString(traversalTriples)
	if _, err := io.ReadIntoGraph(ctx, g, b, literal.DefaultBuilder()); err != nil {
		t.Fatalf("io.ReadIntoGraph failed to read test graph with error %v", err)
	}
	p, pErr := grammar.NewParser(grammar.SemanticBQL())
	if pErr != nil {
		t.Fatalf("grammar.NewParser: should have produced a valid BQL parser with error %v", pErr)
	}
	st := &semantic.Statement{}
	if err := p.Parse(grammar.NewLLk(traversalQuery, 1), st); err != nil {
		t.Errorf("Parser.consume: failed to parse query %q with error %v", traversalQuery, err)
	}
	plnr, err := New(ctx, s, st, 0)
	if err != nil {
		t.Errorf("planner.New failed to create a valid query plan with error %v", err)
	}
	tbl, err := plnr.Execute(ctx)
	if err != nil {
		t.Errorf("planner.Excecute failed for query %q with error %v", traversalQuery, err)
	}
	if got, want := len(tbl.Bindings()), 1; got != want {
		t.Errorf("tbl.Bindings returned the wrong number of bindings for %q; got %d, want %d", traversalQuery, got, want)
	}
	if got, want := len(tbl.Rows()), 1; got != want {
		t.Errorf("planner.Excecute failed to return the expected number of rows for query %q; got %d want %d\nGot:\n%v\n", traversalQuery, got, want, tbl)
	}
}
Example #9
0
func TestWriteIntoGraph(t *testing.T) {
	var buffer bytes.Buffer
	ts, ctx := getTestTriples(t), context.Background()
	g, err := memory.NewStore().NewGraph(ctx, "test")
	if err != nil {
		t.Fatalf("memory.NewStore().NewGraph should have never failed to create a graph")
	}
	if err := g.AddTriples(ctx, ts); err != nil {
		t.Errorf("storage.AddTriples should have not fail to add triples %v with error %v", ts, err)
	}
	cnt, err := WriteGraph(ctx, &buffer, g)
	if err != nil {
		t.Errorf("io.WriteGraph failed to read %s with error %v", buffer.String(), err)
	}
	if cnt != 6 {
		t.Errorf("io.WriteGraph should have been able to write 6 triples not %d", cnt)
	}
}
Example #10
0
func populateTestStore(t *testing.T) storage.Store {
	s := memory.NewStore()
	g, err := s.NewGraph("?test")
	if err != nil {
		t.Fatalf("memory.NewGraph failed to create \"?test\" with error %v", err)
	}
	b := bytes.NewBufferString(testTriples)
	if _, err := io.ReadIntoGraph(g, b, literal.DefaultBuilder()); err != nil {
		t.Fatalf("io.ReadIntoGraph failed to read test graph with error %v", err)
	}
	tpls, err := g.Triples()
	if err != nil {
		t.Fatal(err)
	}
	cnt := 0
	for _ = range tpls {
		cnt++
	}
	if got, want := cnt, len(strings.Split(testTriples, "\n"))-2; got != want {
		t.Fatalf("Failed to import all test triples; got %v, want %v", got, want)
	}
	return s
}
Example #11
0
func TestRun(t *testing.T) {
	testStories := []*Story{
		{
			Name: "First Story",
			Sources: []*Graph{
				{
					ID: "?g",
					Facts: []string{
						"/t<id> \"predicate\"@[] /foo<bar>",
					},
				},
			},
			Assertions: []*Assertion{
				{
					Requires:  "retriving the types",
					Statement: "SELECT ?type FROM ?g WHERE {/t<id> \"predicate\"@[] /foo<bar> TYPE ?type};",
					WillFail:  false,
					MustReturn: []map[string]string{
						{"?type": "/foo"},
					},
				},
			},
		},
		{
			Name: "Second Story",
			Sources: []*Graph{
				{
					ID: "?g",
					Facts: []string{
						"/t<id> \"predicate\"@[] /foo<bar>",
					},
				},
			},
			Assertions: []*Assertion{
				{
					Requires:  "retrieving the object",
					Statement: "SELECT ?o FROM ?g WHERE {/t<id> \"predicate\"@[] ?o};",
					WillFail:  true,
					MustReturn: []map[string]string{
						{"?o": "/foo<bar>"},
					},
				},
			},
		},
		{
			Name: "Third Story",
			Sources: []*Graph{
				{
					ID: "?g",
					Facts: []string{
						"/t<id> \"predicate\"@[] /foo<bar>",
						"/t<id> \"predicate\"@[] /foo<bar2>",
					},
				},
			},
			Assertions: []*Assertion{
				{
					Requires:  "retrieving the objects",
					Statement: "SELECT ?o FROM ?g WHERE {/t<id> \"predicate\"@[] ?o} ORDER BY ?o DESC;",
					WillFail:  true,
					MustReturn: []map[string]string{
						{"?o": "/foo<bar>"},
						{"?o": "/foo<bar2>"},
					},
				},
			},
		},
	}
	ctx := context.Background()
	for _, s := range testStories {
		m, err := s.Run(ctx, memory.NewStore(), literal.DefaultBuilder())
		if err != nil {
			t.Error(err)
		}
		for s, sao := range m {
			if !sao.Equal {
				t.Errorf("%q should have not returned false; got\n%s\nwant\n%s", s, sao.Got, sao.Want)
			}
		}
	}
}