Пример #1
0
func TestCellString(t *testing.T) {
	now := time.Now()
	n := node.NewBlankNode()
	p, err := predicate.NewImmutable("foo")
	if err != nil {
		t.Fatalf("failed to create predicate with error %v", err)
	}
	l, err := literal.DefaultBuilder().Parse(`"true"^^type:bool`)
	if err != nil {
		t.Fatalf("failed to create literal with error %v", err)
	}
	testTable := []struct {
		c    *Cell
		want string
	}{
		{c: &Cell{S: "foo"}, want: `foo`},
		{c: &Cell{N: n}, want: n.String()},
		{c: &Cell{P: p}, want: p.String()},
		{c: &Cell{L: l}, want: l.String()},
		{c: &Cell{T: &now}, want: now.Format(time.RFC3339Nano)},
	}
	for _, entry := range testTable {
		if got := entry.c.String(); got != entry.want {
			t.Errorf("Cell.String failed to return the right string; got %q, want %q", got, entry.want)
		}
	}
}
Пример #2
0
func TestDefaultLookupChecker(t *testing.T) {
	dlu := storage.DefaultLookup
	c := newChecker(dlu)
	ip, tp := predicate.NewImmutable("foo"), predicate.NewTemporal("bar", time.Now())
	if !c.CheckAndUpdate(ip) {
		t.Errorf("Immutable predicates should always validate with default lookup %v", dlu)
	}
	if !c.CheckAndUpdate(tp) {
		t.Errorf("Temporal predicates should always validate with default lookup %v", dlu)
	}
}
Пример #3
0
func TestLimitedItemsLookupChecker(t *testing.T) {
	blu := &storage.LookupOptions{MaxElements: 1}
	c := newChecker(blu)
	ip := predicate.NewImmutable("foo")
	if !c.CheckAndUpdate(ip) {
		t.Errorf("The first predicate should always succeeed on bounded lookup %v", blu)
	}
	for i := 0; i < 10; i++ {
		if c.CheckAndUpdate(ip) {
			t.Errorf("Bounded lookup %v should never succeed after being exahausted", blu)
		}
	}
}
Пример #4
0
// Reify given the current triple it returns the original triple and the newly
// reified ones. It also returns the newly created blank node.
func (t *Triple) Reify() ([]*Triple, *node.Node, error) {
	// Function that create the proper reification predicates.
	rp := func(id string, p *predicate.Predicate) (*predicate.Predicate, error) {
		if p.Type() == predicate.Temporal {
			ta, _ := p.TimeAnchor()
			return predicate.NewTemporal(string(p.ID()), *ta)
		}
		return predicate.NewImmutable(id)
	}

	fmt.Println(t.String())
	b := node.NewBlankNode()
	s, err := rp("_subject", t.p)
	if err != nil {
		return nil, nil, err
	}
	ts, _ := NewTriple(b, s, NewNodeObject(t.s))
	p, err := rp("_predicate", t.p)
	if err != nil {
		return nil, nil, err
	}
	tp, _ := NewTriple(b, p, NewPredicateObject(t.p))
	var to *Triple
	if t.o.l != nil {
		o, err := rp("_object", t.p)
		if err != nil {
			return nil, nil, err
		}
		to, _ = NewTriple(b, o, NewLiteralObject(t.o.l))
	}
	if t.o.n != nil {
		o, err := rp("_object", t.p)
		if err != nil {
			return nil, nil, err
		}
		to, _ = NewTriple(b, o, NewNodeObject(t.o.n))
	}
	if t.o.p != nil {
		o, err := rp("_object", t.p)
		if err != nil {
			return nil, nil, err
		}
		to, _ = NewTriple(b, o, NewPredicateObject(t.o.p))
	}

	return []*Triple{t, ts, tp, to}, b, nil
}
Пример #5
0
// NewRandomGraph creates a new random graph generator.
func NewRandomGraph(n int) (generator.Generator, error) {
	if n < 1 {
		return nil, fmt.Errorf("invalid number of nodes %d<1", n)
	}
	nt, err := node.NewType("/gn")
	if err != nil {
		return nil, err
	}
	p, err := predicate.NewImmutable("follow")
	if err != nil {
		return nil, err
	}
	return &randomGraph{
		nodes:     n,
		nodeType:  nt,
		predicate: p,
	}, nil
}
Пример #6
0
// New creates a new tree generator. The triples are generated using breadth
// search first. All predicates are immutable and use the predicate
// `"parent_of"@[]`.`
func New(branch int) (generator.Generator, error) {
	if branch < 1 {
		return nil, fmt.Errorf("invalid branch factor %d", branch)
	}
	nt, err := node.NewType("/tn")
	if err != nil {
		return nil, err
	}
	p, err := predicate.NewImmutable("parent_of")
	if err != nil {
		return nil, err
	}
	return &treeGenerator{
		branch:    branch,
		nodeType:  nt,
		predicate: p,
	}, nil
}