# Conflicts:
#	RedisStudyTest/RedisHashStudyTest.cs
master
ruyu 7 years ago
commit 36dce7d59e

@ -1,3 +1,4 @@
# RedisStudy # RedisStudy
Redis 缓存学习 Redis 缓存学习, 使用StackExchange.Redis。
命令参考网址 http://www.redis.net.cn

@ -3,24 +3,30 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using StackExchange.Redis;
using Xunit; using Xunit;
using Xunit.Extensions;
using Xunit.Serialization;
using Xunit.Abstractions;
using Xunit.Sdk;
using RedisStudyModel; using RedisStudyModel;
using StackExchange;
using StackExchange.Redis;
using RedisStuy; using RedisStuy;
namespace RedisStudyTest namespace RedisStudyTest
{ {
/// <summary>
/// Redis Hash 类型测试
/// </summary>
public class RedisHashStudyTest : IDisposable public class RedisHashStudyTest : IDisposable
{ {
#region 初始化
private IDatabase redisDatabase = null; private IDatabase redisDatabase = null;
private RedisHashStudy hashStudy = null; private RedisHashStudy hashStudy = null;
private List<Student> students; private List<Student> students;
private Student student = null; private Student student = null;
private string preHashKey = "RedisStudy:Student:"; private string preHashKey = "RedisStudy:Student:";
private int keyExpireSeconds = 20;
/// <summary> /// <summary>
/// 构造 /// 构造
@ -69,9 +75,15 @@ namespace RedisStudyTest
}, },
}; };
//hashStudy.AddStudents(students); DeleteExitsStudents();
} }
#endregion
#region 添加或更新学生
/// <summary>
/// 参数异常 测试
/// </summary>
[Fact] [Fact]
public void AddStudentExceptionTest() public void AddStudentExceptionTest()
{ {
@ -84,13 +96,14 @@ namespace RedisStudyTest
} }
/// <summary> /// <summary>
/// When参数测试 /// 参数 When 测试
/// </summary> /// </summary>
[Fact] [Fact]
public void AddStudentWhenTest() public void AddStudentWhenTest()
{ {
string redisKey = preHashKey + student.Id; string redisKey = preHashKey + student.Id;
//When.NotExists 当前上下文无效
//当前上下文不能使用: When.Exists
var id_When_NotExists_No = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.NotExists); var id_When_NotExists_No = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.NotExists);
Assert.True(id_When_NotExists_No); Assert.True(id_When_NotExists_No);
@ -98,21 +111,16 @@ namespace RedisStudyTest
var id_When_NotExists_Yes = hashStudy.HashSet(redisKey, "Id", 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); Assert.False(id_When_NotExists_Yes);
//字段存在执行成功返回false var id_When_Always_Exists = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.Always);
var id_When_Always_Exists = hashStudy.HashSet(redisKey, "Id", student.Id + 5, When.Always);
Assert.False(id_When_Always_Exists); Assert.False(id_When_Always_Exists);
//字段不存在执行成功返回true
var id_When_Always_NotExists = hashStudy.HashSet(redisKey, "Id4", student.Id + 1, When.Always); var id_When_Always_NotExists = hashStudy.HashSet(redisKey, "Id4", student.Id + 1, When.Always);
Assert.True(id_When_Always_NotExists); Assert.True(id_When_Always_NotExists);
} }
[Fact] /// <summary>
public void AddStudentCommandFlagTest() /// 添加一个默认学生 测试
{ /// </summary>
string redisKey = preHashKey + student.Id;
}
[Fact] [Fact]
public void AddStudentTest() public void AddStudentTest()
{ {
@ -129,96 +137,219 @@ namespace RedisStudyTest
Assert.True(addHash); Assert.True(addHash);
//设置过期 //设置过期
redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(5)); redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(keyExpireSeconds));
} }
/// <summary> /// <summary>
/// 更新学生异常测试 /// 添加一组初始化设置的学生 测试
/// </summary> /// </summary>
[Fact] [Fact]
public void UpdateStudentExceptionTest() public void AddStudentsTest()
{ {
string redisKey = preHashKey + student.Id; foreach (var temp in students)
//不存在Key {
Assert.Throws<Exception>(()=> hashStudy.HashSet(string.Empty, "Id", -1)); 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));
}
} }
/// <summary> /// <summary>
/// 更新学生 /// 添加或更新字段 测试
/// </summary> /// </summary>
[Theory] [Fact]
[InlineData("Id", 1)] public void SetStudentTest()
[InlineData("Name",1)]
[InlineData("Age",1)]
public void UpdateStudentTest(RedisValue fieldName, RedisValue value)
{ {
string redisKey = preHashKey + student.Id; Assert.True(hashStudy.HashSet(preHashKey + student.Id, "Id", student.Id));
var addOrUpdateOne = hashStudy.HashSet(redisKey, fieldName, value+1); Assert.False(hashStudy.HashSet(preHashKey + student.Id, "Id", student.Id));
Assert.True(addOrUpdateOne);
addOrUpdateOne = hashStudy.HashSet(redisKey, "Name", student.Name + 1); Assert.True(hashStudy.HashSet(preHashKey + student.Id, "Name", student.Name));
Assert.True(addOrUpdateOne); Assert.False(hashStudy.HashSet(preHashKey + student.Id, "Name", student.Name));
addOrUpdateOne = hashStudy.HashSet(redisKey, "Age", student.Age + 1);
Assert.True(addOrUpdateOne); Assert.True(hashStudy.HashSet(preHashKey + student.Id, "Age", student.Age));
Assert.False(hashStudy.HashSet(preHashKey + student.Id, "Age", student.Age));
} }
[Theory] /// <summary>
[InlineData(-1)] /// 添加或更新字段 测试
[InlineData(-2)] /// </summary>
[InlineData(-3)] [Fact]
public void DelStudentTest(int studentId) public void SetStudentTest2()
{ {
Assert.False(redisDatabase.KeyDelete(preHashKey + studentId)); 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));
}
/// <summary>
/// 特例Hash表键名为""(空字符串)
/// 结果:添加或更新操作能成功,但是字段值插入不进去。
/// </summary>
[Fact]
public void SetStudentEmptyKeyTest()
{
Assert.True(hashStudy.HashSet(string.Empty, "Name", "wanggaofeng"));
redisDatabase.KeyDelete(string.Empty);
} }
[Theory] /// <summary>
[InlineData(-100)] /// 特例Hash表,字段名为""(空字符串)
public void DelStudentTest2(int studentId) /// 结果:添加或更新操作正常,只是字段键名为""
/// </summary>
[Fact]
public void SetStudentEmptyFieldTest()
{ {
Assert.False(redisDatabase.KeyDelete(preHashKey + studentId)); 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 获取
/// <summary>
/// 获取一个学生
/// </summary>
[Fact] [Fact]
public void QueryOneStudentTest() public void GetOneSutdentTest()
{ {
//hashStudy.AddStudent(this.student); 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);
//var queryStudent = hashStudy.QueryOneStudent(this.student.Id); Assert.NotNull(entries);
//Assert.NotNull(queryStudent); Student myStudent = new Student()
//Assert.True(this.student.Id==queryStudent.Id); {
//Assert.True(this.student.Name == queryStudent.Name); Id = (int)entries.FirstOrDefault(e=>e.Name=="Id").Value,
//Assert.True(this.student.Age == queryStudent.Age); 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()
{
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] [Fact]
public void ExistStudentTest() public void GetAge()
{ {
//Assert.True(hashStudy.ExistStudent(students[0].Id)); string redisKey = preHashKey + student.Id;
//Assert.True(hashStudy.ExistStudent(students[1].Id));
//Assert.True(hashStudy.ExistStudent(students[2].Id)); Assert.NotEqual(student.Age, hashStudy.HashGet(redisKey, "Age"));
//Assert.True(hashStudy.ExistStudent(students[3].Id));
//Assert.True(hashStudy.ExistStudent(students[4].Id)); AddDefaultStudent();
Assert.Equal(student.Age, hashStudy.HashGet(redisKey, "Age"));
//Assert.False(hashStudy.ExistStudent(-1000));
//Assert.False(hashStudy.ExistStudent(-2000));
} }
/// <summary> /// <summary>
/// 查询所有学生 /// 指定字段是否存在
/// </summary> /// </summary>
[Fact] [Fact]
public void QueryAllStudent() public void ExistStudent()
{ {
//List<Student> students = hashStudy.QueryAllStudents(); string redisKey = preHashKey + student.Id;
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
//Assert.NotNull(students); #region 删除学生
//Assert.Equal(students.Count(), students.Count);
/// <summary>
/// 删除学生
/// </summary>
[Fact]
public void DeleteStudent()
{
Assert.False(redisDatabase.KeyDelete(preHashKey + "-2000"));
} }
#endregion
/// <summary> /// <summary>
/// 清理 /// 清理
/// </summary> /// </summary>
public void Dispose() public void Dispose()
{
DeleteExitsStudents();
}
/// <summary>
/// 删除Redis中的测试学生
/// </summary>
private void DeleteExitsStudents()
{ {
if (student != null) if (student != null)
{ {
@ -231,56 +362,18 @@ namespace RedisStudyTest
} }
} }
#region 辅助方法 private void AddDefaultStudent()
public HashEntry[] StudentsToHashEntrys(Student student)
{ {
if (student == null) string redisKey = preHashKey + student.Id;
{ var studentEntries = new HashEntry[]
return null;
}
var entrys = new List<HashEntry>()
{ {
new HashEntry("Id",student.Id), new HashEntry("Id",1),
new HashEntry("Name",student.Name), new HashEntry("Name",student.Name),
new HashEntry("Age",student.Age), 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; //插入Sudent
var addHash = hashStudy.HashSet(redisKey, studentEntries);
} }
#endregion
} }
} }

@ -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<Student> students;
private Student student = null;
private string preHashKey = "RedisStudy:Student:";
/// <summary>
/// 构造
/// </summary>
public RedisHashStudyTest()
{
redisDatabase = RedisHelper.GetRedisDatabase();
hashStudy = new RedisHashStudy();
student = new Student()
{
Id = 1,
Name = "王高峰",
Age = 2 * 9
};
students = new List<Student>()
{
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<ArgumentNullException>(() => hashStudy.HashSet(string.Empty, null));
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet("", null));
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(preHashKey + "-1", null));
Assert.Throws<ArgumentNullException>(() => 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));
}
/// <summary>
/// 更新学生异常测试
/// </summary>
[Fact]
public void UpdateStudentExceptionTest()
{
string redisKey = preHashKey + student.Id;
//不存在Key
Assert.Throws<Exception>(()=> hashStudy.HashSet(string.Empty, "Id", -1));
}
/// <summary>
/// 更新学生:导致测试不能运行
/// </summary>
[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));
}
/// <summary>
/// 清理
/// </summary>
public void Dispose()
{
if (student != null)
{
redisDatabase.KeyDelete(preHashKey + student.Id);
}
foreach (var temp in students)
{
redisDatabase.KeyDelete(preHashKey + temp.Id);
}
}
}
}

