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