github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/tuple/relationship_test.go (about)

     1  package tuple
     2  
     3  import (
     4  	"testing"
     5  
     6  	v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func objRef(typ, id string) *v1.ObjectReference {
    11  	return &v1.ObjectReference{ObjectType: typ, ObjectId: id}
    12  }
    13  
    14  func subRef(typ, id, rel string) *v1.SubjectReference {
    15  	return &v1.SubjectReference{Object: objRef(typ, id), OptionalRelation: rel}
    16  }
    17  
    18  func TestStringObjectRef(t *testing.T) {
    19  	table := []struct {
    20  		ref      *v1.ObjectReference
    21  		expected string
    22  	}{
    23  		// This code assumes input has already been validated.
    24  		{objRef("document", "1"), "document:1"},
    25  		{objRef("", ""), ":"},
    26  		{objRef(":", ":"), ":::"},
    27  	}
    28  	for _, tt := range table {
    29  		require.Equal(t, tt.expected, StringObjectRef(tt.ref))
    30  		require.Equal(t, tt.expected, StringSubjectRef(subRef(tt.ref.ObjectType, tt.ref.ObjectId, "")))
    31  	}
    32  }
    33  
    34  func TestJoinSubjectRef(t *testing.T) {
    35  	table := []struct {
    36  		ref      *v1.SubjectReference
    37  		expected string
    38  	}{
    39  		// This code assumes input has already been validated.
    40  		{subRef("document", "1", ""), "document:1"},
    41  		{subRef("document", "1", "reader"), "document:1#reader"},
    42  	}
    43  	for _, tt := range table {
    44  		require.Equal(t, tt.expected, StringSubjectRef(tt.ref))
    45  	}
    46  }