From b0a0186295b3a1e431225502853788bfce79ab47 Mon Sep 17 00:00:00 2001 From: ruyu Date: Thu, 12 Jul 2018 01:28:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=89=E5=BA=8F=E9=9B=86=E5=90=88=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RedisStudyTest/RedisSortedSetStudyTest.cs | 191 +++++++++++++++++++++- 1 file changed, 189 insertions(+), 2 deletions(-) diff --git a/RedisStudyTest/RedisSortedSetStudyTest.cs b/RedisStudyTest/RedisSortedSetStudyTest.cs index e3ee120..1924be1 100644 --- a/RedisStudyTest/RedisSortedSetStudyTest.cs +++ b/RedisStudyTest/RedisSortedSetStudyTest.cs @@ -1210,11 +1210,198 @@ namespace RedisStudyTest } #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); + } + + /// + /// 目标集合不存在 + /// (创建目标集成) + /// + [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); + } + + /// + /// 目标集合存在 + /// 重写目标集合(效果等同:删除重键) + /// + [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(() => 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] public void SortedSetCombineAndStoreTest() { - //redisSortedSetStudy.SortedSetCombineAndStore(); + + } + + [Fact] + public void SortedSetCombineAndStore_OverLoad_Test() + { + } #endregion