github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/tuple/onrbytypeset_test.go (about) 1 package tuple 2 3 import ( 4 "sort" 5 "testing" 6 7 core "github.com/authzed/spicedb/pkg/proto/core/v1" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func RR(namespaceName string, relationName string) *core.RelationReference { 13 return &core.RelationReference{ 14 Namespace: namespaceName, 15 Relation: relationName, 16 } 17 } 18 19 func TestONRByTypeSet(t *testing.T) { 20 assertHasObjectIds := func(s *ONRByTypeSet, rr *core.RelationReference, expected []string) { 21 wasFound := false 22 s.ForEachType(func(foundRR *core.RelationReference, objectIds []string) { 23 if rr.EqualVT(foundRR) { 24 sort.Strings(objectIds) 25 require.Equal(t, expected, objectIds) 26 wasFound = true 27 } 28 }) 29 require.True(t, wasFound) 30 } 31 32 set := NewONRByTypeSet() 33 require.True(t, set.IsEmpty()) 34 35 // Add some ONRs. 36 set.Add(ParseONR("document:foo#viewer")) 37 set.Add(ParseONR("document:bar#viewer")) 38 set.Add(ParseONR("team:something#member")) 39 set.Add(ParseONR("team:other#member")) 40 set.Add(ParseONR("team:other#manager")) 41 require.False(t, set.IsEmpty()) 42 43 // Run for each type over the set 44 assertHasObjectIds(set, RR("document", "viewer"), []string{"bar", "foo"}) 45 assertHasObjectIds(set, RR("team", "member"), []string{"other", "something"}) 46 assertHasObjectIds(set, RR("team", "manager"), []string{"other"}) 47 48 // Map 49 mapped, err := set.Map(func(rr *core.RelationReference) (*core.RelationReference, error) { 50 if rr.Namespace == "document" { 51 return RR("doc", rr.Relation), nil 52 } 53 54 return rr, nil 55 }) 56 require.NoError(t, err) 57 58 assertHasObjectIds(mapped, RR("doc", "viewer"), []string{"bar", "foo"}) 59 assertHasObjectIds(mapped, RR("team", "member"), []string{"other", "something"}) 60 assertHasObjectIds(mapped, RR("team", "manager"), []string{"other"}) 61 }