Skip to content

Commit 91fd35c

Browse files
authored
Add code snippet for Sorted Sets (#246)
Issue #214 Add a test under `Docs`, with code snippets that can be used in the online documentation for Sorted Sets. Co-authored-by: Gabriel Erzse <[email protected]>
1 parent fe18a3b commit 91fd35c

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

tests/Doc/SortedSetExample.cs

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
//EXAMPLE: ss_tutorial
2+
//HIDE_START
3+
//REMOVE_START
4+
using NRedisStack.Tests;
5+
//REMOVE_END
6+
using StackExchange.Redis;
7+
8+
//REMOVE_START
9+
namespace NRedisStack.Doc;
10+
[Collection("DocsTests")]
11+
//REMOVE_END
12+
public class SortedSetExample
13+
{
14+
//REMOVE_START
15+
[SkipIfRedis(Is.OSSCluster)]
16+
//REMOVE_END
17+
public void run()
18+
{
19+
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
20+
var db = muxer.GetDatabase();
21+
//REMOVE_START
22+
db.KeyDelete("racer_scores");
23+
//REMOVE_END
24+
//HIDE_END
25+
26+
//STEP_START zadd
27+
bool res1 = db.SortedSetAdd("racer_scores", "Norem", 10);
28+
Console.WriteLine(res1); // >>> True
29+
//REMOVE_START
30+
Assert.True(res1);
31+
//REMOVE_END
32+
33+
bool res2 = db.SortedSetAdd("racer_scores", "Castilla", 12);
34+
Console.WriteLine(res2); // >>> True
35+
//REMOVE_START
36+
Assert.True(res2);
37+
//REMOVE_END
38+
39+
long res3 = db.SortedSetAdd("racer_scores", new[]{
40+
new SortedSetEntry("Sam-Bodden", 8),
41+
new SortedSetEntry("Royce", 10),
42+
new SortedSetEntry("Ford", 6),
43+
new SortedSetEntry("Prickett", 14),
44+
new SortedSetEntry("Castilla", 12)
45+
});
46+
Console.WriteLine(res3); // >>> 4
47+
//REMOVE_START
48+
Assert.Equal(4, res3);
49+
//REMOVE_END
50+
//STEP_END
51+
52+
//STEP_START zrange
53+
RedisValue[] res4 = db.SortedSetRangeByRank("racer_scores", 0, -1);
54+
Console.WriteLine(string.Join(", ", res4)); // >>> Ford, Sam-Bodden, Norem, Royce, Castilla, Prickett
55+
//REMOVE_START
56+
Assert.Equal(6, res4.Length);
57+
Assert.Equal("Ford, Sam-Bodden, Norem, Royce, Castilla, Prickett", string.Join(", ", res4));
58+
//REMOVE_END
59+
60+
RedisValue[] res5 = db.SortedSetRangeByRank("racer_scores", 0, -1, Order.Descending);
61+
Console.WriteLine(string.Join(", ", res5)); // >>> Prickett, Castilla, Royce, Norem, Sam-Bodden, Ford
62+
//REMOVE_START
63+
Assert.Equal(6, res5.Length);
64+
Assert.Equal("Prickett, Castilla, Royce, Norem, Sam-Bodden, Ford", string.Join(", ", res5));
65+
//REMOVE_END
66+
//STEP_END
67+
68+
//STEP_START zrange_withscores
69+
SortedSetEntry[] res6 = db.SortedSetRangeByRankWithScores("racer_scores", 0, -1);
70+
Console.WriteLine(string.Join(", ", res6)); // >>> Ford: 6, Sam-Bodden: 8, Norem: 10, Royce: 10, Castilla: 12, Prickett: 14
71+
//REMOVE_START
72+
Assert.Equal(6, res6.Length);
73+
Assert.Equal("Ford: 6, Sam-Bodden: 8, Norem: 10, Royce: 10, Castilla: 12, Prickett: 14", string.Join(", ", res6));
74+
//REMOVE_END
75+
//STEP_END
76+
77+
//STEP_START zrangebyscore
78+
RedisValue[] res7 = db.SortedSetRangeByScore("racer_scores", double.NegativeInfinity, 10);
79+
Console.WriteLine(string.Join(", ", res7)); // >>> Ford, Sam-Bodden, Norem, Royce
80+
//REMOVE_START
81+
Assert.Equal(4, res7.Length);
82+
Assert.Equal("Ford, Sam-Bodden, Norem, Royce", string.Join(", ", res7));
83+
//REMOVE_END
84+
//STEP_END
85+
86+
//STEP_START zremrangebyscore
87+
bool res8 = db.SortedSetRemove("racer_scores", "Castilla");
88+
Console.WriteLine(res8); // >>> True
89+
//REMOVE_START
90+
Assert.True(res8);
91+
//REMOVE_END
92+
93+
long res9 = db.SortedSetRemoveRangeByScore("racer_scores", double.NegativeInfinity, 9);
94+
Console.WriteLine(res9); // >>> 2
95+
//REMOVE_START
96+
Assert.Equal(2, res9);
97+
//REMOVE_END
98+
99+
RedisValue[] res10 = db.SortedSetRangeByRank("racer_scores", 0, -1);
100+
Console.WriteLine(string.Join(", ", res10)); // >>> Norem, Royce, Prickett
101+
//REMOVE_START
102+
Assert.Equal(3, res10.Length);
103+
Assert.Equal("Norem, Royce, Prickett", string.Join(", ", res10));
104+
//REMOVE_END
105+
//STEP_END
106+
107+
//REMOVE_START
108+
Assert.Equal(3, db.SortedSetLength("racer_scores"));
109+
//REMOVE_END
110+
111+
//STEP_START zrank
112+
long? res11 = db.SortedSetRank("racer_scores", "Norem");
113+
Console.WriteLine(res11); // >>> 0
114+
//REMOVE_START
115+
Assert.Equal(0, res11);
116+
//REMOVE_END
117+
118+
long? res12 = db.SortedSetRank("racer_scores", "Norem", Order.Descending);
119+
Console.WriteLine(res12); // >>> 2
120+
//REMOVE_START
121+
Assert.Equal(2, res12);
122+
//REMOVE_END
123+
//STEP_END
124+
125+
//STEP_START zadd_lex
126+
long res13 = db.SortedSetAdd("racer_scores", new[] {
127+
new SortedSetEntry("Norem", 0),
128+
new SortedSetEntry("Sam-Bodden", 0),
129+
new SortedSetEntry("Royce", 0),
130+
new SortedSetEntry("Ford", 0),
131+
new SortedSetEntry("Prickett", 0),
132+
new SortedSetEntry("Castilla", 0)
133+
});
134+
Console.WriteLine(res13); // >>> 3
135+
//REMOVE_START
136+
Assert.Equal(3, res13);
137+
//REMOVE_END
138+
139+
RedisValue[] res14 = db.SortedSetRangeByRank("racer_scores", 0, -1);
140+
Console.WriteLine(string.Join(", ", res14)); // >>> Castilla, Ford, Norem, Pricket, Royce, Sam-Bodden
141+
//REMOVE_START
142+
Assert.Equal(6, res14.Length);
143+
Assert.Equal("Castilla, Ford, Norem, Prickett, Royce, Sam-Bodden", string.Join(", ", res14));
144+
//REMOVE_END
145+
146+
RedisValue[] res15 = db.SortedSetRangeByValue("racer_scores", "A", "L", Exclude.None);
147+
Console.WriteLine(string.Join(", ", res15)); // >>> Castilla, Ford
148+
//REMOVE_START
149+
Assert.Equal(2, res15.Length);
150+
Assert.Equal("Castilla, Ford", string.Join(", ", res15));
151+
//REMOVE_END
152+
//STEP_END
153+
154+
//STEP_START leaderboard
155+
bool res16 = db.SortedSetAdd("racer_scores", "Wood", 100);
156+
Console.WriteLine(res16); // >>> True
157+
//REMOVE_START
158+
Assert.True(res16);
159+
//REMOVE_END
160+
161+
bool res17 = db.SortedSetAdd("racer_scores", "Henshaw", 100);
162+
Console.WriteLine(res17); // >>> True
163+
//REMOVE_START
164+
Assert.True(res17);
165+
//REMOVE_END
166+
167+
bool res18 = db.SortedSetAdd("racer_scores", "Henshaw", 150);
168+
Console.WriteLine(res18); // >>> False
169+
//REMOVE_START
170+
Assert.False(res18);
171+
//REMOVE_END
172+
173+
double res19 = db.SortedSetIncrement("racer_scores", "Wood", 50);
174+
Console.WriteLine(res19); // >>> 150.0
175+
//REMOVE_START
176+
Assert.Equal(150, res19);
177+
//REMOVE_END
178+
179+
double res20 = db.SortedSetIncrement("racer_scores", "Henshaw", 50);
180+
Console.WriteLine(res20); // >>> 200.0
181+
//REMOVE_START
182+
Assert.Equal(200, res20);
183+
//REMOVE_END
184+
//STEP_END
185+
//HIDE_START
186+
}
187+
}
188+
//HIDE_END

0 commit comments

Comments
 (0)