forked from jarias/stormpath-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
139 lines (112 loc) · 2.86 KB
/
cache_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package stormpath_test
import (
"encoding/json"
"os"
"github.com/garyburd/redigo/redis"
. "github.com/jarias/stormpath-sdk-go"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Cache", func() {
Describe("RedisCache", func() {
key := "key"
redisServer := os.Getenv("REDIS_SERVER")
redisConn, err := redis.Dial("tcp", redisServer+":6379")
if err != nil {
panic(err)
}
redisCache := RedisCache{redisConn}
AfterEach(func() {
redisConn.Do("FLUSHDB")
})
Describe("Exists", func() {
It("should return false if the key doesn't exists", func() {
r := redisCache.Exists(key)
Expect(r).To(BeFalse())
})
It("should return true if the key does exists", func() {
redisConn.Do("SET", key, 1)
r := redisCache.Exists(key)
Expect(r).To(BeTrue())
})
})
Describe("Set", func() {
It("should store a new object in the cache", func() {
r := redisCache.Exists(key)
Expect(r).To(BeFalse())
redisCache.Set(key, 1)
r = redisCache.Exists(key)
Expect(r).To(BeTrue())
})
It("should update an existing object in the cache", func() {
var r int
redisCache.Set(key, 1)
redisCache.Set(key, 2)
cacheData, err := redisConn.Do("GET", key)
json.Unmarshal(cacheData.([]byte), &r)
Expect(err).NotTo(HaveOccurred())
Expect(r).To(Equal(2))
})
})
Describe("Get", func() {
It("should load empty data if the key doesn't exists into the given interface", func() {
var r int
err := redisCache.Get(key, &r)
Expect(err).NotTo(HaveOccurred())
Expect(r).To(Equal(0))
})
It("should load data from the cache into the given interface", func() {
var r int
redisCache.Set(key, 2)
err := redisCache.Get(key, &r)
Expect(err).NotTo(HaveOccurred())
Expect(r).To(Equal(2))
})
})
Describe("Del", func() {
It("should delete a given key from the cache", func() {
redisCache.Set(key, 2)
r := redisCache.Exists(key)
Expect(r).To(BeTrue())
redisCache.Del(key)
r = redisCache.Exists(key)
Expect(r).To(BeFalse())
})
})
})
})
var _ = Describe("Cacheable", func() {
Describe("Collection resource", func() {
It("should not be cacheable", func() {
var resources = []interface{}{
&Applications{},
&Accounts{},
&Groups{},
&Directories{},
&AccountStoreMappings{},
}
for _, resource := range resources {
c, ok := resource.(Cacheable)
Expect(ok).To(BeTrue())
Expect(c.IsCacheable()).To(BeFalse())
}
})
})
Describe("Single resource", func() {
It("should be cacheable", func() {
var resources = []interface{}{
&Application{},
&Account{},
&Group{},
&Directory{},
&AccountStoreMapping{},
&Tenant{},
}
for _, resource := range resources {
c, ok := resource.(Cacheable)
Expect(ok).To(BeTrue())
Expect(c.IsCacheable()).To(BeTrue())
}
})
})
})