_callCallback = function (callback, err, result) { if (callback) { process.nextTick(function () { callback(err, result); }); } }
...
if (mockInstance.storage[hash].type === "hash") {
value = mockInstance.storage[hash].value[key];
} else {
err = new Error("ERR Operation against a key holding the wrong kind of value");
}
}
mockInstance._callCallback(callback, err, value);
}
/**
* Hexists
*/
exports.hexists = function (mockInstance, hash, key, callback) {
...
hdel = function (mockInstance, hash, key, callback) { var nb = 0; //TODO: Support multiple values as key if (mockInstance.storage[hash]) { if (mockInstance.storage[hash].type === "hash") { if (mockInstance.storage[hash].value[key]) { delete mockInstance.storage[hash].value[key]; nb++; } } else { err = new Error("ERR Operation against a key holding the wrong kind of value"); } } mockInstance._callCallback(callback, null, nb); }
...
});
it("should delete a value", function (done) {
var r = redismock.createClient();
r.hdel(testHash, testKey, function (err, result) {
result.should.equal(1);
r.end(true);
done();
...
hexists = function (mockInstance, hash, key, callback) { var b = 0; var err = null; if (mockInstance.storage[hash]) { if (mockInstance.storage[hash].type === "hash") { b = mockInstance.storage[hash].value[key] ? 1 : 0; } else { err = new Error("ERR Operation against a key holding the wrong kind of value"); } } mockInstance._callCallback(callback, err, b); }
...
var testKeyNotExist = "myKeyNonExistant";
it("should not say that non-existant values exist", function (done) {
var r = redismock.createClient();
r.hexists(testHash, testKey, function (err, result) {
result.should.equal(0);
r.end(true);
done();
});
...
hget = function (mockInstance, hash, key, callback) { var value = null; var err = null; if (mockInstance.storage[hash]) { if (mockInstance.storage[hash].type === "hash") { value = mockInstance.storage[hash].value[key]; } else { err = new Error("ERR Operation against a key holding the wrong kind of value"); } } mockInstance._callCallback(callback, err, value); }
...
});
it("should get a value that has been set", function (done) {
var r = redismock.createClient();
r.hget(testHash, testKey, function (err, result) {
result.should.equal(testValue);
r.end(true);
done();
...
hgetall = function (mockInstance, hash, callback) { // TODO: Confirm if this should return null or empty obj when key does not exist var obj = {}; var nb = 0; if (mockInstance.storage[hash] && mockInstance.storage[hash].type !== "hash") { return mockInstance._callCallback(callback, new Error("ERR Operation against a key holding the wrong kind of value")); } if (mockInstance.storage[hash]) { for (var k in mockInstance.storage[hash].value) { nb++; obj[k] = mockInstance.storage[hash].value[k]; } } mockInstance._callCallback(callback, null, nb === 0 ? null : obj); }
...
});
//HGETALL
it("should be able to get all values for hash", function (done) {
var r = redismock.createClient();
r.hgetall(mHash2, function (err, result) {
should.exist(result);
result.should.have.property(mKey1, mValue1);
result.should.have.property(mKey2, mValue2);
result.should.have.property(mKey3, mValue3);
result.should.have.property(mKey4, mValue4);
...
hincrby = function (mockInstance, hash, key, increment, callback) { if (mockInstance.storage[hash]) { if (mockInstance.storage[hash].type !== "hash") { return mockInstance._callCallback(callback, new Error("ERR Operation against a key holding the wrong kind of value")); } } else { mockInstance.storage[hash] = Item.createHash(); } if (mockInstance.storage[hash].value[key] && !/^\d+$/.test(mockInstance.storage[hash].value[key])) { return mockInstance._callCallback(callback, new Error("ERR hash value is not an integer")); } mockInstance.storage[hash].value[key] = parseInt(mockInstance.storage[hash].value[key]) || 0; mockInstance.storage[hash].value[key] += increment; mockInstance.storage[hash].value[key] += ""; //Because HGET returns Strings mockInstance._callCallback(callback, null, parseInt(mockInstance.storage[hash].value[key])); //Because HINCRBY returns integers }
...
var testKey = "myKeyToIncr";
it("should increment an attribute of the hash", function (done) {
var r = redismock.createClient();
r.hincrby(testHash, testKey, 2, function (err, result) {
result.should.equal(2);
r.hget(testHash, testKey, function (err, result) {
result.should.equal("2");
r.end(true);
done();
});
...
hincrbyfloat = function (mockInstance, hash, key, increment, callback) { if (mockInstance.storage[hash]) { if (mockInstance.storage[hash].type !== "hash") { return mockInstance._callCallback(callback, new Error("ERR Operation against a key holding the wrong kind of value")); } } else { mockInstance.storage[hash] = Item.createHash(); } function isFloat(n) { return n === +n && n !== (n|0); } if (mockInstance.storage[hash].value[key] && !isFloat(parseFloat(mockInstance.storage[hash].value[key]))) { return mockInstance._callCallback(callback, new Error("ERR value is not a valid float")); } mockInstance.storage[hash].value[key] = parseFloat(mockInstance.storage[hash].value[key]) || 0; mockInstance.storage[hash].value[key] += parseFloat(increment); //convert to string mockInstance.storage[hash].value[key] = mockInstance.storage[hash].value[key].toString(); mockInstance._callCallback(callback, null, mockInstance.storage[hash].value[key]); }
...
var num = 2.591;
var x2 = num * 2;
it("should increment an attribute of the hash", function (done) {
var r = redismock.createClient();
r.hincrbyfloat(testHash, testKey, num, function (err, result) {
result.should.equal(num.toString());
r.hget(testHash, testKey, function (err, result) {
result.should.equal(num.toString());
r.end(true);
done();
});
...
hkeys = function (mockInstance, hash, callback) { var list = []; if (mockInstance.storage[hash] && mockInstance.storage[hash].type !== "hash") { return mockInstance._callCallback(callback, new Error("ERR Operation against a key holding the wrong kind of value")); } if (mockInstance.storage[hash]) { for (var k in mockInstance.storage[hash].value) { list.push(k); } } mockInstance._callCallback(callback, null, list); }
...
})
//HKEYS
it("should be able to get all keys for hash", function (done) {
var r = redismock.createClient();
r.hkeys(mHash2, function (err, result) {
result.indexOf(mKey1).should.not.equal(-1);
result.indexOf(mKey2).should.not.equal(-1);
result.indexOf(mKey3).should.not.equal(-1);
result.indexOf(mKey4).should.not.equal(-1);
r.end(true);
...
hlen = function (mockInstance, hash, callback) { if (!mockInstance.storage[hash]) { return mockInstance._callCallback(callback, null, 0); } if (mockInstance.storage[hash].type !== "hash") { return mockInstance._callCallback(callback, new Error("ERR Operation against a key holding the wrong kind of value")); } var cnt = 0; for (var p in mockInstance.storage[hash].value) { if (mockInstance.storage[hash].value.hasOwnProperty(p)) { cnt++; } } mockInstance._callCallback(callback, null, cnt); }
...
});
it("should return length 0 when key does not exist", function (done) {
var r = redismock.createClient();
r.hlen("newHash", function (err, result) {
result.should.equal(0);
r.end(true);
done();
...
hmget = function (mockInstance) { // We require at least 3 arguments // 0: mockInstance // 1: hash name // 2: key/value object or first key name if (arguments.length <= 3) { return; } var keyValuesToGet = []; for (var i = 2; i < arguments.length; i++) { // Neither key nor value is a callback if ('function' !== typeof arguments[i] && 'function' !== typeof arguments[i]) { keyValuesToGet.push(arguments[i]); } else { break; } } var keyValues = []; var hash = arguments[1]; if (mockInstance.storage[hash]) { if (mockInstance.storage[hash].type !== "hash") { return mockInstance._callCallback(callback, new Error("ERR Operation against a key holding the wrong kind of value")); } else { for (var k in keyValuesToGet) { keyValues.push(mockInstance.storage[hash].value[keyValuesToGet[k]]) } } } else { for (var k in keyValuesToGet) { keyValues.push(null) } } // Do we have a callback? if ('function' === typeof arguments[arguments.length - 1]) { mockInstance._callCallback(arguments[arguments.length - 1], null, keyValues); } }
...
// HMGET
it("should be able to get multiple keys as multiple arguments", function (done) {
var r = redismock.createClient();
r.hmset(mHash, { mKey3: mValue3, mKey4: mValue4});
r.hmget(mHash2, mKey1, mKey2, function (err, result) {
result.should.be.an.Array().and.have.lengthOf(2);
result.should.eql([mValue1, mValue2]);
r.end(true);
done();
...
hmset = function (mockInstance, hash) { // We require at least 3 arguments // 0: mockInstance // 1: hash name // 2..N-2: key // 3..N-1: value // N: callback (optional) var len = arguments.length; if (len <= 3) { return; } var callback; if ('function' === typeof arguments[len - 1]) { callback = arguments[len-1]; } // check to see if this hash exists if (mockInstance.storage[hash]) { if (mockInstance.storage[hash].type !== "hash" && callback) { return mockInstance._callCallback(callback, new Error("ERR Operation against a key holding the wrong kind of value")); } } else { mockInstance.storage[hash] = new Item.createHash(); } for (var i = 2; i < len; i += 2) { if (len <= (i + 1)) { // should skip the callback here break; } var k = arguments[i]; var v = arguments[i + 1]; mockInstance.storage[hash].value[k] = v; } // Do we have a callback? if (callback) { mockInstance._callCallback(callback, null, "OK"); } }
...
});
// HMSET
it("should be able to set multiple keys as multiple arguments", function (done) {
var r = redismock.createClient();
r.hmset(mHash, mKey1, mValue1, mKey2, mValue2, function (err, result) {
result.should.equal("OK");
r.end(true);
done();
...
hset = function (mockInstance, hash, key, value, callback) { var update = false; if (mockInstance.storage[hash]) { if (mockInstance.storage[hash].type !== "hash") { return mockInstance._callCallback(callback, new Error("ERR Operation against a key holding the wrong kind of value")); } if (mockInstance.storage[hash].value[key]) { update = true; } } else { mockInstance.storage[hash] = Item.createHash(); } mockInstance.storage[hash].value[key] = value; mockInstance._callCallback(callback, null, update ? 0 : 1); }
...
/**
* Hsetnx
*/
exports.hsetnx = function (mockInstance, hash, key, value, callback) {
if (!mockInstance.storage[hash]
|| mockInstance.storage[hash].type !== "hash"
|| !mockInstance.storage[hash].value[key]) {
exports.hset(mockInstance, hash, key, value, callback);
} else {
mockInstance._callCallback(callback, null, 0);
}
};
/**
...
hsetnx = function (mockInstance, hash, key, value, callback) { if (!mockInstance.storage[hash] || mockInstance.storage[hash].type !== "hash" || !mockInstance.storage[hash].value[key]) { exports.hset(mockInstance, hash, key, value, callback); } else { mockInstance._callCallback(callback, null, 0); } }
...
});
});
it("should set a value that does not exist", function (done) {
var r = redismock.createClient();
r.hsetnx(testHash, testKey2, testValue, function (err, result) {
result.should.equal(1);
r.end(true);
done();
...
hvals = function (mockInstance, hash, callback) { var list = []; if (mockInstance.storage[hash] && mockInstance.storage[hash].type !== "hash") { return mockInstance._callCallback(callback, new Error("ERR Operation against a key holding the wrong kind of value")); } if (mockInstance.storage[hash]) { for (var k in mockInstance.storage[hash].value) { list.push(mockInstance.storage[hash].value[k]); } } mockInstance._callCallback(callback, null, list); }
...
});
//HVALS
it("should be able to get all vals for hash", function (done) {
var r = redismock.createClient();
r.hvals(mHash2, function (err, result) {
result.should.containEql(mValue1);
result.should.containEql(mValue2);
result.should.containEql(mValue3);
result.should.containEql(mValue4);
r.end(true);
...
initKey = function (mockInstance, key, fn) { mockInstance.storage[key] = mockInstance.storage[key] || fn(); }
...
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);
};
/**
* Llen
*/
exports.llen = function (mockInstance, key, callback) {
var length = mockInstance.storage[key] ? mockInstance.storage[key].value.length : 0;
...
mockCallback = function (err, reply) {}
n/a
parseCallback = function (args) { var callback; var len = args.length; if ('function' === typeof args[len - 1]) { callback = args[len-1]; } return callback; }
...
var push = function (fn, args) {
var len = args.length;
if (len < 2) {
return
}
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
...
patternToRegex = function (pattern) { var fixed = pattern.replace(patternChanger, function(matched) { return charMap[matched] }); return new RegExp('^' + fixed + '$'); }
...
var badStrings = ["h^llo", "h?llo", "h*llo", "h.llo", "h(llo", "h)llo
x22;, "h{llo", "h}llo", "h\\llo", "h[llo", "h]llo"]
testPattern(pattern, goodStrings, badStrings)
})
})
function testPattern(pattern, passes, fails) {
var regex = helpers.patternToRegex(pattern)
withPattern(regex, passes, true)
withPattern(regex, fails, false)
}
function withPattern(regex, strings, expected) {
strings.forEach(function (x) {
should.equal(regex.test(x), expected)
...
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 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);
};
/**
...
_hash = function () { RedisItem.call(this, "hash"); this.value = {}; }
n/a
_item = function (type, expire) { // We keep type so we don't have to use instanceof maybe this // can be changed to something more clever this.type = type || 0; this.expires = expire || -1; }
n/a
_list = function () { RedisItem.call(this, "list"); this.value = []; }
n/a
_set = function () { RedisItem.call(this, "set"); this.value = []; }
n/a
_sortedset = function () { RedisItem.call(this, "sortedset"); this.value = {}; }
n/a
_string = function (value, expires) { RedisItem.call(this, "string", expires); this.value = String(value); }
n/a
_stringify = function (value) { return typeof(value) === "object" ? JSON.stringify(value) : value + ''; }
...
}
len = mockInstance.storage[key].value.length;
if (len <= index || -len > index) {
return mockInstance._callCallback(callback,
new Error("ERR index out of range"));
}
if (index < 0) {
mockInstance.storage[key].value[len + index] = Item._stringify(value);
} else {
mockInstance.storage[key].value[index] = Item._stringify(value);
}
mockInstance._callCallback(callback, null, res);
};
/**
...
createHash = function () { return new RedisHash(); }
...
return mockInstance._callCallback(callback,
new Error("ERR Operation against a key holding the wrong kind of value"));
}
if (mockInstance.storage[hash].value[key]) {
update = true;
}
} else {
mockInstance.storage[hash] = Item.createHash();
}
mockInstance.storage[hash].value[key] = value;
mockInstance._callCallback(callback, null, update ? 0 : 1);
};
...
createList = function () { return new RedisList(); }
...
item.value.should.eql({});
});
});
describe("Item.createList", function () {
it('should create an empty list', function () {
var elt = "foo";
var item = RedisItem.createList();
item.type.should.equal("list");
item.expires.should.equal(-1);
item.value.should.eql([]);
should.exist(item.lpush);
should.exist(item.lpop);
should.exist(item.rpush);
...
createSet = function () { return new RedisSet(); }
...
*/
exports.sadd = function (mockInstance, key, members, callback) {
if (mockInstance.storage[key] && mockInstance.storage[key].type !== 'set') {
var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
return mockInstance._callCallback(callback, err);
}
mockInstance.storage[key] = mockInstance.storage[key] || new Item.createSet();
var set = mockInstance.storage[key].value;
var addCount = 0;
for (var i = 0; i < members.length; i++) {
if (set.indexOf(Item._stringify(members[i])) < 0) {
set.push(Item._stringify(members[i]));
addCount++;
...
createSortedSet = function () { return new RedisSortedSet(); }
...
item.expires.should.equal(1000);
item.value.should.equal(elt);
});
});
describe("Item.createSortedSet", function () {
it('should create an empty sortedset', function () {
var item = RedisItem.createSortedSet();
item.type.should.equal("sortedset");
item.expires.should.equal(-1);
item.value.should.eql({});
});
});
...
createString = function (elt, expire) { return new RedisString(elt, expire); }
...
var Item = require("./item.js");
/**
* Set
*/
exports.set = function (mockInstance, key, value, callback) {
mockInstance.storage[key] = Item.createString(value);
mockInstance._callCallback(callback, null, "OK");
};
/**
* Ping
*/
...
keys = function (mockInstance, pattern, callback) { var regex = patternToRegex(pattern); var keys = []; for (var key in mockInstance.storage) { if (regex.test(key)) { keys.push(key); } } mockInstance._callCallback(callback, null, keys); }
...
*/
exports.punsubscribe = function () {
var self = this
, subcriptions = arguments;
// Unsubscribe from ALL channels
if (!arguments.length) {
subcriptions = Object.keys(self.psubscriptions);
this.pub_sub_mode = false;
}
for (var i = 0; i < subcriptions.length; i++) {
if ('string' == typeof arguments[i]) {
// Event on next tick to emulate an actual server call
var channelName = arguments[i];
...
del = function (mockInstance, keys, callback) { if (!(keys instanceof Array)) { keys = [keys]; } var keysDeleted = 0; for (var i = 0; i < keys.length; i++) { if (keys[i] in mockInstance.storage) { delete mockInstance.storage[keys[i]]; keysDeleted++; } } mockInstance._callCallback(callback, null, keysDeleted); }
...
redismock = require('redis');
}
describe("del", function () {
it("should do nothing with non-existant keys", function (done) {
var r = redismock.createClient();
r.del(["key1", "key2", "key3"], function (err, result
) {
result.should.equal(0);
r.del("key4", function (err, result) {
result.should.equal(0);
r.end(true);
done();
});
});
...
exists = function (mockInstance, key, callback) { var result = key in mockInstance.storage ? 1 : 0; mockInstance._callCallback(callback, null, result); }
...
describe("exists", function () {
it("should return 0 for non-existing keys", function (done) {
var r = redismock.createClient();
r.exists("test", function (err, result) {
result.should.equal(0);
r.end(true);
done();
...
expire = function (mockInstance, key, seconds, callback) { var result = 0; var obj = mockInstance.storage[key]; if (obj) { var now = new Date().getTime(); var milli = Math.min(seconds*1000, Math.pow(2, 31) - 1); if (mockInstance.storage[key]._expire) { clearTimeout(mockInstance.storage[key]._expire); } mockInstance.storage[key].expires = new Date(now + milli); mockInstance.storage[key]._expire = setTimeout(function() { delete mockInstance.storage[key]; }, milli); result = 1; } mockInstance._callCallback(callback, null, result); }
...
});
describe("expire", function () {
it("should return 0 for non-existing key", function (done) {
var r = redismock.createClient();
r.expire("test", 10, function (err, result) {
result.should.equal(0);
r.end(true);
done();
});
});
it("should return 1 when timeout set on existing key", function (done) {
...
ttl = function (mockInstance, key, callback) { var result = 0; var obj = mockInstance.storage[key]; if (obj) { var now = new Date().getTime(); var expires = mockInstance.storage[key].expires instanceof Date ? mockInstance.storage[key].expires.getTime() : -1; var seconds = (expires - now) / 1000; if (seconds > 0) { result = seconds; } else { result = -1; } } else { result = -2; } mockInstance._callCallback(callback, null, result); }
...
r.set("test", "test", function (err, result) {
r.expire("test", 100, function (err, result) {
result.should.equal(1);
setTimeout(function () {
r.ttl("test", function (err, ttl) {
if (err) {
done(err);
}
ttl.should.be.within(1, 99);
r.del("test");
...
blpop = function (mockInstance, keys, timeout, callback) { bpop.call(this, Item._list.prototype.lpop, mockInstance, keys, timeout, callback); }
...
describe("blpop", function () {
it("should block until the end of the timeout", function (done) {
var r = redismock.createClient();
var time = false;
r.blpop("foo8", 1, function (err, result) {
should.not.exist(result);
time.should.equal(true);
done();
});
...
brpop = function (mockInstance, keys, timeout, callback) { bpop.call(this, Item._list.prototype.rpop, mockInstance, keys, timeout, callback); }
...
});
describe("brpop", function () {
it("should block until the end of the timeout", function (done) {
var r = redismock.createClient();
var time = false;
r.brpop("foo", 1, function (err, result) {
should.not.exist(result);
time.should.equal(true);
done();
});
setTimeout(function () {
time = true;
...
lindex = function (mockInstance, key, index, callback) { var val = null; if (mockInstance.storage[key]) { if (mockInstance.storage[key].type !== "list") { return mockInstance._callCallback(callback, new Error("ERR Operation against a key holding the wrong kind of value")); } if (index < 0 && -mockInstance.storage[key].value.length <= index) { val = mockInstance.storage[key].value[mockInstance.storage[key].value.length + index]; } else if (mockInstance.storage[key].value.length > index) { val = mockInstance.storage[key].value[index]; } } mockInstance._callCallback(callback, null, val); }
...
var keyUndefined = "keyUndefined";
var testValues = [1, 2, 3, 4, 5];
it("getting index of non exisiting list", function (done) {
var r = redismock.createClient();
r.lindex(keyUndefined, 0, function (err, result) {
should.not.exist(result);
r.lindex(keyUndefined, 12, function (err, result) {
should.not.exist(result);
...
llen = function (mockInstance, key, callback) { var length = mockInstance.storage[key] ? mockInstance.storage[key].value.length : 0; mockInstance._callCallback(callback, null, length); }
...
var testKey = "myKey3";
var testValues = [1, 2, 3, 4, 5];
var testValue = 10;
it("should return 0", function (done) {
var r = redismock.createClient();
r.llen(testKey, function (err, result) {
result.should.equal(0);
r.end(true);
done();
});
});
it("should return 5 and evolve", function (done) {
...
lpop = function (mockInstance, key, callback) { pop.call(this, Item._list.prototype.lpop, mockInstance, key, callback); }
...
it('should pop at the front of the list', function () {
var item = RedisItem.createList();
item.type.should.equal("list");
item.expires.should.equal(-1);
item.value = ['a', 'b', 'c'];
item.lpop().should.eql('a');
});
});
...
lpush = function () { push(Item._list.prototype.lpush, arguments); }
...
it('should push at the front of the list', function () {
var elt = "foo";
var elt2 = true;
var item = RedisItem.createList();
item.type.should.equal("list");
item.expires.should.equal(-1);
item.lpush([elt]);
item.lpush([elt2]);
item.value.should.eql([elt2 + '', elt]);
});
it('should pop at the back of the list', function () {
var item = RedisItem.createList();
...
lpushx = function (mockInstance, key, value, callback) { pushx(Item._list.prototype.lpush, mockInstance, key, value, callback); }
...
describe("lpushx", function (argument) {
var testKey = "myKey9";
it("tries to push on empty list", function (done) {
var r = redismock.createClient();
r.lpushx(testKey, 3, function (err, result) {
result.should.equal(0);
r.lindex(testKey, 0, function (err, result) {
should.not.exist(result);
...
lrange = function (mockInstance, key, startIndex, stopIndex, callback) { var val = []; var index1 = startIndex; var index2 = stopIndex; if (mockInstance.storage[key]) { if (mockInstance.storage[key].type !== "list") { return mockInstance._callCallback(callback, new Error("ERR Operation against a key holding the wrong kind of value")); } index1 = index1 >= 0 ? index1 : Math.max(mockInstance.storage[key].value.length + index1, 0); index2 = index2 >= 0 ? index2 : Math.max(mockInstance.storage[key].value.length + index2, 0); val = mockInstance.storage[key].value.slice(index1, index2 + 1); } mockInstance._callCallback(callback, null, val); }
...
var key2 = "lrange2";
var key3 = "lrange3";
var keyU = "keyMissing";
var testValues = [1, 2, 3, 4, 5];
it("getting a non-exisiting list", function (done) {
var r = redismock.createClient();
r.lrange(keyU, 0, -1, function (err, result) {
result.should.be.an.Array().and.have.lengthOf(0);
r.end(true);
done();
});
});
it("getting positive indexes of exisiting list", function (done) {
...
lset = function (mockInstance, key, index, value, callback) { var res = "OK"; var len = -1; if (!mockInstance.storage[key]) { return mockInstance._callCallback(callback, new Error("ERR no such key")); } if (mockInstance.storage[key].type !== "list") { return mockInstance._callCallback(callback, new Error("ERR Operation against a key holding the wrong kind of value")); } len = mockInstance.storage[key].value.length; if (len <= index || -len > index) { return mockInstance._callCallback(callback, new Error("ERR index out of range")); } if (index < 0) { mockInstance.storage[key].value[len + index] = Item._stringify(value); } else { mockInstance.storage[key].value[index] = Item._stringify(value); } mockInstance._callCallback(callback, null, res); }
...
var keyUndefined2 = "keyUndefined2";
var testValues = [1, 2, 3, 4, 5];
it("changing value of non exisiting list", function (done) {
var r = redismock.createClient();
r.lset(keyUndefined, 0, 1, function (err, result) {
err.message.should.equal("ERR no such key");
should.not.exist(result);
r.end(true);
done();
});
...
rpop = function (mockInstance, key, callback) { pop.call(this, Item._list.prototype.rpop, mockInstance, key, callback); }
...
it('should pop at the back of the list', function () {
var item = RedisItem.createList();
item.type.should.equal("list");
item.expires.should.equal(-1);
item.value = ['a', 'b', 'c'];
item.rpop().should.eql('c');
});
it('should pop at the front of the list', function () {
var item = RedisItem.createList();
item.type.should.equal("list");
item.expires.should.equal(-1);
...
rpush = function () { push(Item._list.prototype.rpush, arguments); }
...
it('should push at the back of the list', function () {
var elt = "foo";
var elt2 = true;
var item = RedisItem.createList();
item.type.should.equal("list");
item.expires.should.equal(-1);
item.rpush([elt]);
item.rpush([elt2]);
item.value.should.eql([elt, elt2 + '']);
});
it('should push at the front of the list', function () {
var elt = "foo";
var elt2 = true;
...
rpushx = function (mockInstance, key, value, callback) { pushx(Item._list.prototype.rpush, mockInstance, key, value, callback); }
...
describe("rpushx", function (argument) {
var testKey = "myKey8";
it("tries to push on empty list", function (done) {
var r = redismock.createClient();
r.rpushx(testKey, 3, function (err, result) {
result.should.equal(0);
r.lindex(testKey, 0, function (err, result) {
should.not.exist(result);
...
psubscribe = function () { var self = this; if (!arguments.length) { return; } this.pub_sub_mode = true; for (var i = 0; i < arguments.length; i++) { if ('string' == typeof arguments[i]) { // Event on next tick to emulate an actual server call var channelName = arguments[i]; process.nextTick(function () { self.psubscriptions[channelName] = patternToRegex(channelName); self.emit('psubscribe', channelName); }); } } }
...
});
r.on("punsubscribe", function (ch) {
should.equal(ch, channelName);
r.end(true);
done();
});
r.psubscribe(channelName);
});
it("suscribing and publishing with the same connection should make an error", function (done) {
var channelName = "testchannel";
var otherChannel = "otherchannel";
var r = redismock.createClient();
...
publish = function (mockInstance, channel, msg) { this.pub_sub_mode = true; process.nextTick(function () { if ((typeof msg == "object") && (msg !== null)) { msg = JSON.stringify(msg); } mockInstance.emit('message', channel, msg); }); }
...
var otherChannel = "otherchannel";
var r = redismock.createClient();
r.subscribe(channelName);
try {
(function () {
r.publish(otherChannel, "");
}).should.throwError();
} catch (e) {
r.end(true);
done();
}
});
...
punsubscribe = function () { var self = this , subcriptions = arguments; // Unsubscribe from ALL channels if (!arguments.length) { subcriptions = Object.keys(self.psubscriptions); this.pub_sub_mode = false; } for (var i = 0; i < subcriptions.length; i++) { if ('string' == typeof arguments[i]) { // Event on next tick to emulate an actual server call var channelName = arguments[i]; process.nextTick(function () { delete self.psubscriptions[channelName]; self.emit('punsubscribe', channelName); }); } } // TODO: If this was the last subscription, pub_sub_mode should be set to false }
...
var channelName = "testchannel";
should.exist(r.psubscribe);
should.exist(r.punsubscribe);
r.on("psubscribe", function (ch) {
should.equal(ch, channelName);
r.punsubscribe("testchannel");
});
r.on("punsubscribe", function (ch) {
should.equal(ch, channelName);
r.end(true);
done();
});
...
subscribe = function () { var self = this; if (!arguments.length) { return; } this.pub_sub_mode = true; for (var i = 0; i < arguments.length; i++) { if ('string' == typeof arguments[i]) { // Event on next tick to emulate an actual server call var channelName = arguments[i]; process.nextTick(function () { self.subscriptions[channelName] = true; // TODO Should also send length of subscriptions here self.emit('subscribe', channelName); }); } } }
...
should.equal(ch, channelName);
r.end(true);
done();
});
r.subscribe(channelName);
});
it("should psubscribe and punsubscribe to a channel", function (done) {
var r = redismock.createClient();
var channelName = "testchannel";
should.exist(r.psubscribe);
...
unsubscribe = function () { var self = this , subcriptions = arguments; // TODO: Unsubscribe from ALL channels if (!arguments.length) { subcriptions = self.subscriptions.map(function (__, subscription) { return subscription; }) } for (var i = 0; i < subcriptions.length; i++) { if ('string' == typeof arguments[i]) { // Event on next tick to emulate an actual server call var channelName = arguments[i]; process.nextTick(function () { self.subscriptions[channelName] = false; delete self.subscriptions[channelName]; self.emit('unsubscribe', channelName); }); } } // TODO: If this was the last subscription, pub_sub_mode should be set to false this.pub_sub_mode = false; }
...
should.exist(r.unsubscribe);
var channelName = "testchannel";
r.on("subscribe", function (ch) {
should.equal(ch, channelName);
r.unsubscribe("testchannel");
});
r.on("unsubscribe", function (ch) {
should.equal(ch, channelName);
r.end(true);
...
function auth(mockInstance, password, callback) { mockInstance._callCallback(callback, null, 'OK'); }
...
});
describe("auth", function () {
it("should always succeed and call back", function (done) {
var r = redismock.createClient();
r.auth("secret", function (err, result) {
result.should.equal('OK');
done();
r.end(true); //Moved this after the done() call. For some reason, calling `end()` beforehand causes this test to fail.
});
});
});
...
flushall = function (mockInstance, callback) { mockInstance.storage = {}; mockInstance._callCallback(callback, null, 'OK'); }
n/a
flushdb = function (mockInstance, callback) { mockInstance.storage = {}; mockInstance._callCallback(callback, null, 'OK'); }
...
describe("flushdb", function () {
it("should clean database", function (done) {
var r = redismock.createClient();
r.set("foo", "bar", function (err, result) {
r.flushdb(function (err, result) {
result.should.equal("OK");
r.exists("foo", function (err, result) {
result.should.be.equal(0);
r.end(true);
...
sadd = function (mockInstance, key, members, callback) { if (mockInstance.storage[key] && mockInstance.storage[key].type !== 'set') { var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value'); return mockInstance._callCallback(callback, err); } mockInstance.storage[key] = mockInstance.storage[key] || new Item.createSet(); var set = mockInstance.storage[key].value; var addCount = 0; for (var i = 0; i < members.length; i++) { if (set.indexOf(Item._stringify(members[i])) < 0) { set.push(Item._stringify(members[i])); addCount++; } } mockInstance._callCallback(callback, null, addCount); }
...
redismock = require('redis');
}
describe('sadd', function () {
it('should add a member to the set', function (done) {
var r = redismock.createClient();
r.sadd('foo', 'bar', function (err, result) {
result.should.eql(1);
r.smembers('foo', function (err, result) {
result.should.eql(['bar']);
r.end(true);
done();
...
scard = function (mockInstance, key, callback) { var count = 0; if (mockInstance.storage[key]) { if (mockInstance.storage[key].type !== 'set') { var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value'); return mockInstance._callCallback(callback, err); } else { var set = mockInstance.storage[key].value; count = set.length; } } mockInstance._callCallback(callback, null, count); }
...
// TODO: Add tests of SMEMBERS
describe('scard', function () {
it('should return the number of elements', function (done) {
var r = redismock.createClient();
r.sadd('foo', 'bar', 'baz', function (err, result) {
r.scard('foo', function (err, result) {
result.should.eql(2);
r.end(true);
done();
});
});
});
...
sismember = function (mockInstance, key, member, callback) { if (mockInstance.storage[key]) { if (mockInstance.storage[key].type !== 'set') { var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value'); return mockInstance._callCallback(callback, err); } } member = Item._stringify(member); var count = (mockInstance.storage[key].value.indexOf(member) > -1) ? 1 : 0; mockInstance._callCallback(callback, null, count); }
...
describe('sismember', function () {
it('should test if member exists', function (done) {
var r = redismock.createClient();
r.sadd('foo2', 'bar', 'baz', 'qux', function (err, result) {
r.sismember('foo2', 'bar', function (err, result) {
result.should.eql(1);
done();
});
});
});
});
...
smembers = function (mockInstance, key, callback) { var members = null; if (mockInstance.storage[key]) { if (mockInstance.storage[key].type !== 'set') { var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value'); return mockInstance._callCallback(callback, err); } else { members = mockInstance.storage[key].value; } } mockInstance._callCallback(callback, null, members); }
...
describe('sadd', function () {
it('should add a member to the set', function (done) {
var r = redismock.createClient();
r.sadd('foo', 'bar', function (err, result) {
result.should.eql(1);
r.smembers('foo', function (err, result) {
result.should.eql(['bar']);
r.end(true);
done();
});
});
});
...
srem = function (mockInstance, key, members, callback) { var remCount = 0; if (mockInstance.storage[key]) { if (mockInstance.storage[key].type !== 'set') { var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value'); return mockInstance._callCallback(callback, err); } else { var set = mockInstance.storage[key].value; for (var i = 0; i < members.length; i++) { for (var j = 0; j < set.length; j++) { if (set[j] == Item._stringify(members[i])) { set.splice(j, 1); remCount++; } } } } } mockInstance._callCallback(callback, null, remCount); }
...
describe('srem', function () {
it('should remove a member from the set', function (done) {
var r = redismock.createClient();
r.sadd('foo', 'bar', 'baz', 'qux', function (err, result) {
r.srem('foo', 'bar', function (err, result) {
result.should.eql(1);
r.smembers('foo', function (err, result) {
result.should.be.instanceof(Array);
result.should.have.length(2);
result.should.containEql('baz');
result.should.containEql('qux');
...
zadd = function (mockInstance, key) { var len = arguments.length; if (len <= 3) { return } var callback = helpers.parseCallback(arguments); if (!validKeyType(mockInstance, key, callback)) { return } // init key initKey(mockInstance, key); // declare opts var nx = false, xx = false, ch = false, incr = false; var start = 2, count = 0; for (var i=start; i < len; i++) { var opt = arguments[i]; opt = opt.toString().toLowerCase(); // Don't update already existing elements. Always add new elements. if (opt === 'nx') { nx = true; continue; } // Only update elements that already exist. Never add elements. if (opt === 'xx') { xx = true; continue; } // Total number of elements changed if (opt === 'ch') { ch = true; continue; } if (opt === 'incr') { incr = true; continue; } start = i; break; } for (var i = start; i < len; i += 2) { // hold the score and make sure it isn't an opt var score = arguments[i]; // did we reach the end? if (len <= (i + 1)) { break; } var member = Item._stringify(arguments[i + 1]); var existingScore = mockInstance.storage[key].value[member]; var exists = existingScore != undefined; // process opts if ((nx && exists) || (xx && !exists)) { continue; } // convert score to string score = score.toString(); // updating score if memeber doesn't exist // or if ch = true and score changes if (!exists || (ch && existingScore != score)) { count += 1; } // do we need to incr (existing score + score)? if (incr && existingScore) { score = parseFloat(existingScore) + parseFloat(score); score = String(score); } // update score mockInstance.storage[key].value[member] = score; // only one member is allowed update // if we have an incr // this shold behave the same as zincrby // so return the score instead of the updatedCount; if (incr) { count = score; break; } } if (callback) { mockInstance._callCallback(callback, null, count); } }
...
3, 'm3',
];
var aLen = args.length;
var mLen = aLen / 2;
it("should add scores and members", function (done) {
var r = redismock.createClient();
r.zadd([testKey1].concat(args), function(err, result) {
result.should.equal(mLen);
// should only add new members
r.zadd([testKey1, 'nx', 1, 'm1', 0, 'm4'], function(err, result) {
result.should.equal(1);
// only update existing members
// this won't bump the return count
...
zcard = function (mockInstance, key, callback) { var len = arguments.length; if (len < 1) { return } if (callback == undefined) { callback = mockCallback; } if (!validKeyType(mockInstance, key, callback)) { return } initKey(mockInstance, key); var count = Object.keys(mockInstance.storage[key].value).length; mockInstance._callCallback(callback, null, count); }
...
it("should add scores and members", function (done) {
var r = redismock.createClient();
r.zadd(testKey1, testScore1, testMember1, testScore2, testMember2,
function(err, result) {
result.should.equal(2);
r.zcard(testKey1, function(err, result) {
result.should.equal(2);
done();
});
});
});
});
...
zcount = function (mockInstance, key, min, max, callback) { var parse = function(err, result) { if (err) { return mockInstance._callCallback(callback, err); } mockInstance._callCallback(callback, null, result.length); } exports.zrangebyscore(mockInstance, key, min, max, parse); }
...
var mLen = aLen / 2;
it("should return the inclusive min & max count", function (done) {
var r = redismock.createClient();
r.zadd([testKey1].concat(args), function(err, result) {
result.should.equal(mLen);
r.zcount([testKey1, '0', '5'], function(err, result) {
should(result).equal(mLen);
done();
});
});
});
...
zincrby = function (mockInstance, key, increment, member, callback) { var len = arguments.length; if (len < 4) { return } if (callback == undefined) { callback = mockCallback; } if (!validKeyType(mockInstance, key, callback)) { return } initKey(mockInstance, key); member = Item._stringify(member); var s = mockInstance.storage[key].value[member]; var score = parseFloat( s !== undefined ? s : '0'); increment = parseFloat(String(increment)); score += increment; score = String(score); mockInstance.storage[key].value[member] = score; mockInstance._callCallback(callback, null, score); }
...
var testKey1 = "zincrbyKey1";
var testKey2 = "zincrbyKey2";
it("should add and increment a member", function (done) {
var r = redismock.createClient();
r.zadd([testKey1, 1, 'm1'], function(err, result) {
result.should.equal(1);
r.zincrby([testKey1, 5, 'm1'], function(err, result) {
should(result).equal('6');
done();
});
});
});
it("should increment a non-existing member", function (done) {
...
zrange = function (mockInstance, key, start, stop, withscores, callback) { getRange(mockInstance, key, start, stop, withscores, callback, false); }
...
var aLen = args.length;
var mLen = aLen / 2;
it("should return everything withscores", function (done) {
var r = redismock.createClient();
r.zadd([testKey1].concat(args), function(err, result) {
result.should.equal(mLen);
r.zrange([testKey1, '0', '-1', 'withscores'], function
(err, result) {
should(result[0]).equal('m1');
should(result[1]).equal('1');
should(result.length).equal(aLen);
done();
});
});
});
...
zrangebyscore = function ( mockInstance, key, min, max, withscores, limit, offset, count, callback) { getRangeByScore( mockInstance, key, min, max, withscores, limit, offset, count, callback, false); }
...
exports.zcount = function(mockInstance, key, min, max, callback) {
var parse = function(err, result) {
if (err) {
return mockInstance._callCallback(callback, err);
}
mockInstance._callCallback(callback, null, result.length);
}
exports.zrangebyscore(mockInstance, key, min, max, parse);
}
// ZINCRBY key increment member
// Increment the score of a member in a sorted set
exports.zincrby = function(mockInstance, key, increment, member, callback) {
var len = arguments.length;
if (len < 4) {
...
zrank = function (mockInstance, key, member, callback) { getRank(mockInstance, key, member, callback, false); }
...
1.3, 'm1.3',
1.4, 'm1.4',
1.5, 'm1.5'
];
it("should return the rank for a member", function (done) {
var r = redismock.createClient();
r.zadd([testKey1].concat(args), function(err, result) {
r.zrank(testKey1, 'm1.1', function(err, result) {
result.should.equal(1);
done();
});
});
});
it("should return a null rank for a missing member", function (done) {
var r = redismock.createClient();
...
zrem = function (mockInstance, key) { var len = arguments.length; if (len <= 3) { return } var callback = helpers.parseCallback(arguments); if (callback == undefined) { callback = mockCallback; } if (!validKeyType(mockInstance, key, callback)) { return } initKey(mockInstance, key); // The number of members removed from the sorted set, // not including non existing members. var count = 0; for (var i=2, member; i < len; i++) { member = arguments[i]; if ('function' == typeof member) { break; } member = Item._stringify(member); if (mockInstance.storage[key].value[member]) { delete mockInstance.storage[key].value[member]; count += 1; } } mockInstance._callCallback(callback, null, count); }
...
1.3, 'm1.3',
1.4, 'm1.4',
1.5, 'm1.5'
];
it("should add and remove members", function (done) {
var r = redismock.createClient();
r.zadd([testKey1].concat(args), function(err, result) {
r.zrem([testKey1, 'm1', 'm2'], function(err, result) {
result.should.equal(2);
done();
});
});
});
});
...
zremrangebyrank = function (mockInstance, key, start, stop, callback) { var deleteResults = function(err, results) { if (err) { return mockInstance._callCallback(callback, err); } var count = 0; for (var i=0, member; i < results.length; i++) { member = results[i]; if (mockInstance.storage[key].value[member]) { delete mockInstance.storage[key].value[member]; count += 1; } } mockInstance._callCallback(callback, null, count); } getRange(mockInstance, key, start, stop, deleteResults, false); }
...
1.3, 'm1.3',
1.4, 'm1.4',
1.5, 'm1.5'
];
it("should add and remove members by rank", function (done) {
var r = redismock.createClient();
r.zadd([testKey1].concat(args), function(err, result) {
r.zremrangebyrank([testKey1, '0', '1'], function(err, result
) {
result.should.equal(2);
done();
});
});
});
});
...
zremrangebyscore = function (mockInstance, key, min, max, callback) { var deleteResults = function(err, results) { if (err) { return mockInstance._callCallback(callback, err); } var count = 0; for (var i=0, member; i < results.length; i++) { member = results[i]; if (mockInstance.storage[key].value[member]) { delete mockInstance.storage[key].value[member]; count += 1; } } mockInstance._callCallback(callback, null, count); } getRangeByScore(mockInstance, key, min, max, deleteResults, false); }
...
1.3, 'm1.3',
1.4, 'm1.4',
1.5, 'm1.5'
];
it("should add and remove members by rank", function (done) {
var r = redismock.createClient();
r.zadd([testKey1].concat(args), function(err, result) {
r.zremrangebyscore([testKey1, '1.1', '1.5'], function(err
, result) {
result.should.equal(5);
done();
});
});
});
});
...
zrevrange = function (mockInstance, key, start, stop, withscores, callback) { getRange(mockInstance, key, start, stop, withscores, callback, true); }
...
var aLen = args.length;
var mLen = aLen / 2;
it("should return everything withscores", function (done) {
var r = redismock.createClient();
r.zadd([testKey1].concat(args), function(err, result) {
result.should.equal(mLen);
r.zrevrange([testKey1, '0', '-1', 'withscores'], function
(err, result) {
should(result[0]).equal('m3');
should(result[1]).equal('3');
should(result.length).equal(aLen);
done();
});
});
});
...
zrevrangebyscore = function ( mockInstance, key, max, min, withscores, limit, offset, count, callback) { getRangeByScore( mockInstance, key, min, max, withscores, limit, offset, count, callback, true); }
...
var aLen = args.length;
var mLen = aLen / 2;
it("should return the inclusive min & max range withscores", function (done) {
var r = redismock.createClient();
r.zadd([testKey1].concat(args), function(err, result) {
result.should.equal(mLen);
r.zrevrangebyscore([testKey1, '5', '0', 'withscores'
;], function(err, result) {
should(result[0]).equal('m3');
should(result[1]).equal('3');
should(result.length).equal(aLen);
done();
});
});
});
...
zrevrank = function (mockInstance, key, member, callback) { getRank(mockInstance, key, member, callback, true); }
...
1.3, 'm1.3',
1.4, 'm1.4',
1.5, 'm1.5'
];
it("should return the rank for a member", function (done) {
var r = redismock.createClient();
r.zadd([testKey1].concat(args), function(err, result) {
r.zrevrank(testKey1, 'm2', function(err, result) {
result.should.equal(1);
done();
});
});
});
it("should return a null rank for a missing member", function (done) {
var r = redismock.createClient();
...
zscore = function (mockInstance, key, member, callback) { var len = arguments.length; if (len < 3) { return } if (callback == undefined) { callback = mockCallback; } if (!validKeyType(mockInstance, key, callback)) { return } initKey(mockInstance, key); var score = mockInstance.storage[key].value[Item._stringify(member)]; mockInstance._callCallback(callback, null, score); }
...
var testScore1 = 100.00;
var testMember1 = JSON.stringify({'a': 'b'});
it("should add and return member score", function (done) {
var r = redismock.createClient();
r.zadd(testKey1, testScore1, testMember1, function(err, result) {
result.should.equal(1);
r.zscore([testKey1, testMember1], function(err, result) {
result.should.equal(String(100.00));
done();
});
});
});
});
...
get = function (mockInstance, key, callback) { var value = null; var err = null; if (mockInstance.storage[key]) { if (mockInstance.storage[key].type !== "string") { err = new Error("WRONGTYPE Operation against a key holding the wrong kind of value"); } else { value = mockInstance.storage[key].value; } } mockInstance._callCallback(callback, err, value); }
...
}
/**
* Getset
*/
exports.getset = function (mockInstance, key, value, callback) {
exports.get(mockInstance, key, /*callback);,*/ function(err, oldValue) {
if (err) {
return mockInstance._callCallback(callback, err, null);
}
mockInstance.storage[key] = Item.createString(value);
mockInstance._callCallback(callback, err, oldValue);
...
getset = function (mockInstance, key, value, callback) { exports.get(mockInstance, key, /*callback);,*/ function(err, oldValue) { if (err) { return mockInstance._callCallback(callback, err, null); } mockInstance.storage[key] = Item.createString(value); mockInstance._callCallback(callback, err, oldValue); }); }
...
var r;
beforeEach(function () {
r = redismock.createClient();
});
it("should return null for a non-existing key", function (done) {
r.getset("does-not-exist", "newValue", function (err, result) {
should.not.exist(result);
r.end(true);
done();
});
});
...
incr = function (mockInstance, key, callback) { function _isInteger(s) { return parseInt(s, 10) == s; } if (!mockInstance.storage[key]) { var number = 0 + 1; exports.set(mockInstance, key, number); mockInstance._callCallback(callback, null, number); } else if (mockInstance.storage[key].type !== "string") { var err = new Error("WRONGTYPE Operation against a key holding the wrong kind of value"); mockInstance._callCallback(callback, err, null); } else if (_isInteger(mockInstance.storage[key].value)) { var number = parseInt(mockInstance.storage[key].value, 10) + 1; exports.set(mockInstance, key, number); mockInstance._callCallback(callback, null, number); } else { var err = new Error("ERR value is not an integer or out of range"); mockInstance._callCallback(callback, err, null); } }
...
should.exist(multi.exists);
should.exist(multi.hget);
});
describe("exec()", function () {
it("should handle things without errors and callbacks", function (done) {
var multi = r.multi();
multi.get('foo').incr('foo');
r.set('foo', 3, function () {
multi.exec(function (err, results) {
should(err).not.be.ok();
should.deepEqual(results, ['3',4]);
done();
});
...
incrby = function (mockInstance, key, value, callback) { function _isInteger(s) { return parseInt(s, 10) == s; } value = parseInt(value); if (!mockInstance.storage[key]) { var number = 0 + value; exports.set(mockInstance, key, number); mockInstance._callCallback(callback, null, number); } else if (mockInstance.storage[key].type !== "string") { var err = new Error("WRONGTYPE Operation against a key holding the wrong kind of value"); mockInstance._callCallback(callback, err, null); } else if (_isInteger(mockInstance.storage[key].value)) { var number = parseInt(mockInstance.storage[key].value, 10) + value; exports.set(mockInstance, key, number); mockInstance._callCallback(callback, null, number); } else { var err = new Error("ERR value is not an integer or out of range"); mockInstance._callCallback(callback, err, null); } }
...
it("should increment the number stored at key by 2", function (done) {
var r = redismock.createClient();
r.set("foo", "10", function (err, result) {
r.incrby("foo", 2, function (err, result) {
result.should.eql(12);
r.get("foo", function (err, result) {
result.should.eql("12");
...
incrbyfloat = function (mockInstance, key, value, callback) { function _isFloat(s) { return parseFloat(s, 10) == s; } if (!mockInstance.storage[key]) { var number = 0 + parseFloat(value, 10); exports.set(mockInstance, key, number.toString()); mockInstance._callCallback(callback, null, number.toString()); } else if (mockInstance.storage[key].type !== "string") { var err = new Error("WRONGTYPE Operation against a key holding the wrong kind of value"); mockInstance._callCallback(callback, err, null); } else if (_isFloat(mockInstance.storage[key].value) && _isFloat(value)) { var number = parseFloat(mockInstance.storage[key].value, 10) + parseFloat(value, 10); exports.set(mockInstance, key, number.toString()); mockInstance._callCallback(callback, null, number.toString()); } else { var err = new Error("ERR value is not a valid float"); mockInstance._callCallback(callback, err, null); } }
...
it("should increment the number stored at key by a float value", function (done) {
var r = redismock.createClient();
r.set("foo", "1.5", function (err, result) {
r.incrbyfloat("foo", "0.5", function (err, result) {
result.should.eql("2");
r.get("foo", function (err, result) {
result.should.eql("2");
...
mget = function (mockInstance) { var keys = []; var err = null; // Build up the set of keys if ('object' == typeof arguments[1]) { keys = arguments[1]; } else { for (var i = 1; i < arguments.length; i++) { var key = arguments[i]; if ('function' !== typeof key) { keys.push(key); } } } var values = []; for (var j = 0; j < keys.length; j++) { if (mockInstance.storage[keys[j]]) { if (mockInstance.storage[keys[j]].type !== 'string') { err = new Error("ERR Operation against key " + keys[j] + " holding wrong kind of value"); } else { values.push(mockInstance.storage[keys[j]].value); } } else { values.push(null); } } if ('function' === typeof arguments[arguments.length - 1]) { mockInstance._callCallback(arguments[arguments.length - 1], err, values); } }
...
var r = redismock.createClient();
r.set("multi1", "one", function (err, result) {
r.set("multi3", "three", function (err, result) {
r.mget("multi1", "multi2", "multi3", function (
err, result) {
result.should.be.ok;
result[0].should.equal("one");
should.not.exist(result[1]);
result[2].should.equal("three");
...
ping = function (mockInstance, callback) { mockInstance._callCallback(callback, null, "PONG"); }
...
}
describe("ping", function () {
it("should return PONG", function (done) {
var r = redismock.createClient();
r.ping(function (err, result) {
result.should.equal("PONG");
r.end(true);
done();
});
...
set = function (mockInstance, key, value, callback) { mockInstance.storage[key] = Item.createString(value); mockInstance._callCallback(callback, null, "OK"); }
...
/**
* Setnx
*/
exports.setnx = function (mockInstance, key, value, callback) {
if (key in mockInstance.storage) {
mockInstance._callCallback(callback, null, 0);
} else {
exports.set(mockInstance, key, value, /*callback);,*/ function() {
mockInstance._callCallback(callback, null, 1);
});
}
};
/**
* Get
...
setnx = function (mockInstance, key, value, callback) { if (key in mockInstance.storage) { mockInstance._callCallback(callback, null, 0); } else { exports.set(mockInstance, key, value, /*callback);,*/ function() { mockInstance._callCallback(callback, null, 1); }); } }
...
describe("setnx", function () {
it("should set a key", function (done) {
var r = redismock.createClient();
r.setnx("foo", "10", function (err, result) {
result.should.eql(1);
r.get("foo", function (err, result) {
result.should.eql("10");
r.end(true);
done();
});
...