From 3a3e34723ed5fd7dac0173031feab26ffe1d4755 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Tue, 19 Jun 2018 20:17:44 +0800 Subject: [PATCH] =?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 @@ +