]> git.saurik.com Git - redis.git/blame - client-libraries/clojure/src/redis/tests/internal.clj
client libraries updated
[redis.git] / client-libraries / clojure / src / redis / tests / internal.clj
CommitLineData
e59229a2 1(ns redis.tests.internal
2 (:require [redis.internal :as redis])
3 (:use [clojure.contrib.test-is])
4 (:import [java.io StringReader BufferedReader]))
5
6
e59229a2 7;;
8;; Helpers
9;;
10
11(defn- wrap-in-reader
12 [#^String s]
13 (let [reader (BufferedReader. (StringReader. s))]
14 reader))
15
16(defn- read-reply
17 [#^String s]
18 (redis/read-reply (wrap-in-reader s)))
19
20
21;;
22;; Command generation
23;;
24(deftest inline-command
25 (is (= "FOO\r\n"
26 (redis/inline-command "FOO")))
27 (is (= "FOO bar\r\n"
28 (redis/inline-command "FOO" "bar")))
29 (is (= "FOO bar baz\r\n"
30 (redis/inline-command "FOO" "bar" "baz"))))
31
32(deftest bulk-command
33 (is (= "FOO 3\r\nbar\r\n"
34 (redis/bulk-command "FOO" "bar")))
35 (is (= "SET foo 3\r\nbar\r\n"
36 (redis/bulk-command "SET" "foo" "bar")))
37 (is (= "SET foo bar 3\r\nbaz\r\n"
38 (redis/bulk-command "SET" "foo" "bar" "baz"))))
39
40(deftest sort-command
41 (is (= "SORT key\r\n"
42 (redis/sort-command "SORT" "key")))
43 (is (= "SORT key BY pattern\r\n"
44 (redis/sort-command "SORT" "key" :by "pattern")))
45 (is (= "SORT key LIMIT 0 10\r\n"
46 (redis/sort-command "SORT" "key" :limit 0 10)))
47 (is (= "SORT key ASC\r\n"
48 (redis/sort-command "SORT" "key" :asc)))
49 (is (= "SORT key DESC\r\n"
50 (redis/sort-command "SORT" "key" :desc)))
51 (is (= "SORT key ALPHA\r\n"
52 (redis/sort-command "SORT" "key" :alpha)))
53 (is (= "SORT key GET object_* GET object2_*\r\n"
54 (redis/sort-command "SORT" "key" :get "object_*" :get "object2_*")))
55 (is (= "SORT key BY weight_* LIMIT 0 10 GET object_* ALPHA DESC\r\n"
56 (redis/sort-command "SORT" "key"
57 :by "weight_*"
58 :limit 0 10
59 :get "object_*"
60 :alpha
61 :desc))))
62
63
64;;
65;; Reply parsing
66;;
67(deftest read-crlf
68 (is (thrown? Exception
69 (redis/read-crlf (wrap-in-reader "\n"))))
70 (is (thrown? Exception
71 (redis/read-crlf (wrap-in-reader ""))))
72 (is (thrown? Exception
73 (redis/read-crlf (wrap-in-reader "\r1"))))
74 (is (= nil
75 (redis/read-crlf (wrap-in-reader "\r\n")))))
76
77;; (deftest read-newline-crlf
78;; (is (thrown? Exception
79;; (redis/read-line-crlf (wrap-in-reader "")))))
80
81;;
82;; Reply parsing
83;;
84(deftest reply
85 (is (thrown? Exception
86 (read-reply "")))
87 (is (thrown? Exception
88 (read-reply "\r\n"))))
89
90
91(deftest error-reply
92 (is (thrown?
93 Exception
94 (read-reply "-\r\n")))
95 (is (thrown-with-msg?
96 Exception #".*Test"
97 (read-reply "-Test\r\n"))))
98
99(deftest simple-reply
100 (is (thrown? Exception
101 (read-reply "+")))
102 (is (= ""
103 (read-reply "+\r\n")))
104 (is (= "foobar"
105 (read-reply "+foobar\r\n"))))
106
107(deftest integer-reply
108 (is (thrown? Exception
109 (read-reply ":\r\n")))
110 (is (= 0
111 (read-reply ":0\r\n")))
112 (is (= 42
113 (read-reply ":42\r\n")))
114 (is (= 42
115 (read-reply ": 42 \r\n")))
116 (is (= 429348754
117 (read-reply ":429348754\r\n"))))
118
119(deftest bulk-reply
120 (is (thrown? Exception
121 (read-reply "$\r\n")))
122 (is (thrown? Exception
123 (read-reply "$2\r\n1\r\n")))
124 (is (thrown? Exception
125 (read-reply "$3\r\n1\r\n")))
126 (is (= nil
127 (read-reply "$-1\r\n")))
128 (is (= "foobar"
129 (read-reply "$6\r\nfoobar\r\n")))
130 (is (= "foo\r\nbar"
131 (read-reply "$8\r\nfoo\r\nbar\r\n"))))
132
133(deftest multi-bulk-reply
134 (is (thrown? Exception
135 (read-reply "*1\r\n")))
136 (is (thrown? Exception
137 (read-reply "*4\r\n:0\r\n:0\r\n:0\r\n")))
138 (is (= nil
139 (read-reply "*-1\r\n")))
140 (is (= [1]
141 (read-reply "*1\r\n:1\r\n")))
142 (is (= ["foo" "bar"]
143 (read-reply "*2\r\n+foo\r\n+bar\r\n")))
144 (is (= [1 "foo" "foo\r\nbar"]
145 (read-reply "*3\r\n:1\r\n+foo\r\n$8\r\nfoo\r\nbar\r\n"))))
146
147
148
149
150
151