|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
using StackExchange.Redis;
|
|
|
using Xunit;
|
|
|
using Xunit.Extensions;
|
|
|
using Xunit.Serialization;
|
|
|
using Xunit.Abstractions;
|
|
|
using Xunit.Sdk;
|
|
|
|
|
|
using RedisStudyModel;
|
|
|
using RedisStuy;
|
|
|
|
|
|
namespace RedisStudyTest
|
|
|
{
|
|
|
[Trait("RedisString", "All")]
|
|
|
public class RedisListStudyTest : IDisposable
|
|
|
{
|
|
|
#region 初始化
|
|
|
private readonly ITestOutputHelper testOutput;
|
|
|
private IDatabase redisDatabase = null;
|
|
|
private RedisListStudy redisListStudy = null;
|
|
|
private TimeSpan defaultExpiry = TimeSpan.FromSeconds(20);
|
|
|
private string defaultRedisKey = "RedisStudy:List:xUnitTest";
|
|
|
|
|
|
/// <summary>
|
|
|
/// 构造
|
|
|
/// </summary>
|
|
|
public RedisListStudyTest(ITestOutputHelper output)
|
|
|
{
|
|
|
this.testOutput = output;
|
|
|
redisDatabase = RedisHelper.GetRedisDatabase();
|
|
|
redisListStudy = new RedisListStudy();
|
|
|
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region ListLeftPush
|
|
|
[Fact]
|
|
|
public void ListLeftPushTest()
|
|
|
{
|
|
|
var listLength = redisListStudy.ListLeftPush(defaultRedisKey, "first");
|
|
|
Assert.Equal(1, listLength);
|
|
|
|
|
|
listLength = redisListStudy.ListLeftPush(defaultRedisKey, "second");
|
|
|
Assert.Equal(2, listLength);
|
|
|
|
|
|
var second = redisListStudy.ListLeftPop(defaultRedisKey);
|
|
|
Assert.Equal("second", second);
|
|
|
|
|
|
var first = redisListStudy.ListLeftPop(defaultRedisKey);
|
|
|
Assert.Equal("first", first);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region ListRightPush
|
|
|
[Fact]
|
|
|
public void ListRightPushTest()
|
|
|
{
|
|
|
var listLength = redisListStudy.ListRightPush(defaultRedisKey, "first");
|
|
|
Assert.Equal(1, listLength);
|
|
|
|
|
|
listLength = redisListStudy.ListRightPush(defaultRedisKey, "second");
|
|
|
Assert.Equal(2, listLength);
|
|
|
|
|
|
var second = redisListStudy.ListRightPop(defaultRedisKey);
|
|
|
Assert.Equal("second", second);
|
|
|
|
|
|
var first = redisListStudy.ListRightPop(defaultRedisKey);
|
|
|
Assert.Equal("first", first);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region ListInsertBefore
|
|
|
/// <summary>
|
|
|
/// Key不存在时,不执行操作,返回0
|
|
|
/// </summary>
|
|
|
[Fact]
|
|
|
public void ListInsertBeforeNotKeyTest()
|
|
|
{
|
|
|
var listLenth = redisListStudy.ListInsertBefore(defaultRedisKey, "first", "firstBefore");
|
|
|
Assert.Equal(0, listLenth);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 未找到指定参照值时,返回 -1
|
|
|
/// </summary>
|
|
|
[Fact]
|
|
|
public void ListInsertBeforeNotPivotTest()
|
|
|
{
|
|
|
redisListStudy.ListLeftPush(defaultRedisKey, "second");
|
|
|
|
|
|
var listLenth = redisListStudy.ListInsertBefore(defaultRedisKey, "first", "firstBefore");
|
|
|
Assert.Equal(-1, listLenth);
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void ListInsertBeforeTest()
|
|
|
{
|
|
|
redisListStudy.ListLeftPush(defaultRedisKey, "third");
|
|
|
|
|
|
var listLenth = redisListStudy.ListInsertBefore(defaultRedisKey, "third", "second");
|
|
|
Assert.Equal(2, listLenth);
|
|
|
|
|
|
listLenth = redisListStudy.ListInsertBefore(defaultRedisKey, "second", "first");
|
|
|
Assert.Equal(3, listLenth);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region ListInsertAfter
|
|
|
/// <summary>
|
|
|
/// Key不存在时,不执行操作,返回0
|
|
|
/// </summary>
|
|
|
[Fact]
|
|
|
public void ListInsertAfterNotKeyTest()
|
|
|
{
|
|
|
var listLenth = redisListStudy.ListInsertAfter(defaultRedisKey, "first", "firstAfter");
|
|
|
Assert.Equal(0, listLenth);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 未找到指定参照值时,返回 -1
|
|
|
/// </summary>
|
|
|
[Fact]
|
|
|
public void ListInsertAfterNotPivotTest()
|
|
|
{
|
|
|
redisListStudy.ListLeftPush(defaultRedisKey, "second");
|
|
|
|
|
|
var listLenth = redisListStudy.ListInsertAfter(defaultRedisKey, "first", "firstAfter");
|
|
|
Assert.Equal(-1, listLenth);
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void ListInsertAfterTest()
|
|
|
{
|
|
|
redisListStudy.ListLeftPush(defaultRedisKey, "first");
|
|
|
|
|
|
var listLenth = redisListStudy.ListInsertAfter(defaultRedisKey, "first", "second");
|
|
|
Assert.Equal(2, listLenth);
|
|
|
|
|
|
listLenth = redisListStudy.ListInsertAfter(defaultRedisKey, "second", "third");
|
|
|
Assert.Equal(3, listLenth);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region ListSetByIndex
|
|
|
[Fact]
|
|
|
public void ListGetByIndexNotKeyTest()
|
|
|
{
|
|
|
Assert.Throws<RedisServerException>(() => redisListStudy.ListSetByIndex(defaultRedisKey, 0, "first"));
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
public void ListSetByIndexTest()
|
|
|
{
|
|
|
redisListStudy.ListLeftPush(defaultRedisKey, "first");
|
|
|
|
|
|
redisListStudy.ListSetByIndex(defaultRedisKey,0,"firstSetByIndex");
|
|
|
|
|
|
var first = redisListStudy.ListLeftPop(defaultRedisKey);
|
|
|
|
|
|
Assert.Equal("firstSetByIndex", first);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 清理
|
|
|
public void Dispose()
|
|
|
{
|
|
|
redisDatabase.KeyDelete(defaultRedisKey);
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
}
|