From 0d2c37ebfe8f1b4e80050b13d21301414981f160 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Tue, 19 Jun 2018 19:53:44 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=EF=BC=8C=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RedisStudyTest/RedisHashStudyTest.cs | 90 ---------------------------- 1 file changed, 90 deletions(-) diff --git a/RedisStudyTest/RedisHashStudyTest.cs b/RedisStudyTest/RedisHashStudyTest.cs index 8287637..4e3ea0d 100644 --- a/RedisStudyTest/RedisHashStudyTest.cs +++ b/RedisStudyTest/RedisHashStudyTest.cs @@ -177,44 +177,6 @@ namespace RedisStudyTest Assert.False(redisDatabase.KeyDelete(preHashKey + studentId)); } - [Fact] - public void QueryOneStudentTest() - { - //hashStudy.AddStudent(this.student); - - //var queryStudent = hashStudy.QueryOneStudent(this.student.Id); - - //Assert.NotNull(queryStudent); - //Assert.True(this.student.Id==queryStudent.Id); - //Assert.True(this.student.Name == queryStudent.Name); - //Assert.True(this.student.Age == queryStudent.Age); - } - - [Fact] - public void ExistStudentTest() - { - //Assert.True(hashStudy.ExistStudent(students[0].Id)); - //Assert.True(hashStudy.ExistStudent(students[1].Id)); - //Assert.True(hashStudy.ExistStudent(students[2].Id)); - //Assert.True(hashStudy.ExistStudent(students[3].Id)); - //Assert.True(hashStudy.ExistStudent(students[4].Id)); - - //Assert.False(hashStudy.ExistStudent(-1000)); - //Assert.False(hashStudy.ExistStudent(-2000)); - } - - /// - /// 查询所有学生 - /// - [Fact] - public void QueryAllStudent() - { - //List students = hashStudy.QueryAllStudents(); - - //Assert.NotNull(students); - //Assert.Equal(students.Count(), students.Count); - } - /// /// 清理 /// @@ -230,57 +192,5 @@ namespace RedisStudyTest redisDatabase.KeyDelete(preHashKey + temp.Id); } } - - #region 辅助方法 - public HashEntry[] StudentsToHashEntrys(Student student) - { - if (student == null) - { - return null; - } - - var entrys = new List() - { - new HashEntry("Id",student.Id), - new HashEntry("Name",student.Name), - new HashEntry("Age",student.Age), - }; - return entrys.ToArray(); - } - - public Student HashEntrysToStudent(HashEntry[] hashEntrys) - { - if (hashEntrys == null) - { - return null; - } - - if (hashEntrys.Length <= 0) - { - return null; - } - - var student = new Student(); - foreach (var entry in hashEntrys) - { - switch (entry.Name) - { - case "Id": - student.Id = entry.Value.IsInteger ? (int)entry.Value : default(int); ; - break; - case "Name": - student.Name = entry.Value.ToString(); - break; - case "Age": - student.Age = entry.Value.IsInteger ? (int)entry.Value : default(int); - break; - default: - break; - } - } - - return student; - } - #endregion } } From 3ddd05b572f61393b5b7a681d1cdfa30314c5b07 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Tue, 19 Jun 2018 20:02:48 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=EF=BC=9A?= =?UTF-8?q?RedisHashStudyTest=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E5=B7=B2?= =?UTF-8?q?=E6=8E=92=E9=99=A4=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RedisStudyTest/RedisStudyTest.cs | 10 ++++------ RedisStudyTest/RedisStudyTest.csproj | 1 - 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/RedisStudyTest/RedisStudyTest.cs b/RedisStudyTest/RedisStudyTest.cs index 093130e..0be3440 100644 --- a/RedisStudyTest/RedisStudyTest.cs +++ b/RedisStudyTest/RedisStudyTest.cs @@ -14,22 +14,20 @@ namespace RedisStudyTest { public class RedisStudyTest { - private IServer redisServer; - private RedisServerStudy redisServerStudy; + private RedisServerStudy _redisServerStudy; public RedisStudyTest() { - redisServer = RedisHelper.GetDefaultRedisServer(); - redisServerStudy = new RedisServerStudy(); + _redisServerStudy = new RedisServerStudy(); } [Fact] public void Test() { - var info= redisServerStudy.RedisInfo(); + var info = _redisServerStudy.RedisInfo(); Assert.NotEmpty(info); - Assert.True(info.Count()>0); + Assert.True(info.Any()); } } } diff --git a/RedisStudyTest/RedisStudyTest.csproj b/RedisStudyTest/RedisStudyTest.csproj index cbeda78..f59bee6 100644 --- a/RedisStudyTest/RedisStudyTest.csproj +++ b/RedisStudyTest/RedisStudyTest.csproj @@ -77,7 +77,6 @@ - From 3a3e34723ed5fd7dac0173031feab26ffe1d4755 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Tue, 19 Jun 2018 20:17:44 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=86=99Redis=20Hash?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RedisStudyTest/RedisHashStudyTest.cs | 97 +++--------- RedisStudyTest/RedisHashStudyTest22.cs | 196 +++++++++++++++++++++++++ RedisStudyTest/RedisStudyTest.csproj | 1 + 3 files changed, 215 insertions(+), 79 deletions(-) create mode 100644 RedisStudyTest/RedisHashStudyTest22.cs diff --git a/RedisStudyTest/RedisHashStudyTest.cs b/RedisStudyTest/RedisHashStudyTest.cs index 4e3ea0d..9b4efa3 100644 --- a/RedisStudyTest/RedisHashStudyTest.cs +++ b/RedisStudyTest/RedisHashStudyTest.cs @@ -3,19 +3,24 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using StackExchange.Redis; using Xunit; +using Xunit.Extensions; +using Xunit.Serialization; +using Xunit.Abstractions; +using Xunit.Sdk; using RedisStudyModel; - -using StackExchange; -using StackExchange.Redis; - using RedisStuy; namespace RedisStudyTest { + /// + /// Redis Hash 类型测试 + /// public class RedisHashStudyTest : IDisposable { + #region 初始化 private IDatabase redisDatabase = null; private RedisHashStudy hashStudy = null; private List students; @@ -68,10 +73,10 @@ namespace RedisStudyTest Age = 55 }, }; - - //hashStudy.AddStudents(students); } + #endregion + #region 添加或更新学习 [Fact] public void AddStudentExceptionTest() { @@ -88,93 +93,27 @@ namespace RedisStudyTest { string redisKey = preHashKey + student.Id; + //当前上下文不能使用: When.Exists + var id_When_NotExists_No = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.NotExists); Assert.True(id_When_NotExists_No); - var id_When_NotExists_Yes = hashStudy.HashSet(redisKey, "Id2", student.Id + 1, When.NotExists); + var id_When_NotExists_Yes = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.NotExists); Assert.False(id_When_NotExists_Yes); - var id_When_Exists_Yes = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.Exists); - Assert.True(id_When_Exists_Yes); - - var id_When_Exists_No = hashStudy.HashSet(redisKey, "Id3", student.Id + 1, When.Exists); - Assert.False(id_When_Exists_No); - var id_When_Always_Exists = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.Always); - Assert.True(id_When_Always_Exists); + Assert.False(id_When_Always_Exists); var id_When_Always_NotExists = hashStudy.HashSet(redisKey, "Id4", student.Id + 1, When.Always); Assert.True(id_When_Always_NotExists); } - [Fact] - public void AddStudentCommandFlagTest() - { - string redisKey = preHashKey + student.Id; - } - - [Fact] - public void AddStudentTest() - { - 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, CommandFlags.None); - Assert.True(addHash); - - //设置过期 - redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(5)); - } + #endregion - /// - /// 更新学生异常测试 - /// [Fact] - public void UpdateStudentExceptionTest() - { - string redisKey = preHashKey + student.Id; - //不存在Key - Assert.Throws(()=> hashStudy.HashSet(string.Empty, "Id", -1)); - } - - /// - /// 更新学生 - /// - [Theory] - [InlineData("Id", 1)] - [InlineData("Name",1)] - [InlineData("Age",1)] - public void UpdateStudentTest(RedisValue fieldName, RedisValue value) - { - string redisKey = preHashKey + student.Id; - var addOrUpdateOne = hashStudy.HashSet(redisKey, fieldName, value+1); - Assert.True(addOrUpdateOne); - addOrUpdateOne = hashStudy.HashSet(redisKey, "Name", student.Name + 1); - Assert.True(addOrUpdateOne); - addOrUpdateOne = hashStudy.HashSet(redisKey, "Age", student.Age + 1); - Assert.True(addOrUpdateOne); - } - - [Theory] - [InlineData(-1)] - [InlineData(-2)] - [InlineData(-3)] - public void DelStudentTest(int studentId) - { - Assert.False(redisDatabase.KeyDelete(preHashKey + studentId)); - } - - [Theory] - [InlineData(-100)] - public void DelStudentTest2(int studentId) + public void Test() { - Assert.False(redisDatabase.KeyDelete(preHashKey + studentId)); + Assert.IsType(1); } /// diff --git a/RedisStudyTest/RedisHashStudyTest22.cs b/RedisStudyTest/RedisHashStudyTest22.cs new file mode 100644 index 0000000..4e3ea0d --- /dev/null +++ b/RedisStudyTest/RedisHashStudyTest22.cs @@ -0,0 +1,196 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +using RedisStudyModel; + +using StackExchange; +using StackExchange.Redis; + +using RedisStuy; + +namespace RedisStudyTest +{ + public class RedisHashStudyTest : IDisposable + { + private IDatabase redisDatabase = null; + private RedisHashStudy hashStudy = null; + private List students; + private Student student = null; + private string preHashKey = "RedisStudy:Student:"; + + /// + /// 构造 + /// + public RedisHashStudyTest() + { + redisDatabase = RedisHelper.GetRedisDatabase(); + hashStudy = new RedisHashStudy(); + student = new Student() + { + Id = 1, + Name = "王高峰", + Age = 2 * 9 + }; + students = new List() + { + new Student() + { + Id = 1001, + Name = "王高峰", + Age = 11 + }, + new Student() + { + Id = 1002, + Name = "王高峰2", + Age = 22 + }, + new Student() + { + Id = 1003, + Name = "王高峰3", + Age = 33 + }, + new Student() + { + Id = 1004, + Name = "王高峰4", + Age = 44 + }, + new Student() + { + Id = 1005, + Name = "王高峰5", + Age = 55 + }, + }; + + //hashStudy.AddStudents(students); + } + + [Fact] + public void AddStudentExceptionTest() + { + string redisKey = preHashKey + student.Id; + //参数异常测试 + Assert.Throws(() => hashStudy.HashSet(string.Empty, null)); + Assert.Throws(() => hashStudy.HashSet("", null)); + Assert.Throws(() => hashStudy.HashSet(preHashKey + "-1", null)); + Assert.Throws(() => hashStudy.HashSet(preHashKey + "-1", new HashEntry[] { })); + } + + [Fact] + public void AddStudentWhenTest() + { + string redisKey = preHashKey + student.Id; + + var id_When_NotExists_No = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.NotExists); + Assert.True(id_When_NotExists_No); + + var id_When_NotExists_Yes = hashStudy.HashSet(redisKey, "Id2", student.Id + 1, When.NotExists); + Assert.False(id_When_NotExists_Yes); + + var id_When_Exists_Yes = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.Exists); + Assert.True(id_When_Exists_Yes); + + var id_When_Exists_No = hashStudy.HashSet(redisKey, "Id3", student.Id + 1, When.Exists); + Assert.False(id_When_Exists_No); + + var id_When_Always_Exists = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.Always); + Assert.True(id_When_Always_Exists); + + var id_When_Always_NotExists = hashStudy.HashSet(redisKey, "Id4", student.Id + 1, When.Always); + Assert.True(id_When_Always_NotExists); + } + + [Fact] + public void AddStudentCommandFlagTest() + { + string redisKey = preHashKey + student.Id; + } + + [Fact] + public void AddStudentTest() + { + 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, CommandFlags.None); + Assert.True(addHash); + + //设置过期 + redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(5)); + } + + /// + /// 更新学生异常测试 + /// + [Fact] + public void UpdateStudentExceptionTest() + { + string redisKey = preHashKey + student.Id; + //不存在Key + Assert.Throws(()=> hashStudy.HashSet(string.Empty, "Id", -1)); + } + + /// + /// 更新学生 + /// + [Theory] + [InlineData("Id", 1)] + [InlineData("Name",1)] + [InlineData("Age",1)] + public void UpdateStudentTest(RedisValue fieldName, RedisValue value) + { + string redisKey = preHashKey + student.Id; + var addOrUpdateOne = hashStudy.HashSet(redisKey, fieldName, value+1); + Assert.True(addOrUpdateOne); + addOrUpdateOne = hashStudy.HashSet(redisKey, "Name", student.Name + 1); + Assert.True(addOrUpdateOne); + addOrUpdateOne = hashStudy.HashSet(redisKey, "Age", student.Age + 1); + Assert.True(addOrUpdateOne); + } + + [Theory] + [InlineData(-1)] + [InlineData(-2)] + [InlineData(-3)] + public void DelStudentTest(int studentId) + { + Assert.False(redisDatabase.KeyDelete(preHashKey + studentId)); + } + + [Theory] + [InlineData(-100)] + public void DelStudentTest2(int studentId) + { + Assert.False(redisDatabase.KeyDelete(preHashKey + studentId)); + } + + /// + /// 清理 + /// + public void Dispose() + { + if (student != null) + { + redisDatabase.KeyDelete(preHashKey + student.Id); + } + + foreach (var temp in students) + { + redisDatabase.KeyDelete(preHashKey + temp.Id); + } + } + } +} diff --git a/RedisStudyTest/RedisStudyTest.csproj b/RedisStudyTest/RedisStudyTest.csproj index f59bee6..b1bb9f9 100644 --- a/RedisStudyTest/RedisStudyTest.csproj +++ b/RedisStudyTest/RedisStudyTest.csproj @@ -74,6 +74,7 @@ + From b9610f4fd9e5c857fa940c48acb978a780ab0aa2 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Tue, 19 Jun 2018 20:39:59 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E6=89=BE=E5=88=B0=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=B5=8B=E8=AF=95=E5=8E=9F=E5=9B=A0=EF=BC=9A?= =?UTF-8?q?=E5=B9=B6=E9=87=8D=E5=86=99=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RedisStudyTest/RedisHashStudyTest.cs | 68 ++++++++++++++++++++++++++ RedisStudyTest/RedisHashStudyTest22.cs | 2 +- RedisStuy/RedisHashStudy.cs | 3 +- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/RedisStudyTest/RedisHashStudyTest.cs b/RedisStudyTest/RedisHashStudyTest.cs index 9b4efa3..ca0031f 100644 --- a/RedisStudyTest/RedisHashStudyTest.cs +++ b/RedisStudyTest/RedisHashStudyTest.cs @@ -26,6 +26,7 @@ namespace RedisStudyTest private List students; private Student student = null; private string preHashKey = "RedisStudy:Student:"; + private int keyExpireSeconds = 20; /// /// 构造 @@ -77,6 +78,10 @@ namespace RedisStudyTest #endregion #region 添加或更新学习 + + /// + /// 参数异常测试 + /// [Fact] public void AddStudentExceptionTest() { @@ -88,6 +93,9 @@ namespace RedisStudyTest Assert.Throws(() => hashStudy.HashSet(preHashKey + "-1", new HashEntry[] { })); } + /// + /// 参数When测试 + /// [Fact] public void AddStudentWhenTest() { @@ -108,6 +116,66 @@ namespace RedisStudyTest Assert.True(id_When_Always_NotExists); } + /// + /// 添加一个默认学生 + /// + [Fact] + public void AddStudentTest() + { + 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, CommandFlags.None); + Assert.True(addHash); + + //设置过期 + redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(keyExpireSeconds)); + } + + /// + /// 添加一组初始化设置的学生 + /// + [Fact] + public void AddStudentsTest() + { + foreach (var temp in students) + { + string redisKey = preHashKey + temp.Id; + var studentEntries = new HashEntry[] + { + new HashEntry("Id", temp.Id), + new HashEntry("Name", temp.Name), + new HashEntry("Age", temp.Age), + }; + + //插入Sudent + var addStudent = hashStudy.HashSet(redisKey, studentEntries); + Assert.True(addStudent); + + //设置过期 + redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(keyExpireSeconds)); + } + } + + /// + /// 更新学生异常测试 + /// + [Fact] + public void UpdateStudentExceptionTest() + { + string redisKey = preHashKey + student.Id; + //不存在Key, 返回false,不异常 + Assert.False(hashStudy.HashSet(string.Empty, "Id", -1)); + } + + + #endregion [Fact] diff --git a/RedisStudyTest/RedisHashStudyTest22.cs b/RedisStudyTest/RedisHashStudyTest22.cs index 4e3ea0d..64af6d9 100644 --- a/RedisStudyTest/RedisHashStudyTest22.cs +++ b/RedisStudyTest/RedisHashStudyTest22.cs @@ -144,7 +144,7 @@ namespace RedisStudyTest } /// - /// 更新学生 + /// 更新学生:导致测试不能运行 /// [Theory] [InlineData("Id", 1)] diff --git a/RedisStuy/RedisHashStudy.cs b/RedisStuy/RedisHashStudy.cs index 7533b0e..51d7f54 100644 --- a/RedisStuy/RedisHashStudy.cs +++ b/RedisStuy/RedisHashStudy.cs @@ -141,7 +141,8 @@ namespace RedisStuy /// public bool HashSet(RedisKey key, RedisValue hashField, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None) { - return redisDatabase.HashSet(key, hashField, value, when, flags); + var result = redisDatabase.HashSet(key, hashField, value, when, flags); + return result; } /// From 1632c69ff17c208190a0d6151c1510e2848fe3d2 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Tue, 19 Jun 2018 20:45:09 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 26b6c1e..43fc402 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # RedisStudy -Redis 缓存学习 \ No newline at end of file +Redis 缓存学习, 使用StackExchange.Redis。 +命令参考网址 http://www.redis.net.cn \ No newline at end of file From f286f00d2ebaf6ea59ec5519ae844cd3975e7604 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Tue, 19 Jun 2018 20:49:16 +0800 Subject: [PATCH 6/7] Develop --- RedisStudyTest/RedisHashStudyTest.cs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/RedisStudyTest/RedisHashStudyTest.cs b/RedisStudyTest/RedisHashStudyTest.cs index ca0031f..afe09a9 100644 --- a/RedisStudyTest/RedisHashStudyTest.cs +++ b/RedisStudyTest/RedisHashStudyTest.cs @@ -80,7 +80,7 @@ namespace RedisStudyTest #region 添加或更新学习 /// - /// 参数异常测试 + /// 参数异常 测试 /// [Fact] public void AddStudentExceptionTest() @@ -94,7 +94,7 @@ namespace RedisStudyTest } /// - /// 参数When测试 + /// 参数 When 测试 /// [Fact] public void AddStudentWhenTest() @@ -117,7 +117,7 @@ namespace RedisStudyTest } /// - /// 添加一个默认学生 + /// 添加一个默认学生 测试 /// [Fact] public void AddStudentTest() @@ -139,7 +139,7 @@ namespace RedisStudyTest } /// - /// 添加一组初始化设置的学生 + /// 添加一组初始化设置的学生 测试 /// [Fact] public void AddStudentsTest() @@ -163,6 +163,16 @@ namespace RedisStudyTest } } + + /// + /// 添加部分字段 测试 + /// + [Fact] + public void AddPartStudentTest() + { + //todo:待写 + } + /// /// 更新学生异常测试 /// @@ -174,8 +184,6 @@ namespace RedisStudyTest Assert.False(hashStudy.HashSet(string.Empty, "Id", -1)); } - - #endregion [Fact] From 2406b521b71fe52173dfe1dd7834039f5f4ab5cc Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Wed, 20 Jun 2018 19:03:01 +0800 Subject: [PATCH 7/7] RedisHashTest complete --- RedisStudyTest/RedisHashStudyTest.cs | 188 +++++++++++++++++++++++++-- RedisStuy/RedisHashStudy.cs | 4 +- 2 files changed, 180 insertions(+), 12 deletions(-) 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)