HashStudy测试率100%,整理测试代码

master
ruyu 7 years ago
parent aa22dfc6e3
commit 8ae8e924ad

@ -18,18 +18,19 @@ namespace RedisStudyTest
/// <summary> /// <summary>
/// Redis Hash 类型测试 /// Redis Hash 类型测试
/// </summary> /// </summary>
[Trait("RedisHash", "All")]
public class RedisHashStudyTest : IDisposable public class RedisHashStudyTest : IDisposable
{ {
#region 初始化 #region 初始化
private readonly ITestOutputHelper testOutput; private readonly ITestOutputHelper testOutput;
private IDatabase redisDatabase = null; private IDatabase redisDatabase = null;
private RedisHashStudy hashStudy = null; private RedisHashStudy redisHashStudy = null;
private List<Student> students; private List<Student> defaultStudentList;
private Student defaultStudent = null; private Student defaultStudent = null;
private string preHashKey = "RedisStudy:Student:"; private string preStudentHashKey = "RedisStudy:Student:";
private string defaultHashKey = ""; private string defaultStudentHashKey = "";
private int keyExpireSeconds = 20; private int keyDefaultExpireSeconds = 20;
/// <summary> /// <summary>
/// 构造 /// 构造
@ -39,7 +40,7 @@ namespace RedisStudyTest
this.testOutput = output; this.testOutput = output;
redisDatabase = RedisHelper.GetRedisDatabase(); redisDatabase = RedisHelper.GetRedisDatabase();
hashStudy = new RedisHashStudy(); redisHashStudy = new RedisHashStudy();
defaultStudent = new Student() defaultStudent = new Student()
{ {
Id = 1, Id = 1,
@ -47,9 +48,9 @@ namespace RedisStudyTest
Age = 18 Age = 18
}; };
defaultHashKey = preHashKey + defaultStudent.Id; defaultStudentHashKey = preStudentHashKey + defaultStudent.Id;
students = new List<Student>() defaultStudentList = new List<Student>()
{ {
new Student() new Student()
{ {
@ -82,6 +83,8 @@ namespace RedisStudyTest
Age = 55 Age = 55
}, },
}; };
//删理默认学生
DeleteExitsStudents(); DeleteExitsStudents();
} }
#endregion #endregion
@ -90,6 +93,7 @@ namespace RedisStudyTest
/// <summary> /// <summary>
/// xUnit输出信息 测试 /// xUnit输出信息 测试
/// </summary> /// </summary>
[Trait("RedisHash", "HashSet")]
[Fact(DisplayName = "HashSet 输出信息测试")] [Fact(DisplayName = "HashSet 输出信息测试")]
public void HashSetOutputTest() public void HashSetOutputTest()
{ {
@ -100,72 +104,74 @@ namespace RedisStudyTest
/// <summary> /// <summary>
/// 参数异常 测试 /// 参数异常 测试
/// </summary> /// </summary>
[Trait("RedisHash", "HashSet")]
[Fact(DisplayName = "HashSet 异常测试")] [Fact(DisplayName = "HashSet 异常测试")]
public void HashSetExceptionTest() public void HashSetExceptionTest()
{ {
string redisKey = preHashKey + defaultStudent.Id; string redisKey = preStudentHashKey + defaultStudent.Id;
//参数异常测试 //参数异常测试
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(string.Empty, null)); Assert.Throws<ArgumentNullException>(() => redisHashStudy.HashSet(string.Empty, null));
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet("", null)); Assert.Throws<ArgumentNullException>(() => redisHashStudy.HashSet("", null));
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(preHashKey + "-1", null)); Assert.Throws<ArgumentNullException>(() => redisHashStudy.HashSet(preStudentHashKey + "-1", null));
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(preHashKey + "-1", new HashEntry[] { })); Assert.Throws<ArgumentNullException>(() => redisHashStudy.HashSet(preStudentHashKey + "-1", new HashEntry[] { }));
} }
/// <summary> /// <summary>
/// 参数 When 测试 /// 参数 When 测试
/// </summary> /// </summary>
[Trait("HashSet", "When")] [Trait("RedisHash", "HashSet")]
[Fact(DisplayName = "HashSet When参数测试")] [Fact(DisplayName = "HashSet When参数测试")]
public void HashSetWhenTest() public void HashSetWhenTest()
{ {
string redisKey = preHashKey + defaultStudent.Id; string redisKey = preStudentHashKey + defaultStudent.Id;
//当前上下文不能使用: When.Exists //当前上下文不能使用: When.Exists
var id_When_NotExists_No = hashStudy.HashSet(redisKey, "Id", defaultStudent.Id + 1, When.NotExists); var id_When_NotExists_No = redisHashStudy.HashSet(redisKey, "Id", defaultStudent.Id + 1, When.NotExists);
Assert.True(id_When_NotExists_No); Assert.True(id_When_NotExists_No);
var id_When_NotExists_Yes = hashStudy.HashSet(redisKey, "Id", defaultStudent.Id + 1, When.NotExists); var id_When_NotExists_Yes = redisHashStudy.HashSet(redisKey, "Id", defaultStudent.Id + 1, When.NotExists);
Assert.False(id_When_NotExists_Yes); Assert.False(id_When_NotExists_Yes);
var id_When_Always_Exists = hashStudy.HashSet(redisKey, "Id", defaultStudent.Id + 1, When.Always); var id_When_Always_Exists = redisHashStudy.HashSet(redisKey, "Id", defaultStudent.Id + 1, When.Always);
Assert.False(id_When_Always_Exists); Assert.False(id_When_Always_Exists);
var id_When_Always_NotExists = hashStudy.HashSet(redisKey, "Id4", defaultStudent.Id + 1, When.Always); var id_When_Always_NotExists = redisHashStudy.HashSet(redisKey, "Id4", defaultStudent.Id + 1, When.Always);
Assert.True(id_When_Always_NotExists); Assert.True(id_When_Always_NotExists);
} }
/// <summary> /// <summary>
/// 添加一个默认学生 测试 /// 添加一个默认学生 测试
/// </summary> /// </summary>
[Trait("RedisHash", "HashSet")]
[Fact] [Fact]
public void HashSetAddStudentTest() public void HashSetAddStudentTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
var studentEntries = new HashEntry[] var studentEntries = new HashEntry[]
{ {
new HashEntry("Id",1), new HashEntry("Id",defaultStudent.Id),
new HashEntry("Name",defaultStudent.Name), new HashEntry("Name",defaultStudent.Name),
new HashEntry("Age",defaultStudent.Age), new HashEntry("Age",defaultStudent.Age),
}; };
//插入Sudent //插入Sudent
var addHash = hashStudy.HashSet(redisKey, studentEntries, CommandFlags.None); var addHash = redisHashStudy.HashSet(defaultStudentHashKey, studentEntries, CommandFlags.None);
Assert.True(addHash); Assert.True(addHash);
//设置过期 //设置过期
redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(keyExpireSeconds)); redisDatabase.KeyExpire(defaultStudentHashKey, TimeSpan.FromSeconds(keyDefaultExpireSeconds));
} }
/// <summary> /// <summary>
/// 添加一组初始化设置的学生 测试 /// 添加一组初始化设置的学生 测试
/// </summary> /// </summary>
[Trait("RedisHash", "HashSet")]
[Fact] [Fact]
public void HashSetAddGroupStudentTest() public void HashSetAddGroupStudentTest()
{ {
foreach (var temp in students) foreach (var temp in defaultStudentList)
{ {
string redisKey = preHashKey + temp.Id; string redisKey = preStudentHashKey + temp.Id;
var studentEntries = new HashEntry[] var studentEntries = new HashEntry[]
{ {
new HashEntry("Id", temp.Id), new HashEntry("Id", temp.Id),
@ -174,34 +180,35 @@ namespace RedisStudyTest
}; };
//插入Sudent //插入Sudent
var addStudent = hashStudy.HashSet(redisKey, studentEntries); var addStudent = redisHashStudy.HashSet(redisKey, studentEntries);
Assert.True(addStudent); Assert.True(addStudent);
//设置过期 //设置过期
redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(keyExpireSeconds)); redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(keyDefaultExpireSeconds));
} }
//清理删除 //清理删除
foreach (var temp in students) foreach (var temp in defaultStudentList)
{ {
redisDatabase.KeyDelete(preHashKey + temp.Id); redisDatabase.KeyDelete(preStudentHashKey + temp.Id);
} }
} }
/// <summary> /// <summary>
/// 设置一个哈希字段 测试 /// 设置一个哈希字段 测试
/// </summary> /// </summary>
[Trait("RedisHash", "HashSet")]
[Fact] [Fact]
public void HashSetHashfieldTest() public void HashSetHashfieldTest()
{ {
Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Id", defaultStudent.Id)); Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "Id", defaultStudent.Id));
Assert.False(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Id", defaultStudent.Id)); Assert.False(redisHashStudy.HashSet(defaultStudentHashKey, "Id", defaultStudent.Id));
Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Name", defaultStudent.Name)); Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "Name", defaultStudent.Name));
Assert.False(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Name", defaultStudent.Name)); Assert.False(redisHashStudy.HashSet(defaultStudentHashKey, "Name", defaultStudent.Name));
Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Age", defaultStudent.Age)); Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "Age", defaultStudent.Age));
Assert.False(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Age", defaultStudent.Age)); Assert.False(redisHashStudy.HashSet(defaultStudentHashKey, "Age", defaultStudent.Age));
} }
/// <summary> /// <summary>
@ -210,8 +217,8 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashSetGroupHashfieldTest() public void HashSetGroupHashfieldTest()
{ {
Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Id", defaultStudent.Id)); Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "Id", defaultStudent.Id));
Assert.False(hashStudy.HashSet(preHashKey + defaultStudent.Id, "Id", defaultStudent.Id + 1)); Assert.False(redisHashStudy.HashSet(defaultStudentHashKey, "Id", defaultStudent.Id + 1));
var entrys = new HashEntry[] var entrys = new HashEntry[]
{ {
@ -225,18 +232,19 @@ namespace RedisStudyTest
new HashEntry("Age", defaultStudent.Age+1), new HashEntry("Age", defaultStudent.Age+1),
}; };
Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, entrys)); Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, entrys));
Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, entrys2)); Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, entrys2));
} }
/// <summary> /// <summary>
/// 特例测试给key为空的哈希 设置字段 /// 特例测试给key为空的哈希 设置字段
/// 结 果:添加或更新操作能成功,但是字段值插入不进去。 /// 结 果:添加或更新操作能成功,但是字段值插入不进去。
/// </summary> /// </summary>
[Trait("RedisHash", "HashSet")]
[Fact] [Fact]
public void HashSetForEmptyKeyTest() public void HashSetForEmptyKeyTest()
{ {
Assert.True(hashStudy.HashSet(string.Empty, "Name", "wanggaofeng")); Assert.True(redisHashStudy.HashSet(string.Empty, "Name", "wanggaofeng"));
redisDatabase.KeyDelete(string.Empty); redisDatabase.KeyDelete(string.Empty);
} }
@ -244,13 +252,14 @@ namespace RedisStudyTest
/// 特例测试:给字段名为空串的哈希 设置字段 /// 特例测试:给字段名为空串的哈希 设置字段
/// 结 果:添加或更新操作正常,只是字段键名为"" /// 结 果:添加或更新操作正常,只是字段键名为""
/// </summary> /// </summary>
[Trait("RedisHash", "HashSet")]
[Fact] [Fact]
public void HashSetForEmptyFieldTest() public void HashSetForEmptyFieldTest()
{ {
Assert.True(hashStudy.HashSet(preHashKey + defaultStudent.Id, "", defaultStudent.Id)); Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "", defaultStudent.Id));
Assert.False(hashStudy.HashSet(preHashKey + defaultStudent.Id, "", defaultStudent.Id + 1)); Assert.False(redisHashStudy.HashSet(defaultStudentHashKey, "", defaultStudent.Id + 1));
redisDatabase.KeyDelete(preHashKey + defaultStudent.Id); redisDatabase.KeyDelete(defaultStudentHashKey);
} }
#endregion #endregion
@ -264,10 +273,8 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashDecrementExceptionTest() public void HashDecrementExceptionTest()
{ {
string redisKey = preHashKey + defaultStudent.Id; Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "Name", "wanggaofeng"));
Assert.Throws<RedisServerException>(() => redisHashStudy.HashDecrement(defaultStudentHashKey, "Name", 1));
Assert.True(hashStudy.HashSet(redisKey, "Name", "wanggaofeng"));
Assert.Throws<RedisServerException>(() => hashStudy.HashDecrement(redisKey, "Name", 1));
} }
/// <summary> /// <summary>
@ -277,13 +284,11 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashDecrementHashKeyTest() public void HashDecrementHashKeyTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
//Key不存在先创建哈希表再添加值为0的字段然后执行自减操作 //Key不存在先创建哈希表再添加值为0的字段然后执行自减操作
Assert.Equal(-1, hashStudy.HashDecrement(redisKey, "Id", 1)); Assert.Equal(-1, redisHashStudy.HashDecrement(defaultStudentHashKey, "Id", 1));
//存在,直接自减 //存在,直接自减
Assert.Equal(-1, hashStudy.HashGet(redisKey, "Id")); Assert.Equal(-1, redisHashStudy.HashGet(defaultStudentHashKey, "Id"));
} }
/// <summary> /// <summary>
@ -293,11 +298,9 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashDecrementHashFieldTest() public void HashDecrementHashFieldTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
//字段不存在,则创建之 //字段不存在,则创建之
Assert.Equal(-1, hashStudy.HashDecrement(redisKey, "Age", 1)); Assert.Equal(-1, redisHashStudy.HashDecrement(defaultStudentHashKey, "Age", 1));
Assert.Equal(-1, hashStudy.HashGet(redisKey, "Age")); Assert.Equal(-1, redisHashStudy.HashGet(defaultStudentHashKey, "Age"));
} }
/// <summary> /// <summary>
@ -306,10 +309,8 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashDecrementMinusTest() public void HashDecrementMinusTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
//自减负数时,则自增 //自减负数时,则自增
Assert.Equal(2, hashStudy.HashDecrement(redisKey, "Age", -2)); Assert.Equal(2, redisHashStudy.HashDecrement(defaultStudentHashKey, "Age", -2));
} }
/// <summary> /// <summary>
@ -318,12 +319,10 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashDecrementByIntTest() public void HashDecrementByIntTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
//字段减少1 //字段减少1
Assert.Equal(-1, hashStudy.HashDecrement(redisKey, "Age", 1)); Assert.Equal(-1, redisHashStudy.HashDecrement(defaultStudentHashKey, "Age", 1));
//字段减少2 //字段减少2
Assert.Equal(-3, hashStudy.HashDecrement(redisKey, "Age", 2)); Assert.Equal(-3, redisHashStudy.HashDecrement(defaultStudentHashKey, "Age", 2));
} }
/// <summary> /// <summary>
@ -333,15 +332,13 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashDecrementByDoubleTest() public void HashDecrementByDoubleTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
//新字段时,可以用相等比较 //新字段时,可以用相等比较
var dec = hashStudy.HashDecrement(redisKey, "Age", 2.1); var dec = redisHashStudy.HashDecrement(defaultStudentHashKey, "Age", 2.1);
var absDec = Math.Abs(-2.1 - dec); var absDec = Math.Abs(-2.1 - dec);
Assert.Equal(0, absDec); Assert.Equal(0, absDec);
//已经存在的,浮点数,不使用相等比较,而改用两值相差一定的小范围 //已经存在的,浮点数,不使用相等比较,而改用两值相差一定的小范围
dec = hashStudy.HashDecrement(redisKey, "Age", 2.5); dec = redisHashStudy.HashDecrement(defaultStudentHashKey, "Age", 2.5);
absDec = Math.Abs(-4.6 - dec); absDec = Math.Abs(-4.6 - dec);
Assert.True(absDec < 0.01); Assert.True(absDec < 0.01);
} }
@ -357,10 +354,8 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashIncrementExceptionTest() public void HashIncrementExceptionTest()
{ {
string redisKey = preHashKey + defaultStudent.Id; Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "Name", "wanggaofeng"));
Assert.Throws<RedisServerException>(() => redisHashStudy.HashIncrement(defaultStudentHashKey, "Name", 1));
Assert.True(hashStudy.HashSet(redisKey, "Name", "wanggaofeng"));
Assert.Throws<RedisServerException>(() => hashStudy.HashIncrement(redisKey, "Name", 1));
} }
/// <summary> /// <summary>
@ -370,11 +365,9 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashIncrementHashKeyTest() public void HashIncrementHashKeyTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
//Key不存在先创建哈希表再添加值为0的字段然后执行自减操作 //Key不存在先创建哈希表再添加值为0的字段然后执行自减操作
Assert.Equal(1, hashStudy.HashIncrement(redisKey, "Id", 1)); Assert.Equal(1, redisHashStudy.HashIncrement(defaultStudentHashKey, "Id", 1));
Assert.Equal(1, hashStudy.HashGet(redisKey, "Id")); Assert.Equal(1, redisHashStudy.HashGet(defaultStudentHashKey, "Id"));
} }
/// <summary> /// <summary>
@ -384,11 +377,9 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashIncrementHashFieldTest() public void HashIncrementHashFieldTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
//字段不存在,则创建之 //字段不存在,则创建之
Assert.Equal(1, hashStudy.HashIncrement(redisKey, "Age", 1)); Assert.Equal(1, redisHashStudy.HashIncrement(defaultStudentHashKey, "Age", 1));
Assert.Equal(1, hashStudy.HashGet(redisKey, "Age")); Assert.Equal(1, redisHashStudy.HashGet(defaultStudentHashKey, "Age"));
} }
/// <summary> /// <summary>
@ -397,10 +388,8 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashIncrementMinusTest() public void HashIncrementMinusTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
//自增负数时,则自减 //自增负数时,则自减
Assert.Equal(-2, hashStudy.HashIncrement(redisKey, "Age", -2)); Assert.Equal(-2, redisHashStudy.HashIncrement(defaultStudentHashKey, "Age", -2));
} }
/// <summary> /// <summary>
@ -409,12 +398,10 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashIncrementByIntTest() public void HashIncrementByIntTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
//字段减少1 //字段减少1
Assert.Equal(1, hashStudy.HashIncrement(redisKey, "Age", 1)); Assert.Equal(1, redisHashStudy.HashIncrement(defaultStudentHashKey, "Age", 1));
//字段减少2 //字段减少2
Assert.Equal(3, hashStudy.HashIncrement(redisKey, "Age", 2)); Assert.Equal(3, redisHashStudy.HashIncrement(defaultStudentHashKey, "Age", 2));
} }
/// <summary> /// <summary>
@ -424,15 +411,13 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashIncrementByDoubleTest() public void HashIncrementByDoubleTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
//新字段时,可以用相等比较 //新字段时,可以用相等比较
var dec = hashStudy.HashIncrement(redisKey, "Age", 2.1); var dec = redisHashStudy.HashIncrement(defaultStudentHashKey, "Age", 2.1);
var absDec = Math.Abs(2.1 - dec); var absDec = Math.Abs(2.1 - dec);
Assert.Equal(0, absDec); Assert.Equal(0, absDec);
//已经存在的,浮点数,不使用相等比较,而改用两值相差一定的小范围 //已经存在的,浮点数,不使用相等比较,而改用两值相差一定的小范围
dec = hashStudy.HashIncrement(redisKey, "Age", 2.5); dec = redisHashStudy.HashIncrement(defaultStudentHashKey, "Age", 2.5);
absDec = Math.Abs(4.6 - dec); absDec = Math.Abs(4.6 - dec);
Assert.True(absDec < 0.01); Assert.True(absDec < 0.01);
} }
@ -445,19 +430,18 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashGetAllTest() public void HashGetAllTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
var studentEntries = new HashEntry[] var studentEntries = new HashEntry[]
{ {
new HashEntry("Id",1), new HashEntry("Id",defaultStudent.Id),
new HashEntry("Name",defaultStudent.Name), new HashEntry("Name",defaultStudent.Name),
new HashEntry("Age",defaultStudent.Age), new HashEntry("Age",defaultStudent.Age),
}; };
//插入Sudent //插入Sudent
var addHash = hashStudy.HashSet(redisKey, studentEntries); var addHash = redisHashStudy.HashSet(defaultStudentHashKey, studentEntries);
Assert.True(addHash); Assert.True(addHash);
var entries = hashStudy.HashGetAll(redisKey); var entries = redisHashStudy.HashGetAll(defaultStudentHashKey);
Assert.NotNull(entries); Assert.NotNull(entries);
@ -481,8 +465,7 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashGetNotKkeyTest() public void HashGetNotKkeyTest()
{ {
string redisKey = preHashKey + defaultStudent.Id; var result = redisHashStudy.HashGet(defaultStudentHashKey, "Id");
var result = hashStudy.HashGet(redisKey, "Id");
Assert.False(result.HasValue); Assert.False(result.HasValue);
} }
@ -493,24 +476,39 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashGetNotHashfieldTest() public void HashGetNotHashfieldTest()
{ {
string redisKey = preHashKey + defaultStudent.Id; redisHashStudy.HashIncrement(defaultStudentHashKey, "Id", 1);
hashStudy.HashIncrement(redisKey, "Id", 1);
var result = hashStudy.HashGet(redisKey, "Name"); var result = redisHashStudy.HashGet(defaultStudentHashKey, "Name");
Assert.False(result.HasValue); Assert.False(result.HasValue);
} }
[Fact] [Fact]
public void HashGetTest() public void HashGetOneFieldTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
AddDefaultStudent(); AddDefaultStudent();
Assert.Equal(1, hashStudy.HashGet(redisKey, "Id")); Assert.Equal(1, redisHashStudy.HashGet(defaultStudentHashKey, "Id"));
Assert.Equal(defaultStudent.Name, hashStudy.HashGet(redisKey, "Name")); Assert.Equal(defaultStudent.Name, redisHashStudy.HashGet(defaultStudentHashKey, "Name", CommandFlags.None));
Assert.Equal(defaultStudent.Age, hashStudy.HashGet(redisKey, "Age")); Assert.Equal(defaultStudent.Age, redisHashStudy.HashGet(defaultStudentHashKey, "Age", CommandFlags.None));
}
[Fact]
public void HashGetGroupFieldTest()
{
AddDefaultStudent();
RedisValue[] hashFields = new RedisValue[]
{
"Id",
"Name",
"Age",
};
RedisValue[] resultValues = redisHashStudy.HashGet(defaultStudentHashKey, hashFields, CommandFlags.None);
Assert.Contains(defaultStudent.Id, resultValues);
Assert.Contains(defaultStudent.Name, resultValues);
Assert.Contains(defaultStudent.Age, resultValues);
} }
#endregion #endregion
@ -523,7 +521,7 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashKeysNotKeyTest() public void HashKeysNotKeyTest()
{ {
RedisValue[] keys = hashStudy.HashKeys(defaultHashKey); RedisValue[] keys = redisHashStudy.HashKeys(defaultStudentHashKey);
Assert.NotNull(keys); Assert.NotNull(keys);
Assert.Empty(keys); Assert.Empty(keys);
@ -538,7 +536,7 @@ namespace RedisStudyTest
{ {
AddDefaultStudent(); AddDefaultStudent();
RedisValue[] keys = hashStudy.HashKeys(defaultHashKey); RedisValue[] keys = redisHashStudy.HashKeys(defaultStudentHashKey);
Assert.NotEmpty(keys); Assert.NotEmpty(keys);
Assert.Contains("Id", keys); Assert.Contains("Id", keys);
@ -554,7 +552,7 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashValuesNotKeyTest() public void HashValuesNotKeyTest()
{ {
RedisValue[] keys = hashStudy.HashValues(defaultHashKey); RedisValue[] keys = redisHashStudy.HashValues(defaultStudentHashKey);
Assert.NotNull(keys); Assert.NotNull(keys);
Assert.Empty(keys); Assert.Empty(keys);
@ -569,7 +567,7 @@ namespace RedisStudyTest
{ {
AddDefaultStudent(); AddDefaultStudent();
RedisValue[] values = hashStudy.HashValues(defaultHashKey); RedisValue[] values = redisHashStudy.HashValues(defaultStudentHashKey);
Assert.NotEmpty(values); Assert.NotEmpty(values);
Assert.Contains(defaultStudent.Id, values); Assert.Contains(defaultStudent.Id, values);
@ -587,8 +585,7 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashLengthNotKeyTest() public void HashLengthNotKeyTest()
{ {
string redisKey = preHashKey + defaultStudent.Id; var result = redisHashStudy.HashLength(defaultStudentHashKey);
var result = hashStudy.HashLength(redisKey);
Assert.Equal(0, result); Assert.Equal(0, result);
} }
@ -599,10 +596,9 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashLengthTest() public void HashLengthTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
AddDefaultStudent(); AddDefaultStudent();
var result = hashStudy.HashLength(redisKey); var result = redisHashStudy.HashLength(defaultStudentHashKey);
Assert.Equal(3, result); Assert.Equal(3, result);
} }
@ -615,9 +611,9 @@ namespace RedisStudyTest
/// 哈希不存在时HashScan /// 哈希不存在时HashScan
/// </summary> /// </summary>
[Fact] [Fact]
public void HashScanNotKey() public void HashScanNotKeyTest()
{ {
var hashEntrys= hashStudy.HashScan(defaultHashKey); var hashEntrys = redisHashStudy.HashScan(defaultStudentHashKey, "*", 20, CommandFlags.None);
var num = hashEntrys.Count(); var num = hashEntrys.Count();
Assert.Equal(0, num); Assert.Equal(0, num);
@ -626,6 +622,48 @@ namespace RedisStudyTest
Assert.Empty(entryList); Assert.Empty(entryList);
} }
/// <summary>
/// 简单测试,有单独测试类详细测试
/// </summary>
[Fact]
public void HashScanTest()
{
//插入多列hash
List<HashEntry> hashEntries = new List<HashEntry>();
for (int i=1;i<=100; i++)
{
hashEntries.Add(new HashEntry("Field"+i,i));
}
redisHashStudy.HashSet(defaultStudentHashKey, hashEntries.ToArray());
var hashFieldScan = redisHashStudy.HashScan(defaultStudentHashKey);
Assert.Equal(100, hashFieldScan.Count());
var scanTake = hashFieldScan.Skip(10).Take(10).ToList();
Assert.Equal(10, scanTake.Count());
//删除hash
redisDatabase.KeyDelete(defaultStudentHashKey);
}
[Fact]
public void HashScan2Test()
{
//插入多列hash
List<HashEntry> hashEntries = new List<HashEntry>();
for (int i = 1; i <= 100; i++)
{
hashEntries.Add(new HashEntry("Field" + i, i));
}
redisHashStudy.HashSet(defaultStudentHashKey, hashEntries.ToArray());
var hashFieldScan = redisHashStudy.HashScan(defaultStudentHashKey, "*", 20, 0, 0, CommandFlags.None);
Assert.Equal(100, hashFieldScan.Count());
}
#endregion #endregion
#region HashExists #region HashExists
@ -635,20 +673,19 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashExists() public void HashExists()
{ {
string redisKey = preHashKey + defaultStudent.Id;
var studentEntries = new HashEntry[] var studentEntries = new HashEntry[]
{ {
new HashEntry("Id",1), new HashEntry("Id",defaultStudent.Id),
new HashEntry("Name",defaultStudent.Name), new HashEntry("Name",defaultStudent.Name),
}; };
//插入Sudent //插入Sudent
var addHash = hashStudy.HashSet(redisKey, studentEntries); var addHash = redisHashStudy.HashSet(defaultStudentHashKey, studentEntries);
Assert.True(addHash); Assert.True(addHash);
Assert.True(hashStudy.HashExists(redisKey, "Id")); Assert.True(redisHashStudy.HashExists(defaultStudentHashKey, "Id"));
Assert.True(hashStudy.HashExists(redisKey, "Name")); Assert.True(redisHashStudy.HashExists(defaultStudentHashKey, "Name"));
Assert.False(hashStudy.HashExists(redisKey, "Age")); Assert.False(redisHashStudy.HashExists(defaultStudentHashKey, "Age"));
} }
#endregion #endregion
@ -660,8 +697,7 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashDeleteNotKeyTest() public void HashDeleteNotKeyTest()
{ {
string redisKey = preHashKey + defaultStudent.Id; var hashDeleteResult = redisHashStudy.HashDelete(defaultStudentHashKey, "FieldFirst");
var hashDeleteResult = hashStudy.HashDelete(redisKey, "FieldFirst");
Assert.False(hashDeleteResult); Assert.False(hashDeleteResult);
} }
@ -672,31 +708,47 @@ namespace RedisStudyTest
[Fact] [Fact]
public void HashDeleteNotFieldTest() public void HashDeleteNotFieldTest()
{ {
string redisKey = preHashKey + defaultStudent.Id; redisHashStudy.HashIncrement(defaultStudentHashKey, "Id",1);
hashStudy.HashIncrement(redisKey, "Id",1);
var hashDeleteResult = hashStudy.HashDelete(redisKey, "Name"); var hashDeleteResult = redisHashStudy.HashDelete(defaultStudentHashKey, "Name");
Assert.False(hashDeleteResult); Assert.False(hashDeleteResult);
} }
/// <summary> /// <summary>
/// 删除哈希字段 /// 删除哈希一个字段
/// </summary> /// </summary>
[Fact] [Fact]
public void HashDeleteTest() public void HashDeleteTest()
{ {
string redisKey = preHashKey + defaultStudent.Id;
AddDefaultStudent(); AddDefaultStudent();
var hashDeleteResult = hashStudy.HashDelete(redisKey, "Id"); var hashDeleteResult = redisHashStudy.HashDelete(defaultStudentHashKey, "Id");
Assert.True(hashDeleteResult); Assert.True(hashDeleteResult);
hashDeleteResult = hashStudy.HashDelete(redisKey, "Name"); hashDeleteResult = redisHashStudy.HashDelete(defaultStudentHashKey, "Name", CommandFlags.None);
Assert.True(hashDeleteResult); Assert.True(hashDeleteResult);
hashDeleteResult = hashStudy.HashDelete(redisKey, "Age"); hashDeleteResult = redisHashStudy.HashDelete(defaultStudentHashKey, "Age", CommandFlags.None);
Assert.True(hashDeleteResult); Assert.True(hashDeleteResult);
} }
/// <summary>
/// 删除一组哈希字段
/// </summary>
[Fact]
public void HashDeleteGroupFieldTest()
{
AddDefaultStudent();
RedisValue[] delValues = new RedisValue[]
{
"Id",
"Name",
"Age",
};
var hashDeleteResult = redisHashStudy.HashDelete(defaultStudentHashKey, delValues, CommandFlags.None);
Assert.Equal(3,hashDeleteResult);
}
#endregion #endregion
#region HashKeyDelete #region HashKeyDelete
@ -707,7 +759,7 @@ namespace RedisStudyTest
[Fact] [Fact]
public void KeyDeleteTest() public void KeyDeleteTest()
{ {
Assert.False(redisDatabase.KeyDelete(preHashKey + "-2000")); Assert.False(redisDatabase.KeyDelete(preStudentHashKey + "-2000"));
} }
#endregion #endregion
@ -728,22 +780,21 @@ namespace RedisStudyTest
{ {
if (defaultStudent != null) if (defaultStudent != null)
{ {
redisDatabase.KeyDelete(preHashKey + defaultStudent.Id); redisDatabase.KeyDelete(preStudentHashKey + defaultStudent.Id);
} }
} }
private void AddDefaultStudent() private void AddDefaultStudent()
{ {
string redisKey = preHashKey + defaultStudent.Id;
var studentEntries = new HashEntry[] var studentEntries = new HashEntry[]
{ {
new HashEntry("Id",1), new HashEntry("Id",defaultStudent.Id),
new HashEntry("Name",defaultStudent.Name), new HashEntry("Name",defaultStudent.Name),
new HashEntry("Age",defaultStudent.Age), new HashEntry("Age",defaultStudent.Age),
}; };
//插入Sudent //插入Sudent
var addHash = hashStudy.HashSet(redisKey, studentEntries); var addHash = redisHashStudy.HashSet(defaultStudentHashKey, studentEntries);
} }
#endregion #endregion
} }

@ -13,6 +13,7 @@ using Xunit;
namespace RedisStudyTest namespace RedisStudyTest
{ {
[Trait("RedisLock", "All")]
public class RedisLockStudyTest public class RedisLockStudyTest
{ {
private IDatabase redisDatabase; private IDatabase redisDatabase;

@ -12,6 +12,7 @@ using Xunit;
namespace RedisStudyTest namespace RedisStudyTest
{ {
[Trait("RedisServer", "All")]
public class RedisServerStudyTest public class RedisServerStudyTest
{ {
private IServer redisServer; private IServer redisServer;

@ -14,6 +14,7 @@ using RedisStuy;
namespace RedisStudyTest namespace RedisStudyTest
{ {
[Trait("RedisSortSet", "All")]
public class RedisSortSetStudyTest : IDisposable public class RedisSortSetStudyTest : IDisposable
{ {
private IDatabase redisDatabase = null; private IDatabase redisDatabase = null;

@ -168,22 +168,9 @@ namespace RedisStuy
} }
bool result = false;
try
{
redisDatabase.HashSet(key, hashFields, flags); redisDatabase.HashSet(key, hashFields, flags);
result = true;
}
catch (ArgumentNullException argumentNullException)
{
throw argumentNullException;
}
catch (Exception baseException)
{
throw baseException;
}
return result; return true;
} }
/// <summary> /// <summary>

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RedisStuy
{
/// <summary>
/// Scan命令学习包括Scan SScan HScan ZScan
/// Scan命令比较特殊并且此组命令相似所以放在一起单独学习
/// </summary>
public class RedisScanStudy
{
}
}

@ -50,6 +50,7 @@
<Compile Include="RedisListStudy.cs" /> <Compile Include="RedisListStudy.cs" />
<Compile Include="RedisDatabaseStudy.cs" /> <Compile Include="RedisDatabaseStudy.cs" />
<Compile Include="RedisLockStudy.cs" /> <Compile Include="RedisLockStudy.cs" />
<Compile Include="RedisScanStudy.cs" />
<Compile Include="RedisSetStudy.cs" /> <Compile Include="RedisSetStudy.cs" />
<Compile Include="RedisSortSetStudy.cs" /> <Compile Include="RedisSortSetStudy.cs" />
<Compile Include="RedisTransactionStudy.cs" /> <Compile Include="RedisTransactionStudy.cs" />

Loading…
Cancel
Save