Skip to content

Commit 4aa0b33

Browse files
committed
tests for sorted-set-starts-with condition
IDE noise cleanup
1 parent 256d3c1 commit 4aa0b33

File tree

1 file changed

+112
-39
lines changed

1 file changed

+112
-39
lines changed

tests/StackExchange.Redis.Tests/TransactionTests.cs

Lines changed: 112 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ public async Task BasicTranWithStringLengthCondition(string? value, ComparisonTy
362362
db.KeyDelete(key, CommandFlags.FireAndForget);
363363
db.KeyDelete(key2, CommandFlags.FireAndForget);
364364

365-
var expectSuccess = false;
366-
Condition? condition = null;
365+
bool expectSuccess;
366+
Condition? condition;
367367
var valueLength = value?.Length ?? 0;
368368
switch (type)
369369
{
@@ -441,8 +441,8 @@ public async Task BasicTranWithHashLengthCondition(string value, ComparisonType
441441
db.KeyDelete(key, CommandFlags.FireAndForget);
442442
db.KeyDelete(key2, CommandFlags.FireAndForget);
443443

444-
var expectSuccess = false;
445-
Condition? condition = null;
444+
bool expectSuccess;
445+
Condition? condition;
446446
var valueLength = value?.Length ?? 0;
447447
switch (type)
448448
{
@@ -520,8 +520,8 @@ public async Task BasicTranWithSetCardinalityCondition(string value, ComparisonT
520520
db.KeyDelete(key, CommandFlags.FireAndForget);
521521
db.KeyDelete(key2, CommandFlags.FireAndForget);
522522

523-
var expectSuccess = false;
524-
Condition? condition = null;
523+
bool expectSuccess;
524+
Condition? condition;
525525
var valueLength = value?.Length ?? 0;
526526
switch (type)
527527
{
@@ -640,8 +640,8 @@ public async Task BasicTranWithSortedSetCardinalityCondition(string value, Compa
640640
db.KeyDelete(key, CommandFlags.FireAndForget);
641641
db.KeyDelete(key2, CommandFlags.FireAndForget);
642642

643-
var expectSuccess = false;
644-
Condition? condition = null;
643+
bool expectSuccess;
644+
Condition? condition;
645645
var valueLength = value?.Length ?? 0;
646646
switch (type)
647647
{
@@ -719,8 +719,8 @@ public async Task BasicTranWithSortedSetRangeCountCondition(double min, double m
719719
db.KeyDelete(key, CommandFlags.FireAndForget);
720720
db.KeyDelete(key2, CommandFlags.FireAndForget);
721721

722-
var expectSuccess = false;
723-
Condition? condition = null;
722+
bool expectSuccess;
723+
Condition? condition;
724724
var valueLength = (int)(max - min) + 1;
725725
switch (type)
726726
{
@@ -812,43 +812,115 @@ public async Task BasicTranWithSortedSetContainsCondition(bool demandKeyExists,
812812
}
813813
}
814814

815+
public enum SortedSetValue
816+
{
817+
None,
818+
Exact,
819+
Shorter,
820+
Longer,
821+
}
822+
815823
[Theory]
816-
[InlineData(false, false, true)]
817-
[InlineData(false, true, false)]
818-
[InlineData(true, false, false)]
819-
[InlineData(true, true, true)]
820-
public async Task BasicTranWithSortedSetStartsWithCondition(bool demandKeyExists, bool keyExists, bool expectTranResult)
824+
[InlineData(false, SortedSetValue.None, true)]
825+
[InlineData(false, SortedSetValue.Shorter, true)]
826+
[InlineData(false, SortedSetValue.Exact, false)]
827+
[InlineData(false, SortedSetValue.Longer, false)]
828+
[InlineData(true, SortedSetValue.None, false)]
829+
[InlineData(true, SortedSetValue.Shorter, false)]
830+
[InlineData(true, SortedSetValue.Exact, true)]
831+
[InlineData(true, SortedSetValue.Longer, true)]
832+
public async Task BasicTranWithSortedSetStartsWithCondition_String(bool requestExists, SortedSetValue existingValue, bool expectTranResult)
821833
{
822-
using var conn = Create(disabledCommands: new[] { "info", "config" });
834+
using var conn = Create();
823835

824-
RedisKey key = Me(), key2 = Me() + "2";
836+
RedisKey key1 = Me() + "_1", key2 = Me() + "_2";
825837
var db = conn.GetDatabase();
826-
db.KeyDelete(key, CommandFlags.FireAndForget);
838+
db.KeyDelete(key1, CommandFlags.FireAndForget);
827839
db.KeyDelete(key2, CommandFlags.FireAndForget);
828-
RedisValue member = "value";
829-
byte[] startWith = new byte[] { 118, 97, 108 }; // = "val"
830-
if (keyExists) db.SortedSetAdd(key2, member, 0.0, flags: CommandFlags.FireAndForget);
831-
Assert.False(db.KeyExists(key));
832-
Assert.Equal(keyExists, db.SortedSetScore(key2, member).HasValue);
840+
841+
db.SortedSetAdd(key2, "unrelated", 0.0, flags: CommandFlags.FireAndForget);
842+
switch (existingValue)
843+
{
844+
case SortedSetValue.Shorter:
845+
db.SortedSetAdd(key2, "see", 0.0, flags: CommandFlags.FireAndForget);
846+
break;
847+
case SortedSetValue.Exact:
848+
db.SortedSetAdd(key2, "seek", 0.0, flags: CommandFlags.FireAndForget);
849+
break;
850+
case SortedSetValue.Longer:
851+
db.SortedSetAdd(key2, "seeks", 0.0, flags: CommandFlags.FireAndForget);
852+
break;
853+
}
833854

834855
var tran = db.CreateTransaction();
835-
var cond = tran.AddCondition(demandKeyExists ? Condition.SortedSetContainsStarting(key2, startWith) : Condition.SortedSetNotContainsStarting(key2, startWith));
836-
var incr = tran.StringIncrementAsync(key);
837-
var exec = tran.ExecuteAsync();
838-
var get = db.StringGet(key);
856+
var cond = tran.AddCondition(requestExists ? Condition.SortedSetContainsStarting(key2, "seek") : Condition.SortedSetNotContainsStarting(key2, "seek"));
857+
var incr = tran.StringIncrementAsync(key1);
858+
var exec = await tran.ExecuteAsync();
859+
var get = await db.StringGetAsync(key1);
839860

840-
Assert.Equal(expectTranResult, await exec);
841-
if (demandKeyExists == keyExists)
861+
Assert.Equal(expectTranResult, exec);
862+
Assert.Equal(expectTranResult, cond.WasSatisfied);
863+
864+
if (expectTranResult)
865+
{
866+
Assert.Equal(1, await incr); // eq: incr
867+
Assert.Equal(1, (long)get); // eq: get
868+
}
869+
else
870+
{
871+
Assert.Equal(TaskStatus.Canceled, SafeStatus(incr)); // neq: incr
872+
Assert.Equal(0, (long)get); // neq: get
873+
}
874+
}
875+
876+
[Theory]
877+
[InlineData(false, SortedSetValue.None, true)]
878+
[InlineData(false, SortedSetValue.Shorter, true)]
879+
[InlineData(false, SortedSetValue.Exact, false)]
880+
[InlineData(false, SortedSetValue.Longer, false)]
881+
[InlineData(true, SortedSetValue.None, false)]
882+
[InlineData(true, SortedSetValue.Shorter, false)]
883+
[InlineData(true, SortedSetValue.Exact, true)]
884+
[InlineData(true, SortedSetValue.Longer, true)]
885+
public async Task BasicTranWithSortedSetStartsWithCondition_Integer(bool requestExists, SortedSetValue existingValue, bool expectTranResult)
886+
{
887+
using var conn = Create();
888+
889+
RedisKey key1 = Me() + "_1", key2 = Me() + "_2";
890+
var db = conn.GetDatabase();
891+
db.KeyDelete(key1, CommandFlags.FireAndForget);
892+
db.KeyDelete(key2, CommandFlags.FireAndForget);
893+
894+
db.SortedSetAdd(key2, 789, 0.0, flags: CommandFlags.FireAndForget);
895+
switch (existingValue)
896+
{
897+
case SortedSetValue.Shorter:
898+
db.SortedSetAdd(key2, 123, 0.0, flags: CommandFlags.FireAndForget);
899+
break;
900+
case SortedSetValue.Exact:
901+
db.SortedSetAdd(key2, 1234, 0.0, flags: CommandFlags.FireAndForget);
902+
break;
903+
case SortedSetValue.Longer:
904+
db.SortedSetAdd(key2, 12345, 0.0, flags: CommandFlags.FireAndForget);
905+
break;
906+
}
907+
908+
var tran = db.CreateTransaction();
909+
var cond = tran.AddCondition(requestExists ? Condition.SortedSetContainsStarting(key2, 1234) : Condition.SortedSetNotContainsStarting(key2, 1234));
910+
var incr = tran.StringIncrementAsync(key1);
911+
var exec = await tran.ExecuteAsync();
912+
var get = await db.StringGetAsync(key1);
913+
914+
Assert.Equal(expectTranResult, exec);
915+
Assert.Equal(expectTranResult, cond.WasSatisfied);
916+
917+
if (expectTranResult)
842918
{
843-
Assert.True(await exec, "eq: exec");
844-
Assert.True(cond.WasSatisfied, "eq: was satisfied");
845919
Assert.Equal(1, await incr); // eq: incr
846920
Assert.Equal(1, (long)get); // eq: get
847921
}
848922
else
849923
{
850-
Assert.False(await exec, "neq: exec");
851-
Assert.False(cond.WasSatisfied, "neq: was satisfied");
852924
Assert.Equal(TaskStatus.Canceled, SafeStatus(incr)); // neq: incr
853925
Assert.Equal(0, (long)get); // neq: get
854926
}
@@ -935,8 +1007,8 @@ public async Task BasicTranWithSortedSetScoreExistsCondition(bool member1HasScor
9351007
}
9361008

9371009
Assert.False(db.KeyExists(key));
938-
Assert.Equal(member1HasScore ? (double?)Score : null, db.SortedSetScore(key2, member1));
939-
Assert.Equal(member2HasScore ? (double?)Score : null, db.SortedSetScore(key2, member2));
1010+
Assert.Equal(member1HasScore ? Score : null, db.SortedSetScore(key2, member1));
1011+
Assert.Equal(member2HasScore ? Score : null, db.SortedSetScore(key2, member2));
9401012

9411013
var tran = db.CreateTransaction();
9421014
var cond = tran.AddCondition(demandScoreExists ? Condition.SortedSetScoreExists(key2, Score) : Condition.SortedSetScoreNotExists(key2, Score));
@@ -1056,8 +1128,8 @@ public async Task BasicTranWithListLengthCondition(string value, ComparisonType
10561128
db.KeyDelete(key, CommandFlags.FireAndForget);
10571129
db.KeyDelete(key2, CommandFlags.FireAndForget);
10581130

1059-
var expectSuccess = false;
1060-
Condition? condition = null;
1131+
bool expectSuccess;
1132+
Condition? condition;
10611133
var valueLength = value?.Length ?? 0;
10621134
switch (type)
10631135
{
@@ -1135,8 +1207,8 @@ public async Task BasicTranWithStreamLengthCondition(string value, ComparisonTyp
11351207
db.KeyDelete(key, CommandFlags.FireAndForget);
11361208
db.KeyDelete(key2, CommandFlags.FireAndForget);
11371209

1138-
var expectSuccess = false;
1139-
Condition? condition = null;
1210+
bool expectSuccess;
1211+
Condition? condition;
11401212
var valueLength = value?.Length ?? 0;
11411213
switch (type)
11421214
{
@@ -1269,6 +1341,7 @@ public async Task TransactionWithAdHocCommandsAndSelectDisabled()
12691341
var tran = db.CreateTransaction("state");
12701342
var a = tran.ExecuteAsync("SET", "foo", "bar");
12711343
Assert.True(await tran.ExecuteAsync());
1344+
await a;
12721345
var setting = db.StringGet("foo");
12731346
Assert.Equal("bar", setting);
12741347
}

0 commit comments

Comments
 (0)