diff --git a/RedisStudyTest/RedisHashStudyTest.cs b/RedisStudyTest/RedisHashStudyTest.cs index afe09a9..9c5071b 100644 --- a/RedisStudyTest/RedisHashStudyTest.cs +++ b/RedisStudyTest/RedisHashStudyTest.cs @@ -74,10 +74,12 @@ namespace RedisStudyTest Age = 55 }, }; + + DeleteExitsStudents(); } #endregion - #region 添加或更新学习 + #region 添加或更新学生 /// /// 参数异常 测试 @@ -165,37 +167,189 @@ namespace RedisStudyTest /// - /// 添加部分字段 测试 + /// 添加或更新字段 测试 + /// + [Fact] + public void SetStudentTest() + { + Assert.True(hashStudy.HashSet(preHashKey + student.Id, "Id", student.Id)); + Assert.False(hashStudy.HashSet(preHashKey + student.Id, "Id", student.Id)); + + Assert.True(hashStudy.HashSet(preHashKey + student.Id, "Name", student.Name)); + Assert.False(hashStudy.HashSet(preHashKey + student.Id, "Name", student.Name)); + + Assert.True(hashStudy.HashSet(preHashKey + student.Id, "Age", student.Age)); + Assert.False(hashStudy.HashSet(preHashKey + student.Id, "Age", student.Age)); + } + + /// + /// 添加或更新字段 测试 + /// + [Fact] + public void SetStudentTest2() + { + Assert.True(hashStudy.HashSet(preHashKey + student.Id, "Id", student.Id)); + Assert.False(hashStudy.HashSet(preHashKey + student.Id, "Id", student.Id + 1)); + + var entrys=new HashEntry[] + { + new HashEntry("Name", student.Name), + new HashEntry("Age", student.Age), + }; + + var entrys2 = new HashEntry[] + { + new HashEntry("Name", student.Name+"2"), + new HashEntry("Age", student.Age+1), + }; + + Assert.True(hashStudy.HashSet(preHashKey + student.Id, entrys)); + Assert.True(hashStudy.HashSet(preHashKey + student.Id, entrys2)); + } + + /// + /// 特例:Hash表键名为""(空字符串) + /// 结果:添加或更新操作能成功,但是字段值插入不进去。 + /// + [Fact] + public void SetStudentEmptyKeyTest() + { + Assert.True(hashStudy.HashSet(string.Empty, "Name", "wanggaofeng")); + redisDatabase.KeyDelete(string.Empty); + } + + /// + /// 特例:Hash表,字段名为""(空字符串) + /// 结果:添加或更新操作正常,只是字段键名为"" /// [Fact] - public void AddPartStudentTest() + public void SetStudentEmptyFieldTest() + { + Assert.True(hashStudy.HashSet(preHashKey + student.Id, "", student.Id)); + Assert.False(hashStudy.HashSet(preHashKey + student.Id, "", student.Id+1)); + + redisDatabase.KeyDelete(preHashKey + student.Id); + } + + #endregion + + #region 获取 + + /// + /// 获取一个学生 + /// + [Fact] + public void GetOneSutdentTest() + { + string redisKey = preHashKey + student.Id; + var studentEntries = new HashEntry[] + { + new HashEntry("Id",1), + new HashEntry("Name",student.Name), + new HashEntry("Age",student.Age), + }; + + //插入Sudent + var addHash = hashStudy.HashSet(redisKey, studentEntries); + Assert.True(addHash); + + var entries = hashStudy.HashGetAll(redisKey); + + Assert.NotNull(entries); + + Student myStudent = new Student() + { + Id = (int)entries.FirstOrDefault(e=>e.Name=="Id").Value, + Name = entries.FirstOrDefault(e => e.Name == "Name").Value, + Age = (int)entries.FirstOrDefault(e => e.Name == "Age").Value, + }; + + Assert.True(myStudent.Id==student.Id && myStudent.Name==student.Name && myStudent.Age==student.Age); + + } + + [Fact] + public void GetId() { - //todo:待写 + string redisKey = preHashKey + student.Id; + + Assert.NotEqual(student.Id, hashStudy.HashGet(redisKey,"Id")); + + AddDefaultStudent(); + Assert.Equal(1, hashStudy.HashGet(redisKey, "Id")); + } + + [Fact] + public void GetName() + { + string redisKey = preHashKey + student.Id; + + Assert.NotEqual(student.Name, hashStudy.HashGet(redisKey, "Name").ToString()); + + AddDefaultStudent(); + Assert.Equal(student.Name, hashStudy.HashGet(redisKey, "Name")); + } + + [Fact] + public void GetAge() + { + string redisKey = preHashKey + student.Id; + + Assert.NotEqual(student.Age, hashStudy.HashGet(redisKey, "Age")); + + AddDefaultStudent(); + Assert.Equal(student.Age, hashStudy.HashGet(redisKey, "Age")); } /// - /// 更新学生异常测试 + /// 指定字段是否存在 /// [Fact] - public void UpdateStudentExceptionTest() + public void ExistStudent() { string redisKey = preHashKey + student.Id; - //不存在Key, 返回false,不异常 - Assert.False(hashStudy.HashSet(string.Empty, "Id", -1)); + var studentEntries = new HashEntry[] + { + new HashEntry("Id",1), + new HashEntry("Name",student.Name), + }; + + //插入Sudent + var addHash = hashStudy.HashSet(redisKey, studentEntries); + Assert.True(addHash); + + Assert.True(hashStudy.HashExists(redisKey, "Id")); + Assert.True(hashStudy.HashExists(redisKey, "Name")); + Assert.False(hashStudy.HashExists(redisKey, "Age")); } #endregion + #region 删除学生 + + /// + /// 删除学生 + /// [Fact] - public void Test() + public void DeleteStudent() { - Assert.IsType(1); + Assert.False(redisDatabase.KeyDelete(preHashKey + "-2000")); } + #endregion + /// /// 清理 /// public void Dispose() + { + DeleteExitsStudents(); + } + + /// + /// 删除Redis中的测试学生 + /// + private void DeleteExitsStudents() { if (student != null) { @@ -207,5 +361,19 @@ namespace RedisStudyTest redisDatabase.KeyDelete(preHashKey + temp.Id); } } + + private void AddDefaultStudent() + { + string redisKey = preHashKey + student.Id; + var studentEntries = new HashEntry[] + { + new HashEntry("Id",1), + new HashEntry("Name",student.Name), + new HashEntry("Age",student.Age), + }; + + //插入Sudent + var addHash = hashStudy.HashSet(redisKey, studentEntries); + } } } diff --git a/RedisStuy/RedisHashStudy.cs b/RedisStuy/RedisHashStudy.cs index 51d7f54..108d9c2 100644 --- a/RedisStuy/RedisHashStudy.cs +++ b/RedisStuy/RedisHashStudy.cs @@ -167,11 +167,11 @@ namespace RedisStuy throw new ArgumentNullException("hashFields", "参数hashFields.Length "); } - redisDatabase.HashSet(key, hashFields, flags); + bool result = false; try { - + redisDatabase.HashSet(key, hashFields, flags); result = true; } catch (ArgumentNullException argumentNullException)