master
ruyu 7 years ago
parent 3fc5019c0f
commit 6b1220d2d5

@ -29,6 +29,12 @@ namespace RedisStudyTest
{ {
redisDatabase = RedisHelper.GetRedisDatabase(); redisDatabase = RedisHelper.GetRedisDatabase();
hashStudy = new RedisHashStudy(); hashStudy = new RedisHashStudy();
student = new Student()
{
Id = 1,
Name = "王高峰",
Age = 2 * 9
};
students = new List<Student>() students = new List<Student>()
{ {
new Student() new Student()
@ -67,23 +73,50 @@ namespace RedisStudyTest
} }
[Fact] [Fact]
public void AddStudentTest() public void AddStudentExceptionTest()
{
student = new Student()
{ {
Id = 1,
Name = "王高峰",
Age = 2 * 9
};
string redisKey = preHashKey + student.Id; string redisKey = preHashKey + student.Id;
//参数异常测试
//参数异常
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(string.Empty, null)); Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(string.Empty, null));
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet("", null)); Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet("", null));
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(preHashKey + "-1", null)); Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(preHashKey + "-1", null));
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(preHashKey + "-1", new HashEntry[] { })); 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[] var studentEntries = new HashEntry[]
{ {
new HashEntry("Id",1), new HashEntry("Id",1),
@ -93,18 +126,37 @@ namespace RedisStudyTest
//插入Sudent //插入Sudent
var addHash = hashStudy.HashSet(redisKey, studentEntries, CommandFlags.None); var addHash = hashStudy.HashSet(redisKey, studentEntries, CommandFlags.None);
Assert.True(addHash);
//设置过期 //设置过期
redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(5)); redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(5));
Assert.True(addHash); }
//更新(插入)一项 /// <summary>
/// 更新学生异常测试
/// </summary>
[Fact]
public void UpdateStudentExceptionTest()
{
string redisKey = preHashKey + student.Id;
//不存在Key
Assert.Throws<Exception>(()=> hashStudy.HashSet(string.Empty, "Id", -1));
}
var addOrUpdateOne = hashStudy.HashSet(redisKey, "Id",student.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); Assert.True(addOrUpdateOne);
addOrUpdateOne = hashStudy.HashSet(redisKey, "Name", student.Name + 1); addOrUpdateOne = hashStudy.HashSet(redisKey, "Name", student.Name + 1);
Assert.True(addOrUpdateOne); Assert.True(addOrUpdateOne);
addOrUpdateOne = hashStudy.HashSet(redisKey, "Age", student.Age + 1); addOrUpdateOne = hashStudy.HashSet(redisKey, "Age", student.Age + 1);
Assert.True(addOrUpdateOne); Assert.True(addOrUpdateOne);
} }
@ -115,14 +167,14 @@ namespace RedisStudyTest
[InlineData(-3)] [InlineData(-3)]
public void DelStudentTest(int studentId) public void DelStudentTest(int studentId)
{ {
//Assert.False(hashStudy.DelStudentById(studentId)); Assert.False(redisDatabase.KeyDelete(preHashKey + studentId));
} }
[Theory] [Theory]
[InlineData(-100)] [InlineData(-100)]
public void DelStudentTest2(int studentId) public void DelStudentTest2(int studentId)
{ {
//Assert.False(hashStudy.DelStudentById(studentId)); Assert.False(redisDatabase.KeyDelete(preHashKey + studentId));
} }
[Fact] [Fact]
@ -172,11 +224,11 @@ namespace RedisStudyTest
{ {
redisDatabase.KeyDelete(preHashKey + student.Id); redisDatabase.KeyDelete(preHashKey + student.Id);
} }
//hashStudy.DelStudentById(student.Id);
//foreach (var temp in students) foreach (var temp in students)
//{ {
// hashStudy.DelStudentById(temp.Id); redisDatabase.KeyDelete(preHashKey + temp.Id);
//} }
} }
#region 辅助方法 #region 辅助方法
@ -195,6 +247,40 @@ namespace RedisStudyTest
}; };
return entrys.ToArray(); 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 #endregion
} }
} }

Loading…
Cancel
Save