This repository has been archived by the owner on Apr 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gotoredis_test.go
197 lines (157 loc) · 3.86 KB
/
gotoredis_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
package gotoredis_test
import (
"fmt"
"code.google.com/p/go-uuid/uuid"
"github.com/craigfurman/gotoredis"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type SimpleStruct struct {
String string
Int64 int64
Int32 int32
Int16 int16
Int8 int8
Int int
Uint64 uint64
Uint32 uint32
Uint16 uint16
Uint8 uint8
Uint uint
Uintptr uintptr
Byte byte
Rune rune
Float32 float32
Float64 float64
Complex64 complex64
Complex128 complex128
Bool bool
}
var _ = Describe("saving objects in Redis", func() {
Context("when Redis is running on supplied host and port", func() {
var (
g *gotoredis.StructMapper
connectErr error
)
BeforeEach(func() {
g, connectErr = gotoredis.New("localhost:6379")
})
AfterEach(func() {
err := g.Close()
Expect(err).ToNot(HaveOccurred())
})
Describe("Creating a gotoredis client", func() {
It("does not error", func() {
Expect(connectErr).NotTo(HaveOccurred())
})
})
Context("when a struct is saved", func() {
var (
key string
savedStruct SimpleStruct
saveErr error
)
BeforeEach(func() {
key = uuid.New()
var c64r float32 = 1.0
var c64i float32 = 1.1
var c128r float64 = 1.2
var c128i float64 = 1.3
savedStruct = SimpleStruct{
String: "some string",
Int64: 64,
Int32: 32,
Int16: 16,
Int8: 8,
Int: 1000,
Uint64: 25,
Uint32: 9,
Uint16: 15,
Uint8: 10,
Uint: 1,
Uintptr: 77,
Byte: 100,
Rune: 101,
Float32: 1.1,
Float64: 1.2,
Complex64: complex(c64r, c64i),
Complex128: complex(c128r, c128i),
Bool: true,
}
})
JustBeforeEach(func() {
saveErr = g.Save(key, savedStruct)
})
It("does not error", func() {
Expect(saveErr).ToNot(HaveOccurred())
})
Describe("retrieving structs", func() {
var (
keyToLoad string
retrievedStruct SimpleStruct
retrieveErr error
)
BeforeEach(func() {
keyToLoad = key
})
JustBeforeEach(func() {
retrieveErr = g.Load(keyToLoad, &retrievedStruct)
})
It("populates struct fields", func() {
Expect(retrievedStruct).To(Equal(savedStruct))
})
It("does not error", func() {
Expect(retrieveErr).ToNot(HaveOccurred())
})
Context("when there is no redis entry for key specified", func() {
BeforeEach(func() {
keyToLoad = "some_key_that_does_not_exist"
})
It("returns an error", func() {
Expect(retrieveErr).To(MatchError(fmt.Sprintf("No Redis hash found for key %s", keyToLoad)))
})
})
})
Describe("updating structs", func() {
var (
updateErr error
)
BeforeEach(func() {
savedStruct.String = "a new value"
})
JustBeforeEach(func() {
updateErr = g.Save(key, savedStruct)
})
It("does not error", func() {
Expect(updateErr).ToNot(HaveOccurred())
})
It("updates the struct", func() {
var retrievedStruct SimpleStruct
err := g.Load(key, &retrievedStruct)
Expect(err).ToNot(HaveOccurred())
Expect(retrievedStruct).To(Equal(savedStruct))
})
})
Describe("deleting structs", func() {
var deleteErr error
JustBeforeEach(func() {
deleteErr = g.Delete(key)
})
It("does not error", func() {
Expect(deleteErr).NotTo(HaveOccurred())
})
It("cannot be retrieved from Redis", func() {
var retrievedStruct SimpleStruct
err := g.Load(key, &retrievedStruct)
Expect(err).To(HaveOccurred())
})
})
})
})
Context("when Redis is not running on the supplied host and port", func() {
It("returns an error", func() {
_, err := gotoredis.New("localhost:9999")
Expect(err).To(HaveOccurred())
})
})
})