@ -14,22 +14,20 @@ namespace RedisStudyTest
{ {
public class RedisStudyTest public class RedisStudyTest
{ {
private IServer redisServer; private RedisServerStudy _redisServerStudy;
private RedisServerStudy redisServerStudy;
public RedisStudyTest() public RedisStudyTest()
{ {
redisServer = RedisHelper.GetDefaultRedisServer(); _redisServerStudy = new RedisServerStudy();
redisServerStudy = new RedisServerStudy();
} }
[Fact] [Fact]
public void Test() public void Test()
{ {
var info= redisServerStudy.RedisInfo(); var info = _redisServerStudy.RedisInfo();
Assert.NotEmpty(info); Assert.NotEmpty(info);
Assert.True(info.Count()>0); Assert.True(info.Any());
} }
} }
} }

@ -74,10 +74,10 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="RedisHashStudyTest.cs" />
<Compile Include="RedisLockStudyTest.cs" /> <Compile Include="RedisLockStudyTest.cs" />
<Compile Include="RedisStudyTest.cs" /> <Compile Include="RedisStudyTest.cs" />
<Compile Include="RedisSortSetStudyTest.cs" /> <Compile Include="RedisSortSetStudyTest.cs" />
<Compile Include="RedisHashStudyTest.cs" />
<Compile Include="RedisHelperTest.cs" /> <Compile Include="RedisHelperTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RedisServerStudyTest.cs" /> <Compile Include="RedisServerStudyTest.cs" />

@ -141,7 +141,8 @@ namespace RedisStuy
/// </summary> /// </summary>
public bool HashSet(RedisKey key, RedisValue hashField, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None) 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;
} }
/// <summary> /// <summary>
@ -166,11 +167,11 @@ namespace RedisStuy
throw new ArgumentNullException("hashFields", "参数hashFields.Length "); throw new ArgumentNullException("hashFields", "参数hashFields.Length ");
} }
redisDatabase.HashSet(key, hashFields, flags);
bool result = false; bool result = false;
try try
{ {
redisDatabase.HashSet(key, hashFields, flags);
result = true; result = true;
} }
catch (ArgumentNullException argumentNullException) catch (ArgumentNullException argumentNullException)

Loading…
Cancel
Save