Skip to content

Instantly share code, notes, and snippets.

@theidexisted
Created November 16, 2017 07:44
Show Gist options
  • Save theidexisted/b2971e9598728622051b548c8053c08c to your computer and use it in GitHub Desktop.
Save theidexisted/b2971e9598728622051b548c8053c08c to your computer and use it in GitHub Desktop.
// Note:
// To test RedisClient, a Redis Server must be running at localhost:6479
#include "RedisClient.h"
#include <gtest/gtest.h>
#include <thread>
template <class client_t>
class RedisClientTest : public testing::Test {
public:
virtual void SetUp () override {
EXPECT_TRUE(client_t::Init());
}
void GetExisted() {
{
auto upsert_fut = client_t::UpsertEntry("1", "2", "3", "value");
auto reply = upsert_fut.get();
EXPECT_TRUE(reply.is_integer());
EXPECT_EQ(reply.as_integer(), 1);
}
{
auto get_fut = client_t::GetEntry("1", "2", "3");
auto reply = get_fut.get();
EXPECT_TRUE(reply.is_string());
EXPECT_EQ(reply.as_string(), std::string("value"));
}
}
void Update() {
{
auto upsert_fut = client_t::UpsertEntry("k1", "k2", "k3", "value123");
auto reply = upsert_fut.get();
EXPECT_TRUE(reply.is_integer());
EXPECT_EQ(reply.as_integer(), 1);
}
{
auto upsert_fut = client_t::UpsertEntry("k1", "k2", "k3", "value123shadow");
auto reply = upsert_fut.get();
EXPECT_TRUE(reply.is_integer());
EXPECT_EQ(reply.as_integer(), 1);
}
{
auto get_fut = client_t::GetEntry("k1", "k2", "k3");
auto reply = get_fut.get();
EXPECT_TRUE(reply.is_string());
EXPECT_EQ(reply.as_string(), std::string("value123shadow"));
}
}
void InsertMore() {
{
auto upsert_fut = client_t::UpsertEntry("k1", "k2", "k3", "value123");
auto reply = upsert_fut.get();
EXPECT_TRUE(reply.is_integer());
EXPECT_EQ(reply.as_integer(), 1);
}
{
auto upsert_fut = client_t::UpsertEntry("k1", "k2", "k4", "value124");
auto reply = upsert_fut.get();
EXPECT_TRUE(reply.is_integer());
EXPECT_EQ(reply.as_integer(), 1);
}
{
auto upsert_fut = client_t::UpsertEntry("k1", "k3", "k4", "value134");
auto reply = upsert_fut.get();
EXPECT_TRUE(reply.is_integer());
EXPECT_EQ(reply.as_integer(), 1);
}
{
auto get_fut = client_t::GetEntry("k1", "k2", "k3");
auto reply = get_fut.get();
EXPECT_TRUE(reply.is_string());
EXPECT_EQ(reply.as_string(), std::string("value123"));
}
{
auto get_fut = client_t::GetEntry("k1", "k2", "k4");
auto reply = get_fut.get();
EXPECT_TRUE(reply.is_string());
EXPECT_EQ(reply.as_string(), std::string("value124"));
}
}
void GetNotExisted() {
{
auto upsert_fut = client_t::UpsertEntry("k1", "k2", "k3", "value123");
auto reply = upsert_fut.get();
EXPECT_TRUE(reply.is_integer());
EXPECT_EQ(reply.as_integer(), 1);
}
{
auto get_fut = client_t::GetEntry("k1", "k2", "k5");
auto reply = get_fut.get();
EXPECT_TRUE(reply.is_string());
EXPECT_EQ(reply.as_string(), std::string(""));
}
{
auto get_fut = client_t::GetEntry("4", "5", "6");
auto reply = get_fut.get();
EXPECT_TRUE(reply.is_string());
EXPECT_EQ(reply.as_string(), std::string(""));
}
}
void RemoveExisted() {
{
auto upsert_fut = client_t::UpsertEntry("1", "2", "3", "value");
auto reply = upsert_fut.get();
EXPECT_TRUE(reply.is_integer());
EXPECT_EQ(reply.as_integer(), 1);
}
{
auto rm_fut = client_t::RemoveEntry("1", "2", "3");
auto reply = rm_fut.get();
EXPECT_TRUE(reply.is_integer());
EXPECT_EQ(reply.as_integer(), 1);
}
}
void RemoveNotExisted() {
auto rm_fut = client_t::RemoveEntry("foo", "bar", "emmmmm");
auto reply = rm_fut.get();
EXPECT_TRUE(reply.is_integer());
EXPECT_EQ(reply.as_integer(), 0);
}
};
#if GTEST_HAS_TYPED_TEST
using testing::Types;
typedef Types<Redis::RedisClient, Redis::RedisClientHook> Implementations;
TYPED_TEST_CASE(RedisClientTest, Implementations);
TYPED_TEST(RedisClientTest, Status) {
EXPECT_EQ(TypeParam::Status(), Redis::RedisClientStatus::Connected);
}
TYPED_TEST(RedisClientTest, GetExisted) {
this->GetExisted();
}
TYPED_TEST(RedisClientTest, Update) {
this->Update();
}
TYPED_TEST(RedisClientTest, InsertMore) {
this->InsertMore();
}
TYPED_TEST(RedisClientTest, GetNotExisted) {
this->GetNotExisted();
}
TYPED_TEST(RedisClientTest, RemoveExisted) {
this->RemoveExisted();
}
TYPED_TEST(RedisClientTest, RemoveNotExisted) {
this->RemoveNotExisted();
}
// One Addational test for hook
TEST(RedisClientHook, Exception) {
EXPECT_EQ(Redis::RedisClientHook::Reset(), true);
ASSERT_THROW(Redis::RedisClientHook::UpsertEntry("1", "2", "3", "value"),
cpp_redis::redis_error);
EXPECT_EQ(Redis::RedisClientHook::Init(), true);
}
#else
#warning "Your gtest should support GTEST_HAS_TYPED_TEST to build this test"
#endif
int main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment