Skip to content

Commit

Permalink
DOC-2857 added doc code sample for TopK (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-stark-redis committed May 14, 2024
1 parent d571924 commit 6a8e0b2
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions tests/Doc/Topk_tutorial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// EXAMPLE: topk_tutorial
// HIDE_START

using NRedisStack.RedisStackCommands;
using NRedisStack.Tests;
using StackExchange.Redis;

// HIDE_END

// REMOVE_START
namespace Doc;
[Collection("DocsTests")]
// REMOVE_END

// HIDE_START
public class Topk_tutorial
{

[SkipIfRedis(Is.OSSCluster)]
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
//REMOVE_START
// Clear any keys here before using them in tests.
db.KeyDelete("bikes:keywords");
//REMOVE_END
// HIDE_END


// STEP_START topk
bool res1 = db.TOPK().Reserve("bikes:keywords", 5, 2000, 7, 0.925);
Console.WriteLine(res1); // >>> True

RedisResult[]? res2 = db.TOPK().Add("bikes:keywords",
"store",
"seat",
"handlebars",
"handles",
"pedals",
"tires",
"store",
"seat"
);

if (res2 is not null)
{
Console.WriteLine(string.Join(", ", string.Join(", ", res2.Select(r => $"{(r.IsNull ? "Null" : r)}"))));
// >>> Null, Null, Null, Null, Null, handlebars, Null, Null
}

RedisResult[] res3 = db.TOPK().List("bikes:keywords");

if (res3 is not null)
{
Console.WriteLine(string.Join(", ", string.Join(", ", res3.Select(r => $"{(r.IsNull ? "Null" : r)}"))));
// >>> store, seat, pedals, tires, handles
}

bool[] res4 = db.TOPK().Query("bikes:keywords", "store", "handlebars");
Console.WriteLine(string.Join(", ", res4)); // >>> True, False
// STEP_END

// Tests for 'topk' step.
// REMOVE_START
Assert.True(res1);
Assert.NotNull(res2);
Assert.Equal(
"Null, Null, Null, Null, Null, handlebars, Null, Null",
string.Join(", ", string.Join(", ", res2.Select(r => $"{(r.IsNull ? "Null" : r)}")))
);
Assert.NotNull(res3);
Assert.Equal(
"store, seat, pedals, tires, handles",
string.Join(", ", string.Join(", ", res3.Select(r => $"{(r.IsNull ? "Null" : r)}")))
);
Assert.Equal("True, False", string.Join(", ", res4));
// REMOVE_END


// HIDE_START
}
}
// HIDE_END

0 comments on commit 6a8e0b2

Please sign in to comment.