Skip to content

Commit b81ad62

Browse files
committed
simplify and fix key prefix tests
1 parent 1817d93 commit b81ad62

2 files changed

Lines changed: 63 additions & 93 deletions

File tree

tests/StackExchange.Redis.Tests/KeyPrefixedDatabaseTests.cs

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Linq.Expressions;
45
using System.Net;
56
using System.Text;
@@ -18,6 +19,14 @@ public sealed class KeyPrefixedDatabaseTests
1819
private readonly IDatabase mock;
1920
private readonly IDatabase prefixed;
2021

22+
internal static RedisKey[] IsKeys(params RedisKey[] expected) => IsRaw(expected);
23+
internal static RedisValue[] IsValues(params RedisValue[] expected) => IsRaw(expected);
24+
private static T[] IsRaw<T>(T[] expected)
25+
{
26+
Expression<Predicate<T[]>> lambda = actual => actual.Length == expected.Length && expected.SequenceEqual(actual);
27+
return Arg.Is(lambda);
28+
}
29+
2130
public KeyPrefixedDatabaseTests()
2231
{
2332
mock = Substitute.For<IDatabase>();
@@ -237,10 +246,8 @@ public void HyperLogLogMerge_1()
237246
[Fact]
238247
public void HyperLogLogMerge_2()
239248
{
240-
RedisKey[] keys = ["a", "b"];
241-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 2 && _[0] == "prefix:a" && _[1] == "prefix:b";
242-
prefixed.HyperLogLogMerge("destination", keys, CommandFlags.None);
243-
mock.Received().HyperLogLogMerge("prefix:destination", Arg.Is(valid), CommandFlags.None);
249+
prefixed.HyperLogLogMerge("destination", ["a", "b"], CommandFlags.None);
250+
mock.Received().HyperLogLogMerge("prefix:destination", IsKeys(["prefix:a", "prefix:b"]), CommandFlags.None);
244251
}
245252

246253
[Fact]
@@ -267,10 +274,8 @@ public void KeyDelete_1()
267274
[Fact]
268275
public void KeyDelete_2()
269276
{
270-
RedisKey[] keys = ["a", "b"];
271-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 2 && _[0] == "prefix:a" && _[1] == "prefix:b";
272-
prefixed.KeyDelete(keys, CommandFlags.None);
273-
mock.Received().KeyDelete(Arg.Is(valid), CommandFlags.None);
277+
prefixed.KeyDelete(["a", "b"], CommandFlags.None);
278+
mock.Received().KeyDelete(IsKeys(["prefix:a", "prefix:b"]), CommandFlags.None);
274279
}
275280

276281
[Fact]
@@ -594,19 +599,17 @@ public void ScriptEvaluate_1()
594599
byte[] hash = Array.Empty<byte>();
595600
RedisValue[] values = Array.Empty<RedisValue>();
596601
RedisKey[] keys = ["a", "b"];
597-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 2 && _[0] == "prefix:a" && _[1] == "prefix:b";
598602
prefixed.ScriptEvaluate(hash, keys, values, CommandFlags.None);
599-
mock.Received().ScriptEvaluate(hash, Arg.Is(valid), values, CommandFlags.None);
603+
mock.Received().ScriptEvaluate(hash, IsKeys(["prefix:a", "prefix:b"]), values, CommandFlags.None);
600604
}
601605

602606
[Fact]
603607
public void ScriptEvaluate_2()
604608
{
605609
RedisValue[] values = Array.Empty<RedisValue>();
606610
RedisKey[] keys = ["a", "b"];
607-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 2 && _[0] == "prefix:a" && _[1] == "prefix:b";
608611
prefixed.ScriptEvaluate(script: "script", keys: keys, values: values, flags: CommandFlags.None);
609-
mock.Received().ScriptEvaluate(script: "script", keys: Arg.Is(valid), values: values, flags: CommandFlags.None);
612+
mock.Received().ScriptEvaluate(script: "script", keys: IsKeys(["prefix:a", "prefix:b"]), values: values, flags: CommandFlags.None);
610613
}
611614

