Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hmset when no callback provided #46

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion lib/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ exports.hmset = function (mockInstance) {
// 0: mockInstance
// 1: hash name
// 2: key/value object or first key name
if (arguments.length <= 3) {
if (arguments.length < 3) {
return;
}

Expand Down
32 changes: 32 additions & 0 deletions lib/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,38 @@ exports.lindex = function (mockInstance, key, index, callback) {
mockInstance._callCallback(callback, null, val);
};

/**
* Lrange
*/
exports.lrange = function (mockInstance, key, start, end, callback) {
if (mockInstance.storage[key] && mockInstance.storage[key].type !== "list") {
return mockInstance._callCallback(callback,
new Error("WRONGTYPE Operation against a key holding the wrong kind of value"));
}
var item = mockInstance.storage[key];
var list = item ? item.value : [];
var args = end == -1 ? [start] : [start, end + 1];
var res = list.slice.apply(list, args);
mockInstance._callCallback(callback, null, res);
}

/**
* Ltrim
*/
exports.ltrim = function (mockInstance, key, start, end, callback) {
var res = "OK";
var item = mockInstance.storage[key];
if (item && item.type !== "list") {
return mockInstance._callCallback(callback,
new Error("WRONGTYPE Operation against a key holding the wrong kind of value"));
}

var args = end == -1 ? [start] : [start, end + 1];
var list = item.value;
item.value = list.slice.apply(list, args);
mockInstance._callCallback(callback, null, res);
}

/**
* Lset
*/
Expand Down
30 changes: 22 additions & 8 deletions lib/redis-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,22 @@ RedisClient.prototype.llen = RedisClient.prototype.LLEN = function (key, callbac
}

var push = function (fn, key, values, callback) {
var len = arguments.length;
var hasCallback = typeof(arguments[len - 1]) === "function";
// Kind of a messy interface here... basically we need to support
// - push(key, 'myval')
// - push(key, 'val1', 'val2', ...)
// - push(key, ['val1, 'val2'])

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(Array.isArray(values)) {
vals = values;
} else {
for(var i=2;i < (hasCallback ? len - 1 : len); i++) {
vals.push(arguments[i]);
}
}

if (hasCallback) {
fn.call(this, MockInstance, key, vals, arguments[arguments.length - 1]);
} else {
Expand Down Expand Up @@ -349,6 +360,14 @@ RedisClient.prototype.lindex = RedisClient.prototype.LINDEX = function (key, ind
listfunctions.lindex.call(this, MockInstance, key, index, callback);
}

RedisClient.prototype.lrange = RedisClient.prototype.LRANGE = function (key, start, end, callback) {
listfunctions.lrange.call(this, MockInstance, key, start, end, callback);
}

RedisClient.prototype.ltrim = RedisClient.prototype.LTRIM = function (key, start, end, callback) {
listfunctions.ltrim.call(this, MockInstance, key, start, end, callback);
}

RedisClient.prototype.lset = RedisClient.prototype.LSET = function (key, index, value, callback) {
listfunctions.lset.call(this, MockInstance, key, index, value, callback);
}
Expand Down Expand Up @@ -417,8 +436,3 @@ RedisMock.prototype.createClient = function (port_arg, host_arg, options) {

return new RedisClient();
}





12 changes: 12 additions & 0 deletions test/redis-mock.hash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,18 @@ describe("multiple get/set", function () {
});
});

it("should be able to set multiple keys with no callback", function (done) {
var r = redismock.createClient();
var d = {mKey1: mValue1};

r.hmset('myKey', {mKey1: mValue1});
r.hgetall('myKey', function (err, result) {
should(result).have.property(mKey1, mValue1);
done();
});

});

it("should be able to set multiple keys as an object", function (done) {


Expand Down
95 changes: 94 additions & 1 deletion test/redis-mock.list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var sinon = require('./timer-helper')

if (process.env['VALID_TESTS']) {
redismock = require('redis');

}

describe("basic pushing/poping list", function () {
Expand Down Expand Up @@ -122,6 +122,99 @@ describe("llen", function () {

});

describe("lrange", function () {
var testKey = "myKey123";
it("should return empty list", function (done) {
var r = redismock.createClient();
r.lrange(testKey, 0, 1, function (err, result) {
result.length.should.equal(0);
r.end();
done();
});
});

it("should return error for wrong operation", function (done) {
var r = redismock.createClient();

r.set(testKey, 'foo', function (err, result) {
r.lrange(testKey, 0, 1, function (err, result) {
should.not.exist(result);
should.exist(err);
r.end();
done();
});
});
});

it("should return multiple values", function (done) {
var r = redismock.createClient();
r.rpush(testKey, 'foo', function (err, result) {
r.rpush(testKey, 'bar', function (err, result) {
// lrange is right inclusive
r.lrange(testKey, 0, 1, function (err, result) {
result.length.should.equal(2);
result[0].should.equal('foo');
result[1].should.equal('bar');
r.end();
done();
});
});
});
});

it("should return full list with negative indexes", function (done) {
var r = redismock.createClient();
r.rpush(testKey, 'foo', function (err, result) {
r.rpush(testKey, 'bar', function (err, result) {
r.lrange(testKey, 0, -1, function (err, result) {
result.length.should.equal(2);
result[0].should.equal('foo');
result[1].should.equal('bar');
r.end();
done();
});
});
});
});
});

describe("ltrim", function () {
var values = ['a', 'b', 'c', 'd'];
var testKey = 'foo';
it("should trim list", function (done) {
var r = redismock.createClient();
r.rpush(testKey, values, function (err, result) {
r.ltrim(testKey, 0, 1, function (err, result) {
result.should.equal('OK');
r.lrange(testKey, 0, 1, function (err, result) {
result.length.should.equal(2);
result[0].should.equal('a');
result[1].should.equal('b');
r.end();
done();
});
});
});
});

it("should trim list with negative start/end", function (done) {
var r = redismock.createClient();
r.rpush(testKey, values, function (err, result) {
r.ltrim(testKey, -2, -1, function (err, result) {
result.should.equal('OK');
r.lrange(testKey, 0, 1, function (err, result) {
result.length.should.equal(2);
result[0].should.equal('c');
result[1].should.equal('d');
r.end();
done();
});
});
});
});
});


describe("lindex", function () {
var testKey = "myKey4";
var testKey2 = "myKey5";
Expand Down