|
| 1 | +package schema |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + |
| 8 | + "github.com/authzed/spicedb/internal/datastore/common" |
| 9 | + "github.com/authzed/spicedb/pkg/datastore" |
| 10 | +) |
| 11 | + |
| 12 | +func TestIndexForFilter(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + filter datastore.RelationshipsFilter |
| 16 | + expected string |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "no filter", |
| 20 | + filter: datastore.RelationshipsFilter{}, |
| 21 | + expected: "", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "filter by resource type", |
| 25 | + filter: datastore.RelationshipsFilter{OptionalResourceType: "foo"}, |
| 26 | + expected: "pk_relation_tuple", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "filter by resource type and relation", |
| 30 | + filter: datastore.RelationshipsFilter{ |
| 31 | + OptionalResourceType: "foo", |
| 32 | + OptionalResourceRelation: "bar", |
| 33 | + }, |
| 34 | + expected: "pk_relation_tuple", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "filter by resource type, resource ID and relation", |
| 38 | + filter: datastore.RelationshipsFilter{ |
| 39 | + OptionalResourceType: "foo", |
| 40 | + OptionalResourceIds: []string{"baz"}, |
| 41 | + OptionalResourceRelation: "bar", |
| 42 | + }, |
| 43 | + expected: "pk_relation_tuple", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "filter by subject type, subject ID and relation", |
| 47 | + filter: datastore.RelationshipsFilter{ |
| 48 | + OptionalSubjectsSelectors: []datastore.SubjectsSelector{ |
| 49 | + { |
| 50 | + OptionalSubjectType: "foo", |
| 51 | + OptionalSubjectIds: []string{"bar"}, |
| 52 | + RelationFilter: datastore.SubjectRelationFilter{ |
| 53 | + NonEllipsisRelation: "baz", |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + expected: "ix_relation_tuple_by_subject", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "filter by subject type, subject ID", |
| 62 | + filter: datastore.RelationshipsFilter{ |
| 63 | + OptionalSubjectsSelectors: []datastore.SubjectsSelector{ |
| 64 | + { |
| 65 | + OptionalSubjectType: "foo", |
| 66 | + OptionalSubjectIds: []string{"bar"}, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + expected: "ix_relation_tuple_by_subject", |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "filter by subject relation, subject ID", |
| 74 | + filter: datastore.RelationshipsFilter{ |
| 75 | + OptionalSubjectsSelectors: []datastore.SubjectsSelector{ |
| 76 | + { |
| 77 | + OptionalSubjectIds: []string{"bar"}, |
| 78 | + RelationFilter: datastore.SubjectRelationFilter{ |
| 79 | + NonEllipsisRelation: "baz", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + expected: "ix_relation_tuple_by_subject", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "filter by subject type", |
| 88 | + filter: datastore.RelationshipsFilter{ |
| 89 | + OptionalSubjectsSelectors: []datastore.SubjectsSelector{ |
| 90 | + { |
| 91 | + OptionalSubjectType: "foo", |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + expected: "", |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "filter by resource type and subject type", |
| 99 | + filter: datastore.RelationshipsFilter{ |
| 100 | + OptionalResourceType: "foo", |
| 101 | + OptionalSubjectsSelectors: []datastore.SubjectsSelector{ |
| 102 | + { |
| 103 | + OptionalSubjectType: "foo", |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + expected: "pk_relation_tuple", |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "filter by resource type and subject object ID", |
| 111 | + filter: datastore.RelationshipsFilter{ |
| 112 | + OptionalResourceType: "foo", |
| 113 | + OptionalSubjectsSelectors: []datastore.SubjectsSelector{ |
| 114 | + { |
| 115 | + OptionalSubjectIds: []string{"bar"}, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + expected: "", |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + schema := Schema(common.ColumnOptimizationOptionNone, false, false) |
| 124 | + for _, test := range tests { |
| 125 | + t.Run(test.name, func(t *testing.T) { |
| 126 | + index := IndexForFilter(*schema, test.filter) |
| 127 | + if test.expected == "" { |
| 128 | + require.Nil(t, index) |
| 129 | + } else { |
| 130 | + require.NotNil(t, index) |
| 131 | + require.Equal(t, test.expected, index.Name) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments