Redis .Net ClientライブラリBeetle.Redisテストケース


Beetle.Redisの第1段階の機能は基本的に完成して,主にいくつかの簡単な構造の処理を実現して,GET,SET MGET,MHSET,MHGETとLISTの関連するいくつかのよく使う構造.すでに開通したテスト例
namespace Beetle.RedisClient.TestLib
{
    [TestClass]
    public class RedisClientTest
    {

        private static Beetle.Redis.RedisClient db = new Redis.RedisClient("redisClientSection");

        [TestMethod]
        public void Set_GET_String()
        {
            string key = "Set_GET_String";
            string value = "henry";
            db.SetString(key, value);
            Assert.AreEqual(value, db.GetString(key));

        }

        [TestMethod]
        public void Set_Get_Json()
        {
            string key = "get_set_json";
            UserBase user = new UserBase();
            user.Name = "henry";
            user.City = "gz";
            user.Counrty = "cn";
            db.SetJson<UserBase>(key, user);
            UserBase result = db.GetJson<UserBase>(key);
            Assert.AreEqual(user.Name, result.Name);
        }

        [TestMethod]
        public void Get_Set_Protobuf()
        {
            string key = "get_set_protobuf";
            UserBase user = new UserBase();
            user.Name = "henry";
            user.City = "gz";
            user.Counrty = "cn";
            db.SetProbobuf<UserBase>(key, user);
            UserBase result = db.GetProtobuf<UserBase>(key);
            Assert.AreEqual(user.Name, result.Name);
        }

        [TestMethod]
        public void Get_Strings()
        {
            string[] keys = new string[] { "_test1", "_test2", "_test3" };
            string[] values = new string[] { "_test1", "_test2", "_test3" };
            for (int i = 0; i < keys.Length; i++)
            {
                db.SetString(keys[i], values[i]);
            }
            IList<string> result = db.GetStrings(keys);
            for (int i = 0; i < keys.Length; i++)
            {
                Assert.AreEqual(result[i], values[i]);
            }
        }

        [TestMethod]
        public void Get_Jsons()
        {
            string[] keys = new string[] { "json_test1", "json_test2", "json_test3" };
            UserBase[] values = new UserBase[] { 
             new UserBase{ Name="u1"},
             new UserBase{ Name="u2"},
             new UserBase{ Name="u2"}
            };
            for (int i = 0; i < keys.Length; i++)
            {
                db.SetJson<UserBase>(keys[i], values[i]);
            }
            IList<object> result= db.GetJsons<UserBase, UserBase, UserBase>(keys[0], keys[1], keys[2]);
            for (int i = 0; i < keys.Length; i++)
            {
                Assert.AreEqual(((UserBase)result[i]).Name, values[i].Name);
            }
        }

        [TestMethod]
        public void Get_Protobufs()
        {
            string[] keys = new string[] { "protobuf_test1", "protobuf_test2", "protobuf_test3" };
            UserBase[] values = new UserBase[] { 
             new UserBase{ Name="u1"},
             new UserBase{ Name="u2"},
             new UserBase{ Name="u2"}
            };
            for (int i = 0; i < keys.Length; i++)
            {
                db.SetProbobuf<UserBase>(keys[i], values[i]);
            }
            IList<object> result = db.GetProtobufs<UserBase, UserBase, UserBase>(keys[0], keys[1], keys[2]);
            for (int i = 0; i < keys.Length; i++)
            {
                Assert.AreEqual(((UserBase)result[i]).Name, values[i].Name);
            }
        }

        [TestMethod]
        public void String_Record()
        {
            string name = "NAME";
            string qq = "QQ";
            StringRecord sr = new StringRecord("henry",db);
            sr[name] = "henryfan";
            sr[qq] = "28304340";
            sr.Save();
            sr = new StringRecord("henry", db);
            sr.Load();
            Assert.AreEqual(sr[name], "henryfan");
            Assert.AreEqual(sr[qq], "28304340");

        }

        [TestMethod]
        public void Json_Record()
        {
            JsonRecord jr = new JsonRecord("json_henry",db);
            UserBase user = new UserBase { Name="herny" };
            Contact con = new Contact { EMail="[email protected]", QQ="28304340" };
            jr.Set<UserBase>(user);
            jr.Set<Contact>(con);
            jr.Save();
            jr = new JsonRecord("json_henry", db);
            jr.Load<UserBase, Contact>();
            Assert.AreEqual(jr.Get<UserBase>().Name,user.Name);
            Assert.AreEqual(jr.Get<Contact>().QQ,con.QQ);
        }

        [TestMethod]
        public void Protobuf_Record()
        {

            ProtobufRecord jr = new ProtobufRecord("protobuf_henry", db);
            UserBase user = new UserBase { Name = "herny" };
            Contact con = new Contact { EMail = "[email protected]", QQ = "28304340" };
            jr.Set<UserBase>(user);
            jr.Set<Contact>(con);
            jr.Save();
            jr = new ProtobufRecord("protobuf_henry", db);
            jr.Load<UserBase, Contact>();
            Assert.AreEqual(jr.Get<UserBase>().Name, user.Name);
            Assert.AreEqual(jr.Get<Contact>().QQ, con.QQ);
        }

        [TestMethod]
        public void StringList_Pop_Push()
        {
            StringList sl = new StringList("_list",db);
            sl.Clear();
            string value = "henry";
            sl.Push(value);
            Assert.AreEqual("henry", sl.Pop());
        }
         
        [TestMethod]
        public void StringList_Add_Remove()
        {
            StringList sl = new StringList("_list", db);
            sl.Clear();
            string value = "henry";
            sl.Add(value);
            Assert.AreEqual("henry", sl.Remove());
        }

        [TestMethod]
        public void StringList_ToList()
        {
            StringList sl = new StringList("_list", db);
            sl.Clear();
            string[] values = new string[] {"a","b","c","d" };
            foreach (string item in values)
            {
                sl.Add(item);
            }
            IList<string> result = sl.ToList();
            for (int i = 0; i < result.Count; i++)
            {
                Assert.AreEqual(values[i], result[i]);
            }
            result = sl.ToList(1, 2);
            for (int i = 1; i <= 2; i++)
            {
                Assert.AreEqual(values[i], result[i-1]);
            }

        }

        [TestMethod]
        public void JsonList_Pop_Push()
        {
            JsonList<UserBase> sl = new JsonList<UserBase>("json_list", db);
            sl.Clear();
            UserBase user = new UserBase { Name="henry" };
            sl.Push(user);
            user = new UserBase { Name = "henry1" };
            sl.Push(user);
            Assert.AreEqual(user.Name, sl.Pop().Name);
        }

        [TestMethod]
        public void JsonList_Add_Remove()
        {
            JsonList<UserBase> sl = new JsonList<UserBase>("json_list", db);
            sl.Clear();
            UserBase user = new UserBase { Name = "henry" };
            sl.Add(user);
            user = new UserBase { Name = "henry1" };
            sl.Add(user);
            Assert.AreEqual(user.Name, sl.Remove().Name);
        }

        [TestMethod]
        public void JsonList_ToList()
        {
            JsonList<UserBase> sl = new JsonList<UserBase>("json_list", db);
            sl.Clear();
            UserBase[] values = new UserBase[] { 
                new UserBase{ Name="a"},
                new UserBase{ Name="b"},
                new UserBase{ Name="c"},
                new UserBase{ Name="d"}

            };
            foreach (UserBase item in values)
            {
                sl.Add(item);
            }
            IList<UserBase> result = sl.ToList();
            for (int i = 0; i < result.Count; i++)
            {
                Assert.AreEqual(values[i].Name, result[i].Name);
            }
            result = sl.ToList(1, 2);
            for (int i = 1; i <= 2; i++)
            {
                Assert.AreEqual(values[i].Name, result[i - 1].Name);
            }

        }

        [TestMethod]
        public void ProtobufList_Pop_Push()
        {
            ProtobufList<UserBase> sl = new ProtobufList<UserBase>("protobuf_list", db);
            sl.Clear();
            UserBase user = new UserBase { Name = "henry" };
            sl.Push(user);
            user = new UserBase { Name = "henry1" };
            sl.Push(user);
            Assert.AreEqual(user.Name, sl.Pop().Name);
        }

        [TestMethod]
        public void ProtobufList_Add_Remove()
        {
            ProtobufList<UserBase> sl = new ProtobufList<UserBase>("protobuf_list", db);
            sl.Clear();
            UserBase user = new UserBase { Name = "henry" };
            sl.Add(user);
            user = new UserBase { Name = "henry1" };
            sl.Add(user);
            Assert.AreEqual(user.Name, sl.Remove().Name);
        }

        [TestMethod]
        public void ProtobufList_ToList()
        {
            ProtobufList<UserBase> sl = new ProtobufList<UserBase>("protobuf_list", db);
            sl.Clear();
            UserBase[] values = new UserBase[] { 
                new UserBase{ Name="a"},
                new UserBase{ Name="b"},
                new UserBase{ Name="c"},
                new UserBase{ Name="d"}

            };
            foreach (UserBase item in values)
            {
                sl.Add(item);
            }
            IList<UserBase> result = sl.ToList();
            for (int i = 0; i < result.Count; i++)
            {
                Assert.AreEqual(values[i].Name, result[i].Name);
            }
            result = sl.ToList(1, 2);
            for (int i = 1; i <= 2; i++)
            {
                Assert.AreEqual(values[i].Name, result[i - 1].Name);
            }

        }
    }
}