-
Notifications
You must be signed in to change notification settings - Fork 10
/
key_test.go
107 lines (85 loc) · 2.2 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
98
99
100
101
102
103
104
105
106
107
package cache
import (
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/suite"
)
type keySuite struct {
suite.Suite
}
func (s *keySuite) SetupSuite() {}
func (s *keySuite) TearDownSuite() {}
func (s *keySuite) SetupTest() {}
func (s *keySuite) TearDownTest() {
clearRegisteredKey()
}
func TestKeySuite(t *testing.T) {
suite.Run(t, new(keySuite))
}
func clearRegisteredKey() {
regPkgKey = packageKey
regKeyOnce = sync.Once{}
}
func (s *keySuite) TestGetPrefixAndKey() {
tests := []struct {
Desc string
CacheKey string
ExpPfx string
ExpKey string
}{
{
Desc: "invalid cache key without delimiter",
CacheKey: "12345",
ExpPfx: "12345",
ExpKey: "",
},
{
Desc: "invalid cache key with only one delimiter",
CacheKey: fmt.Sprintf("%s%s%s", "123", cacheDelim, "abc"),
ExpPfx: "abc",
ExpKey: "",
},
{
Desc: "normal case",
CacheKey: getCacheKey("prefix", "key"),
ExpPfx: "prefix",
ExpKey: "key",
},
}
for _, t := range tests {
pfx, key := getPrefixAndKey(t.CacheKey)
s.Require().Equal(t.ExpPfx, pfx, t.Desc)
s.Require().Equal(t.ExpKey, key, t.Desc)
s.TearDownTest()
}
}
func (s *keySuite) TestRegister() {
s.Require().Equal(packageKey, regPkgKey)
Register("specified")
s.Require().Equal("specified", regPkgKey)
Register("another")
s.Require().Equal("specified", regPkgKey) // no change
clearRegisteredKey()
s.Require().Equal(packageKey, regPkgKey) // set to default
Register("another")
s.Require().Equal("another", regPkgKey) // set to another
}
func (s *keySuite) TestRegisterAndGetCacheKey() {
var cKey, pfx, key string
s.Require().Equal(fmt.Sprintf("%s:pfx:key", packageKey), getCacheKey("pfx", "key"))
Register("my")
cKey = getCacheKey("pfx", "key")
s.Require().Equal("my:pfx:key", cKey)
pfx, key = getPrefixAndKey(cKey)
s.Require().Equal(pfx, "pfx")
s.Require().Equal(key, "key")
clearRegisteredKey()
s.Require().Equal(fmt.Sprintf("%s:pfx:key", packageKey), getCacheKey("pfx", "key")) // set to default
Register("") // empty package key
cKey = getCacheKey("pfx", "key")
s.Require().Equal("pfx:key", cKey)
pfx, key = getPrefixAndKey(cKey)
s.Require().Equal(pfx, "pfx")
s.Require().Equal(key, "key")
}