612615
[Fact]
@@ -635,9 +638,8 @@ public void SetCombine_1()
635638
public void SetCombine_2()
636639
{
637640
RedisKey[] keys = ["a", "b"];
638-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 2 && _[0] == "prefix:a" && _[1] == "prefix:b";
639641
prefixed.SetCombine(SetOperation.Intersect, keys, CommandFlags.None);
640-
mock.Received().SetCombine(SetOperation.Intersect, Arg.Is(valid), CommandFlags.None);
642+
mock.Received().SetCombine(SetOperation.Intersect, IsKeys(["prefix:a", "prefix:b"]), CommandFlags.None);
641643
}
642644

643645
[Fact]
@@ -651,9 +653,8 @@ public void SetCombineAndStore_1()
651653
public void SetCombineAndStore_2()
652654
{
653655
RedisKey[] keys = ["a", "b"];
654-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 2 && _[0] == "prefix:a" && _[1] == "prefix:b";
655656
prefixed.SetCombineAndStore(SetOperation.Intersect, "destination", keys, CommandFlags.None);
656-
mock.Received().SetCombineAndStore(SetOperation.Intersect, "prefix:destination", Arg.Is(valid), CommandFlags.None);
657+
mock.Received().SetCombineAndStore(SetOperation.Intersect, "prefix:destination", IsKeys(["prefix:a", "prefix:b"]), CommandFlags.None);
657658
}
658659

659660
[Fact]
@@ -674,9 +675,8 @@ public void SetContains_2()
674675
[Fact]
675676
public void SetIntersectionLength()
676677
{
677-
var keys = new RedisKey[] { "key1", "key2" };
678-
prefixed.SetIntersectionLength(keys);
679-
mock.Received().SetIntersectionLength(keys, 0, CommandFlags.None);
678+
prefixed.SetIntersectionLength(["key1", "key2"]);
679+
mock.Received().SetIntersectionLength(IsKeys(["prefix:key1", "prefix:key2"]), 0, CommandFlags.None);
680680
}
681681

682682
[Fact]
@@ -764,26 +764,24 @@ public void SetScan_Full()
764764
public void Sort()
765765
{
766766
RedisValue[] get = ["a", "#"];
767-
Expression<Predicate<RedisValue[]>> valid = _ => _.Length == 2 && _[0] == "prefix:a" && _[1] == "#";
768767

769768
prefixed.Sort("key", 123, 456, Order.Descending, SortType.Alphabetic, "nosort", get, CommandFlags.None);
770769
prefixed.Sort("key", 123, 456, Order.Descending, SortType.Alphabetic, "by", get, CommandFlags.None);
771770

772-
mock.Received().Sort("prefix:key", 123, 456, Order.Descending, SortType.Alphabetic, "nosort", Arg.Is(valid), CommandFlags.None);
773-
mock.Received().Sort("prefix:key", 123, 456, Order.Descending, SortType.Alphabetic, "prefix:by", Arg.Is(valid), CommandFlags.None);
771+
mock.Received().Sort("prefix:key", 123, 456, Order.Descending, SortType.Alphabetic, "nosort", IsValues(["prefix:a", "#"]), CommandFlags.None);
772+
mock.Received().Sort("prefix:key", 123, 456, Order.Descending, SortType.Alphabetic, "prefix:by", IsValues(["prefix:a", "#"]), CommandFlags.None);
774773
}
775774

