Add scard

This commit is contained in:
fbraem
2015-11-14 20:41:12 +01:00
parent 36108e2378
commit d4a791d79c
4 changed files with 197 additions and 0 deletions

View File

@@ -30,6 +30,10 @@ namespace Redis {
class Redis_API Command : public Array
/// Helper class for creating commands. This class contains
/// factory methods for commonly used Redis commands.
/// There are two ways of building commands:
/// 1. Use this class (which uses option 2 in the factory methods)
/// 2. Use the Array class and build the command using the add method
/// or << operator.
{
public:
Command(const std::string& command);
@@ -105,6 +109,18 @@ public:
static Command mset(const std::map<std::string, std::string>& keyvalues, bool create = true);
/// Returns a MSET or MSETNX (when create is false) command
static Command sadd(const std::string& key, const std::string& value);
/// Returns a SADD command
static Command sadd(const std::string& key, const std::vector<std::string>& values);
/// Returns a SADD command
static Command scard(const std::string& key);
/// Returns a SCARD command
static Command smembers(const std::string& key);
/// Returns a SMEMBERS command
static Command set(const std::string& key, const std::string& value, bool overwrite = true, const Poco::Timespan& expireTime = 0, bool create = true);
/// Returns a SET command to set the key with a value

View File

@@ -245,6 +245,46 @@ Command Command::mset(const std::map<std::string, std::string>& keyvalues, bool
return cmd;
}
Command Command::sadd(const std::string& key, const std::string& value)
{
Command cmd("SADD");
cmd << key << value;
return cmd;
}
Command Command::sadd(const std::string& key, const std::vector<std::string>& values)
{
Command cmd("SADD");
cmd << key;
for(std::vector<std::string>::const_iterator it = values.begin(); it != values.end(); ++it)
{
cmd << *it;
}
return cmd;
}
Command Command::scard(const std::string& key)
{
Command cmd("SCARD");
cmd << key;
return cmd;
}
Command Command::smembers(const std::string& key)
{
Command cmd("SMEMBERS");
cmd << key;
return cmd;
}
Command Command::set(const std::string& key, const std::string& value, bool overwrite, const Poco::Timespan& expireTime, bool create)
{
Command cmd("SET");

View File

@@ -1110,6 +1110,94 @@ void RedisTest::testPubSub()
_redis.flush();
}
void RedisTest::testSAdd()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
delKey("myset");
Command sadd = Command::sadd("myset", "Hello");
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 1);
}
catch(RedisException &e)
{
fail(e.message());
}
sadd = Command::sadd("myset", "World");
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 1);
}
catch(RedisException &e)
{
fail(e.message());
}
sadd = Command::sadd("myset", "World");
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testSCard()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
delKey("myset");
Command sadd = Command::sadd("myset", "Hello");
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 1);
}
catch(RedisException &e)
{
fail(e.message());
}
sadd = Command::sadd("myset", "World");
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 1);
}
catch(RedisException &e)
{
fail(e.message());
}
Command scard = Command::scard("myset");
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(scard);
assert(result == 2);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testSet()
{
if (!_connected)
@@ -1184,6 +1272,53 @@ void RedisTest::testStrlen()
}
}
void RedisTest::testSMembers()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
delKey("myset");
Command sadd = Command::sadd("myset", "Hello");
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 1);
}
catch(RedisException &e)
{
fail(e.message());
}
sadd = Command::sadd("myset", "World");
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
assert(result == 1);
}
catch(RedisException &e)
{
fail(e.message());
}
Command smembers = Command::smembers("myset");
try
{
Array result = _redis.execute<Array>(smembers);
assert(result.size() == 2);
assert(result.get<BulkString>(0).value().compare("World") == 0);
assert(result.get<BulkString>(1).value().compare("Hello") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testRename()
{
if (!_connected)
@@ -1537,7 +1672,10 @@ CppUnit::Test* RedisTest::suite()
CppUnit_addTest(pSuite, RedisTest, testPipeliningWithSendCommands);
CppUnit_addTest(pSuite, RedisTest, testPipeliningWithWriteCommand);
CppUnit_addTest(pSuite, RedisTest, testPubSub);
CppUnit_addTest(pSuite, RedisTest, testSAdd);
CppUnit_addTest(pSuite, RedisTest, testSCard);
CppUnit_addTest(pSuite, RedisTest, testSet);
CppUnit_addTest(pSuite, RedisTest, testSMembers);
CppUnit_addTest(pSuite, RedisTest, testStrlen);
CppUnit_addTest(pSuite, RedisTest, testRename);
CppUnit_addTest(pSuite, RedisTest, testRenameNx);

View File

@@ -51,7 +51,10 @@ public:
void testPipeliningWithSendCommands();
void testPipeliningWithWriteCommand();
void testPubSub();
void testSAdd();
void testSCard();
void testSet();
void testSMembers();
void testStrlen();
void testRename();
void testRenameNx();