|
| 1 | +// EXAMPLE: cmds_set |
| 2 | +using StackExchange.Redis; |
| 3 | +// REMOVE_START |
| 4 | +using NRedisStack.Tests; |
| 5 | +namespace Doc; |
| 6 | + |
| 7 | +[Collection("DocsTests")] |
| 8 | +// REMOVE_END |
| 9 | + |
| 10 | +public class CmdsSetExample |
| 11 | +// REMOVE_START |
| 12 | +: AbstractNRedisStackTest, IDisposable |
| 13 | +// REMOVE_END |
| 14 | +{ |
| 15 | + // REMOVE_START |
| 16 | + public CmdsSetExample(EndpointsFixture fixture) : base(fixture) { } |
| 17 | + |
| 18 | + [SkippableFact] |
| 19 | + // REMOVE_END |
| 20 | + public void run() |
| 21 | + { |
| 22 | + // REMOVE_START |
| 23 | + // This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection |
| 24 | + SkipIfTargetConnectionDoesNotExist(EndpointsFixture.Env.Standalone); |
| 25 | + var _ = GetCleanDatabase(EndpointsFixture.Env.Standalone); |
| 26 | + // REMOVE_END |
| 27 | + var muxer = ConnectionMultiplexer.Connect("localhost:6379"); |
| 28 | + var db = muxer.GetDatabase(); |
| 29 | + // REMOVE_START |
| 30 | + // Clear any keys here before using them in tests. |
| 31 | + db.KeyDelete("myset"); |
| 32 | + // REMOVE_END |
| 33 | + |
| 34 | + // STEP_START sadd |
| 35 | + bool sAddResult1 = db.SetAdd("myset", "Hello"); |
| 36 | + Console.WriteLine(sAddResult1); // >>> True |
| 37 | + |
| 38 | + bool sAddResult2 = db.SetAdd("myset", "World"); |
| 39 | + Console.WriteLine(sAddResult2); // >>> True |
| 40 | + |
| 41 | + bool sAddResult3 = db.SetAdd("myset", "World"); |
| 42 | + Console.WriteLine(sAddResult2); // >>> False |
| 43 | + |
| 44 | + RedisValue[] sAddResult4 = db.SetMembers("myset"); |
| 45 | + Array.Sort(sAddResult4); |
| 46 | + Console.WriteLine(string.Join(", ", sAddResult4)); |
| 47 | + // >>> Hello, World |
| 48 | + // STEP_END |
| 49 | + // REMOVE_START |
| 50 | + Assert.True(sAddResult1); |
| 51 | + Assert.True(sAddResult2); |
| 52 | + Assert.False(sAddResult3); |
| 53 | + Assert.Equal("Hello, World", string.Join(", ", sAddResult4)); |
| 54 | + db.KeyDelete("myset"); |
| 55 | + // REMOVE_END |
| 56 | + |
| 57 | + // STEP_START smembers |
| 58 | + long sMembersResult1 = db.SetAdd( |
| 59 | + "myset", new RedisValue[] { "Hello", "World" } |
| 60 | + ); |
| 61 | + Console.WriteLine(sMembersResult1); // >>> 2 |
| 62 | + |
| 63 | + RedisValue[] sMembersResult2 = db.SetMembers("myset"); |
| 64 | + Array.Sort(sMembersResult2); |
| 65 | + Console.WriteLine(string.Join(", ", sMembersResult2)); |
| 66 | + // >>> Hello, World |
| 67 | + // STEP_END |
| 68 | + // REMOVE_START |
| 69 | + Assert.Equal(2, sMembersResult1); |
| 70 | + Assert.Equal("Hello, World", string.Join(", ", sMembersResult2)); |
| 71 | + // REMOVE_END |
| 72 | + } |
| 73 | +} |
0 commit comments