forked from CyCoreSystems/ari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_test.go
97 lines (80 loc) · 2.5 KB
/
key_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package ari
import "testing"
func TestKeyMatch(t *testing.T) {
// two empty keys should match
ok := NewKey("", "").Match(NewKey("", ""))
if !ok {
t.Errorf("Two empty keys should match")
}
ok = AppKey("app").Match(NewKey("", ""))
if !ok {
t.Errorf("App key should match any subkey")
}
ok = AppKey("app").Match(AppKey("app2"))
if ok {
t.Errorf("Two separate app keys should not match")
}
ok = NodeKey("app", "node").Match(NewKey("", ""))
if !ok {
t.Errorf("Node key should match any subkey")
}
ok = NewKey("application", "id1").Match(NewKey("application", "id1"))
if !ok {
t.Errorf("Application/id1 should match")
}
ok = NewKey("application", "").Match(NewKey("application", "id1"))
if !ok {
t.Errorf("Application/* should match application/id")
}
ok = NewKey("channel", "id1").Match(NewKey("channel", "id2"))
if ok {
t.Errorf("Differing IDs should not match")
}
}
func TestKeysFilter(t *testing.T) {
keys := Keys{
NewKey(ApplicationKey, "app1"),
NewKey(ChannelKey, "ch1"),
NewKey(BridgeKey, "br1"),
}
newKeys := keys.Filter(KindKey(ApplicationKey))
if len(newKeys) != 1 {
t.Errorf("Expected filters keys by app to be of length 1, got %d", len(newKeys))
} else {
if newKeys[0].Kind != ApplicationKey && newKeys[0].ID != "app1" {
t.Errorf("Unexpected first index %v", newKeys[0])
}
}
newKeys = keys.Without(KindKey(ApplicationKey))
if len(newKeys) != 2 {
t.Errorf("Expected without keys by app to be of length 2, got %d", len(newKeys))
} else {
if newKeys[0].Kind != ChannelKey && newKeys[0].ID != "ch1" {
t.Errorf("Unexpected first index %v", newKeys[0])
}
if newKeys[1].Kind != BridgeKey && newKeys[1].ID != "br1" {
t.Errorf("Unexpected second index %v", newKeys[1])
}
}
newKeys = keys.Filter(KindKey(ChannelKey), KindKey(BridgeKey))
if len(newKeys) != 2 {
t.Errorf("Expected without keys by app to be of length 2, got %d", len(newKeys))
} else {
if newKeys[0].Kind != ChannelKey && newKeys[0].ID != "ch1" {
t.Errorf("Unexpected first index %v", newKeys[0])
}
if newKeys[1].Kind != BridgeKey && newKeys[1].ID != "br1" {
t.Errorf("Unexpected second index %v", newKeys[1])
}
}
newKeys = keys.Filter(KindKey(ChannelKey), KindKey(BridgeKey)).Without(MatchFunc(func(k *Key) bool {
return k.ID == "br1"
}))
if len(newKeys) != 1 {
t.Errorf("Expected without keys by app to be of length 2, got %d", len(newKeys))
} else {
if newKeys[0].Kind != ChannelKey && newKeys[0].ID != "ch1" {
t.Errorf("Unexpected first index %v", newKeys[0])
}
}
}