RedisHashTest complete

master
bicijinlian 7 years ago
parent f286f00d2e
commit 2406b521b7

@ -74,10 +74,12 @@ namespace RedisStudyTest
Age = 55 Age = 55
}, },
}; };
DeleteExitsStudents();
} }
#endregion #endregion
#region 添加或更新学 #region 添加或更新学
/// <summary> /// <summary>
/// 参数异常 测试 /// 参数异常 测试
@ -165,37 +167,189 @@ namespace RedisStudyTest
/// <summary> /// <summary>
/// 添加部分字段 测试 /// 添加或更新字段 测试
/// </summary>
[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));
}
/// <summary>
/// 添加或更新字段 测试
/// </summary>
[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));
}
/// <summary>
/// 特例Hash表键名为""(空字符串)
/// 结果:添加或更新操作能成功,但是字段值插入不进去。
/// </summary>
[Fact]
public void SetStudentEmptyKeyTest()
{
Assert.True(hashStudy.HashSet(string.Empty, "Name", "wanggaofeng"));
redisDatabase.KeyDelete(string.Empty);
}
/// <summary>
/// 特例Hash表,字段名为""(空字符串)
/// 结果:添加或更新操作正常,只是字段键名为""
/// </summary> /// </summary>
[Fact] [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 获取
/// <summary>
/// 获取一个学生
/// </summary>
[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"));
} }
/// <summary> /// <summary>
/// 更新学生异常测试 /// 指定字段是否存在
/// </summary> /// </summary>
[Fact] [Fact]
public void UpdateStudentExceptionTest() public void ExistStudent()
{ {
string redisKey = preHashKey + student.Id; string redisKey = preHashKey + student.Id;
//不存在Key, 返回false,不异常 var studentEntries = new HashEntry[]
Assert.False(hashStudy.HashSet(string.Empty, "Id", -1)); {
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 #endregion
#region 删除学生
/// <summary>
/// 删除学生
/// </summary>
[Fact] [Fact]
public void Test() public void DeleteStudent()
{ {
Assert.IsType<int>(1); 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)
{ {
@ -207,5 +361,19 @@ namespace RedisStudyTest
redisDatabase.KeyDelete(preHashKey + temp.Id); 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);
}
} }
} }

@ -167,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