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.

307 lines
11 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
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 RedisStuy;
namespace RedisStudyTest
{
/// <summary>
/// Redis 集合学习 测试
/// </summary>
[Trait("RedisSortedSet", "All")]
public class RedisSortedSetStudyTest : IDisposable
{
#region 初始化
private readonly ITestOutputHelper testOutput;
private IDatabase redisDatabase = null;
private RedisSortedSetStudy redisSortedSetStudy = null;
private TimeSpan defaultExpiry = TimeSpan.FromSeconds(20);
private string defaultRedisKey = "RedisStudy:SortedSet:xUnitTest";
/// <summary>
/// 构造
/// </summary>
public RedisSortedSetStudyTest(ITestOutputHelper output)
{
this.testOutput = output;
redisDatabase = RedisHelper.GetRedisDatabase();
redisSortedSetStudy = new RedisSortedSetStudy();
}
#endregion
#region SetAdd
[Fact]
public void SortedAdd_NotKey_Test()
{
var addNumber = redisSortedSetStudy.SortedSetAdd(defaultRedisKey, new SortedSetEntry[] { }, CommandFlags.None);
Assert.Equal(0, addNumber);
var exits = redisDatabase.KeyExists(defaultRedisKey);
Assert.False(exits);
var result = redisSortedSetStudy.SortedSetAdd(defaultRedisKey, "first", 1, CommandFlags.None);
Assert.True(result);
var number = redisSortedSetStudy.SortedSetLength(defaultRedisKey);
Assert.Equal(1, number);
}
[Fact]
public void SetAdd_SortedSetEntrys_Empty_Test()
{
var addNumber = redisSortedSetStudy.SortedSetAdd(defaultRedisKey, new SortedSetEntry[] { }, CommandFlags.None);
Assert.Equal(0, addNumber);
}
[Fact]
public void SetAddByOneTest()
{
//重载方法1
var addResult = redisSortedSetStudy.SortedSetAdd(defaultRedisKey,"first",1);
Assert.True(addResult);
//重载方法2
var addResult2 = redisSortedSetStudy.SortedSetAdd(defaultRedisKey, "second", 2, CommandFlags.None);
Assert.True(addResult2);
//反向验证
var members = redisSortedSetStudy.SortedSetLength(defaultRedisKey);
Assert.Equal(2, members);
}
[Fact]
public void SetAddByGroupTest()
{
SortedSetEntry[] sortedSetEntrys = new SortedSetEntry[]
{
new SortedSetEntry("first", 1),
new SortedSetEntry("second", 2),
new SortedSetEntry("third", 3),
new SortedSetEntry("four", 4),
new SortedSetEntry("five", 5),
};
SortedSetEntry[] sortedSetEntrys2 = new SortedSetEntry[]
{
new SortedSetEntry("six", 6),
new SortedSetEntry("seven", 7),
};
//重载方法1
var memberNumber = redisSortedSetStudy.SortedSetAdd(defaultRedisKey, sortedSetEntrys, CommandFlags.None);
Assert.Equal(5, memberNumber);
//重载方法2
var memberNumber2 = redisSortedSetStudy.SortedSetAdd(defaultRedisKey, sortedSetEntrys2, When.Always, CommandFlags.None);
Assert.Equal(2, memberNumber2);
//验证数据
var totalNumber = redisSortedSetStudy.SortedSetLength(defaultRedisKey);
Assert.Equal(7, totalNumber);
}
#endregion
#region SortedSetIncrement
[Fact]
public void SortedSetIncrement_NotKey_Test()
{
var incResult = redisSortedSetStudy.SortedSetIncrement(defaultRedisKey, "first", 1.0);
Assert.Equal(1.0, incResult);
}
[Fact]
public void SortedSetIncrementTest()
{
var incResult = redisSortedSetStudy.SortedSetIncrement(defaultRedisKey, "first", 1.0);
Assert.Equal(1.0, incResult);
incResult = redisSortedSetStudy.SortedSetIncrement(defaultRedisKey, "first", 1.5);
double differenceValue = incResult - 2.5;
Assert.True(differenceValue < 0.01);
//增加负值等于减少
incResult = redisSortedSetStudy.SortedSetIncrement(defaultRedisKey, "first", -2.1);
differenceValue =0.4 - incResult;
Assert.True(differenceValue < 0.01);
}
#endregion
#region SortedSetDecrement
[Fact]
public void SortedSetDecrement_NotKey_Test()
{
var incResult = redisSortedSetStudy.SortedSetDecrement(defaultRedisKey, "first", 1.0);
Assert.Equal(-1.0, incResult);
}
[Fact]
public void SortedSetDecrementTest()
{
var decResult = redisSortedSetStudy.SortedSetIncrement(defaultRedisKey, "first", -1.0);
Assert.Equal(-1.0, decResult);
decResult = redisSortedSetStudy.SortedSetIncrement(defaultRedisKey, "first", -1.5);
double differenceValue =Math.Abs( -2.5 - decResult);
Assert.True(differenceValue < 0.01);
//减少负值等于增加
decResult = redisSortedSetStudy.SortedSetIncrement(defaultRedisKey, "first", 2.1);
differenceValue =Math.Abs(-0.4 - decResult);
Assert.True(differenceValue < 0.01);
}
#endregion
#region SortedSetLength
[Fact]
public void SortedSetLength_NotKey_Test()
{
var memberCount = redisSortedSetStudy.SortedSetLength(defaultRedisKey);
Assert.Equal(0, memberCount);
}
/// <summary>
/// 排它性参数Exclude设置 测试
/// </summary>
[Fact]
public void SortedSetLength_Exclude_Test()
{
SortedSetEntry[] sortedSetEntries = new SortedSetEntry[]
{
new SortedSetEntry("first",1),
new SortedSetEntry("second",2),
new SortedSetEntry("third",3),
new SortedSetEntry("four",4),
new SortedSetEntry("five",5),
};
redisSortedSetStudy.SortedSetAdd(defaultRedisKey, sortedSetEntries);
//默认Exclude.None [Start,Stop]
var memberCount = redisSortedSetStudy.SortedSetLength(defaultRedisKey, 2, 4, Exclude.None, CommandFlags.None);
Assert.Equal(3, memberCount);
//Exclude.Start (Start,Stop]
memberCount = redisSortedSetStudy.SortedSetLength(defaultRedisKey, 2, 4, Exclude.Start, CommandFlags.None);
Assert.Equal(2, memberCount);
//Exclude.Stop [Start,Stop)
memberCount = redisSortedSetStudy.SortedSetLength(defaultRedisKey, 2, 4, Exclude.Stop, CommandFlags.None);
Assert.Equal(2, memberCount);
//Exclude.Both (Start,Stop)
memberCount = redisSortedSetStudy.SortedSetLength(defaultRedisKey, 2, 4, Exclude.Both, CommandFlags.None);
Assert.Equal(1, memberCount);
}
[Fact]
public void SortedSetLengthTest()
{
SortedSetEntry[] sortedSetEntries=new SortedSetEntry[]
{
new SortedSetEntry("first",1),
new SortedSetEntry("second",2),
new SortedSetEntry("third",3),
new SortedSetEntry("four",4),
new SortedSetEntry("five",5),
};
redisSortedSetStudy.SortedSetAdd(defaultRedisKey, sortedSetEntries);
var memberCount = redisSortedSetStudy.SortedSetLength(defaultRedisKey, 1, 5);
Assert.Equal(sortedSetEntries.Length, memberCount);
}
#endregion
#region SortedSetLengthByValue 分数相同则字典排序
[Fact]
public void SortedSetLengthByValue_NotKey_Test()
{
var memberCount = redisSortedSetStudy.SortedSetLengthByValue(defaultRedisKey,1,200);
Assert.Equal(0, memberCount);
}
/// <summary>
/// 排它性参数Exclude设置 测试
/// </summary>
[Fact]
public void SortedSetLengthByValue_Exclude_Test()
{
//todo:排序规则,还不清楚
SortedSetEntry[] sortedSetEntries = new SortedSetEntry[]
{
new SortedSetEntry("Andy",0),
new SortedSetEntry("and",0),
new SortedSetEntry("And",0),
new SortedSetEntry("Banana",0),
new SortedSetEntry("color",0),
new SortedSetEntry("query",0),
new SortedSetEntry("remove",0),
new SortedSetEntry("101",0),
new SortedSetEntry("304",0),
new SortedSetEntry("王高峰",0),
new SortedSetEntry("刘山东",0),
};
redisSortedSetStudy.SortedSetAdd(defaultRedisKey, sortedSetEntries);
//默认Exclude.None [Start,Stop]
var memberCount = redisSortedSetStudy.SortedSetLengthByValue(defaultRedisKey, "1", "B", Exclude.None, CommandFlags.None);
Assert.Equal(4, memberCount);
////Exclude.Start (Start,Stop]
//memberCount = redisSortedSetStudy.SortedSetLengthByValue(defaultRedisKey, 2, 4, Exclude.Start, CommandFlags.None);
//Assert.Equal(2, memberCount);
////Exclude.Stop [Start,Stop)
//memberCount = redisSortedSetStudy.SortedSetLengthByValue(defaultRedisKey, 2, 4, Exclude.Stop, CommandFlags.None);
//Assert.Equal(2, memberCount);
////Exclude.Both (Start,Stop)
//memberCount = redisSortedSetStudy.SortedSetLengthByValue(defaultRedisKey, 2, 4, Exclude.Both, CommandFlags.None);
//Assert.Equal(1, memberCount);
}
[Fact]
public void SortedSetLengthByValueTest()
{
SortedSetEntry[] sortedSetEntries = new SortedSetEntry[]
{
new SortedSetEntry("first",1),
new SortedSetEntry("second",2),
new SortedSetEntry("third",3),
new SortedSetEntry("four",4),
new SortedSetEntry("five",5),
};
redisSortedSetStudy.SortedSetAdd(defaultRedisKey, sortedSetEntries);
var memberCount = redisSortedSetStudy.SortedSetLength(defaultRedisKey, 1, 5);
Assert.Equal(sortedSetEntries.Length, memberCount);
}
#endregion
#region 清理
public void Dispose()
{
redisDatabase.KeyDelete(defaultRedisKey);
}
#endregion
}
}