From b9610f4fd9e5c857fa940c48acb978a780ab0aa2 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Tue, 19 Jun 2018 20:39:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=BE=E5=88=B0=E4=B8=8D=E8=83=BD=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=B5=8B=E8=AF=95=E5=8E=9F=E5=9B=A0=EF=BC=9A=E5=B9=B6?= =?UTF-8?q?=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; } ///