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.

212 lines
6.0 KiB
C#

6 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;
6 years ago
using Xunit;
using Xunit.Extensions;
using Xunit.Serialization;
using Xunit.Abstractions;
using Xunit.Sdk;
6 years ago
using RedisStudyModel;
using RedisStuy;
namespace RedisStudyTest
{
/// <summary>
/// Redis Hash 类型测试
/// </summary>
6 years ago
public class RedisHashStudyTest : IDisposable
{
#region 初始化
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:";
private int keyExpireSeconds = 20;
6 years ago
/// <summary>
/// 构造
/// </summary>
public RedisHashStudyTest()
{
6 years ago
redisDatabase = RedisHelper.GetRedisDatabase();
6 years ago
hashStudy = new RedisHashStudy();
6 years ago
student = new Student()
{
Id = 1,
Name = "王高峰",
Age = 2 * 9
};
6 years ago
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
},
};
}
#endregion
6 years ago
#region 添加或更新学习
/// <summary>
6 years ago
/// 参数异常 测试
/// </summary>
6 years ago
[Fact]
6 years ago
public void AddStudentExceptionTest()
6 years ago
{
6 years ago
string redisKey = preHashKey + student.Id;
6 years ago
//参数异常测试
6 years ago
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
/// <summary>
6 years ago
/// 参数 When 测试
/// </summary>
6 years ago
[Fact]
public void AddStudentWhenTest()
{
string redisKey = preHashKey + student.Id;
//当前上下文不能使用: When.Exists
6 years ago
var id_When_NotExists_No = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.NotExists);
Assert.True(id_When_NotExists_No);
var id_When_NotExists_Yes = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.NotExists);
6 years ago
Assert.False(id_When_NotExists_Yes);
var id_When_Always_Exists = hashStudy.HashSet(redisKey, "Id", student.Id + 1, When.Always);
Assert.False(id_When_Always_Exists);
6 years ago
var id_When_Always_NotExists = hashStudy.HashSet(redisKey, "Id4", student.Id + 1, When.Always);
Assert.True(id_When_Always_NotExists);
}
/// <summary>
6 years ago
/// 添加一个默认学生 测试
/// </summary>
[Fact]
public void AddStudentTest()
{
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, CommandFlags.None);
Assert.True(addHash);
//设置过期
redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(keyExpireSeconds));
}
/// <summary>
6 years ago
/// 添加一组初始化设置的学生 测试
/// </summary>
[Fact]
public void AddStudentsTest()
{
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));
}
}
6 years ago
/// <summary>
/// 添加部分字段 测试
/// </summary>
[Fact]
public void AddPartStudentTest()
{
//todo:待写
}
/// <summary>
/// 更新学生异常测试
/// </summary>
[Fact]
public void UpdateStudentExceptionTest()
{
string redisKey = preHashKey + student.Id;
//不存在Key, 返回false,不异常
Assert.False(hashStudy.HashSet(string.Empty, "Id", -1));
}
#endregion
6 years ago
6 years ago
[Fact]
public void Test()
6 years ago
{
Assert.IsType<int>(1);
6 years ago
}
/// <summary>
/// 清理
/// </summary>
public void Dispose()
{
6 years ago
if (student != null)
{
redisDatabase.KeyDelete(preHashKey + student.Id);
}
6 years ago
foreach (var temp in students)
{
redisDatabase.KeyDelete(preHashKey + temp.Id);
}
6 years ago
}
}
}