-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_test.go
233 lines (202 loc) · 4.38 KB
/
hash_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package redis_pack
import (
"testing"
)
func TestHashRds_Hash(t *testing.T) {
type Hash struct {
Name string
Age int64
}
conn, _ := NewConnectionByPool(testPool)
key := "hash"
h := &Hash{"newgoo", 24}
err := conn.Hash.HMSetFromStruct(key, h).error
if err != nil {
t.Error(err)
}
h2 := new(Hash)
err = conn.Hash.HGetAll(key).ScanStruct(h2)
if err != nil {
t.Error(err)
}
if h2.Name != h.Name || h2.Age != h.Age {
t.Error("收到值不正确")
}
}
func TestHashRds_HGetAndSet(t *testing.T) {
type HSet struct {
Key string
Filed string
Value int64
}
conn, _ := NewConnectionByPool(testPool)
key := "HashSet"
hs := []HSet{
{Key: key, Filed: "filed1", Value: 1},
{Key: key, Filed: "filed2", Value: 2},
{Key: key, Filed: "filed3", Value: 3},
}
//测试对hash对象操作
for _, hash := range hs {
if err := conn.Hash.HSet(hash.Key, hash.Filed, hash.Value).error; err != nil {
t.Error(err)
}
}
for _, hash := range hs {
v, err := conn.Hash.HGet(hash.Key, hash.Filed).Int64()
if err != nil {
t.Error(err)
}
if v != hash.Value {
t.Error("获取到的值不正确")
}
}
// 测试对字段操作
value, err := conn.Hash.HMGet(key, []string{"filed1", "filed2"}).Int64s()
if err != nil {
t.Error(err)
}
if value[0] != 1 && value[1] != 2 {
t.Error(err)
}
exist, err := conn.Hash.HExists(key, "filed1").Bool()
if err != nil {
t.Error(err)
}
if !exist {
t.Error("判断出错")
}
// 获取hash所有字段
fileds, err := conn.Hash.HKeys(key).Strings()
if err != nil {
t.Error(err)
}
if len(fileds) != len(hs) {
t.Error("获取hash字段出错")
}
// 获取hash字段数量
filedNum, err := conn.Hash.HLen(key).Int()
if err != nil {
t.Error(err)
}
if filedNum != len(hs) {
t.Error("获取hash字段数量")
}
// 获取hash所有字段值
values, err := conn.Hash.HVals(key).Int64s()
if err != nil {
t.Error(err)
}
if len(values) != len(hs) {
t.Error("获取hash所有值失败")
}
// 测试删除字段
err = conn.Hash.HDel(key, []string{"filed1", "filed2"}).error
if err != nil {
t.Error(err)
}
exist1, err := conn.Hash.HExists(key, "filed1").Bool()
if err != nil {
t.Error(err)
}
exist2, err := conn.Hash.HExists(key, "filed2").Bool()
if err != nil {
t.Error(err)
}
if exist1 || exist2 {
t.Error("hdel,删除失败")
}
}
func TestHashRds_HIncr(t *testing.T) {
type HSet struct {
Key string
Filed string
Value int64
}
conn, _ := NewConnectionByPool(testPool)
key := "HashSet"
hs := []HSet{
{Key: key, Filed: "filed1", Value: 1},
{Key: key, Filed: "filed2", Value: 2},
{Key: key, Filed: "filed3", Value: 3},
}
//测试对hash对象操作
for _, hash := range hs {
if err := conn.Hash.HSet(hash.Key, hash.Filed, hash.Value).error; err != nil {
t.Error(err)
}
}
for _, hash := range hs {
err := conn.Hash.HIncrBy(hash.Key, hash.Filed, -1).error
if err != nil {
t.Error(err)
}
}
for _, hash := range hs {
v, err := conn.Hash.HGet(hash.Key, hash.Filed).Int64()
if err != nil {
t.Error(err)
}
if v != hash.Value-1 {
t.Error("hincr,方法错误")
}
}
}
func TestHashRds_HIncrByFloat(t *testing.T) {
type HSet struct {
Key string
Filed string
Value int64
}
conn, _ := NewConnectionByPool(testPool)
key := "HashSet"
hs := []HSet{
{Key: key, Filed: "filed1", Value: 1},
{Key: key, Filed: "filed2", Value: 2},
{Key: key, Filed: "filed3", Value: 3},
}
//测试对hash对象操作
for _, hash := range hs {
if err := conn.Hash.HSet(hash.Key, hash.Filed, hash.Value).error; err != nil {
t.Error(err)
}
}
for _, hash := range hs {
err := conn.Hash.HIncrByFloat(hash.Key, hash.Filed, 0.2).error
if err != nil {
t.Error(err)
}
}
for _, hash := range hs {
v, err := conn.Hash.HGet(hash.Key, hash.Filed).float64()
if err != nil {
t.Error(err)
}
if v != float64(hash.Value)+0.2 {
t.Error("hincrbyfloat,方法错误")
}
}
}
func TestHashRds_HMSetFromMap(t *testing.T) {
conn, _ := NewConnectionByPool(testPool)
key := "maphash"
mp := map[interface{}]interface{}{"filed1": 5, "filed2": 6}
err := conn.Hash.HMSetFromMap(key, mp).error
if err != nil {
t.Error(err)
}
v, err := conn.Hash.HGet(key, "filed1").Int64()
if err != nil {
t.Error(err)
}
if v != 5 {
t.Error("hmset map 失败")
}
v, err = conn.Hash.HGet(key, "filed2").Int64()
if err != nil {
t.Error(err)
}
if v != 6 {
t.Error("hmset map 失败")
}
}