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

DOC-4448 code examples for hgetall and hvals #370

Merged
merged 3 commits into from
Jan 17, 2025
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
52 changes: 42 additions & 10 deletions tests/Doc/CmdsHashExample.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// EXAMPLE: cmds_hash
// HIDE_START

using NRedisStack.Tests;
using StackExchange.Redis;

// HIDE_END

// REMOVE_START
using NRedisStack.Tests;

namespace Doc;
[Collection("DocsTests")]
// REMOVE_END
Expand All @@ -26,7 +24,6 @@ public void run()
//REMOVE_END
// HIDE_END


// STEP_START hget
bool res1 = db.HashSet("myhash", "field1", "foo");

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

RedisValue res3 = db.HashGet("myhash", "field2");
Console.WriteLine(res3); // >>> Null
// STEP_END

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

// Tests for 'hset' step.
// STEP_END
// REMOVE_START
Assert.True(res4);
Assert.Equal("Hello", res5);
Expand All @@ -79,9 +74,46 @@ public void run()
"field1: Hello, field2: Hi, field3: World",
string.Join(", ", res8.Select(h => $"{h.Name}: {h.Value}"))
);
db.KeyDelete("myhash");
// REMOVE_END

// STEP_START hgetall
db.HashSet("myhash",
new HashEntry[] {
new HashEntry("field1", "Hello"),
new HashEntry("field2", "World")
}
);

HashEntry[] hGetAllResult = db.HashGetAll("myhash");
Array.Sort(hGetAllResult, (a1, a2) => a1.Name.CompareTo(a2.Name));
Console.WriteLine(
string.Join(", ", hGetAllResult.Select(e => $"{e.Name}: {e.Value}"))
);
// >>> field1: Hello, field2: World
// STEP_END
// REMOVE_START
Assert.Equal("field1: Hello, field2: World", string.Join(", ", hGetAllResult.Select(e => $"{e.Name}: {e.Value}")));
db.KeyDelete("myhash");
// REMOVE_END

// STEP_START hvals
db.HashSet("myhash",
new HashEntry[] {
new HashEntry("field1", "Hello"),
new HashEntry("field2", "World")
}
);

RedisValue[] hValsResult = db.HashValues("myhash");
Array.Sort(hValsResult);
Console.WriteLine(string.Join(", ", hValsResult));
// >>> Hello, World
// STEP_END
// REMOVE_START
Assert.Equal("Hello, World", string.Join(", ", hValsResult));
// REMOVE_END
// HIDE_START
}
}
// HIDE_END

Loading