From 9416d390077332cada331fa1c8b7b710325ea461 Mon Sep 17 00:00:00 2001 From: Sergey Romanov Date: Fri, 20 Mar 2015 23:34:16 +0500 Subject: [PATCH] append command for strings --- lib/redis-mock.js | 4 ++++ lib/strings.js | 13 +++++++++++ test/redis-mock.strings.test.js | 40 +++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/lib/redis-mock.js b/lib/redis-mock.js index 4c02bd5..921b846 100755 --- a/lib/redis-mock.js +++ b/lib/redis-mock.js @@ -203,6 +203,10 @@ RedisClient.prototype.setnx = RedisClient.prototype.SETNX = function (key, value stringfunctions.setnx.call(this, MockInstance, key, value, callback ); } +RedisClient.prototype.append = RedisClient.prototype.APPEND = function(key, value, callback) { + stringfunctions.append.call(this, MockInstance, key, value, callback) +} + /** * Hashing functions */ diff --git a/lib/strings.js b/lib/strings.js index ca9bab3..9cff787 100644 --- a/lib/strings.js +++ b/lib/strings.js @@ -110,3 +110,16 @@ exports.setnx = function (mockInstance, key, value, callback) { }); } }; + +/** + * Append + */ +exports.append = function(mockInstance, key, value, callback) { + if (mockInstance.storage[key]) { + var newstr = mockInstance.storage[key].value + value; + exports.set(mockInstance, key, newstr); + mockInstance._callCallback(callback, null, newstr); + } else { + exports.set(mockInstance, key, value, callback); + } +} diff --git a/test/redis-mock.strings.test.js b/test/redis-mock.strings.test.js index b29d955..7ade0e3 100644 --- a/test/redis-mock.strings.test.js +++ b/test/redis-mock.strings.test.js @@ -242,3 +242,43 @@ describe("setnx", function () { }); }); + +describe("append", function() { + + it("should append two values after set", function(done) { + + var r = redismock.createClient(); + + r.set("foo", "bar1", function(err, result) { + + r.append("foo", "bar2", function(err, result) { + + r.get("foo", function(err, result) { + + result.should.equal("bar1bar2") + r.end(); + done(); + }); + }); + }); + }); + + it("should append two values after append command", function(done) { + + var r = redismock.createClient(); + + r.append("foo", "bar1", function(err, result) { + + r.append("foo", "bar2", function(err, result) { + + r.get("foo", function(err, result) { + + result.should.equal("bar1bar2") + r.end(); + done(); + }); + }); + }); + }); +}); +