776775
[Fact]
777776
public void SortAndStore()
778777
{
779778
RedisValue[] get = ["a", "#"];
780-
Expression<Predicate<RedisValue[]>> valid = _ => _.Length == 2 && _[0] == "prefix:a" && _[1] == "#";
781779

782780
prefixed.SortAndStore("destination", "key", 123, 456, Order.Descending, SortType.Alphabetic, "nosort", get, CommandFlags.None);
783781
prefixed.SortAndStore("destination", "key", 123, 456, Order.Descending, SortType.Alphabetic, "by", get, CommandFlags.None);
784782

785-
mock.Received().SortAndStore("prefix:destination", "prefix:key", 123, 456, Order.Descending, SortType.Alphabetic, "nosort", Arg.Is(valid), CommandFlags.None);
786-
mock.Received().SortAndStore("prefix:destination", "prefix:key", 123, 456, Order.Descending, SortType.Alphabetic, "prefix:by", Arg.Is(valid), CommandFlags.None);
783+
mock.Received().SortAndStore("prefix:destination", "prefix:key", 123, 456, Order.Descending, SortType.Alphabetic, "nosort", IsValues(["prefix:a", "#"]), CommandFlags.None);
784+
mock.Received().SortAndStore("prefix:destination", "prefix:key", 123, 456, Order.Descending, SortType.Alphabetic, "prefix:by", IsValues(["prefix:a", "#"]), CommandFlags.None);
787785
}
788786

789787
[Fact]
@@ -813,16 +811,15 @@ public void SortedSetAdd_3()
813811
public void SortedSetCombine()
814812
{
815813
RedisKey[] keys = ["a", "b"];
816-
prefixed.SortedSetCombine(SetOperation.Intersect, keys);
817-
mock.Received().SortedSetCombine(SetOperation.Intersect, keys, null, Aggregate.Sum, CommandFlags.None);
814+
prefixed.SortedSetCombine(SetOperation.Intersect, ["a", "b"]);
815+
mock.Received().SortedSetCombine(SetOperation.Intersect, IsKeys(["prefix:a", "prefix:b"]), null, Aggregate.Sum, CommandFlags.None);
818816
}
819817

820818
[Fact]
821819
public void SortedSetCombineWithScores()
822820
{
823-
RedisKey[] keys = ["a", "b"];
824-
prefixed.SortedSetCombineWithScores(SetOperation.Intersect, keys);
825-
mock.Received().SortedSetCombineWithScores(SetOperation.Intersect, keys, null, Aggregate.Sum, CommandFlags.None);
821+
prefixed.SortedSetCombineWithScores(SetOperation.Intersect, ["a", "b"]);
822+
mock.Received().SortedSetCombineWithScores(SetOperation.Intersect, IsKeys("prefix:a", "prefix:b"), null, Aggregate.Sum, CommandFlags.None);
826823
}
827824

828825
[Fact]
@@ -836,9 +833,8 @@ public void SortedSetCombineAndStore_1()
836833
public void SortedSetCombineAndStore_2()
837834
{
838835
RedisKey[] keys = ["a", "b"];
839-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 2 && _[0] == "prefix:a" && _[1] == "prefix:b";
840836
prefixed.SetCombineAndStore(SetOperation.Intersect, "destination", keys, CommandFlags.None);
841-
mock.Received().SetCombineAndStore(SetOperation.Intersect, "prefix:destination", Arg.Is(valid), CommandFlags.None);
837+
mock.Received().SetCombineAndStore(SetOperation.Intersect, "prefix:destination", IsKeys("prefix:a", "prefix:b"), CommandFlags.None);
842838
}
843839

844840
[Fact]
@@ -858,9 +854,8 @@ public void SortedSetIncrement()
858854
[Fact]
859855
public void SortedSetIntersectionLength()
860856
{
861-
RedisKey[] keys = ["a", "b"];
862-
prefixed.SortedSetIntersectionLength(keys, 1, CommandFlags.None);
863-
mock.Received().SortedSetIntersectionLength(keys, 1, CommandFlags.None);
857+
prefixed.SortedSetIntersectionLength(["a", "b"], 1, CommandFlags.None);
858+
mock.Received().SortedSetIntersectionLength(IsKeys("prefix:a", "prefix:b"), 1, CommandFlags.None);
864859
}
865860

866861
[Fact]
@@ -1255,45 +1250,40 @@ public void StringBitOperation_1()
12551250
public void StringBitOperation_2()
12561251
{
12571252
RedisKey[] keys = ["a", "b"];
1258-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 2 && _[0] == "prefix:a" && _[1] == "prefix:b";
12591253
prefixed.StringBitOperation(Bitwise.Xor, "destination", keys, CommandFlags.None);
1260-
mock.Received().StringBitOperation(Bitwise.Xor, "prefix:destination", Arg.Is(valid), CommandFlags.None);
1254+
mock.Received().StringBitOperation(Bitwise.Xor, "prefix:destination", IsKeys("prefix:a", "prefix:b"), CommandFlags.None);
12611255
}
12621256

