redis-mock
Advanced tools
Comparing version 0.14.0 to 0.15.0
@@ -26,1 +26,25 @@ var charMap = { | ||
} | ||
var mockCallback = exports.mockCallback = function(err, reply) {}; | ||
var parseCallback = exports.parseCallback = function(args) { | ||
var callback; | ||
var len = args.length; | ||
if ('function' === typeof args[len - 1]) { | ||
callback = args[len-1]; | ||
} | ||
return callback; | ||
}; | ||
var validKeyType = exports.validKeyType = function(mockInstance, key, type, callback) { | ||
if (mockInstance.storage[key] && mockInstance.storage[key].type !== type) { | ||
var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value'); | ||
mockInstance._callCallback(callback, err); | ||
return false; | ||
} | ||
return true; | ||
}; | ||
var initKey = exports.initKey = function(mockInstance, key, fn) { | ||
mockInstance.storage[key] = mockInstance.storage[key] || fn(); | ||
}; |
@@ -0,3 +1,15 @@ | ||
var helpers = require("./helpers.js"); | ||
var Item = require("./item.js"); | ||
var mockCallback = helpers.mockCallback; | ||
var validKeyType = function(mockInstance, key, callback) { | ||
return helpers.validKeyType(mockInstance, key, 'list', callback) | ||
}; | ||
var initKey = function(mockInstance, key) { | ||
return helpers.initKey(mockInstance, key, Item.createList); | ||
}; | ||
/** | ||
@@ -11,9 +23,28 @@ * Llen | ||
var push = function (fn, mockInstance, key, values, callback) { | ||
if (mockInstance.storage[key] && mockInstance.storage[key].type !== "list") { | ||
return mockInstance._callCallback(callback, | ||
new Error("ERR Operation against a key holding the wrong kind of value")); | ||
var push = function (fn, args) { | ||
var len = args.length; | ||
if (len < 2) { | ||
return | ||
} | ||
mockInstance.storage[key] = mockInstance.storage[key] || new Item.createList(); | ||
var mockInstance = args[0]; | ||
var key = args[1]; | ||
var callback = helpers.parseCallback(args); | ||
if (callback == undefined) { | ||
callback = mockCallback; | ||
} | ||
if (!validKeyType(mockInstance, key, callback)) { | ||
return | ||
} | ||
// init key | ||
initKey(mockInstance, key); | ||
// parse only the values from the args; | ||
var values = []; | ||
for (var i=2, val; i < len; i++) { | ||
val = args[i]; | ||
if ('function' == typeof val) { | ||
break; | ||
} | ||
values.push(val); | ||
} | ||
fn.call(mockInstance.storage[key], values); | ||
@@ -28,4 +59,4 @@ var length = mockInstance.storage[key].value.length; | ||
*/ | ||
exports.lpush = function (mockInstance, key, values, callback) { | ||
push(Item._list.prototype.lpush, mockInstance, key, values, callback); | ||
exports.lpush = function () { | ||
push(Item._list.prototype.lpush, arguments); | ||
}; | ||
@@ -36,4 +67,4 @@ | ||
*/ | ||
exports.rpush = function (mockInstance, key, values, callback) { | ||
push(Item._list.prototype.rpush, mockInstance, key, values, callback); | ||
exports.rpush = function () { | ||
push(Item._list.prototype.rpush, arguments); | ||
}; | ||
@@ -40,0 +71,0 @@ |
@@ -445,31 +445,12 @@ /** | ||
var push = function (fn, key, values, callback) { | ||
var vals = []; | ||
var hasCallback = typeof(arguments[arguments.length - 1]) === "function"; | ||
for (var i = 2; i < (hasCallback ? arguments.length - 1 : arguments.length); i++) { | ||
vals.push(arguments[i]); | ||
} | ||
if (hasCallback) { | ||
fn.call(this, MockInstance, key, vals, arguments[arguments.length - 1]); | ||
} else { | ||
fn.call(this, MockInstance, key, vals); | ||
} | ||
RedisClient.prototype.lpush = RedisClient.prototype.LPUSH = function () { | ||
var args = parseArguments(arguments); | ||
listfunctions.lpush.apply(this, [MockInstance].concat(args)); | ||
} | ||
RedisClient.prototype.lpush = RedisClient.prototype.LPUSH = function (key, values, callback) { | ||
var args = [listfunctions.lpush]; | ||
for (var i = 0; i < arguments.length; i++) { | ||
args.push(arguments[i]); | ||
} | ||
push.apply(this, args); | ||
RedisClient.prototype.rpush = RedisClient.prototype.RPUSH = function () { | ||
var args = parseArguments(arguments); | ||
listfunctions.rpush.apply(this, [MockInstance].concat(args)); | ||
} | ||
RedisClient.prototype.rpush = RedisClient.prototype.RPUSH = function (key, values, callback) { | ||
var args = [listfunctions.rpush]; | ||
for (var i = 0; i < arguments.length; i++) { | ||
args.push(arguments[i]); | ||
} | ||
push.apply(this, args); | ||
} | ||
RedisClient.prototype.lpushx = RedisClient.prototype.LPUSHX = function (key, value, callback) { | ||
@@ -476,0 +457,0 @@ listfunctions.lpushx.call(this, MockInstance, key, value, callback); |
@@ -1,2 +0,3 @@ | ||
var Item = require('./item.js'); | ||
var helpers = require("./helpers.js"); | ||
var Item = require("./item.js"); | ||
@@ -33,16 +34,10 @@ /** | ||
var mockCallback = function(err, reply) {}; | ||
var mockCallback = helpers.mockCallback; | ||
var validKeyType = function(mockInstance, key, callback) { | ||
if (mockInstance.storage[key] && mockInstance.storage[key].type !== 'sortedset') { | ||
var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value'); | ||
mockInstance._callCallback(callback, err); | ||
return false; | ||
} | ||
return true; | ||
return helpers.validKeyType(mockInstance, key, 'sortedset', callback) | ||
} | ||
var initKey = function(mockInstance, key) { | ||
mockInstance.storage[key] = mockInstance.storage[key] || | ||
new Item.createSortedSet(); | ||
return helpers.initKey(mockInstance, key, Item.createSortedSet); | ||
} | ||
@@ -278,11 +273,2 @@ | ||
var parseCallback = function(args) { | ||
var callback; | ||
var len = args.length; | ||
if ('function' === typeof args[len - 1]) { | ||
callback = args[len-1]; | ||
} | ||
return callback; | ||
}; | ||
// ZADD key [NX|XX] [CH] [INCR] score member [score member ...] | ||
@@ -296,3 +282,3 @@ // Add one or more members to a sorted set, or update its score if it already exists | ||
} | ||
var callback = parseCallback(arguments); | ||
var callback = helpers.parseCallback(arguments); | ||
if (!validKeyType(mockInstance, key, callback)) { | ||
@@ -485,3 +471,3 @@ return | ||
var callback = parseCallback(arguments); | ||
var callback = helpers.parseCallback(arguments); | ||
if (callback == undefined) { | ||
@@ -488,0 +474,0 @@ callback = mockCallback; |
{ | ||
"name": "redis-mock", | ||
"version": "0.14.0", | ||
"version": "0.15.0", | ||
"description": "Redis client mock object for unit testing", | ||
@@ -5,0 +5,0 @@ "author": "Kristian Faeldt <kristian.faeldt@gmail.com>", |
@@ -57,2 +57,26 @@ var redismock = require("../") | ||
it("should check a single rpush array arg works", function (done) { | ||
var r = redismock.createClient(); | ||
r.rpush([testKey, testValue], function (err, result) { | ||
result.should.equal(1); | ||
r.rpop(testKey, function (err, result) { | ||
result.should.equal(testValue + ""); | ||
r.end(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("should check a single lpush array arg works", function (done) { | ||
var r = redismock.createClient(); | ||
r.lpush([testKey, testValue], function (err, result) { | ||
result.should.equal(1); | ||
r.rpop(testKey, function (err, result) { | ||
result.should.equal(testValue + ""); | ||
r.end(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("should be a queue", function (done) { | ||
@@ -59,0 +83,0 @@ var r = redismock.createClient(); |
174272
5157