@@ -109,33 +109,147 @@ describe('The node_redis client', function () {
109
109
110
110
describe ( 'send_command' , function ( ) {
111
111
112
- it ( 'omitting args should be fine in some cases' , function ( done ) {
112
+ it ( 'omitting args should be fine' , function ( done ) {
113
+ client . server_info = { } ;
114
+ client . send_command ( 'info' ) ;
115
+ client . send_command ( 'ping' , function ( err , res ) {
116
+ assert . strictEqual ( res , 'PONG' ) ;
117
+ // Check if the previous info command used the internal individual info command
118
+ assert . notDeepEqual ( client . server_info , { } ) ;
119
+ client . server_info = { } ;
120
+ } ) ;
121
+ client . send_command ( 'info' , null , undefined ) ;
122
+ client . send_command ( 'ping' , null , function ( err , res ) {
123
+ assert . strictEqual ( res , 'PONG' ) ;
124
+ // Check if the previous info command used the internal individual info command
125
+ assert . notDeepEqual ( client . server_info , { } ) ;
126
+ client . server_info = { } ;
127
+ } ) ;
128
+ client . send_command ( 'info' , undefined , undefined ) ;
129
+ client . send_command ( 'ping' , function ( err , res ) {
130
+ assert . strictEqual ( res , 'PONG' ) ;
131
+ // Check if the previous info command used the internal individual info command
132
+ assert . notDeepEqual ( client . server_info , { } ) ;
133
+ client . server_info = { } ;
134
+ } ) ;
113
135
client . send_command ( 'info' , undefined , function ( err , res ) {
114
136
assert ( / r e d i s _ v e r s i o n / . test ( res ) ) ;
137
+ // The individual info command should also be called by using send_command
138
+ // console.log(info, client.server_info);
139
+ assert . notDeepEqual ( client . server_info , { } ) ;
140
+ done ( ) ;
141
+ } ) ;
142
+ } ) ;
143
+
144
+ it ( 'using multi with send_command should work as individual command instead of using the internal multi' , function ( done ) {
145
+ // This is necessary to keep backwards compatibility and it is the only way to handle multis as you want in node_redis
146
+ client . send_command ( 'multi' ) ;
147
+ client . send_command ( 'set' , [ 'foo' , 'bar' ] , helper . isString ( 'QUEUED' ) ) ;
148
+ client . get ( 'foo' ) ;
149
+ client . exec ( function ( err , res ) { // exec is not manipulated if not fired by the individual multi command
150
+ // As the multi command is handled individually by the user he also has to handle the return value
151
+ assert . strictEqual ( res [ 0 ] . toString ( ) , 'OK' ) ;
152
+ assert . strictEqual ( res [ 1 ] . toString ( ) , 'bar' ) ;
115
153
done ( ) ;
116
154
} ) ;
117
155
} ) ;
118
156
119
- it ( 'using another type as cb should just work as if there were no callback parameter' , function ( done ) {
120
- client . send_command ( 'set' , [ 'test' , 'bla' ] , [ true ] ) ;
121
- client . get ( 'test' , function ( err , res ) {
122
- assert . equal ( res , 'bla' ) ;
157
+ it ( 'multi should be handled special' , function ( done ) {
158
+ client . send_command ( 'multi' , undefined , helper . isString ( 'OK' ) ) ;
159
+ var args = [ 'test' , 'bla' ] ;
160
+ client . send_command ( 'set' , args , helper . isString ( 'QUEUED' ) ) ;
161
+ assert . deepEqual ( args , [ 'test' , 'bla' ] ) ; // Check args manipulation
162
+ client . get ( 'test' , helper . isString ( 'QUEUED' ) ) ;
163
+ client . exec ( function ( err , res ) {
164
+ // As the multi command is handled individually by the user he also has to handle the return value
165
+ assert . strictEqual ( res [ 0 ] . toString ( ) , 'OK' ) ;
166
+ assert . strictEqual ( res [ 1 ] . toString ( ) , 'bla' ) ;
167
+ done ( ) ;
168
+ } ) ;
169
+ } ) ;
170
+
171
+ it ( 'using another type as cb should throw' , function ( ) {
172
+ try {
173
+ client . send_command ( 'set' , [ 'test' , 'bla' ] , [ true ] ) ;
174
+ throw new Error ( 'failed' ) ;
175
+ } catch ( err ) {
176
+ assert . strictEqual ( err . message , 'Wrong input type "Array" for callback function' ) ;
177
+ }
178
+ try {
179
+ client . send_command ( 'set' , [ 'test' , 'bla' ] , null ) ;
180
+ throw new Error ( 'failed' ) ;
181
+ } catch ( err ) {
182
+ assert . strictEqual ( err . message , 'Wrong input type "null" for callback function' ) ;
183
+ }
184
+ } ) ;
185
+
186
+ it ( 'command argument has to be of type string' , function ( ) {
187
+ try {
188
+ client . send_command ( true , [ 'test' , 'bla' ] , function ( ) { } ) ;
189
+ throw new Error ( 'failed' ) ;
190
+ } catch ( err ) {
191
+ assert . strictEqual ( err . message , 'Wrong input type "Boolean" for command name' ) ;
192
+ }
193
+ try {
194
+ client . send_command ( undefined , [ 'test' , 'bla' ] , function ( ) { } ) ;
195
+ throw new Error ( 'failed' ) ;
196
+ } catch ( err ) {
197
+ assert . strictEqual ( err . message , 'Wrong input type "undefined" for command name' ) ;
198
+ }
199
+ try {
200
+ client . send_command ( null , [ 'test' , 'bla' ] , function ( ) { } ) ;
201
+ throw new Error ( 'failed' ) ;
202
+ } catch ( err ) {
203
+ assert . strictEqual ( err . message , 'Wrong input type "null" for command name' ) ;
204
+ }
205
+ } ) ;
206
+
207
+ it ( 'args may only be of type Array or undefined' , function ( ) {
208
+ try {
209
+ client . send_command ( 'info' , 123 ) ;
210
+ throw new Error ( 'failed' ) ;
211
+ } catch ( err ) {
212
+ assert . strictEqual ( err . message , 'Wrong input type "Number" for args' ) ;
213
+ }
214
+ } ) ;
215
+
216
+ it ( 'passing a callback as args and as callback should throw' , function ( ) {
217
+ try {
218
+ client . send_command ( 'info' , function a ( ) { } , function b ( ) { } ) ;
219
+ throw new Error ( 'failed' ) ;
220
+ } catch ( err ) {
221
+ assert . strictEqual ( err . message , 'Wrong input type "Function" for args' ) ;
222
+ }
223
+ } ) ;
224
+
225
+ it ( 'multi should be handled special' , function ( done ) {
226
+ client . send_command ( 'multi' , undefined , helper . isString ( 'OK' ) ) ;
227
+ var args = [ 'test' , 'bla' ] ;
228
+ client . send_command ( 'set' , args , helper . isString ( 'QUEUED' ) ) ;
229
+ assert . deepEqual ( args , [ 'test' , 'bla' ] ) ; // Check args manipulation
230
+ client . get ( 'test' , helper . isString ( 'QUEUED' ) ) ;
231
+ client . exec ( function ( err , res ) {
232
+ // As the multi command is handled individually by the user he also has to handle the return value
233
+ assert . strictEqual ( res [ 0 ] . toString ( ) , 'OK' ) ;
234
+ assert . strictEqual ( res [ 1 ] . toString ( ) , 'bla' ) ;
123
235
done ( ) ;
124
236
} ) ;
125
237
} ) ;
126
238
127
- it ( 'misusing the function should eventually throw (no command)' , function ( done ) {
128
- client . send_command ( true , 'info' , function ( err , res ) {
129
- assert ( / E R R P r o t o c o l e r r o r / . test ( err . message ) ) ;
130
- assert . equal ( err . command , undefined ) ;
131
- assert . equal ( err . code , 'ERR' ) ;
239
+ it ( 'the args array may contain a arbitrary number of arguments' , function ( done ) {
240
+ client . send_command ( 'mset' , [ 'foo' , 1 , 'bar' , 2 , 'baz' , 3 ] , helper . isString ( 'OK' ) ) ;
241
+ client . mget ( [ 'foo' , 'bar' , 'baz' ] , function ( err , res ) {
242
+ // As the multi command is handled individually by the user he also has to handle the return value
243
+ assert . strictEqual ( res [ 0 ] . toString ( ) , '1' ) ;
244
+ assert . strictEqual ( res [ 1 ] . toString ( ) , '2' ) ;
245
+ assert . strictEqual ( res [ 2 ] . toString ( ) , '3' ) ;
132
246
done ( ) ;
133
247
} ) ;
134
248
} ) ;
135
249
136
- it ( 'misusing the function should eventually throw (wrong args) ' , function ( done ) {
137
- client . send_command ( 'info' , false , function ( err , res ) {
138
- assert . equal ( err . message , ' ERR Protocol error: invalid multibulk length' ) ;
250
+ it ( 'send_command with callback as args' , function ( done ) {
251
+ client . send_command ( 'abcdef' , function ( err , res ) {
252
+ assert . strictEqual ( err . message , " ERR unknown command 'abcdef'" ) ;
139
253
done ( ) ;
140
254
} ) ;
141
255
} ) ;
0 commit comments