Add rename

This commit is contained in:
fbraem 2015-11-14 20:12:33 +01:00
parent 4e5fe3f874
commit 36108e2378
4 changed files with 113 additions and 1 deletions

View File

@ -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

View File

@ -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");

View File

@ -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<std::string>(set);
assert(result.compare("OK") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
Command rename = Command::rename("mykey", "myotherkey");
try
{
std::string result = _redis.execute<std::string>(rename);
assert(result.compare("OK") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
Command get = Command::get("myotherkey");
try
{
BulkString result = _redis.execute<BulkString>(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<std::string>(set);
assert(result.compare("OK") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
set = Command::set("myotherkey", "World");
try
{
std::string result = _redis.execute<std::string>(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<Poco::Int64>(rename);
assert(result == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
Command get = Command::get("myotherkey");
try
{
BulkString result = _redis.execute<BulkString>(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);

View File

@ -53,6 +53,8 @@ public:
void testPubSub();
void testSet();
void testStrlen();
void testRename();
void testRenameNx();
void testRPop();
void testRPoplPush();
void testRPush();