diff --git a/RedisStudyTest/RedisHashStudyTest.cs b/RedisStudyTest/RedisHashStudyTest.cs
index d90b408..23c6bcd 100644
--- a/RedisStudyTest/RedisHashStudyTest.cs
+++ b/RedisStudyTest/RedisHashStudyTest.cs
@@ -237,29 +237,53 @@ namespace RedisStudyTest
}
///
- /// 特例测试:给key为空的哈希 设置字段
- /// 结 果:添加或更新操作能成功,但是字段值插入不进去。
+ /// 特例测试:给key为string.Empty的哈希 设置字段
+ /// 结 果:添加或更新操作正常。只是"Redis Desktop Manager"管理工具显示空白
///
[Trait("RedisHash", "HashSet")]
[Fact]
public void HashSetForEmptyKeyTest()
{
- Assert.True(redisHashStudy.HashSet(string.Empty, "Name", "wanggaofeng"));
+ //清理遗留数据
+ redisDatabase.KeyDelete(string.Empty);
+
+ var result = redisHashStudy.HashSet(string.Empty, "Name", "wanggaofeng");
+ Assert.True(result);
+
+ var fieldValue = redisDatabase.HashGet(string.Empty, "Name");
+ Assert.Equal("wanggaofeng", fieldValue);
+
+ result = redisHashStudy.HashSet(string.Empty, "Name", "wangxiangqian");
+ Assert.False(result);
+
+ fieldValue = redisDatabase.HashGet(string.Empty, "Name");
+ Assert.Equal("wangxiangqian", fieldValue);
+
+ //清理
redisDatabase.KeyDelete(string.Empty);
}
///
/// 特例测试:给字段名为空串的哈希 设置字段
- /// 结 果:添加或更新操作正常,只是字段键名为""
+ /// 结 果:添加或更新操作正常。只是"Redis Desktop Manager"管理工具显示空白
///
[Trait("RedisHash", "HashSet")]
[Fact]
public void HashSetForEmptyFieldTest()
{
- Assert.True(redisHashStudy.HashSet(defaultStudentHashKey, "", defaultStudent.Id));
- Assert.False(redisHashStudy.HashSet(defaultStudentHashKey, "", defaultStudent.Id + 1));
+ //添加正常
+ var result = redisHashStudy.HashSet(defaultStudentHashKey, "", defaultStudent.Id);
+ Assert.True(result);
- redisDatabase.KeyDelete(defaultStudentHashKey);
+ var fieldValue = redisHashStudy.HashGet(defaultStudentHashKey, "");
+ Assert.Equal(defaultStudent.Id, fieldValue);
+
+ //更新正常
+ result = redisHashStudy.HashSet(defaultStudentHashKey, "", defaultStudent.Id + 1);
+ Assert.False(result);
+
+ fieldValue = redisHashStudy.HashGet(defaultStudentHashKey, "");
+ Assert.Equal(defaultStudent.Id+1, fieldValue);
}
#endregion
diff --git a/RedisStudyTest/RedisStringStudyTest.cs b/RedisStudyTest/RedisStringStudyTest.cs
index e3495f0..24b5850 100644
--- a/RedisStudyTest/RedisStringStudyTest.cs
+++ b/RedisStudyTest/RedisStringStudyTest.cs
@@ -4,6 +4,9 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Serialization;
+
using StackExchange.Redis;
using Xunit;
using Xunit.Extensions;
@@ -40,12 +43,152 @@ namespace RedisStudyTest
#region StringSet
+ ///
+ /// 键名为空串时,操作正常
+ /// 只是"Redis Desktop Manager"管理工具显示空白
+ ///
[Fact]
- public void StringSet()
+ public void StringSetKeyIsEmptyTest()
{
+ //添加正常
+ var setResult = redisDatabase.StringSet(string.Empty, "Redis String key is string.empty");
+ Assert.True(setResult);
+
+ var getValue = redisDatabase.StringGet(string.Empty);
+ Assert.Equal("Redis String key is string.empty", getValue);
+
+ //更新正常
+ setResult = redisDatabase.StringSet(string.Empty, "Updage empty key value to new values");
+ Assert.True(setResult);
+
+ getValue = redisDatabase.StringGet(string.Empty);
+ Assert.Equal("Updage empty key value to new values", getValue);
+
+ //清理
+ redisDatabase.KeyDelete(string.Empty);
+ }
+
+ ///
+ /// 设置一个键值对
+ ///
+ [Fact]
+ public void StringSetOneKeyValueTest()
+ {
+ //不存在时,添加
var setResult = redisStringStudy.StringSet(defaultRedisKey, "xUnit", defaultExpiry);
+ Assert.True(setResult);
+
+ var redisValue = redisStringStudy.StringGet(defaultRedisKey);
+ Assert.Equal("xUnit", redisValue);
+ //存在时,更新
+ var setResult2 = redisStringStudy.StringSet(defaultRedisKey, "xUnit update", defaultExpiry);
+ Assert.True(setResult2);
+
+ var redisValue2 = redisStringStudy.StringGet(defaultRedisKey);
+ Assert.Equal("xUnit update", redisValue2);
+ }
+
+ ///
+ /// 设置值为字符串
+ ///
+ [Fact]
+ public void StringSetForString()
+ {
+ var setResult = redisStringStudy.StringSet(defaultRedisKey, "I am string", defaultExpiry);
+ Assert.True(setResult);
+
+ var redisValue = redisStringStudy.StringGet(defaultRedisKey);
+ Assert.Equal("I am string", redisValue);
+ }
+
+ ///
+ /// 设置值为数字
+ ///
+ [Fact]
+ public void StringSetForNumber()
+ {
+ var setResult = redisStringStudy.StringSet(defaultRedisKey, 20000, defaultExpiry);
Assert.True(setResult);
+
+ var redisValue = redisStringStudy.StringGet(defaultRedisKey);
+ Assert.Equal(20000, redisValue);
+ }
+
+ ///
+ /// 设置值为json
+ ///
+ [Fact]
+ public void StringSetForJson()
+ {
+ var stuent = new
+ {
+ Id=1,
+ Name="王高峰",
+ Age=30,
+ Number="05121098"
+ };
+
+ var json = JsonConvert.SerializeObject(stuent);
+
+ var setResult = redisStringStudy.StringSet(defaultRedisKey, json, defaultExpiry);
+ Assert.True(setResult);
+
+ //Newtonsoft.Json 反序列化为匿名类型
+ var redisValue = redisStringStudy.StringGet(defaultRedisKey);
+ var anonymousType = JsonConvert.DeserializeAnonymousType(redisValue, stuent);
+ Assert.True(anonymousType.Id == stuent.Id && anonymousType.Name == stuent.Name && anonymousType.Age == stuent.Age && anonymousType.Number == stuent.Number);
+
+ }
+
+ ///
+ /// 设置值为Bit
+ ///
+ [Fact]
+ public void StringSetForBit()
+ {
+ var setResult = redisStringStudy.StringSet(defaultRedisKey, Encoding.UTF8.GetBytes("我要转变为二进制数据,再存储"), defaultExpiry);
+ Assert.True(setResult);
+
+ var redisValue = (byte[])redisStringStudy.StringGet(defaultRedisKey);
+
+ Assert.Equal("我要转变为二进制数据,再存储", Encoding.UTF8.GetString(redisValue, 0, redisValue.Length));
+ }
+
+ ///
+ /// 设置一组键值对
+ ///
+ [Fact]
+ public void StringSetGroupKeyValueTest()
+ {
+ //添加
+ KeyValuePair[] keyValuePairs = new KeyValuePair[]
+ {
+ new KeyValuePair("RedisStudy:String:1","group1"),
+ new KeyValuePair("RedisStudy:String:2",20),
+ new KeyValuePair("RedisStudy:String:3","{\"Id\":1,\"Name\":\"wanggaofeng\",\"Age\":20}"),
+ new KeyValuePair("RedisStudy:String:3",BitConverter.GetBytes(123)),
+ };
+
+ var setResult = redisStringStudy.StringSet(keyValuePairs);
+ Assert.True(setResult);
+
+ //更新
+ keyValuePairs = new KeyValuePair[]
+ {
+ new KeyValuePair("RedisStudy:String:1","group3"),
+ new KeyValuePair("RedisStudy:String:2",20),
+ new KeyValuePair("RedisStudy:String:3","{\"Id\":1,\"Name\":\"wanggaofeng\",\"Age\":20}"),
+ new KeyValuePair("RedisStudy:String:3",BitConverter.GetBytes(200)),
+ };
+ setResult = redisStringStudy.StringSet(keyValuePairs);
+ Assert.True(setResult);
+
+ //清理
+ redisDatabase.KeyDelete("RedisStudy:String:1");
+ redisDatabase.KeyDelete("RedisStudy:String:2");
+ redisDatabase.KeyDelete("RedisStudy:String:3");
+
}
#endregion
diff --git a/RedisStudyTest/RedisStudyTest.csproj b/RedisStudyTest/RedisStudyTest.csproj
index 71fec17..f0af8da 100644
--- a/RedisStudyTest/RedisStudyTest.csproj
+++ b/RedisStudyTest/RedisStudyTest.csproj
@@ -41,6 +41,9 @@
..\packages\Moq.4.8.2\lib\net45\Moq.dll
+
+ ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll
+
..\packages\StackExchange.Redis.1.2.6\lib\net46\StackExchange.Redis.dll
diff --git a/RedisStudyTest/packages.config b/RedisStudyTest/packages.config
index f0a07ae..359d5f6 100644
--- a/RedisStudyTest/packages.config
+++ b/RedisStudyTest/packages.config
@@ -2,6 +2,7 @@
+