github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/namespace/annotate_test.go (about)

     1  package namespace
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/authzed/spicedb/internal/datastore/memdb"
    10  	"github.com/authzed/spicedb/pkg/schemadsl/compiler"
    11  	"github.com/authzed/spicedb/pkg/schemadsl/input"
    12  	"github.com/authzed/spicedb/pkg/typesystem"
    13  )
    14  
    15  func TestAnnotateNamespace(t *testing.T) {
    16  	require := require.New(t)
    17  
    18  	ds, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
    19  	require.NoError(err)
    20  
    21  	compiled, err := compiler.Compile(compiler.InputSchema{
    22  		Source: input.Source("schema"),
    23  		SchemaString: `definition document {
    24  	relation viewer: document
    25  	relation editor: document
    26  
    27  	permission aliased = viewer
    28  	permission computed = viewer + editor
    29  	permission other = editor - viewer
    30  	permission also_aliased = viewer
    31  }`,
    32  	}, compiler.AllowUnprefixedObjectType())
    33  	require.NoError(err)
    34  
    35  	lastRevision, err := ds.HeadRevision(context.Background())
    36  	require.NoError(err)
    37  
    38  	ts, err := typesystem.NewNamespaceTypeSystem(compiled.ObjectDefinitions[0], typesystem.ResolverForDatastoreReader(ds.SnapshotReader(lastRevision)))
    39  	require.NoError(err)
    40  
    41  	ctx := context.Background()
    42  	vts, terr := ts.Validate(ctx)
    43  	require.NoError(terr)
    44  
    45  	aerr := AnnotateNamespace(vts)
    46  	require.NoError(aerr)
    47  
    48  	require.NotEmpty(ts.MustGetRelation("aliased").AliasingRelation)
    49  	require.NotEmpty(ts.MustGetRelation("also_aliased").AliasingRelation)
    50  	require.Empty(ts.MustGetRelation("computed").AliasingRelation)
    51  	require.Empty(ts.MustGetRelation("other").AliasingRelation)
    52  
    53  	require.NotEmpty(ts.MustGetRelation("also_aliased").CanonicalCacheKey)
    54  	require.NotEmpty(ts.MustGetRelation("aliased").CanonicalCacheKey)
    55  	require.NotEmpty(ts.MustGetRelation("computed").CanonicalCacheKey)
    56  	require.NotEmpty(ts.MustGetRelation("other").CanonicalCacheKey)
    57  }