github.com/cayleygraph/cayley@v0.7.7/schema/namespaces_test.go (about)

     1  package schema_test
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/cayleygraph/cayley/graph"
    10  	"github.com/cayleygraph/cayley/graph/memstore"
    11  	"github.com/cayleygraph/cayley/schema"
    12  	"github.com/cayleygraph/quad"
    13  	"github.com/cayleygraph/quad/voc"
    14  )
    15  
    16  func TestSaveNamespaces(t *testing.T) {
    17  	sch := schema.NewConfig()
    18  	save := []voc.Namespace{
    19  		{Full: "http://example.org/", Prefix: "ex:"},
    20  		{Full: "http://cayley.io/", Prefix: "c:"},
    21  	}
    22  	var ns voc.Namespaces
    23  	for _, n := range save {
    24  		ns.Register(n)
    25  	}
    26  	qs := memstore.New()
    27  	err := sch.WriteNamespaces(qs, &ns)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	var ns2 voc.Namespaces
    32  	err = sch.LoadNamespaces(context.TODO(), qs, &ns2)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	got := ns2.List()
    37  	sort.Sort(voc.ByFullName(save))
    38  	sort.Sort(voc.ByFullName(got))
    39  	if !reflect.DeepEqual(save, got) {
    40  		t.Fatalf("wrong namespaces returned: got: %v, expect: %v", got, save)
    41  	}
    42  	qr := graph.NewQuadStoreReader(qs)
    43  	q, err := quad.ReadAll(qr)
    44  	qr.Close()
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	expect := []quad.Quad{
    49  		quad.MakeIRI("http://cayley.io/", "cayley:prefix", "c:", ""),
    50  		quad.MakeIRI("http://cayley.io/", "rdf:type", "cayley:namespace", ""),
    51  
    52  		quad.MakeIRI("http://example.org/", "cayley:prefix", "ex:", ""),
    53  		quad.MakeIRI("http://example.org/", "rdf:type", "cayley:namespace", ""),
    54  	}
    55  	sort.Sort(quad.ByQuadString(expect))
    56  	sort.Sort(quad.ByQuadString(q))
    57  	if !reflect.DeepEqual(expect, q) {
    58  		t.Fatalf("wrong quads returned: got: %v, expect: %v", q, expect)
    59  	}
    60  }