You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.8 KiB
C#

6 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using RedisStudyModel;
using StackExchange;
using StackExchange.Redis;
using RedisStuy;
namespace RedisStudyTest
{
public class RedisHashStudyTest : IDisposable
{
6 years ago
private IDatabase redisDatabase = null;
6 years ago
private RedisHashStudy hashStudy = null;
private List<Student> students;
6 years ago
private Student student = null;
private string preHashKey = "RedisStudy:Student:";
6 years ago
/// <summary>
/// 构造
/// </summary>
public RedisHashStudyTest()
{
6 years ago
redisDatabase = RedisHelper.GetRedisDatabase();
6 years ago
hashStudy = new RedisHashStudy();
students = new List<Student>()
{
new Student()
{
Id = 1001,
Name = "王高峰",
Age = 11
},
new Student()
{
Id = 1002,
Name = "王高峰2",
Age = 22
},
new Student()
{
Id = 1003,
Name = "王高峰3",
Age = 33
},
new Student()
{
Id = 1004,
Name = "王高峰4",
Age = 44
},
new Student()
{
Id = 1005,
Name = "王高峰5",
Age = 55
},
};
6 years ago
//hashStudy.AddStudents(students);
6 years ago
}
[Fact]
public void AddStudentTest()
{
6 years ago
student = new Student()
{
Id = 1,
Name = "王高峰",
Age = 2 * 9
};
string redisKey = preHashKey + student.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[] { }));
6 years ago
6 years ago
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, CommandFlags.None);
//设置过期
redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(5));
Assert.True(addHash);
//更新(插入)一项
var addOrUpdateOne = hashStudy.HashSet(redisKey, "Id",student.Id+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);
6 years ago
}
[Theory]
[InlineData(-1)]
[InlineData(-2)]
[InlineData(-3)]
public void DelStudentTest(int studentId)
{
6 years ago
//Assert.False(hashStudy.DelStudentById(studentId));
6 years ago
}
[Theory]
[InlineData(-100)]
public void DelStudentTest2(int studentId)
{
6 years ago
//Assert.False(hashStudy.DelStudentById(studentId));
6 years ago
}
[Fact]
public void QueryOneStudentTest()
{
6 years ago
//hashStudy.AddStudent(this.student);
6 years ago
6 years ago
//var queryStudent = hashStudy.QueryOneStudent(this.student.Id);
6 years ago
6 years ago
//Assert.NotNull(queryStudent);
//Assert.True(this.student.Id==queryStudent.Id);
//Assert.True(this.student.Name == queryStudent.Name);
//Assert.True(this.student.Age == queryStudent.Age);
6 years ago
}
[Fact]
public void ExistStudentTest()
{
6 years ago
//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));
6 years ago
}
/// <summary>
/// 查询所有学生
/// </summary>
[Fact]
public void QueryAllStudent()
{
6 years ago
//List<Student> students = hashStudy.QueryAllStudents();
6 years ago
6 years ago
//Assert.NotNull(students);
//Assert.Equal(students.Count(), students.Count);
6 years ago
}
/// <summary>
/// 清理
/// </summary>
public void Dispose()
{
6 years ago
if (student != null)
{
redisDatabase.KeyDelete(preHashKey + student.Id);
}
6 years ago
//hashStudy.DelStudentById(student.Id);
//foreach (var temp in students)
//{
// hashStudy.DelStudentById(temp.Id);
//}
6 years ago
}
6 years ago
#region 辅助方法
public HashEntry[] StudentsToHashEntrys(Student student)
{
if (student == null)
{
return null;
}
var entrys = new List<HashEntry>()
{
new HashEntry("Id",student.Id),
new HashEntry("Name",student.Name),
new HashEntry("Age",student.Age),
};
return entrys.ToArray();
}
#endregion
6 years ago
}
}