From 36108e2378186327d49a458b0332c015e3490b88 Mon Sep 17 00:00:00 2001 From: fbraem Date: Sat, 14 Nov 2015 20:12:33 +0100 Subject: [PATCH] Add rename --- Redis/include/Poco/Redis/Command.h | 5 +- Redis/src/Command.cpp | 10 +++ Redis/testsuite/src/RedisTest.cpp | 97 ++++++++++++++++++++++++++++++ Redis/testsuite/src/RedisTest.h | 2 + 4 files changed, 113 insertions(+), 1 deletion(-) diff --git a/Redis/include/Poco/Redis/Command.h b/Redis/include/Poco/Redis/Command.h index 1cfa879e6..c8b85e750 100644 --- a/Redis/include/Poco/Redis/Command.h +++ b/Redis/include/Poco/Redis/Command.h @@ -29,7 +29,7 @@ namespace Redis { class Redis_API Command : public Array /// Helper class for creating commands. This class contains - /// factory methods for common used Redis commands. + /// factory methods for commonly used Redis commands. { public: Command(const std::string& command); @@ -111,6 +111,9 @@ public: static Command set(const std::string& key, Int64 value, bool overwrite = true, const Poco::Timespan& expireTime = 0, bool create = true); /// Returns a SET command to set the key with a value + static Command rename(const std::string& key, const std::string& newName, bool overwrite = true); + /// Returns a RENAME or RENAMENX when overwrite is false + static Command rpop(const std::string& list); /// Returns a RPOP command diff --git a/Redis/src/Command.cpp b/Redis/src/Command.cpp index 382e3b528..8fdc6ce5f 100644 --- a/Redis/src/Command.cpp +++ b/Redis/src/Command.cpp @@ -262,6 +262,16 @@ Command Command::set(const std::string& key, Int64 value, bool overwrite, const return set(key, NumberFormatter::format(value), overwrite, expireTime, create); } +Command Command::rename(const std::string& key, const std::string& newName, bool overwrite) +{ + Command cmd(overwrite ? "RENAME" : "RENAMENX"); + + cmd << key << newName; + + return cmd; +} + + Command Command::rpop(const std::string& list) { Command cmd("RPOP"); diff --git a/Redis/testsuite/src/RedisTest.cpp b/Redis/testsuite/src/RedisTest.cpp index c27278950..21d53c984 100644 --- a/Redis/testsuite/src/RedisTest.cpp +++ b/Redis/testsuite/src/RedisTest.cpp @@ -1184,6 +1184,101 @@ void RedisTest::testStrlen() } } +void RedisTest::testRename() +{ + if (!_connected) + { + std::cout << "Not connected, test skipped." << std::endl; + return; + } + + Command set = Command::set("mykey", "Hello"); + try + { + std::string result = _redis.execute(set); + assert(result.compare("OK") == 0); + } + catch(RedisException &e) + { + fail(e.message()); + } + + Command rename = Command::rename("mykey", "myotherkey"); + try + { + std::string result = _redis.execute(rename); + assert(result.compare("OK") == 0); + } + catch(RedisException &e) + { + fail(e.message()); + } + + Command get = Command::get("myotherkey"); + try + { + BulkString result = _redis.execute(get); + assert(result.value().compare("Hello") == 0); + } + catch(RedisException &e) + { + fail(e.message()); + } +} + +void RedisTest::testRenameNx() +{ + if (!_connected) + { + std::cout << "Not connected, test skipped." << std::endl; + return; + } + + Command set = Command::set("mykey", "Hello"); + try + { + std::string result = _redis.execute(set); + assert(result.compare("OK") == 0); + } + catch(RedisException &e) + { + fail(e.message()); + } + + set = Command::set("myotherkey", "World"); + try + { + std::string result = _redis.execute(set); + assert(result.compare("OK") == 0); + } + catch(RedisException &e) + { + fail(e.message()); + } + + Command rename = Command::rename("mykey", "myotherkey", false); + try + { + Poco::Int64 result = _redis.execute(rename); + assert(result == 0); + } + catch(RedisException &e) + { + fail(e.message()); + } + + Command get = Command::get("myotherkey"); + try + { + BulkString result = _redis.execute(get); + assert(result.value().compare("World") == 0); + } + catch(RedisException &e) + { + fail(e.message()); + } +} + void RedisTest::testRPop() { if (!_connected) @@ -1444,6 +1539,8 @@ CppUnit::Test* RedisTest::suite() CppUnit_addTest(pSuite, RedisTest, testPubSub); CppUnit_addTest(pSuite, RedisTest, testSet); CppUnit_addTest(pSuite, RedisTest, testStrlen); + CppUnit_addTest(pSuite, RedisTest, testRename); + CppUnit_addTest(pSuite, RedisTest, testRenameNx); CppUnit_addTest(pSuite, RedisTest, testRPop); CppUnit_addTest(pSuite, RedisTest, testRPoplPush); CppUnit_addTest(pSuite, RedisTest, testRPush); diff --git a/Redis/testsuite/src/RedisTest.h b/Redis/testsuite/src/RedisTest.h index 155031a20..ab387865b 100644 --- a/Redis/testsuite/src/RedisTest.h +++ b/Redis/testsuite/src/RedisTest.h @@ -53,6 +53,8 @@ public: void testPubSub(); void testSet(); void testStrlen(); + void testRename(); + void testRenameNx(); void testRPop(); void testRPoplPush(); void testRPush();