Skip to content

Commit c54f639

Browse files
DOC-4448 code examples for hgetall and hvals (#370)
* DOC-4448 code examples for hgetall and hvals * DOC-4448 tidied final example formatting --------- Co-authored-by: atakavci <[email protected]>
1 parent 00c7895 commit c54f639

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

tests/Doc/CmdsHashExample.cs

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// EXAMPLE: cmds_hash
22
// HIDE_START
3-
4-
using NRedisStack.Tests;
53
using StackExchange.Redis;
6-
74
// HIDE_END
8-
95
// REMOVE_START
6+
using NRedisStack.Tests;
7+
108
namespace Doc;
119
[Collection("DocsTests")]
1210
// REMOVE_END
@@ -26,7 +24,6 @@ public void run()
2624
//REMOVE_END
2725
// HIDE_END
2826

29-
3027
// STEP_START hget
3128
bool res1 = db.HashSet("myhash", "field1", "foo");
3229

@@ -35,9 +32,8 @@ public void run()
3532

3633
RedisValue res3 = db.HashGet("myhash", "field2");
3734
Console.WriteLine(res3); // >>> Null
38-
// STEP_END
3935

40-
// Tests for 'hget' step.
36+
// STEP_END
4137
// REMOVE_START
4238
Assert.True(res1);
4339
Assert.Equal("foo", res2);
@@ -67,9 +63,8 @@ public void run()
6763
HashEntry[] res8 = db.HashGetAll("myhash");
6864
Console.WriteLine($"{string.Join(", ", res8.Select(h => $"{h.Name}: {h.Value}"))}");
6965
// >>> field1: Hello, field2: Hi, field3: World
70-
// STEP_END
7166

72-
// Tests for 'hset' step.
67+
// STEP_END
7368
// REMOVE_START
7469
Assert.True(res4);
7570
Assert.Equal("Hello", res5);
@@ -79,9 +74,46 @@ public void run()
7974
"field1: Hello, field2: Hi, field3: World",
8075
string.Join(", ", res8.Select(h => $"{h.Name}: {h.Value}"))
8176
);
77+
db.KeyDelete("myhash");
78+
// REMOVE_END
79+
80+
// STEP_START hgetall
81+
db.HashSet("myhash",
82+
new HashEntry[] {
83+
new HashEntry("field1", "Hello"),
84+
new HashEntry("field2", "World")
85+
}
86+
);
87+
88+
HashEntry[] hGetAllResult = db.HashGetAll("myhash");
89+
Array.Sort(hGetAllResult, (a1, a2) => a1.Name.CompareTo(a2.Name));
90+
Console.WriteLine(
91+
string.Join(", ", hGetAllResult.Select(e => $"{e.Name}: {e.Value}"))
92+
);
93+
// >>> field1: Hello, field2: World
94+
// STEP_END
95+
// REMOVE_START
96+
Assert.Equal("field1: Hello, field2: World", string.Join(", ", hGetAllResult.Select(e => $"{e.Name}: {e.Value}")));
97+
db.KeyDelete("myhash");
98+
// REMOVE_END
99+
100+
// STEP_START hvals
101+
db.HashSet("myhash",
102+
new HashEntry[] {
103+
new HashEntry("field1", "Hello"),
104+
new HashEntry("field2", "World")
105+
}
106+
);
107+
108+
RedisValue[] hValsResult = db.HashValues("myhash");
109+
Array.Sort(hValsResult);
110+
Console.WriteLine(string.Join(", ", hValsResult));
111+
// >>> Hello, World
112+
// STEP_END
113+
// REMOVE_START
114+
Assert.Equal("Hello, World", string.Join(", ", hValsResult));
82115
// REMOVE_END
83116
// HIDE_START
84117
}
85118
}
86119
// HIDE_END
87-

0 commit comments

Comments
 (0)