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