github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/tuple/onrset_test.go (about) 1 package tuple 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 core "github.com/authzed/spicedb/pkg/proto/core/v1" 9 ) 10 11 func TestONRSet(t *testing.T) { 12 set := NewONRSet() 13 require.True(t, set.IsEmpty()) 14 require.Equal(t, uint64(0), set.Length()) 15 16 require.True(t, set.Add(ParseONR("resource:1#viewer"))) 17 require.False(t, set.IsEmpty()) 18 require.Equal(t, uint64(1), set.Length()) 19 20 require.True(t, set.Add(ParseONR("resource:2#viewer"))) 21 require.True(t, set.Add(ParseONR("resource:3#viewer"))) 22 require.Equal(t, uint64(3), set.Length()) 23 24 require.False(t, set.Add(ParseONR("resource:1#viewer"))) 25 require.True(t, set.Add(ParseONR("resource:1#editor"))) 26 27 require.True(t, set.Has(ParseONR("resource:1#viewer"))) 28 require.True(t, set.Has(ParseONR("resource:1#editor"))) 29 require.False(t, set.Has(ParseONR("resource:1#owner"))) 30 require.False(t, set.Has(ParseONR("resource:1#admin"))) 31 require.False(t, set.Has(ParseONR("resource:1#reader"))) 32 33 require.True(t, set.Has(ParseONR("resource:2#viewer"))) 34 } 35 36 func TestONRSetUpdate(t *testing.T) { 37 set := NewONRSet() 38 set.Update([]*core.ObjectAndRelation{ 39 ParseONR("resource:1#viewer"), 40 ParseONR("resource:2#viewer"), 41 ParseONR("resource:3#viewer"), 42 }) 43 require.Equal(t, uint64(3), set.Length()) 44 45 set.Update([]*core.ObjectAndRelation{ 46 ParseONR("resource:1#viewer"), 47 ParseONR("resource:1#editor"), 48 ParseONR("resource:1#owner"), 49 ParseONR("resource:1#admin"), 50 ParseONR("resource:1#reader"), 51 }) 52 require.Equal(t, uint64(7), set.Length()) 53 } 54 55 func TestONRSetIntersect(t *testing.T) { 56 set1 := NewONRSet() 57 set1.Update([]*core.ObjectAndRelation{ 58 ParseONR("resource:1#viewer"), 59 ParseONR("resource:2#viewer"), 60 ParseONR("resource:3#viewer"), 61 }) 62 63 set2 := NewONRSet() 64 set2.Update([]*core.ObjectAndRelation{ 65 ParseONR("resource:1#viewer"), 66 ParseONR("resource:1#editor"), 67 ParseONR("resource:1#owner"), 68 ParseONR("resource:1#admin"), 69 ParseONR("resource:2#viewer"), 70 ParseONR("resource:1#reader"), 71 }) 72 73 require.Equal(t, uint64(2), set1.Intersect(set2).Length()) 74 require.Equal(t, uint64(2), set2.Intersect(set1).Length()) 75 } 76 77 func TestONRSetSubtract(t *testing.T) { 78 set1 := NewONRSet() 79 set1.Update([]*core.ObjectAndRelation{ 80 ParseONR("resource:1#viewer"), 81 ParseONR("resource:2#viewer"), 82 ParseONR("resource:3#viewer"), 83 }) 84 85 set2 := NewONRSet() 86 set2.Update([]*core.ObjectAndRelation{ 87 ParseONR("resource:1#viewer"), 88 ParseONR("resource:1#editor"), 89 ParseONR("resource:1#owner"), 90 ParseONR("resource:1#admin"), 91 ParseONR("resource:2#viewer"), 92 ParseONR("resource:1#reader"), 93 }) 94 95 require.Equal(t, uint64(1), set1.Subtract(set2).Length()) 96 require.Equal(t, uint64(4), set2.Subtract(set1).Length()) 97 } 98 99 func TestONRSetUnion(t *testing.T) { 100 set1 := NewONRSet() 101 set1.Update([]*core.ObjectAndRelation{ 102 ParseONR("resource:1#viewer"), 103 ParseONR("resource:2#viewer"), 104 ParseONR("resource:3#viewer"), 105 }) 106 107 set2 := NewONRSet() 108 set2.Update([]*core.ObjectAndRelation{ 109 ParseONR("resource:1#viewer"), 110 ParseONR("resource:1#editor"), 111 ParseONR("resource:1#owner"), 112 ParseONR("resource:1#admin"), 113 ParseONR("resource:2#viewer"), 114 ParseONR("resource:1#reader"), 115 }) 116 117 require.Equal(t, uint64(7), set1.Union(set2).Length()) 118 require.Equal(t, uint64(7), set2.Union(set1).Length()) 119 } 120 121 func TestONRSetWith(t *testing.T) { 122 set1 := NewONRSet() 123 set1.Update([]*core.ObjectAndRelation{ 124 ParseONR("resource:1#viewer"), 125 ParseONR("resource:2#viewer"), 126 ParseONR("resource:3#viewer"), 127 }) 128 129 added := set1.With(ParseONR("resource:1#editor")) 130 require.Equal(t, uint64(3), set1.Length()) 131 require.Equal(t, uint64(4), added.Length()) 132 } 133 134 func TestONRSetAsSlice(t *testing.T) { 135 set := NewONRSet() 136 set.Update([]*core.ObjectAndRelation{ 137 ParseONR("resource:1#viewer"), 138 ParseONR("resource:2#viewer"), 139 ParseONR("resource:3#viewer"), 140 }) 141 142 require.Equal(t, 3, len(set.AsSlice())) 143 }