Skip to content

Commit

Permalink
DOC-4093 HGET/HSET command code samples (redis#330)
Browse files Browse the repository at this point in the history
* DOC-4093 added hash examples

* DOC-4093 dotnet format changes
  • Loading branch information
andy-stark-redis authored Aug 28, 2024
1 parent 0ae51b4 commit 4a503d2
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions tests/Doc/CmdsHashExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// EXAMPLE: cmds_hash
// HIDE_START

using NRedisStack.Tests;
using StackExchange.Redis;

// HIDE_END

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

// HIDE_START
public class CmdsHashExample
{

[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("myhash");
//REMOVE_END
// HIDE_END


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

RedisValue res2 = db.HashGet("myhash", "field1");
Console.WriteLine(res2); // >>> foo

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

// Tests for 'hget' step.
// REMOVE_START
Assert.True(res1);
Assert.Equal("foo", res2);
Assert.Equal(RedisValue.Null, res3);
db.KeyDelete("myhash");
// REMOVE_END

// STEP_START hset
bool res4 = db.HashSet("myhash", "field1", "Hello");
RedisValue res5 = db.HashGet("myhash", "field1");
Console.WriteLine(res5); // >>> Hello

db.HashSet(
"myhash",
new HashEntry[] {
new HashEntry("field2", "Hi"),
new HashEntry("field3", "World")
}
);

RedisValue res6 = db.HashGet("myhash", "field2");
Console.WriteLine(res6); // >>> Hi

RedisValue res7 = db.HashGet("myhash", "field3");
Console.WriteLine(res7); // >>> World

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.
// REMOVE_START
Assert.True(res4);
Assert.Equal("Hello", res5);
Assert.Equal("Hi", res6);
Assert.Equal("World", res7);
Assert.Equal(
"field1: Hello, field2: Hi, field3: World",
string.Join(", ", res8.Select(h => $"{h.Name}: {h.Value}"))
);
// REMOVE_END
// HIDE_START
}
}
// HIDE_END

0 comments on commit 4a503d2

Please sign in to comment.