Skip to content

DOC-4493 added set command examples #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions tests/Doc/CmdsSetExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// EXAMPLE: cmds_set
using StackExchange.Redis;
// REMOVE_START
using NRedisStack.Tests;
namespace Doc;

[Collection("DocsTests")]
// REMOVE_END

public class CmdsSetExample
// REMOVE_START
: AbstractNRedisStackTest, IDisposable
// REMOVE_END
{
// REMOVE_START
public CmdsSetExample(EndpointsFixture fixture) : base(fixture) { }

[SkippableFact]
// REMOVE_END
public void run()
{
// REMOVE_START
// This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection
SkipIfTargetConnectionDoesNotExist(EndpointsFixture.Env.Standalone);
var _ = GetCleanDatabase(EndpointsFixture.Env.Standalone);
// REMOVE_END
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
// REMOVE_START
// Clear any keys here before using them in tests.
db.KeyDelete("myset");
// REMOVE_END

// STEP_START sadd
bool sAddResult1 = db.SetAdd("myset", "Hello");
Console.WriteLine(sAddResult1); // >>> True

bool sAddResult2 = db.SetAdd("myset", "World");
Console.WriteLine(sAddResult2); // >>> True

bool sAddResult3 = db.SetAdd("myset", "World");
Console.WriteLine(sAddResult2); // >>> False

RedisValue[] sAddResult4 = db.SetMembers("myset");
Array.Sort(sAddResult4);
Console.WriteLine(string.Join(", ", sAddResult4));
// >>> Hello, World
// STEP_END
// REMOVE_START
Assert.True(sAddResult1);
Assert.True(sAddResult2);
Assert.False(sAddResult3);
Assert.Equal("Hello, World", string.Join(", ", sAddResult4));
db.KeyDelete("myset");
// REMOVE_END

// STEP_START smembers
long sMembersResult1 = db.SetAdd(
"myset", new RedisValue[] { "Hello", "World" }
);
Console.WriteLine(sMembersResult1); // >>> 2

RedisValue[] sMembersResult2 = db.SetMembers("myset");
Array.Sort(sMembersResult2);
Console.WriteLine(string.Join(", ", sMembersResult2));
// >>> Hello, World
// STEP_END
// REMOVE_START
Assert.Equal(2, sMembersResult1);
Assert.Equal("Hello, World", string.Join(", ", sMembersResult2));
// REMOVE_END
}
}
Loading