12631257
[Fact]
12641258
public void StringBitOperation_Diff()
12651259
{
12661260
RedisKey[] keys = ["x", "y1", "y2"];
1267-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 3 && _[0] == "prefix:x" && _[1] == "prefix:y1" && _[2] == "prefix:y2";
12681261
prefixed.StringBitOperation(Bitwise.Diff, "destination", keys, CommandFlags.None);
1269-
mock.Received().StringBitOperation(Bitwise.Diff, "prefix:destination", Arg.Is(valid), CommandFlags.None);
1262+
mock.Received().StringBitOperation(Bitwise.Diff, "prefix:destination", IsKeys("prefix:x", "prefix:y1", "prefix:y2"), CommandFlags.None);
12701263
}
12711264

12721265
[Fact]
12731266
public void StringBitOperation_Diff1()
12741267
{
12751268
RedisKey[] keys = ["x", "y1", "y2"];
1276-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 3 && _[0] == "prefix:x" && _[1] == "prefix:y1" && _[2] == "prefix:y2";
12771269
prefixed.StringBitOperation(Bitwise.Diff1, "destination", keys, CommandFlags.None);
1278-
mock.Received().StringBitOperation(Bitwise.Diff1, "prefix:destination", Arg.Is(valid), CommandFlags.None);
1270+
mock.Received().StringBitOperation(Bitwise.Diff1, "prefix:destination", IsKeys("prefix:x", "prefix:y1", "prefix:y2"), CommandFlags.None);
12791271
}
12801272

12811273
[Fact]
12821274
public void StringBitOperation_AndOr()
12831275
{
12841276
RedisKey[] keys = ["x", "y1", "y2"];
1285-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 3 && _[0] == "prefix:x" && _[1] == "prefix:y1" && _[2] == "prefix:y2";
12861277
prefixed.StringBitOperation(Bitwise.AndOr, "destination", keys, CommandFlags.None);
1287-
mock.Received().StringBitOperation(Bitwise.AndOr, "prefix:destination", Arg.Is(valid), CommandFlags.None);
1278+
mock.Received().StringBitOperation(Bitwise.AndOr, "prefix:destination", IsKeys("prefix:x", "prefix:y1", "prefix:y2"), CommandFlags.None);
12881279
}
12891280

12901281
[Fact]
12911282
public void StringBitOperation_One()
12921283
{
12931284
RedisKey[] keys = ["a", "b", "c"];
1294-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 3 && _[0] == "prefix:a" && _[1] == "prefix:b" && _[2] == "prefix:c";
12951285
prefixed.StringBitOperation(Bitwise.One, "destination", keys, CommandFlags.None);
1296-
mock.Received().StringBitOperation(Bitwise.One, "prefix:destination", Arg.Is(valid), CommandFlags.None);
1286+
mock.Received().StringBitOperation(Bitwise.One, "prefix:destination", IsKeys("prefix:a", "prefix:b", "prefix:c"), CommandFlags.None);
12971287
}
12981288

12991289
[Fact]
@@ -1335,9 +1325,8 @@ public void StringGet_1()
13351325
public void StringGet_2()
13361326
{
13371327
RedisKey[] keys = ["a", "b"];
1338-
Expression<Predicate<RedisKey[]>> valid = _ => _.Length == 2 && _[0] == "prefix:a" && _[1] == "prefix:b";
13391328
prefixed.StringGet(keys, CommandFlags.None);
1340-
mock.Received().StringGet(Arg.Is(valid), CommandFlags.None);
1329+
mock.Received().StringGet(IsKeys("prefix:a", "prefix:b"), CommandFlags.None);
13411330
}
13421331

13431332
[Fact]

0 commit comments

Comments
 (0)