From 1dda5524bbff41bacfdbda7fe450761f5b91acdd Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Wed, 17 Jul 2024 10:40:30 +0100 Subject: [PATCH 01/10] DOC-2525 add ListTutorial.cs to fork --- tests/Doc/ListTutorial.cs | 334 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 334 insertions(+) create mode 100644 tests/Doc/ListTutorial.cs diff --git a/tests/Doc/ListTutorial.cs b/tests/Doc/ListTutorial.cs new file mode 100644 index 00000000..d258b835 --- /dev/null +++ b/tests/Doc/ListTutorial.cs @@ -0,0 +1,334 @@ +//EXAMPLE: list_tutorial +//HIDE_START +//REMOVE_START +using NRedisStack; +using NRedisStack.Tests; +using StackExchange.Redis; +//REMOVE_END + + +//REMOVE_START +namespace Doc; +[Collection("DocsTests")] +//REMOVE_END +public class ListExample +{ + //REMOVE_START + [SkipIfRedis(Is.OSSCluster)] + //REMOVE_END + public void run() + { + var muxer = ConnectionMultiplexer.Connect("localhost:6379"); + var db = muxer.GetDatabase(); + //HIDE_END + //REMOVE_START + db.KeyDelete("bikes:repairs"); + db.KeyDelete("bikes:finished"); + db.KeyDelete("{bikes}:repairs"); + db.KeyDelete("{bikes}:finished"); + //REMOVE_END + + + //STEP_START queue + long res1 = db.ListLeftPush("bikes:repairs", "bike:1"); + Console.WriteLine(res1); // >>> 1 + + long res2 = db.ListLeftPush("bikes:repairs", "bike:2"); + Console.WriteLine(res2); // >>> 2 + + RedisValue res3 = db.ListRightPop("bikes:repairs"); + Console.WriteLine(res3); // >>> "bike:1" + + RedisValue res4 = db.ListRightPop("bikes:repairs"); + Console.WriteLine(res4); // >>> "bike:2" + //STEP_END + + //REMOVE_START + Assert.Equal(1, res1); + Assert.Equal(2, res2); + Assert.Equal("bike:1", res3); + Assert.Equal("bike:2", res4); + //REMOVE_END + + //STEP_START stack + long res5 = db.ListLeftPush("bikes:repairs", "bike:1"); + Console.WriteLine(res5); // >>> 1 + + long res6 = db.ListLeftPush("bikes:repairs", "bike:2"); + Console.WriteLine(res6); // >>> 2 + + RedisValue res7 = db.ListLeftPop("bikes:repairs"); + Console.WriteLine(res7); // >>> "bike:2" + + RedisValue res8 = db.ListLeftPop("bikes:repairs"); + Console.WriteLine(res8); // >>> "bike:1" + //STEP_END + + //REMOVE_START + Assert.Equal("bike:2", res7); + Assert.Equal("bike:1", res8); + //REMOVE_END + + + //STEP_START llen + long res9 = db.ListLength("bikes:repairs"); + Console.WriteLine(res9); // >>> 0 + //STEP_END + + //REMOVE_START + Assert.Equal(0, res9); + //REMOVE_END + + + //STEP_START lmove_lrange + long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1"); + Console.WriteLine(res10); // >>> 1 + + long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2"); + Console.WriteLine(res11); // >>> 2 + + RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left); + Console.Write(res12); // >>> "bike:2" + + RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1); + Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1" + + RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1); + Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2" + //STEP_END + + //REMOVE_START + Assert.Equal(1, res10); + Assert.Equal(2, res11); + Assert.Equal("bike:2", res12); + Assert.Equal("bike:1", string.Join(", ", res13)); + Assert.Equal("bike:2", string.Join(", ", res14)); + bool delRes = db.KeyDelete("{bikes}:repairs"); + delRes = db.KeyDelete("{bikes}:finished"); + //REMOVE_END + + //STEP_START lpush_rpush + long res15 = db.ListRightPush("bikes:repairs", "bike:1"); + Console.WriteLine(res15); // >>> 1 + + long res16 = db.ListRightPush("bikes:repairs", "bike:2"); + Console.WriteLine(res16); // >>> 2 + + long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike"); + Console.WriteLine(res17); // >>> 3 + + RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1); + Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2" + //STEP_END + + //REMOVE_START + Assert.Equal(1, res15); + Assert.Equal(2, res16); + Assert.Equal(3, res17); + Assert.Equal("bike:important_bike, bike:1, bike:2", string.Join(", ", res18)); + delRes = db.KeyDelete("bikes:repairs"); + //REMOVE_END + + //STEP_START variadic + long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); + Console.WriteLine(res19); // >>> 3 + + long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" }); + Console.WriteLine(res20); // >>> 5 + + RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1); + Console.WriteLine(string.Join(", ", res21)); + // >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3" + //STEP_END + + //REMOVE_START + Assert.Equal(3, res19); + Assert.Equal(5, res20); + Assert.Equal("bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3", string.Join(", ", res21)); + delRes = db.KeyDelete("bikes:repairs"); + //REMOVE_END + + //STEP_START lpop_rpop + long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); + Console.WriteLine(res22); // >>> 3 + + RedisValue res23 = db.ListRightPop("bikes:repairs"); + Console.WriteLine(res23); // >>> "bike:3" + + RedisValue res24 = db.ListLeftPop("bikes:repairs"); + Console.WriteLine(res24); // >>> "bike:1" + + RedisValue res25 = db.ListRightPop("bikes:repairs"); + Console.WriteLine(res25); // >>> "bike:2" + + RedisValue res26 = db.ListRightPop("bikes:repairs"); + Console.WriteLine(res26); // >>> + //STEP_END + + //REMOVE_START + Assert.Equal(3, res22); + Assert.Equal("bike:3", res23); + Assert.Equal("bike:1", res24); + Assert.Equal("bike:2", res25); + Assert.Equal("", string.Join(", ", res26)); + //REMOVE_END + + //STEP_START ltrim + long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); + Console.WriteLine(res27); // >>> 5 + + db.ListTrim("bikes:repairs", 0, 2); + RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1); + Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3" + //STEP_END + + //REMOVE_START + Assert.Equal(5, res27); + Assert.Equal("bike:5, bike:4, bike:3", string.Join(", ", res28)); + delRes = db.KeyDelete("bikes:repairs"); + //REMOVE_END + + //STEP_START ltrim_end_of_list + long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); + Console.WriteLine(res29); // >>> 5 + + db.ListTrim("bikes:repairs", -3, -1); + RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1); + Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5" + //STEP_END + + //REMOVE_START + Assert.Equal(5, res29); + Assert.Equal("bike:3, bike:4, bike:5", string.Join(", ", res30)); + delRes = db.KeyDelete("bikes:repairs"); + //REMOVE_END + + //STEP_START brpop + long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" }); + Console.WriteLine(res31); // >>> 2 + + Tuple? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); + + if (res32 != null) + Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2" + + Tuple? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); + + if (res33 != null) + Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1" + + Tuple? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); + Console.WriteLine(res34); // >>> "Null" + //STEP_END + + //REMOVE_START + Assert.Equal(2, res31); + + if (res32 != null) + Assert.Equal("bikes:repairs -> bike:2", $"{res32.Item1} -> {res32.Item2}"); + if (res33 != null) + Assert.Equal("bikes:repairs -> bike:1", $"{res33.Item1} -> {res33.Item2}"); + + Assert.Null(res34); + //REMOVE_END + + //STEP_START rule_1 + bool res35 = db.KeyDelete("new_bikes"); + Console.WriteLine(res35); // >>> False + + long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); + Console.WriteLine(res36); // >>> 3 + //STEP_END + + //REMOVE_START + Assert.False(res35); + Assert.Equal(3, res36); + //REMOVE_END + + //STEP_START rule_1.1 + bool res37 = db.StringSet("new_bikes", "bike:1"); + Console.WriteLine(res37); // >>> True + + RedisType res38 = db.KeyType("new_bikes"); + Console.WriteLine(res38); // >>> RedisType.String + + try + { + long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" }); + } + catch (Exception e) + { + Console.WriteLine(e); + } + //STEP_END + + //REMOVE_START + Assert.True(res37); + Assert.Equal(RedisType.String, res38); + delRes = db.KeyDelete("new_bikes"); + //REMOVE_END + + //STEP_START rule_2 + long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); + Console.WriteLine(res40); // >>> 3 + + bool res41 = db.KeyExists("bikes:repairs"); + Console.WriteLine(res41); // >>> True + + RedisValue res42 = db.ListLeftPop("bikes:repairs"); + Console.WriteLine(res42); // >>> "bike:3" + + RedisValue res43 = db.ListLeftPop("bikes:repairs"); + Console.WriteLine(res43); // >>> "bike:2" + + RedisValue res44 = db.ListLeftPop("bikes:repairs"); + Console.WriteLine(res44); // >>> "bike:1" + + bool res45 = db.KeyExists("bikes:repairs"); + Console.WriteLine(res45); // >>> False + //STEP_END + + //REMOVE_START + Assert.Equal(3, res40); + Assert.True(res41); + Assert.Equal("bike:3", res42); + Assert.Equal("bike:2", res43); + Assert.Equal("bike:1", res44); + Assert.False(res45); + //REMOVE_END + + //STEP_START rule_3 + bool res46 = db.KeyDelete("bikes:repairs"); + Console.WriteLine(res46); // >>> False + + long res47 = db.ListLength("bikes:repairs"); + Console.WriteLine(res47); // >>> 0 + + RedisValue res48 = db.ListLeftPop("bikes:repairs"); + Console.WriteLine(res48); // >>> Null + //STEP_END + + //REMOVE_START + Assert.False(res46); + Assert.Equal(0, res47); + Assert.Equal(RedisValue.Null, res48); + //REMOVE_END + + //STEP_START ltrim.1 + long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); + Console.WriteLine(res49); // >>> 5 + + db.ListTrim("bikes:repairs", 0, 2); + RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1); + Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3" + //STEP_END + + //REMOVE_START + Assert.Equal(5, res49); + Assert.Equal("bike:5, bike:4, bike:3", string.Join(", ", res50)); + //REMOVE_END + + //HIDE_START + } +} +//HIDE_END From 859c39819688dddf4953b7caced50ca8a9e6e397 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Wed, 17 Jul 2024 10:53:28 +0100 Subject: [PATCH 02/10] DOC-2525 dotnet format changes --- tests/Doc/ListTutorial.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doc/ListTutorial.cs b/tests/Doc/ListTutorial.cs index d258b835..3c362edb 100644 --- a/tests/Doc/ListTutorial.cs +++ b/tests/Doc/ListTutorial.cs @@ -27,7 +27,7 @@ public void run() db.KeyDelete("{bikes}:repairs"); db.KeyDelete("{bikes}:finished"); //REMOVE_END - + //STEP_START queue long res1 = db.ListLeftPush("bikes:repairs", "bike:1"); From 8b344cc3017f1f0df54e2ed4d680d32e98e3a364 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Wed, 17 Jul 2024 11:11:22 +0100 Subject: [PATCH 03/10] DOC-2525 try changing all files to same namespace --- tests/Doc/SetsTutorial.cs | 2 +- tests/Doc/SortedSetExample.cs | 2 +- tests/Doc/StreamTutorial.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Doc/SetsTutorial.cs b/tests/Doc/SetsTutorial.cs index f324b513..916b9f0b 100644 --- a/tests/Doc/SetsTutorial.cs +++ b/tests/Doc/SetsTutorial.cs @@ -8,7 +8,7 @@ // HIDE_END //REMOVE_START -namespace NRedisStack.Doc; +namespace Doc; [Collection("DocsTests")] //REMOVE_END diff --git a/tests/Doc/SortedSetExample.cs b/tests/Doc/SortedSetExample.cs index 8ee3fc52..d7771d58 100644 --- a/tests/Doc/SortedSetExample.cs +++ b/tests/Doc/SortedSetExample.cs @@ -6,7 +6,7 @@ using StackExchange.Redis; //REMOVE_START -namespace NRedisStack.Doc; +namespace Doc; [Collection("DocsTests")] //REMOVE_END public class SortedSetExample diff --git a/tests/Doc/StreamTutorial.cs b/tests/Doc/StreamTutorial.cs index db37b928..d1bf89f8 100644 --- a/tests/Doc/StreamTutorial.cs +++ b/tests/Doc/StreamTutorial.cs @@ -8,7 +8,7 @@ // HIDE_END // REMOVE_START -namespace NRedisStack.Doc; +namespace Doc; [Collection("DocsTests")] // REMOVE_END From 230b9974de72241257d8cb36c5b687b4fa4a6190 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Wed, 17 Jul 2024 11:20:52 +0100 Subject: [PATCH 04/10] DOC-2525 added xunit.runner.json with parallelisation turned off --- tests/Doc/Doc.csproj | 5 +++++ tests/Doc/xunit.runner.json | 4 ++++ 2 files changed, 9 insertions(+) create mode 100644 tests/Doc/xunit.runner.json diff --git a/tests/Doc/Doc.csproj b/tests/Doc/Doc.csproj index 5f16c1c2..6ab0fa3b 100644 --- a/tests/Doc/Doc.csproj +++ b/tests/Doc/Doc.csproj @@ -22,4 +22,9 @@ + + + PreserveNewest + + \ No newline at end of file diff --git a/tests/Doc/xunit.runner.json b/tests/Doc/xunit.runner.json new file mode 100644 index 00000000..1adceda1 --- /dev/null +++ b/tests/Doc/xunit.runner.json @@ -0,0 +1,4 @@ +{ + "parallelizeAssembly": false, + "parallelizeTestCollections": false +} \ No newline at end of file From a48bfb12351317e431d1a6b2a3e9e2e275917741 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Wed, 17 Jul 2024 11:34:41 +0100 Subject: [PATCH 05/10] DOC-2525 alternative xunit.runner config --- tests/Doc/Doc.csproj | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/Doc/Doc.csproj b/tests/Doc/Doc.csproj index 6ab0fa3b..07ebe435 100644 --- a/tests/Doc/Doc.csproj +++ b/tests/Doc/Doc.csproj @@ -23,8 +23,6 @@ - - PreserveNewest - + \ No newline at end of file From 06746e2f360b7f1404e541ca04ba7add84c19314 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Wed, 17 Jul 2024 16:25:59 +0100 Subject: [PATCH 06/10] DOC-2525 try removing OutputType module --- tests/Doc/Doc.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doc/Doc.csproj b/tests/Doc/Doc.csproj index 07ebe435..fd48be28 100644 --- a/tests/Doc/Doc.csproj +++ b/tests/Doc/Doc.csproj @@ -7,7 +7,7 @@ enable latest false - Module + From b83b10875e5c36d71684eaac6d5675b51c7a8a59 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Mon, 5 Aug 2024 12:22:36 +0100 Subject: [PATCH 07/10] DOC-2525 fixed test problem with double string formatting --- tests/Doc/Geo_tutorial.cs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tests/Doc/Geo_tutorial.cs b/tests/Doc/Geo_tutorial.cs index 33fa330f..bf912fcc 100644 --- a/tests/Doc/Geo_tutorial.cs +++ b/tests/Doc/Geo_tutorial.cs @@ -65,18 +65,15 @@ public void run() // Tests for 'geosearch' step. // REMOVE_START Assert.Equal(3, res4.Length); - Assert.Equal( - "Member: 'station:1', distance: 0.0001, position: -122.27652043104172 37.80518485897756", - $"Member: '{res4[0].Member}', distance: {res4[0].Distance}, position: {res4[0].Position}" - ); - Assert.Equal( - "Member: 'station:2', distance: 0.8047, position: -122.26745992898941 37.80623423353753", - $"Member: '{res4[1].Member}', distance: {res4[1].Distance}, position: {res4[1].Position}" - ); - Assert.Equal( - "Member: 'station:3', distance: 2.6596, position: -122.24698394536972 37.81040384984464", - $"Member: '{res4[2].Member}', distance: {res4[2].Distance}, position: {res4[2].Position}" - ); + + Assert.Equal("station:1", res4[0].Member); + Assert.Equal(new GeoPosition(-122.27652, 37.805186), res4[0].Position); + + Assert.Equal("station:2", res4[1].Member); + Assert.Equal(new GeoPosition(-122.2674626, 37.8062344), res4[1].Position); + + Assert.Equal("station:3", res4[2].Member); + Assert.Equal(new GeoPosition(-122.2469854, 37.8104049), res4[2].Position); // REMOVE_END From ccd086aa6279544ce9a940cc4735ac403654b70e Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Mon, 5 Aug 2024 12:45:08 +0100 Subject: [PATCH 08/10] DOC-2525 another attempt to fix test string formatting --- tests/Doc/Geo_tutorial.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/Doc/Geo_tutorial.cs b/tests/Doc/Geo_tutorial.cs index bf912fcc..f21d5df9 100644 --- a/tests/Doc/Geo_tutorial.cs +++ b/tests/Doc/Geo_tutorial.cs @@ -67,13 +67,16 @@ public void run() Assert.Equal(3, res4.Length); Assert.Equal("station:1", res4[0].Member); - Assert.Equal(new GeoPosition(-122.27652, 37.805186), res4[0].Position); + GeoPosition pos1 = res4[0].Position ?? new GeoPosition(); + Assert.Equal("-122.27652, 37.80518", $"{pos1.Longitude:F5}, {pos1.Latitude:F5}"); Assert.Equal("station:2", res4[1].Member); - Assert.Equal(new GeoPosition(-122.2674626, 37.8062344), res4[1].Position); + GeoPosition pos2 = res4[1].Position ?? new GeoPosition(); + Assert.Equal("-122.26746, 37.80623", $"{pos2.Longitude:F5}, {pos2.Latitude:F5}"); Assert.Equal("station:3", res4[2].Member); - Assert.Equal(new GeoPosition(-122.2469854, 37.8104049), res4[2].Position); + GeoPosition pos3 = res4[2].Position ?? new GeoPosition(); + Assert.Equal("-122.24699, 37.81040", $"{pos3.Longitude:F5}, {pos3.Latitude:F5}"); // REMOVE_END From 7639ee454df20289be8a07e5a61dde556897d5fc Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Mon, 5 Aug 2024 12:55:45 +0100 Subject: [PATCH 09/10] DOC-2525 another minor test fix --- tests/Doc/Geo_tutorial.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doc/Geo_tutorial.cs b/tests/Doc/Geo_tutorial.cs index f21d5df9..94acb9cc 100644 --- a/tests/Doc/Geo_tutorial.cs +++ b/tests/Doc/Geo_tutorial.cs @@ -76,7 +76,7 @@ public void run() Assert.Equal("station:3", res4[2].Member); GeoPosition pos3 = res4[2].Position ?? new GeoPosition(); - Assert.Equal("-122.24699, 37.81040", $"{pos3.Longitude:F5}, {pos3.Latitude:F5}"); + Assert.Equal("-122.24698, 37.81040", $"{pos3.Longitude:F5}, {pos3.Latitude:F5}"); // REMOVE_END From 4537e2851e2a28092217db5a4b71a678ff91a7bb Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 6 Aug 2024 12:11:09 +0100 Subject: [PATCH 10/10] DOC-2525 removed xunit config --- tests/Doc/Doc.csproj | 3 --- tests/Doc/xunit.runner.json | 4 ---- 2 files changed, 7 deletions(-) delete mode 100644 tests/Doc/xunit.runner.json diff --git a/tests/Doc/Doc.csproj b/tests/Doc/Doc.csproj index fd48be28..2577990c 100644 --- a/tests/Doc/Doc.csproj +++ b/tests/Doc/Doc.csproj @@ -22,7 +22,4 @@ - - - \ No newline at end of file diff --git a/tests/Doc/xunit.runner.json b/tests/Doc/xunit.runner.json deleted file mode 100644 index 1adceda1..00000000 --- a/tests/Doc/xunit.runner.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "parallelizeAssembly": false, - "parallelizeTestCollections": false -} \ No newline at end of file