redis-mock
Advanced tools
Comparing version 0.16.0 to 0.17.0
@@ -258,2 +258,37 @@ var helpers = require("./helpers.js"); | ||
/** | ||
* ltrim | ||
*/ | ||
exports.ltrim = function(mockInstance, key, start, end, callback) { | ||
var res = "OK"; | ||
var len = -1; | ||
if (!mockInstance.storage[key]) { | ||
return mockInstance._callCallback(callback, null, res); | ||
} | ||
if (mockInstance.storage[key].type !== "list") { | ||
return mockInstance._callCallback(callback, | ||
new Error("WRONGTYPE Operation against a key holding the wrong kind of value")); | ||
} | ||
len = mockInstance.storage[key].value.length; | ||
if (start < 0) { | ||
start = len + start; | ||
} | ||
if (end < 0) { | ||
end = len + end; | ||
} | ||
if (end >= len) { | ||
end = len - 1; | ||
} | ||
if (start >= len || start > end) { | ||
// trim whole list | ||
delete mockInstance.storage[key]; | ||
} else { | ||
mockInstance.storage[key].value = mockInstance.storage[key].value.slice(start, end + 1); | ||
} | ||
mockInstance._callCallback(callback, null, res); | ||
}; | ||
/** | ||
* Used to follow a list depending on its key (used by blpop and brpop mainly) | ||
@@ -260,0 +295,0 @@ */ |
@@ -124,2 +124,3 @@ | ||
'lset', | ||
'ltrim', | ||
'mget', | ||
@@ -126,0 +127,0 @@ 'ping', |
@@ -513,2 +513,6 @@ /** | ||
RedisClient.prototype.ltrim = RedisClient.prototype.LTRIM = function (key, start, end, callback) { | ||
listfunctions.ltrim.call(this, MockInstance, key, start, end, callback); | ||
} | ||
/** | ||
@@ -515,0 +519,0 @@ * Set functions |
{ | ||
"name": "redis-mock", | ||
"version": "0.16.0", | ||
"version": "0.17.0", | ||
"description": "Redis client mock object for unit testing", | ||
@@ -5,0 +5,0 @@ "author": "Kristian Faeldt <kristian.faeldt@gmail.com>", |
@@ -826,1 +826,109 @@ var redismock = require("../") | ||
}); | ||
describe("ltrim", function(argument) { | ||
var testKey = "myKey10"; | ||
var testKey2 = "myKey11"; | ||
var testKey3 = "myKey12"; | ||
var testKey4 = "myKey13"; | ||
var keyUndefined = "keyUndefined"; | ||
var keyUndefined2 = "keyUndefined2"; | ||
var testValues = [1, 2, 3, 4, 5]; | ||
it("does nothing for a non-existent list", function(done) { | ||
var r = redismock.createClient(); | ||
r.ltrim(testKey, 0, 2, function(err, result) { | ||
result.should.equal('OK'); | ||
r.end(true); | ||
done(); | ||
}); | ||
}); | ||
it("removes the whole list when start/end outside list length", function(done) { | ||
var r = redismock.createClient(); | ||
r.rpush(testKey, 1, 2, 3, 4, 5, function(err, result) { | ||
r.ltrim(testKey, 5, 8, function(err, result) { | ||
result.should.equal('OK'); | ||
r.lrange(testKey, 0, 4, function(err, result) { | ||
result.should.have.length(0); | ||
r.end(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("removes the whole list when start > end", function(done) { | ||
var r = redismock.createClient(); | ||
r.rpush(testKey, 1, 2, 3, 4, 5, function(err, result) { | ||
r.ltrim(testKey, 3, 2, function(err, result) { | ||
result.should.equal('OK'); | ||
r.lrange(testKey, 0, 4, function(err, result) { | ||
result.should.have.length(0); | ||
r.end(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("trims correctly for positive numbers", function(done) { | ||
var r = redismock.createClient(); | ||
r.rpush(testKey, 1, 2, 3, 4, 5, function(err, result) { | ||
r.ltrim(testKey, 0, 2, function(err, result) { | ||
result.should.equal('OK'); | ||
r.lrange(testKey, 0, 2, function(err, result) { | ||
// result.should.have.length(3); | ||
result.should.be.eql(["1", "2", "3"]); | ||
r.end(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("trims correctly for negative numbers", function(done) { | ||
var r = redismock.createClient(); | ||
r.rpush(testKey, 1, 2, 3, 4, 5, function(err, result) { | ||
r.ltrim(testKey, -2, -1, function(err, result) { | ||
result.should.equal('OK'); | ||
r.lrange(testKey, 0, 2, function(err, result) { | ||
// result.should.have.length(3); | ||
result.should.be.eql(["4", "5"]); | ||
r.end(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("trims correctly for end > len", function(done) { | ||
var r = redismock.createClient(); | ||
r.rpush(testKey2, 1, 2, 3, 4, 5, function(err, result) { | ||
r.ltrim(testKey2, 1, 5, function(err, result) { | ||
result.should.equal('OK'); | ||
r.lrange(testKey2, 0, 8, function(err, result) { | ||
// result.should.have.length(3); | ||
result.should.be.eql(["2", "3", "4", "5"]); | ||
r.end(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("trims correctly for one negativ number", function(done) { | ||
var r = redismock.createClient(); | ||
r.rpush(testKey3, 1, 2, 3, 4, 5, function(err, result) { | ||
r.ltrim(testKey3, 1, -1, function(err, result) { | ||
result.should.equal('OK'); | ||
r.lrange(testKey3, 0, 5, function(err, result) { | ||
// result.should.have.length(3); | ||
result.should.be.eql(["2", "3", "4", "5"]); | ||
r.end(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
180002
5335
16