From 7732d756ad2c828f46659e38e241b9ee70bd3563 Mon Sep 17 00:00:00 2001 From: fbraem Date: Mon, 16 Nov 2015 21:58:12 +0100 Subject: [PATCH] add srem --- Redis/include/Poco/Redis/Command.h | 6 ++ Redis/src/Command.cpp | 22 +++++ Redis/testsuite/src/RedisTest.cpp | 152 +++++++++++++++++++++-------- Redis/testsuite/src/RedisTest.h | 1 + 4 files changed, 143 insertions(+), 38 deletions(-) diff --git a/Redis/include/Poco/Redis/Command.h b/Redis/include/Poco/Redis/Command.h index b68c0792e..96230cc0b 100644 --- a/Redis/include/Poco/Redis/Command.h +++ b/Redis/include/Poco/Redis/Command.h @@ -157,6 +157,12 @@ public: static Command smove(const std::string& source, const std::string& destination, const std::string& member); /// Returns a SMOVE command + static Command srem(const std::string& set, const std::string& member); + /// Returns a SREM command + + static Command srem(const std::string& set, const std::vector& member); + /// Returns a SREM command + static Command sunion(const std::string& set1, const std::string& set2); /// Returns a SUNION command diff --git a/Redis/src/Command.cpp b/Redis/src/Command.cpp index 4f83c767f..a9a2444de 100644 --- a/Redis/src/Command.cpp +++ b/Redis/src/Command.cpp @@ -408,6 +408,28 @@ Command Command::smove(const std::string& source, const std::string& destination return cmd; } +Command Command::srem(const std::string& set1, const std::string& member) +{ + Command cmd("SREM"); + + cmd << set1 << member; + + return cmd; +} + +Command Command::srem(const std::string& set, const std::vector& members) +{ + Command cmd("SREM"); + + cmd << set; + for(std::vector::const_iterator it = members.begin(); it != members.end(); ++it) + { + cmd << *it; + } + + return cmd; +} + Command Command::sunion(const std::string& set1, const std::string& set2) { Command cmd("SUNION"); diff --git a/Redis/testsuite/src/RedisTest.cpp b/Redis/testsuite/src/RedisTest.cpp index 38ca7fdbc..e955dec1d 100644 --- a/Redis/testsuite/src/RedisTest.cpp +++ b/Redis/testsuite/src/RedisTest.cpp @@ -1639,6 +1639,119 @@ void RedisTest::testSMove() } } +void RedisTest::testStrlen() +{ + if (!_connected) + { + std::cout << "Not connected, test skipped." << std::endl; + return; + } + + Array command; + command.add("SET").add("mykey").add("Hello World"); + + // A set responds with a simple OK string + try + { + std::string result = _redis.execute(command); + assert(result.compare("OK") == 0); + } + catch(RedisException &e) + { + fail(e.message()); + } + + command.clear(); + command.add("STRLEN") + .add("mykey"); + + try + { + Poco::Int64 result = _redis.execute(command); + + assert(result == 11); + } + catch(RedisException &e) + { + fail(e.message()); + } +} + +void RedisTest::testSRem() +{ + if (!_connected) + { + std::cout << "Not connected, test skipped." << std::endl; + return; + } + + delKey("myset"); + + Command sadd = Command::sadd("myset", "one"); + try + { + Poco::Int64 result = _redis.execute(sadd); + assert(result == 1); + } + catch(RedisException &e) + { + fail(e.message()); + } + sadd = Command::sadd("myset", "two"); + try + { + Poco::Int64 result = _redis.execute(sadd); + assert(result == 1); + } + catch(RedisException &e) + { + fail(e.message()); + } + sadd = Command::sadd("myset", "three"); + try + { + Poco::Int64 result = _redis.execute(sadd); + assert(result == 1); + } + catch(RedisException &e) + { + fail(e.message()); + } + + Command srem = Command::srem("myset", "one"); + try + { + Poco::Int64 result = _redis.execute(srem); + assert(result == 1); + } + catch(RedisException &e) + { + fail(e.message()); + } + + srem = Command::srem("myset", "four"); + try + { + Poco::Int64 result = _redis.execute(srem); + assert(result == 0); + } + catch(RedisException &e) + { + fail(e.message()); + } + + Command smembers = Command::smembers("myset"); + try + { + Array result = _redis.execute(smembers); + assert(result.size() == 2); + } + catch(RedisException &e) + { + fail(e.message()); + } +} + void RedisTest::testSUnion() { if (!_connected) @@ -1692,44 +1805,6 @@ void RedisTest::testSUnion() } } -void RedisTest::testStrlen() -{ - if (!_connected) - { - std::cout << "Not connected, test skipped." << std::endl; - return; - } - - Array command; - command.add("SET").add("mykey").add("Hello World"); - - // A set responds with a simple OK string - try - { - std::string result = _redis.execute(command); - assert(result.compare("OK") == 0); - } - catch(RedisException &e) - { - fail(e.message()); - } - - command.clear(); - command.add("STRLEN") - .add("mykey"); - - try - { - Poco::Int64 result = _redis.execute(command); - - assert(result == 11); - } - catch(RedisException &e) - { - fail(e.message()); - } -} - void RedisTest::testSUnionStore() { if (!_connected) @@ -2158,6 +2233,7 @@ CppUnit::Test* RedisTest::suite() CppUnit_addTest(pSuite, RedisTest, testSIsmember); CppUnit_addTest(pSuite, RedisTest, testSMembers); CppUnit_addTest(pSuite, RedisTest, testSMove); + CppUnit_addTest(pSuite, RedisTest, testSRem); CppUnit_addTest(pSuite, RedisTest, testStrlen); CppUnit_addTest(pSuite, RedisTest, testSUnion); CppUnit_addTest(pSuite, RedisTest, testSUnionStore); diff --git a/Redis/testsuite/src/RedisTest.h b/Redis/testsuite/src/RedisTest.h index 801502161..9703a216e 100644 --- a/Redis/testsuite/src/RedisTest.h +++ b/Redis/testsuite/src/RedisTest.h @@ -61,6 +61,7 @@ public: void testSIsmember(); void testSMembers(); void testSMove(); + void testSRem(); void testSUnion(); void testSUnionStore(); void testStrlen();