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.

155 lines
4.0 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
{
private IDatabase redisDb = null;
private RedisHashStudy hashStudy = null;
private Student student;
private List<Student> students;
/// <summary>
/// 构造
/// </summary>
public RedisHashStudyTest()
{
redisDb = RedisHelper.GetRedisDatabase();
hashStudy = new RedisHashStudy();
student = new Student()
{
Id = 1,
Name = "王高峰",
Age = 2 * 9
};
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
},
};
hashStudy.AddStudents(students);
}
[Fact]
public void AddStudentTest()
{
hashStudy.AddStudent(student);
var newStudent = hashStudy.QueryOneStudent(student.Id);
Assert.NotNull(newStudent);
Assert.Equal(1, newStudent.Id);
Assert.Equal("王高峰", newStudent.Name);
Assert.Equal(18, actual: newStudent.Age);
}
[Theory]
[InlineData(-1)]
[InlineData(-2)]
[InlineData(-3)]
public void DelStudentTest(int studentId)
{
Assert.False(hashStudy.DelStudentById(studentId));
}
[Theory]
[InlineData(-100)]
public void DelStudentTest2(int studentId)
{
Assert.False(hashStudy.DelStudentById(studentId));
}
[Fact]
public void QueryOneStudentTest()
{
hashStudy.AddStudent(this.student);
var queryStudent = hashStudy.QueryOneStudent(this.student.Id);
Assert.NotNull(queryStudent);
Assert.True(this.student.Id==queryStudent.Id);
Assert.True(this.student.Name == queryStudent.Name);
Assert.True(this.student.Age == queryStudent.Age);
}
[Fact]
public void ExistStudentTest()
{
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));
}
/// <summary>
/// 查询所有学生
/// </summary>
[Fact]
public void QueryAllStudent()
{
List<Student> students = hashStudy.QueryAllStudents();
Assert.NotNull(students);
Assert.Equal(students.Count(), students.Count);
}
/// <summary>
/// 清理
/// </summary>
public void Dispose()
{
hashStudy.DelStudentById(student.Id);
foreach (var temp in students)
{
hashStudy.DelStudentById(temp.Id);
}
}
}
}