-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathTopk_tutorial.cs
96 lines (80 loc) · 2.78 KB
/
Topk_tutorial.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 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
// REMOVE_START
: AbstractNRedisStackTest, IDisposable
// REMOVE_END
{
// REMOVE_START
public Topk_tutorial(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("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