|
| 1 | +// EXAMPLE: cmds_list |
| 2 | +// HIDE_START |
| 3 | + |
| 4 | +using NRedisStack.Tests; |
| 5 | +using StackExchange.Redis; |
| 6 | + |
| 7 | +// HIDE_END |
| 8 | + |
| 9 | +// REMOVE_START |
| 10 | +namespace Doc; |
| 11 | +[Collection("DocsTests")] |
| 12 | +// REMOVE_END |
| 13 | + |
| 14 | +// HIDE_START |
| 15 | +public class CmdsListExample |
| 16 | +// REMOVE_START |
| 17 | +: AbstractNRedisStackTest, IDisposable |
| 18 | +// REMOVE_END |
| 19 | +{ |
| 20 | + // REMOVE_START |
| 21 | + public CmdsListExample(EndpointsFixture fixture) : base(fixture) { } |
| 22 | + |
| 23 | + [SkippableFact] |
| 24 | + // REMOVE_END |
| 25 | + public void run() |
| 26 | + { |
| 27 | + //REMOVE_START |
| 28 | + // This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection |
| 29 | + SkipIfTargetConnectionDoesNotExist(EndpointsFixture.Env.Standalone); |
| 30 | + var _ = GetCleanDatabase(EndpointsFixture.Env.Standalone); |
| 31 | + //REMOVE_END |
| 32 | + var muxer = ConnectionMultiplexer.Connect("localhost:6379"); |
| 33 | + var db = muxer.GetDatabase(); |
| 34 | + //REMOVE_START |
| 35 | + // Clear any keys here before using them in tests. |
| 36 | + db.KeyDelete("mylist"); |
| 37 | + //REMOVE_END |
| 38 | + // HIDE_END |
| 39 | + |
| 40 | + // STEP_START llen |
| 41 | + long lLenResult1 = db.ListLeftPush("mylist", "World"); |
| 42 | + Console.WriteLine(lLenResult1); // >>> 1 |
| 43 | + |
| 44 | + long lLenResult2 = db.ListLeftPush("mylist", "Hello"); |
| 45 | + Console.WriteLine(lLenResult2); // >>> 2 |
| 46 | + |
| 47 | + long lLenResult3 = db.ListLength("mylist"); |
| 48 | + Console.WriteLine(lLenResult3); // >>> 2 |
| 49 | + // STEP_END |
| 50 | + |
| 51 | + // Tests for 'llen' step. |
| 52 | + // REMOVE_START |
| 53 | + Assert.Equal(1, lLenResult1); |
| 54 | + Assert.Equal(2, lLenResult2); |
| 55 | + Assert.Equal(2, lLenResult3); |
| 56 | + db.KeyDelete("mylist"); |
| 57 | + // REMOVE_END |
| 58 | + |
| 59 | + // STEP_START lpop |
| 60 | + long lPopResult1 = db.ListRightPush("mylist", new RedisValue[] { "one", "two", "three", "four", "five" }); |
| 61 | + Console.WriteLine(lPopResult1); // >>> 5 |
| 62 | + |
| 63 | + RedisValue lPopResult2 = db.ListLeftPop("mylist"); |
| 64 | + Console.WriteLine(lPopResult2); // >>> one |
| 65 | + |
| 66 | + RedisValue[] lPopResult3 = db.ListLeftPop("mylist", 2); |
| 67 | + Console.WriteLine($"[{string.Join(", ", lPopResult3)}]"); |
| 68 | + // >>> [two, three] |
| 69 | + |
| 70 | + RedisValue[] lPopResult4 = db.ListRange("mylist", 0, -1); |
| 71 | + Console.WriteLine($"[{string.Join(", ", lPopResult4)}]"); |
| 72 | + // >>> [four, five] |
| 73 | + // STEP_END |
| 74 | + |
| 75 | + // Tests for 'lpop' step. |
| 76 | + // REMOVE_START |
| 77 | + Assert.Equal(5, lPopResult1); |
| 78 | + Assert.Equal("one", lPopResult2); |
| 79 | + Assert.Equal("[two, three]", $"[{string.Join(", ", lPopResult3)}]"); |
| 80 | + Assert.Equal("[four, five]", $"[{string.Join(", ", lPopResult4)}]"); |
| 81 | + db.KeyDelete("mylist"); |
| 82 | + // REMOVE_END |
| 83 | + |
| 84 | + // STEP_START lpush |
| 85 | + long lPushResult1 = db.ListLeftPush("mylist", "World"); |
| 86 | + Console.WriteLine(lPushResult1); // >>> 1 |
| 87 | + |
| 88 | + long lPushResult2 = db.ListLeftPush("mylist", "Hello"); |
| 89 | + Console.WriteLine(lPushResult2); // >>> 2 |
| 90 | + |
| 91 | + RedisValue[] lPushResult3 = db.ListRange("mylist", 0, -1); |
| 92 | + Console.WriteLine($"[{string.Join(", ", lPushResult3)}]"); |
| 93 | + // >>> [Hello, World] |
| 94 | + // STEP_END |
| 95 | + |
| 96 | + // Tests for 'lpush' step. |
| 97 | + // REMOVE_START |
| 98 | + Assert.Equal(1, lPushResult1); |
| 99 | + Assert.Equal(2, lPushResult2); |
| 100 | + Assert.Equal("[Hello, World]", $"[{string.Join(", ", lPushResult3)}]"); |
| 101 | + db.KeyDelete("mylist"); |
| 102 | + // REMOVE_END |
| 103 | + |
| 104 | + // STEP_START lrange |
| 105 | + long lRangeResult1 = db.ListRightPush("mylist", new RedisValue[] { "one", "two", "three" }); |
| 106 | + Console.WriteLine(lRangeResult1); // >>> 3 |
| 107 | + |
| 108 | + RedisValue[] lRangeResult2 = db.ListRange("mylist", 0, 0); |
| 109 | + Console.WriteLine($"[{string.Join(", ", lRangeResult2)}]"); |
| 110 | + // >>> [one] |
| 111 | + |
| 112 | + RedisValue[] lRangeResult3 = db.ListRange("mylist", -3, 2); |
| 113 | + Console.WriteLine($"[{string.Join(", ", lRangeResult3)}]"); |
| 114 | + // >>> [one, two, three] |
| 115 | + |
| 116 | + RedisValue[] lRangeResult4 = db.ListRange("mylist", -100, 100); |
| 117 | + Console.WriteLine($"[{string.Join(", ", lRangeResult4)}]"); |
| 118 | + // >>> [one, two, three] |
| 119 | + |
| 120 | + RedisValue[] lRangeResult5 = db.ListRange("mylist", 5, 10); |
| 121 | + Console.WriteLine($"[{string.Join(", ", lRangeResult5)}]"); |
| 122 | + // >>> [] |
| 123 | + // STEP_END |
| 124 | + |
| 125 | + // Tests for 'lrange' step. |
| 126 | + // REMOVE_START |
| 127 | + Assert.Equal(3, lRangeResult1); |
| 128 | + Assert.Equal("[one]", $"[{string.Join(", ", lRangeResult2)}]"); |
| 129 | + Assert.Equal("[one, two, three]", $"[{string.Join(", ", lRangeResult3)}]"); |
| 130 | + Assert.Equal("[one, two, three]", $"[{string.Join(", ", lRangeResult4)}]"); |
| 131 | + Assert.Equal("[]", $"[{string.Join(", ", lRangeResult5)}]"); |
| 132 | + db.KeyDelete("mylist"); |
| 133 | + // REMOVE_END |
| 134 | + |
| 135 | + // STEP_START rpop |
| 136 | + long rPopResult1 = db.ListRightPush("mylist", new RedisValue[] { "one", "two", "three", "four", "five" }); |
| 137 | + Console.WriteLine(rPopResult1); // >>> 5 |
| 138 | + |
| 139 | + RedisValue rPopResult2 = db.ListRightPop("mylist"); |
| 140 | + Console.WriteLine(rPopResult2); // >>> five |
| 141 | + |
| 142 | + RedisValue[] rPopResult3 = db.ListRightPop("mylist", 2); |
| 143 | + Console.WriteLine($"[{string.Join(", ", rPopResult3)}]"); |
| 144 | + // >>> [four, three] |
| 145 | + |
| 146 | + RedisValue[] rPopResult4 = db.ListRange("mylist", 0, -1); |
| 147 | + Console.WriteLine($"[{string.Join(", ", rPopResult4)}]"); |
| 148 | + // >>> [one, two] |
| 149 | + // STEP_END |
| 150 | + |
| 151 | + // Tests for 'rpop' step. |
| 152 | + // REMOVE_START |
| 153 | + Assert.Equal(5, rPopResult1); |
| 154 | + Assert.Equal("five", rPopResult2); |
| 155 | + Assert.Equal("[four, three]", $"[{string.Join(", ", rPopResult3)}]"); |
| 156 | + Assert.Equal("[one, two]", $"[{string.Join(", ", rPopResult4)}]"); |
| 157 | + db.KeyDelete("mylist"); |
| 158 | + // REMOVE_END |
| 159 | + |
| 160 | + // STEP_START rpush |
| 161 | + long rPushResult1 = db.ListRightPush("mylist", "hello"); |
| 162 | + Console.WriteLine(rPushResult1); // >>> 1 |
| 163 | + |
| 164 | + long rPushResult2 = db.ListRightPush("mylist", "world"); |
| 165 | + Console.WriteLine(rPushResult2); // >>> 2 |
| 166 | + |
| 167 | + RedisValue[] rPushResult3 = db.ListRange("mylist", 0, -1); |
| 168 | + Console.WriteLine($"[{string.Join(", ", rPushResult3)}]"); |
| 169 | + // >>> [hello, world] |
| 170 | + // STEP_END |
| 171 | + |
| 172 | + // Tests for 'rpush' step. |
| 173 | + // REMOVE_START |
| 174 | + Assert.Equal(1, rPushResult1); |
| 175 | + Assert.Equal(2, rPushResult2); |
| 176 | + Assert.Equal("[hello, world]", $"[{string.Join(", ", rPushResult3)}]"); |
| 177 | + db.KeyDelete("mylist"); |
| 178 | + // REMOVE_END |
| 179 | + |
| 180 | + // HIDE_START |
| 181 | + } |
| 182 | +} |
| 183 | +// HIDE_END |
0 commit comments