]>
Commit | Line | Data |
---|---|---|
e59229a2 | 1 | (ns redis.tests |
2 | (:refer-clojure :exclude [get set keys type sort]) | |
3 | (:require redis) | |
4 | (:use [clojure.contrib.test-is])) | |
5 | ||
6 | ||
7 | (defn server-fixture [f] | |
8 | (redis/with-server | |
9 | {:host "127.0.0.1" | |
10 | :port 6379 | |
11 | :db 15} | |
12 | ;; String value | |
13 | (redis/set "foo" "bar") | |
14 | ;; List with three items | |
15 | (redis/rpush "list" "one") | |
16 | (redis/rpush "list" "two") | |
17 | (redis/rpush "list" "three") | |
18 | ;; Set with three members | |
19 | (redis/sadd "set" "one") | |
20 | (redis/sadd "set" "two") | |
21 | (redis/sadd "set" "three") | |
22 | (f) | |
23 | (redis/flushdb))) | |
24 | ||
25 | (use-fixtures :each server-fixture) | |
26 | ||
27 | (deftest ping | |
28 | (is (= "PONG" (redis/ping)))) | |
29 | ||
30 | (deftest set | |
31 | (redis/set "bar" "foo") | |
32 | (is (= "foo" (redis/get "bar"))) | |
33 | (redis/set "foo" "baz") | |
34 | (is (= "baz" (redis/get "foo")))) | |
35 | ||
36 | (deftest get | |
37 | (is (= nil (redis/get "bar"))) | |
38 | (is (= "bar" (redis/get "foo")))) | |
39 | ||
40 | (deftest getset | |
41 | (is (= nil (redis/getset "bar" "foo"))) | |
42 | (is (= "foo" (redis/get "bar"))) | |
43 | (is (= "bar" (redis/getset "foo" "baz"))) | |
44 | (is (= "baz" (redis/get "foo")))) | |
45 | ||
46 | (deftest mget | |
47 | (is (= [nil] (redis/mget "bar"))) | |
48 | (redis/set "bar" "baz") | |
49 | (redis/set "baz" "buz") | |
50 | (is (= ["bar"] (redis/mget "foo"))) | |
51 | (is (= ["bar" "baz"] (redis/mget "foo" "bar"))) | |
52 | (is (= ["bar" "baz" "buz"] (redis/mget "foo" "bar" "baz"))) | |
53 | (is (= ["bar" nil "buz"] (redis/mget "foo" "bra" "baz"))) | |
54 | ) | |
55 | ||
56 | (deftest setnx | |
57 | (is (= true (redis/setnx "bar" "foo"))) | |
58 | (is (= "foo" (redis/get "bar"))) | |
59 | (is (= false (redis/setnx "foo" "baz"))) | |
60 | (is (= "bar" (redis/get "foo")))) | |
61 | ||
62 | (deftest incr | |
63 | (is (= 1 (redis/incr "nonexistent"))) | |
64 | (is (= 1 (redis/incr "foo"))) | |
65 | (is (= 2 (redis/incr "foo")))) | |
66 | ||
67 | (deftest incrby | |
68 | (is (= 42 (redis/incrby "nonexistent" 42))) | |
69 | (is (= 0 (redis/incrby "foo" 0))) | |
70 | (is (= 5 (redis/incrby "foo" 5)))) | |
71 | ||
72 | (deftest decr | |
73 | (is (= -1 (redis/decr "nonexistent"))) | |
74 | (is (= -1 (redis/decr "foo"))) | |
75 | (is (= -2 (redis/decr "foo")))) | |
76 | ||
77 | (deftest decrby | |
78 | (is (= -42 (redis/decrby "nonexistent" 42))) | |
79 | (is (= 0 (redis/decrby "foo" 0))) | |
80 | (is (= -5 (redis/decrby "foo" 5)))) | |
81 | ||
82 | (deftest exists | |
83 | (is (= true (redis/exists "foo"))) | |
84 | (is (= false (redis/exists "nonexistent")))) | |
85 | ||
86 | (deftest del | |
87 | (is (= false (redis/del "nonexistent"))) | |
88 | (is (= true (redis/del "foo"))) | |
89 | (is (= nil (redis/get "foo")))) | |
90 | ||
91 | (deftest type | |
92 | (is (= :none (redis/type "nonexistent"))) | |
93 | (is (= :string (redis/type "foo"))) | |
94 | (is (= :list (redis/type "list"))) | |
95 | (is (= :set (redis/type "set")))) | |
96 | ||
97 | (deftest keys | |
98 | (is (= nil (redis/keys "a*"))) | |
99 | (is (= ["foo"] (redis/keys "f*"))) | |
100 | (is (= ["foo"] (redis/keys "f?o"))) | |
101 | (redis/set "fuu" "baz") | |
102 | (is (= #{"foo" "fuu"} (clojure.core/set (redis/keys "f*"))))) | |
103 | ||
104 | (deftest randomkey | |
105 | (redis/flushdb) | |
106 | (redis/set "foo" "bar") | |
107 | (is (= "foo" (redis/randomkey))) | |
108 | (redis/flushdb) | |
109 | (is (= "" (redis/randomkey)))) | |
110 | ||
111 | (deftest rename | |
112 | (is (thrown? Exception (redis/rename "foo" "foo"))) | |
113 | (is (thrown? Exception (redis/rename "nonexistent" "foo"))) | |
114 | (redis/rename "foo" "bar") | |
115 | (is (= "bar" (redis/get "bar"))) | |
116 | (is (= nil (redis/get "foo"))) | |
117 | (redis/set "foo" "bar") | |
118 | (redis/set "bar" "baz") | |
119 | (redis/rename "foo" "bar") | |
120 | (is (= "bar" (redis/get "bar"))) | |
121 | (is (= nil (redis/get "foo"))) | |
122 | ) | |
123 | ||
124 | (deftest renamenx | |
125 | (is (thrown? Exception (redis/renamenx "foo" "foo"))) | |
126 | (is (thrown? Exception (redis/renamenx "nonexistent" "foo"))) | |
127 | (is (= true (redis/renamenx "foo" "bar"))) | |
128 | (is (= "bar" (redis/get "bar"))) | |
129 | (is (= nil (redis/get "foo"))) | |
130 | (redis/set "foo" "bar") | |
131 | (redis/set "bar" "baz") | |
132 | (is (= false (redis/renamenx "foo" "bar"))) | |
133 | ) | |
134 | ||
135 | (deftest dbsize | |
136 | (let [size-before (redis/dbsize)] | |
137 | (redis/set "anewkey" "value") | |
138 | (let [size-after (redis/dbsize)] | |
139 | (is (= size-after | |
140 | (+ 1 size-before)))))) | |
141 | ||
142 | (deftest expire | |
143 | (is (= true (redis/expire "foo" 1))) | |
144 | (Thread/sleep 2000) | |
145 | (is (= false (redis/exists "foo"))) | |
146 | (redis/set "foo" "bar") | |
147 | (is (= true (redis/expire "foo" 20))) | |
148 | (is (= false (redis/expire "foo" 10))) | |
149 | (is (= false (redis/expire "nonexistent" 42))) | |
150 | ) | |
151 | ||
152 | (deftest ttl | |
153 | (is (= -1 (redis/ttl "nonexistent"))) | |
154 | (is (= -1 (redis/ttl "foo"))) | |
155 | (redis/expire "foo" 42) | |
156 | (is (< 40 (redis/ttl "foo")))) | |
157 | ||
158 | ||
159 | ;; | |
160 | ;; List commands | |
161 | ;; | |
162 | (deftest rpush | |
163 | (is (thrown? Exception (redis/rpush "foo"))) | |
164 | (redis/rpush "newlist" "one") | |
165 | (is (= 1 (redis/llen "newlist"))) | |
166 | (is (= "one" (redis/lindex "newlist" 0))) | |
167 | (redis/del "newlist") | |
168 | (redis/rpush "list" "item") | |
169 | (is (= "item" (redis/rpop "list")))) | |
170 | ||
171 | (deftest lpush | |
172 | (is (thrown? Exception (redis/lpush "foo"))) | |
173 | (redis/lpush "newlist" "item") | |
174 | (is (= 1 (redis/llen "newlist"))) | |
175 | (is (= "item" (redis/lindex "newlist" 0))) | |
176 | (redis/lpush "list" "item") | |
177 | (is (= "item" (redis/lpop "list")))) | |
178 | ||
179 | (deftest llen | |
180 | (is (thrown? Exception (redis/llen "foo"))) | |
181 | (is (= 0 (redis/llen "newlist"))) | |
182 | (is (= 3 (redis/llen "list")))) | |
183 | ||
184 | (deftest lrange | |
185 | (is (thrown? Exception (redis/lrange "foo" 0 1))) | |
186 | (is (= nil (redis/lrange "newlist" 0 42))) | |
187 | (is (= ["one"] (redis/lrange "list" 0 0))) | |
188 | (is (= ["three"] (redis/lrange "list" -1 -1))) | |
189 | (is (= ["one" "two"] (redis/lrange "list" 0 1))) | |
190 | (is (= ["one" "two" "three"] (redis/lrange "list" 0 2))) | |
191 | (is (= ["one" "two" "three"] (redis/lrange "list" 0 42))) | |
192 | (is (= [] (redis/lrange "list" 42 0))) | |
193 | ) | |
194 | ||
195 | ;; TBD | |
196 | (deftest ltrim | |
197 | (is (thrown? Exception (redis/ltrim "foo" 0 0)))) | |
198 | ||
199 | (deftest lindex | |
200 | (is (thrown? Exception (redis/lindex "foo" 0))) | |
201 | (is (= nil (redis/lindex "list" 42))) | |
202 | (is (= nil (redis/lindex "list" -4))) | |
203 | (is (= "one" (redis/lindex "list" 0))) | |
204 | (is (= "three" (redis/lindex "list" 2))) | |
205 | (is (= "three" (redis/lindex "list" -1)))) | |
206 | ||
207 | (deftest lset | |
208 | (is (thrown? Exception (redis/lset "foo" 0 "bar"))) | |
209 | (is (thrown? Exception (redis/lset "list" 42 "value"))) | |
210 | (redis/lset "list" 0 "test") | |
211 | (is (= "test" (redis/lindex "list" 0))) | |
212 | (redis/lset "list" 2 "test2") | |
213 | (is (= "test2" (redis/lindex "list" 2))) | |
214 | (redis/lset "list" -1 "test3") | |
215 | (is (= "test3" (redis/lindex "list" 2)))) | |
216 | ||
e59229a2 | 217 | (deftest lrem |
218 | (is (thrown? Exception (redis/lrem "foo" 0 "bar"))) | |
111d9959 | 219 | (is (= 0 (redis/lrem "newlist" 0 ""))) |
220 | (is (= 1 (redis/lrem "list" 1 "two"))) | |
221 | (is (= 1 (redis/lrem "list" 42 "three"))) | |
222 | (is (= 1 (redis/llen "list")))) | |
e59229a2 | 223 | |
224 | ||
225 | (deftest lpop | |
226 | (is (thrown? Exception (redis/lpop "foo"))) | |
111d9959 | 227 | (is (= nil (redis/lpop "newlist"))) |
228 | (is (= "one" (redis/lpop "list"))) | |
229 | (is (= 2 (redis/llen "list")))) | |
e59229a2 | 230 | |
231 | (deftest rpop | |
232 | (is (thrown? Exception (redis/rpop "foo"))) | |
111d9959 | 233 | (is (= nil (redis/rpop "newlist"))) |
234 | (is (= "three" (redis/rpop "list"))) | |
235 | (is (= 2 (redis/llen "list")))) | |
e59229a2 | 236 | |
237 | ;; | |
238 | ;; Set commands | |
239 | ;; | |
240 | (deftest sadd | |
241 | (is (thrown? Exception (redis/sadd "foo" "bar"))) | |
242 | (is (= true (redis/sadd "newset" "member"))) | |
243 | (is (= true (redis/sismember "newset" "member"))) | |
244 | (is (= false (redis/sadd "set" "two"))) | |
245 | (is (= true (redis/sadd "set" "four"))) | |
246 | (is (= true (redis/sismember "set" "four")))) | |
247 | ||
248 | (deftest srem | |
249 | (is (thrown? Exception (redis/srem "foo" "bar"))) | |
111d9959 | 250 | (is (= false (redis/srem "newset" "member"))) |
e59229a2 | 251 | (is (= true (redis/srem "set" "two"))) |
252 | (is (= false (redis/sismember "set" "two"))) | |
253 | (is (= false (redis/srem "set" "blahonga")))) | |
254 | ||
111d9959 | 255 | (deftest spop |
256 | (is (thrown? Exception (redis/spop "foo" "bar"))) | |
257 | (is (= nil (redis/spop "newset"))) | |
258 | (is (contains? #{"one" "two" "three"} (redis/spop "set")))) | |
259 | ||
e59229a2 | 260 | (deftest smove |
261 | (is (thrown? Exception (redis/smove "foo" "set" "one"))) | |
262 | (is (thrown? Exception (redis/smove "set" "foo" "one"))) | |
263 | (redis/sadd "set1" "two") | |
264 | (is (= false (redis/smove "set" "set1" "four"))) | |
265 | (is (= #{"two"} (redis/smembers "set1"))) | |
266 | (is (= true (redis/smove "set" "set1" "one"))) | |
267 | (is (= #{"one" "two"} (redis/smembers "set1")))) | |
268 | ||
269 | (deftest scard | |
270 | (is (thrown? Exception (redis/scard "foo"))) | |
271 | (is (= 3 (redis/scard "set")))) | |
272 | ||
273 | (deftest sismember | |
274 | (is (thrown? Exception (redis/sismember "foo" "bar"))) | |
275 | (is (= false (redis/sismember "set" "blahonga"))) | |
276 | (is (= true (redis/sismember "set" "two")))) | |
277 | ||
278 | (deftest sinter | |
279 | (is (thrown? Exception (redis/sinter "foo" "set"))) | |
280 | (is (= #{} (redis/sinter "nonexistent"))) | |
281 | (redis/sadd "set1" "one") | |
282 | (redis/sadd "set1" "four") | |
283 | (is (= #{"one" "two" "three"} (redis/sinter "set"))) | |
284 | (is (= #{"one"} (redis/sinter "set" "set1"))) | |
285 | (is (= #{} (redis/sinter "set" "set1" "nonexistent")))) | |
286 | ||
287 | (deftest sinterstore | |
288 | (redis/sinterstore "foo" "set") | |
289 | (is (= #{"one" "two" "three"} (redis/smembers "foo"))) | |
290 | (redis/sadd "set1" "one") | |
291 | (redis/sadd "set1" "four") | |
292 | (redis/sinterstore "newset" "set" "set1") | |
293 | (is (= #{"one"} (redis/smembers "newset")))) | |
294 | ||
295 | (deftest sunion | |
296 | (is (thrown? Exception (redis/sunion "foo" "set"))) | |
297 | (is (= #{} (redis/sunion "nonexistent"))) | |
298 | (redis/sadd "set1" "one") | |
299 | (redis/sadd "set1" "four") | |
300 | (is (= #{"one" "two" "three"} (redis/sunion "set"))) | |
301 | (is (= #{"one" "two" "three" "four"} (redis/sunion "set" "set1"))) | |
302 | (is (= #{"one" "two" "three" "four"} (redis/sunion "set" "set1" "nonexistent")))) | |
303 | ||
304 | (deftest sunionstore | |
305 | (redis/sunionstore "foo" "set") | |
306 | (is (= #{"one" "two" "three"} (redis/smembers "foo"))) | |
307 | (redis/sadd "set1" "one") | |
308 | (redis/sadd "set1" "four") | |
309 | (redis/sunionstore "newset" "set" "set1") | |
310 | (is (= #{"one" "two" "three" "four"} (redis/smembers "newset")))) | |
311 | ||
312 | (deftest sdiff | |
313 | (is (thrown? Exception (redis/sdiff "foo" "set"))) | |
314 | (is (= #{} (redis/sdiff "nonexistent"))) | |
315 | (redis/sadd "set1" "one") | |
316 | (redis/sadd "set1" "four") | |
317 | (is (= #{"one" "two" "three"} (redis/sdiff "set"))) | |
318 | (is (= #{"two" "three"} (redis/sdiff "set" "set1"))) | |
319 | (is (= #{"two" "three"} (redis/sdiff "set" "set1" "nonexistent")))) | |
320 | ||
321 | (deftest sdiffstore | |
322 | (redis/sdiffstore "foo" "set") | |
323 | (is (= #{"one" "two" "three"} (redis/smembers "foo"))) | |
324 | (redis/sadd "set1" "one") | |
325 | (redis/sadd "set1" "four") | |
326 | (redis/sdiffstore "newset" "set" "set1") | |
327 | (is (= #{"two" "three"} (redis/smembers "newset")))) | |
328 | ||
329 | (deftest smembers | |
330 | (is (thrown? Exception (redis/smembers "foo"))) | |
331 | (is (= #{"one" "two" "three"} (redis/smembers "set")))) | |
332 | ||
333 | ||
334 | ;; | |
335 | ;; Sorting | |
336 | ;; | |
337 | (deftest sort | |
338 | (redis/lpush "ids" 1) | |
339 | (redis/lpush "ids" 4) | |
340 | (redis/lpush "ids" 2) | |
341 | (redis/lpush "ids" 3) | |
342 | (redis/set "object_1" "one") | |
343 | (redis/set "object_2" "two") | |
344 | (redis/set "object_3" "three") | |
345 | (redis/set "object_4" "four") | |
346 | (redis/set "name_1" "Derek") | |
347 | (redis/set "name_2" "Charlie") | |
348 | (redis/set "name_3" "Bob") | |
349 | (redis/set "name_4" "Alice") | |
350 | ||
351 | (is (= ["one" "two" "three"] | |
352 | (redis/sort "list"))) | |
353 | (is (= ["one" "three" "two"] | |
354 | (redis/sort "list" :alpha))) | |
355 | (is (= ["1" "2" "3" "4"] | |
356 | (redis/sort "ids"))) | |
357 | (is (= ["1" "2" "3" "4"] | |
358 | (redis/sort "ids" :asc))) | |
359 | (is (= ["4" "3" "2" "1"] | |
360 | (redis/sort "ids" :desc))) | |
361 | (is (= ["1" "2"] | |
362 | (redis/sort "ids" :asc :limit 0 2))) | |
363 | (is (= ["4" "3"] | |
364 | (redis/sort "ids" :desc :limit 0 2))) | |
365 | (is (= ["4" "3" "2" "1"] | |
366 | (redis/sort "ids" :by "name_*" :alpha))) | |
367 | (is (= ["one" "two" "three" "four"] | |
368 | (redis/sort "ids" :get "object_*"))) | |
369 | (is (= ["one" "two"] | |
370 | (redis/sort "ids" :by "name_*" :alpha :limit 0 2 :desc :get "object_*")))) | |
371 | ||
372 | ||
373 | ||
374 | ;; | |
375 | ;; Multiple database handling commands | |
376 | ;; | |
377 | (deftest select | |
378 | (redis/select 0) | |
379 | (is (= nil (redis/get "akeythat_probably_doesnotexsistindb0")))) | |
380 | ||
381 | (deftest flushdb | |
382 | (redis/flushdb) | |
383 | (is (= 0 (redis/dbsize)))) | |
384 | ||
385 | ;; | |
386 | ;; Persistence commands | |
387 | ;; | |
388 | (deftest save | |
389 | (redis/save)) | |
390 | ||
391 | (deftest bgsave | |
392 | (redis/bgsave)) | |
393 | ||
394 | (deftest lastsave | |
395 | (let [ages-ago (new java.util.Date (long 1))] | |
396 | (is (.before ages-ago (redis/lastsave))))) | |
397 |