Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 3 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
45 README.md
@@ -279,7 +279,7 @@ fakeredis provides some additional methods on the client object.
fakeredisClient.pretty ();
fakeredisClient.pretty ( "p*tte?n" );
- fakeredisClient.pretty ({ label : "zsets before dbflush", pattern : "myz*" });
+ fakeredisClient.pretty ( options );
`.pretty()` will prettyprint to stdout the entire keyspace
or a subset of keys specificed with a redis pattern
@@ -295,7 +295,7 @@ so that you can do stuff like:
client.SADD ( 'hello', 'world', 'Jenny', 'Sam' );
client.LPUSH ( 'mylist', 'hey', 'ho', 'letsgo' );
- client.pretty ({ label : "my stuff" });
+ client.pretty ({ label : "my stuff", pattern : "*" });
Which would print *(in color!)*
@@ -311,16 +311,16 @@ Which would print *(in color!)*
### Keyspace dumps:
fakeredisClient.getKeypsace ( callback );
- fakeredisClient.getKeypsace ( "p*tte?n", callback )
- fakeredisClient.getKeypsace ({ label : "zsets before dbflush", pattern : "myz*" }, callback );
+ fakeredisClient.getKeypsace ( "p*tte?n", callback );
+ fakeredisClient.getKeyspace ( options, callback );
Will callback ( err, data ) with an array
that enumerates the whole keyspace,
or the requested subset, in the following manner:
[
- [ key1, ttl1, type1, value1 ],
- [ key2, ttl2, type2, value2 ],
+ key1, ttl1, type1, value1,
+ key2, ttl2, type2, value2,
...
]
@@ -328,12 +328,43 @@ The keyspace is sorted lexicographically by key,
string values are strings,
list values are the output of `LRANGE 0 -1`,
hashes come out as the output of `HGETALL` for hashes
-(no syntactic sugar though, so an Array of `[ field, value, field, value, ... ]`,
+(no syntactic sugar though, so an Array of `[ field, value, field, value, ... ]`),
`SMEMBERS` output is used for sets,
and `ZRANGE 0 -1 WITHSCORES` for sorted sets,
each of which is sorted lexicographically in a way that makes sense,
so that the final result is simple enough to assert deep equality against.
+In any case, you'll probably need to reformat these keyspace dumps
+to a format that makes more sense for your testing needs.
+There are a couple of transforms that are included out of the box:
+
+ fakeredisClient.getKeypsace ({ pattern : "myz*", map : true }, callback );
+
+If you only care about the key and value of each entry,
+you can set the **map** option to a truthy value,
+you will instead receive the keyspace dump as a key-value map of the kind:
+
+ {
+ key1 : value1,
+ key2 : value2,
+ ...
+ }
+
+This means you're skipping ttl and key type info though. You can also do:
+
+ fakeredisClient.getKeypsace ({ pattern : "myz*", group : true }, callback );
+
+Which will return an `Array` of `Array`s,
+one for each keyspace entry, so that you end up with:
+
+ [
+ [ key1, ttl1, type1, value1 ],
+ [ key2, ttl2, type2, value2 ],
+ ...
+ ]
+
+The benefit of this option is that you can sort the outer array as you like more easily.
+
## LICENSE - MIT
View
8 lib/backend.js
@@ -1775,13 +1775,13 @@ exports.Backend.prototype =
*/
FAKE_DUMP : function ( pattern )
{
- var keys = this.KEYS ( pattern ), i, n = keys.length, arr = [], out, key, type;
+ var keys = this.KEYS ( pattern ), i, n = keys.length, out = [], key, type;
for ( i = 0; i < n; i ++ )
{
key = keys [ i ];
type = this.TYPE ( key );
- out = [ key, this.TTL ( key ), type.getStatus () ];
+ out.push ( key, this.TTL ( key ), type.getStatus () );
if ( type === STRING )
out.push ( this.GET ( key ) );
@@ -1795,11 +1795,9 @@ exports.Backend.prototype =
out.push ( this.ZRANGE ( key, '-inf', '+inf', 'withscores' ) );
else
throw new Error ( "WOOT! Key type is " + type );
-
- arr [ i ] = out;
}
- return arr;
+ return out;
}
};
View
9 lib/connection.js
@@ -287,6 +287,9 @@ exports.Connection = function ( backend, minLatency, maxLatency )
var q = queue, entry, func, out, err, data, resp = [];
queue = null;
+ if ( connection.verbose )
+ console.log ( '\n' );
+
if ( q ) while (( entry = q.shift () ))
{
if ( entry === 'SKIP' )
@@ -295,6 +298,9 @@ exports.Connection = function ( backend, minLatency, maxLatency )
func = backend [ entry.command ];
out = null;
+ if ( connection.verbose )
+ console.log ( "fakeredis>", entry.command, entry.args.join ( ' ' ) );
+
if ( entry.override )
{
out = entry.override ( q );
@@ -349,6 +355,9 @@ exports.Connection = function ( backend, minLatency, maxLatency )
resp.push ( entry.callback.bind ( null, err, data ) );
}
+ if ( connection.verbose )
+ console.log ( '\n' );
+
if ( resp.length )
flush ( resp );
}
View
59 lib/helpers.js
@@ -48,7 +48,7 @@ exports.pretty = function ( options )
this.send_command ( "FAKE_DUMP", [ pattern || "*" ], function ( err, dump )
{
- var i, n = dump && dump.length, entry, style, key, ttl, type, value;
+ var i, n = dump && dump.length, style, key, ttl, type, value;
if ( err )
throw err;
@@ -57,13 +57,12 @@ exports.pretty = function ( options )
else
process.stdout.write ( '\n' );
- for ( i = 0; i < n; i ++ )
+ for ( i = 0; i < n; i += 4 )
{
- entry = dump [ i ];
- key = entry [ 0 ];
- ttl = entry [ 1 ];
- type = entry [ 2 ];
- value = entry [ 3 ];
+ key = dump [ i ];
+ ttl = dump [ i + 1 ];
+ type = dump [ i + 2 ];
+ value = dump [ i + 3 ];
style = 'white';
@@ -108,6 +107,8 @@ exports.pretty = function ( options )
exports.getKeyspace = function ( options, callback )
{
+ var cb;
+
if ( !callback && typeof options === 'function' )
{
callback = options;
@@ -117,7 +118,49 @@ exports.getKeyspace = function ( options, callback )
if ( typeof options === 'string' )
options = { pattern : options };
- this.send_command ( "FAKE_DUMP", [ options && options.pattern || "*" ], callback );
+
+ //// By default respond with an array of [ key, ttl, type, value, key2, ttl2, type2, value2, ... ]
+
+ cb = callback;
+
+
+ //// Respond with a key-value map.
+
+ if ( options && options.map )
+ cb = function ( err, data )
+ {
+ var out, i, n;
+ if ( data )
+ {
+ out = {};
+ n = data.length;
+ for ( i = 0; i < n; i += 4 )
+ out [ data [ i ] ] = data [ i + 3 ];
+ }
+
+ callback ( err, out );
+ };
+
+
+ //// Respond with an array of arrays.
+
+ else if ( options && options.group )
+ cb = function ( err, data )
+ {
+ var out, i, n;
+ if ( data )
+ {
+ out = [];
+ n = data.length;
+ for ( i = 0; i < n; i += 4 )
+ out.push ( data.slice ( i, 4 ) );
+ }
+
+ callback ( err, out );
+ };
+
+
+ this.send_command ( "FAKE_DUMP", [ options && options.pattern || "*" ], cb );
};
View
2  package.json
@@ -1,5 +1,5 @@
{ "name" : "fakeredis",
- "version" : "0.0.11",
+ "version" : "0.0.12",
"description" : "Fake redis client/pseudo-backend for the purpose of simpler testing, works as a drop-in replacement for node_redis",
"keywords" : [ "test", "tool", "simulated", "redis", "client" ],
"author" : "Hristo Dachev <tutini@gmail.com>",
View
8 test.js
@@ -240,7 +240,7 @@ process.stdout.write ( 'testing fakeredis ...\n\n' );
redis.HSETNX ( "h", "F1", "V1", test ( "HSETNX", null, 1 ) );
redis.HGETALL ( "h", test ( "HGETALL", null, { "F1": "V1", "f1": "v1", "f2": "v2", "field-3": "3", "field-4": "4" } ) );
- redis.getKeyspace ( "*h", test ( "getKeyspace() with pattern", null, [ [ "h", "-1", "hash", [ "F1", "V1", "f1", "v1", "f2", "v2", "field-3", "3", "field-4", "4" ] ] ] ) );
+ redis.getKeyspace ( "*h", test ( "getKeyspace() with pattern", null, [ "h", "-1", "hash", [ "F1", "V1", "f1", "v1", "f2", "v2", "field-3", "3", "field-4", "4" ] ] ) );
redis.HKEYS ( "h", test ( "HKEYS", null, [ "F1", "f1", "f2", "field-3", "field-4" ] ) );
redis.send_command ( "HINCRBYFLOAT", [ "h", "f1", 3.5 ], test ( "HINCRBYFLOAT fail", BAD_FLOAT, null ) );
@@ -291,7 +291,7 @@ process.stdout.write ( 'testing fakeredis ...\n\n' );
redis.LREM ( "list3", -1, "three", test ( "LREM right", null, 1 ) );
redis.LREM ( "list3", -2, "what", test ( "LREM 2right", null, 2 ) );
- redis.getKeyspace ( "*list*", test ( "lists outcome", null, [ [ "list3", "-1", "list", [ "three", "two" ] ], [ "newlist", "-1", "list", [ "what", "where", "why" ] ] ] ) );
+ redis.getKeyspace ( "*list*", test ( "lists outcome", null, [ "list3", "-1", "list", [ "three", "two" ], "newlist", "-1", "list", [ "what", "where", "why" ] ] ) );
@@ -307,7 +307,7 @@ process.stdout.write ( 'testing fakeredis ...\n\n' );
redis2.RPUSH ( "BL-c", "CC1", "CC2", "CC3", test ( "RPUSH + BLPOP", null, 3 ) );
- redis.getKeyspace ( "BL-?", test ( "blocking lists outcome", null, [ [ "BL-b", "-1", "list", [ "BB1", "BB2" ] ], [ "BL-c", "-1", "list", [ "CC2", "CC3" ] ] ] ) );
+ redis.getKeyspace ( "BL-?", test ( "blocking lists outcome", null, [ "BL-b", "-1", "list", [ "BB1", "BB2" ], "BL-c", "-1", "list", [ "CC2", "CC3" ] ] ) );
@@ -334,7 +334,7 @@ process.stdout.write ( 'testing fakeredis ...\n\n' );
redis.FLUSHDB ();
redis.GETSET ( "hello", "world", test ( "GETSET null", null, null ) );
- redis.getKeyspace ( test ( "getKeyspace() flushed, nopat", null, [ [ "hello", "-1", "string", "world" ] ] ) );
+ redis.getKeyspace ( test ( "getKeyspace() flushed, nopat", null, [ "hello", "-1", "string", "world" ] ) );
},
1000
);

No commit comments for this range

Something went wrong with that request. Please try again.