Skip to content
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

set_tutorial snippets #197

Merged
merged 3 commits into from
Oct 22, 2023
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
81 changes: 81 additions & 0 deletions tests/Doc/StringSnippets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// EXAMPLE: set_tutorial
// HIDE_START

//REMOVE_START

using NRedisStack.Tests;
using StackExchange.Redis;

namespace NRedisStack.Doc;
[Collection("DocsTests")]
//REMOVE_END
public class StringSnippets
{
//REMOVE_START
[SkipIfRedis(Is.OSSCluster)]
//REMOVE_END
public void run()
{
var redis = ConnectionMultiplexer.Connect("localhost:6379");
var db = redis.GetDatabase();

//HIDE_END

//REMOVE_START
db.KeyDelete(new RedisKey[] { "bike:1", "bike:2", "bike:3", "total_crashes" });
//REMOVE_END

// STEP_START set_get
var res1 = db.StringSet("bike:1", "Deimos");
Console.WriteLine(res1); // true
var res2 = db.StringGet("bike:1");
Console.WriteLine(res2); // Deimos
// STEP_END

//REMOVE_START
Assert.True(res1);
Assert.Equal("Deimos", res2);
//REMOVE_END

//STEP_START setnx_xx
var res3 = db.StringSet("bike:1", "bike", when: When.NotExists);
Console.WriteLine(res3); // false
Console.WriteLine(db.StringGet("bike:1"));
var res4 = db.StringSet("bike:1", "bike", when: When.Exists);
Console.WriteLine(res4); // true
//STEP_END

//REMOVE_START
Assert.False(res3);
Assert.True(res4);
//REMOVE_END

//STEP_START mset
var res5 = db.StringSet(new KeyValuePair<RedisKey, RedisValue>[]
{
new ("bike:1", "Deimos"), new("bike:2", "Ares"), new("bike:3", "Vanth")
});
Console.WriteLine(res5);
var res6 = db.StringGet(new RedisKey[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res6);
//STEP_END

//REMOVE_START
Assert.True(res5);
Assert.Equal(new[] { "Deimos", "Ares", "Vanth" }, res6.Select(x => x.ToString()).ToArray());
//REMOVE_END

//STEP_START incr
db.StringSet("total_crashes", 0);
var res7 = db.StringIncrement("total_crashes");
Console.WriteLine(res7); // 1
var res8 = db.StringIncrement("total_crashes", 10);
Console.WriteLine(res8);
//STEP_END

//REMOVE_START
Assert.Equal(1, res7);
Assert.Equal(11, res8);
//REMOVE_END
}
}