-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dragonfly_test.go
150 lines (114 loc) · 3.72 KB
/
Dragonfly_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
140
141
142
143
144
145
146
147
148
149
150
package Dragonfly_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/viper"
. "github.com/NBCFB/Dragonfly"
)
var _ = Describe("Dragonfly", func() {
var caller *RedisCallers
BeforeEach(func() {
// Set server config
serverConfig := viper.New()
serverConfig.AddConfigPath(".")
serverConfig.SetConfigName("test-config")
serverConfig.SetConfigType("json")
// Read config file
err := serverConfig.ReadInConfig()
Expect(err).NotTo(HaveOccurred())
Expect(serverConfig.Get("test.redisDB.host")).To(Equal("127.0.0.1"))
caller = NewCaller(serverConfig)
Expect(caller.Client.FlushDB().Err()).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(caller.Client.Close()).NotTo(HaveOccurred())
})
It("can set", func() {
v, err := caller.Set("key:1", "val:1", 0)
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal("val:1"))
})
It("fails to set [due-to: empty args]", func() {
_, err := caller.Set("", "val:1", 0)
Expect(err).Should(HaveOccurred())
})
It("can set in batch", func() {
objs := make([]RedisObj, 3)
objs[0] = RedisObj{K: "key:1", V: "val:1"}
objs[1] = RedisObj{K: "key:2", V: "val:2"}
objs[2] = RedisObj{K: "key:3", V: "val:3"}
err := caller.SetInBatch(objs)
Expect(err).NotTo(HaveOccurred())
expected, _ := caller.SearchByKeys("key*", nil)
Expect(len(expected)).To(Equal(3))
})
It("fails to set in batch", func() {
err := caller.SetInBatch([]RedisObj{})
Expect(err).Should(HaveOccurred())
})
It("gets", func() {
_, err := caller.Set("key:1", "val:1", 0)
Expect(err).NotTo(HaveOccurred())
v, err := caller.Get("key:1")
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal("val:1"))
})
It("fails to set [due-to: empty key]", func() {
_, err := caller.Get("")
Expect(err).Should(HaveOccurred())
})
It("fails to get [due-to: wrong keys]", func() {
_, err := caller.Set("key:1", "val:1", 0)
Expect(err).NotTo(HaveOccurred())
_, err = caller.Get("ghost_key:1")
Expect(err).Should(HaveOccurred())
})
It("can search", func() {
_, err := caller.Set("key:1", "val:1", 0)
Expect(err).NotTo(HaveOccurred())
_, err = caller.Set("key:2", "val:2", 0)
Expect(err).NotTo(HaveOccurred())
_, err = caller.Set("wired_key:2", "val:1", 0)
Expect(err).NotTo(HaveOccurred())
objs, err := caller.SearchByKeys("key*", nil)
Expect(err).NotTo(HaveOccurred())
Expect(len(objs)).To(Equal(2))
objs, err = caller.SearchByKeys("ghost_key*", nil)
Expect(err).NotTo(HaveOccurred())
Expect(len(objs)).To(Equal(0))
})
It("can search with keywords", func() {
_, err := caller.Set("key:1", "val:1", 0)
Expect(err).NotTo(HaveOccurred())
_, err = caller.Set("key:2", "val:2", 0)
Expect(err).NotTo(HaveOccurred())
objs, err := caller.SearchByKeys("key:*", []string{"val:2"})
Expect(err).NotTo(HaveOccurred())
Expect(len(objs)).To(Equal(1))
Expect(objs).To(Equal([]RedisObj{{K: "key:2", V: "val:2"}}))
})
It("fails to search [due-to: empty patten]", func() {
_, err := caller.Set("key:1", "val:1", 0)
Expect(err).NotTo(HaveOccurred())
_, err = caller.Set("key:2", "val:2", 0)
Expect(err).NotTo(HaveOccurred())
_, err = caller.SearchByKeys("", []string{"val:2"})
Expect(err).Should(HaveOccurred())
})
It("can delete", func() {
_, err := caller.Set("key:1", "val:1", 0)
Expect(err).NotTo(HaveOccurred())
_, err = caller.Set("key:2", "val:1", 0)
Expect(err).NotTo(HaveOccurred())
err = caller.Del("key:1")
Expect(err).NotTo(HaveOccurred())
objs, _ := caller.SearchByKeys("key*", nil)
Expect(len(objs)).To(Equal(1))
err = caller.Del("ghost_key:1")
Expect(err).NotTo(HaveOccurred())
})
It("fails to delete [due-to: empty key(s)]", func() {
err := caller.Del("", "")
Expect(err).Should(HaveOccurred())
})
})