1
|
|
|
package context |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"testing" |
5
|
|
|
|
6
|
|
|
"github.com/Permify/permify/pkg/database" |
7
|
|
|
base "github.com/Permify/permify/pkg/pb/base/v1" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
func TestQueryAttributes1(t *testing.T) { |
11
|
|
|
attributes := []*base.Attribute{ |
12
|
|
|
{Entity: &base.Entity{Type: "type1", Id: "1"}, Attribute: "attribute1", Value: nil}, |
13
|
|
|
{Entity: &base.Entity{Type: "type3", Id: "3"}, Attribute: "attribute2", Value: nil}, |
14
|
|
|
{Entity: &base.Entity{Type: "type5", Id: "5"}, Attribute: "attribute3", Value: nil}, |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
contextualAttributes := NewContextualAttributes(attributes...) |
18
|
|
|
filter := &base.AttributeFilter{Entity: &base.EntityFilter{Type: "type1", Ids: []string{"1"}}, Attributes: []string{"attribute1"}} |
19
|
|
|
|
20
|
|
|
iterator, err := contextualAttributes.QueryAttributes(filter, database.NewCursorPagination()) |
21
|
|
|
if err != nil { |
22
|
|
|
t.Errorf("Unexpected error: %v", err) |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
if !iterator.HasNext() { |
26
|
|
|
t.Errorf("Expected at least one attribute, got none") |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
filteredAttribute := iterator.GetNext() |
30
|
|
|
if filteredAttribute.Entity.Type != "type1" || filteredAttribute.Entity.Id != "1" || filteredAttribute.Attribute != "attribute1" { |
31
|
|
|
t.Errorf("Unexpected attribute: %+v", filteredAttribute) |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if iterator.HasNext() { |
35
|
|
|
t.Errorf("Expected exactly one attribute, got more") |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
func TestQueryAttributes2(t *testing.T) { |
40
|
|
|
attributes := []*base.Attribute{ |
41
|
|
|
{Entity: &base.Entity{Type: "type1", Id: "1"}, Attribute: "attribute1", Value: nil}, |
42
|
|
|
{Entity: &base.Entity{Type: "type3", Id: "3"}, Attribute: "attribute2", Value: nil}, |
43
|
|
|
{Entity: &base.Entity{Type: "type5", Id: "5"}, Attribute: "attribute3", Value: nil}, |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
contextualAttributes := NewContextualAttributes(attributes...) |
47
|
|
|
filter := &base.AttributeFilter{Entity: &base.EntityFilter{Type: "type3", Ids: []string{"3"}}, Attributes: []string{"attribute1", "attribute2"}} |
48
|
|
|
|
49
|
|
|
iterator, err := contextualAttributes.QueryAttributes(filter, database.NewCursorPagination()) |
50
|
|
|
if err != nil { |
51
|
|
|
t.Errorf("Unexpected error: %v", err) |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if !iterator.HasNext() { |
55
|
|
|
t.Errorf("Expected at least one attributes, got none") |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
filteredTuple := iterator.GetNext() |
59
|
|
|
if filteredTuple.Entity.Type != "type3" || filteredTuple.Entity.Id != "3" || filteredTuple.Attribute != "attribute2" { |
60
|
|
|
t.Errorf("Unexpected attribute: %+v", filteredTuple) |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if iterator.HasNext() { |
64
|
|
|
t.Errorf("Expected exactly one attribute, got more") |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
func TestQueryAttributes3(t *testing.T) { |
69
|
|
|
attributes := []*base.Attribute{ |
70
|
|
|
{Entity: &base.Entity{Type: "type1", Id: "1"}, Attribute: "attribute1", Value: nil}, |
71
|
|
|
{Entity: &base.Entity{Type: "type3", Id: "3"}, Attribute: "attribute2", Value: nil}, |
72
|
|
|
{Entity: &base.Entity{Type: "type5", Id: "5"}, Attribute: "attribute3", Value: nil}, |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
contextualAttributes := NewContextualAttributes(attributes...) |
76
|
|
|
filter := &base.AttributeFilter{Entity: &base.EntityFilter{Type: "type3", Ids: []string{"3"}}, Attributes: []string{"attribute1", "attribute2"}} |
77
|
|
|
|
78
|
|
|
attr, err := contextualAttributes.QuerySingleAttribute(filter) |
79
|
|
|
if err != nil { |
80
|
|
|
t.Errorf("Unexpected error: %v", err) |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if attr.Entity.Type != "type3" || attr.Entity.Id != "3" || attr.Attribute != "attribute2" { |
84
|
|
|
t.Errorf("Unexpected attribute: %+v", attr) |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
func TestQueryAttributes4(t *testing.T) { |
89
|
|
|
attributes := []*base.Attribute{ |
90
|
|
|
{Entity: &base.Entity{Type: "type1", Id: "1"}, Attribute: "attribute1", Value: nil}, |
91
|
|
|
{Entity: &base.Entity{Type: "type3", Id: "3"}, Attribute: "attribute2", Value: nil}, |
92
|
|
|
{Entity: &base.Entity{Type: "type5", Id: "5"}, Attribute: "attribute3", Value: nil}, |
93
|
|
|
{Entity: &base.Entity{Type: "type5", Id: "4"}, Attribute: "attribute4", Value: nil}, |
94
|
|
|
{Entity: &base.Entity{Type: "type5", Id: "12"}, Attribute: "attribute12", Value: nil}, |
95
|
|
|
{Entity: &base.Entity{Type: "type5", Id: "22"}, Attribute: "attribute22", Value: nil}, |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
contextualAttributes := NewContextualAttributes(attributes...) |
99
|
|
|
filter := &base.AttributeFilter{Entity: &base.EntityFilter{Type: "type5", Ids: []string{}}, Attributes: []string{}} |
100
|
|
|
|
101
|
|
|
iterator, err := contextualAttributes.QueryAttributes(filter, database.NewCursorPagination(database.Cursor("MjI="), database.Sort("entity_id"))) |
102
|
|
|
if err != nil { |
103
|
|
|
t.Errorf("Unexpected error: %v", err) |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if !iterator.HasNext() { |
107
|
|
|
t.Errorf("Expected at least one attribute, got none") |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
filteredAttribute := iterator.GetNext() |
111
|
|
|
if filteredAttribute.Entity.Type != "type5" || filteredAttribute.Entity.Id != "22" || filteredAttribute.Attribute != "attribute22" { |
112
|
|
|
t.Errorf("Unexpected attribute: %+v", filteredAttribute) |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if !iterator.HasNext() { |
116
|
|
|
t.Errorf("Expected at least one attribute, got none") |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
filteredAttribute2 := iterator.GetNext() |
120
|
|
|
if filteredAttribute2.Entity.Type != "type5" || filteredAttribute2.Entity.Id != "4" || filteredAttribute2.Attribute != "attribute4" { |
121
|
|
|
t.Errorf("Unexpected attribute: %+v", filteredAttribute2) |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if !iterator.HasNext() { |
125
|
|
|
t.Errorf("Expected at least one attribute, got none") |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
filteredAttribute3 := iterator.GetNext() |
129
|
|
|
if filteredAttribute3.Entity.Type != "type5" || filteredAttribute3.Entity.Id != "5" || filteredAttribute3.Attribute != "attribute3" { |
130
|
|
|
t.Errorf("Unexpected attribute: %+v", filteredAttribute3) |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|