有序集合测试方法

master
ruyu 7 years ago
parent 7fa0593598
commit b0a0186295

@ -1210,11 +1210,198 @@ namespace RedisStudyTest
} }
#endregion #endregion
#region SortedSetCombineAndStore todo:待做任务 #region SortedSetCombineAndStore
[Fact]
public void SortedSetCombineAndStore_NotKey_Test()
{
var memberNumber = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Union, "destination", "first", "second", Aggregate.Sum, CommandFlags.None);
Assert.Equal(0, memberNumber);
}
/// <summary>
/// 目标集合不存在
/// (创建目标集成)
/// </summary>
[Fact]
public void SortedSetCombineAndStore_NotDestination_Test()
{
//准备
RedisKey destinationKey = "RedisStudy:SortedSet:destination";
RedisKey firstKey = "RedisStudy:SortedSet:first";
RedisKey secondKey = "RedisStudy:SortedSet:second";
//删除旧数据
redisDatabase.KeyDelete(destinationKey);
redisDatabase.KeyDelete(firstKey);
redisDatabase.KeyDelete(secondKey);
SortedSetEntry[] firstEntries = new SortedSetEntry[]
{
new SortedSetEntry("first",1),
new SortedSetEntry("second",2),
};
SortedSetEntry[] secondEntries = new SortedSetEntry[]
{
new SortedSetEntry("third",3),
new SortedSetEntry("fourth",4),
};
redisSortedSetStudy.SortedSetAdd(firstKey, firstEntries);
redisSortedSetStudy.SortedSetAdd(secondKey, secondEntries);
//操作
var combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Union, destinationKey, firstKey, secondKey, Aggregate.Sum, CommandFlags.None);
Assert.Equal(4, combineNum);
var destinationLenth = redisSortedSetStudy.SortedSetLength(destinationKey);
Assert.Equal(4, destinationLenth);
//清理
redisDatabase.KeyDelete(destinationKey);
redisDatabase.KeyDelete(firstKey);
redisDatabase.KeyDelete(secondKey);
}
/// <summary>
/// 目标集合存在
/// 重写目标集合(效果等同:删除重键)
/// </summary>
[Fact]
public void SortedSetCombineAndStore_HasDestination_Test()
{
//准备
RedisKey destinationKey = "RedisStudy:SortedSet:destination";
RedisKey firstKey = "RedisStudy:SortedSet:first";
RedisKey secondKey = "RedisStudy:SortedSet:second";
//删除旧数据
redisDatabase.KeyDelete(destinationKey);
redisDatabase.KeyDelete(firstKey);
redisDatabase.KeyDelete(secondKey);
SortedSetEntry[] destinationEntries = new SortedSetEntry[]
{
new SortedSetEntry("destination1",1),
new SortedSetEntry("destination2",2),
};
SortedSetEntry[] firstEntries = new SortedSetEntry[]
{
new SortedSetEntry("first",1),
new SortedSetEntry("second",2),
new SortedSetEntry("third",3),
};
SortedSetEntry[] secondEntries = new SortedSetEntry[]
{
new SortedSetEntry("first",1),
new SortedSetEntry("third",3),
new SortedSetEntry("fourth",4),
};
redisSortedSetStudy.SortedSetAdd(destinationKey, destinationEntries);
redisSortedSetStudy.SortedSetAdd(firstKey, firstEntries);
redisSortedSetStudy.SortedSetAdd(secondKey, secondEntries);
//集合运算后,存储为目标集合(已存在的目标集合被覆盖)
var combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Union, destinationKey, firstKey, secondKey, Aggregate.Sum, CommandFlags.None);
Assert.Equal(4, combineNum);
var destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey);
Assert.Equal(4, destinationMembers.Length);
Assert.Equal(2, destinationMembers.FirstOrDefault(m=>m.Element=="first").Score);
Assert.Equal(2, destinationMembers.FirstOrDefault(m => m.Element == "second").Score);
Assert.Equal(6, destinationMembers.FirstOrDefault(m => m.Element == "third").Score);
Assert.Equal(4, destinationMembers.FirstOrDefault(m => m.Element == "fourth").Score);
//目标集合再次被覆盖
combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Union, destinationKey, firstKey, secondKey, Aggregate.Min, CommandFlags.None);
Assert.Equal(4, combineNum);
destinationMembers = redisSortedSetStudy.SortedSetRangeByRankWithScores(destinationKey);
Assert.Equal(4, destinationMembers.Length);
Assert.Equal(1, destinationMembers.FirstOrDefault(m => m.Element == "first").Score);
Assert.Equal(2, destinationMembers.FirstOrDefault(m => m.Element == "second").Score);
Assert.Equal(3, destinationMembers.FirstOrDefault(m => m.Element == "third").Score);
Assert.Equal(4, destinationMembers.FirstOrDefault(m => m.Element == "fourth").Score);
//清理
redisDatabase.KeyDelete(destinationKey);
redisDatabase.KeyDelete(firstKey);
redisDatabase.KeyDelete(secondKey);
}
[Fact]
public void SortedSetCombineAndStore_SetOperation_Test()
{
//准备
RedisKey destinationKey = "RedisStudy:SortedSet:destination";
RedisKey firstKey = "RedisStudy:SortedSet:first";
RedisKey secondKey = "RedisStudy:SortedSet:second";
//删除旧数据
redisDatabase.KeyDelete(destinationKey);
redisDatabase.KeyDelete(firstKey);
redisDatabase.KeyDelete(secondKey);
SortedSetEntry[] firstEntries = new SortedSetEntry[]
{
new SortedSetEntry("first",1),
new SortedSetEntry("second",2),
new SortedSetEntry("third",3),
new SortedSetEntry("fourth",4),
new SortedSetEntry("sixth",6),
new SortedSetEntry("eighth",8),
};
SortedSetEntry[] secondEntries = new SortedSetEntry[]
{
new SortedSetEntry("first",1),
new SortedSetEntry("second",2),
new SortedSetEntry("third",3),
new SortedSetEntry("fifth",5),
new SortedSetEntry("seventh",7),
new SortedSetEntry("ninth",9),
};
redisSortedSetStudy.SortedSetAdd(firstKey, firstEntries);
redisSortedSetStudy.SortedSetAdd(secondKey, secondEntries);
//SetOperation.Union:并集
var combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Union, destinationKey, firstKey, secondKey, Aggregate.Sum, CommandFlags.None);
Assert.Equal(9, combineNum);
//SetOperation.Intersect:交集
combineNum = redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Intersect, destinationKey, firstKey, secondKey, Aggregate.Sum, CommandFlags.None);
Assert.Equal(3, combineNum);
//SetOperation.Difference差集程序抛出异常
Assert.Throws<ArgumentOutOfRangeException>(() => redisSortedSetStudy.SortedSetCombineAndStore(SetOperation.Difference, destinationKey, firstKey, secondKey, Aggregate.Min, CommandFlags.None));
//清理
redisDatabase.KeyDelete(destinationKey);
redisDatabase.KeyDelete(firstKey);
redisDatabase.KeyDelete(secondKey);
}
[Fact]
public void SortedSetCombineAndStore_Aggregate_Test()
{
}
[Fact] [Fact]
public void SortedSetCombineAndStoreTest() public void SortedSetCombineAndStoreTest()
{ {
//redisSortedSetStudy.SortedSetCombineAndStore();
}
[Fact]
public void SortedSetCombineAndStore_OverLoad_Test()
{
} }
#endregion #endregion

Loading…
Cancel
Save