RedisClientTests
StringsTests.cs
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;
namespace FreeRedis.Tests.RedisClientTests
{
public clast StringsTests : TestBase
{
[Fact]
public void Append()
{
var key = "TestAppend_null";
cli.Set(key, String);
cli.Append(key, Null);
astert.Equal(cli.Get(key), String);
key = "TestAppend_string";
cli.Set(key, String);
cli.Append(key, String);
astert.Equal(cli.Get(key), String + String);
var ms = new MemoryStream();
cli.Get(key, ms);
astert.Equal(Encoding.UTF8.GetString(ms.ToArray()), String + String);
ms.Close();
key = "TestAppend_bytes";
cli.Set(key, Bytes);
cli.Append(key, Bytes);
astert.Equal(Convert.ToBase64String(cli.Get(key)), Convert.ToBase64String(Bytes.Concat(Bytes).ToArray()));
}
[Fact]
public void BitCount()
{
cli.Del("TestBitCount");
var key = "TestBitCount";
cli.SetBit(key, 100, true);
cli.SetBit(key, 90, true);
cli.SetBit(key, 80, true);
astert.Equal(3, cli.BitCount(key, 0, 101));
astert.Equal(3, cli.BitCount(key, 0, 100));
astert.Equal(3, cli.BitCount(key, 0, 99));
astert.Equal(3, cli.BitCount(key, 0, 60));
}
[Fact]
public void BitOp()
{
cli.Del("BitOp1", "BitOp2");
cli.SetBit("BitOp1", 100, true);
cli.SetBit("BitOp2", 100, true);
var r1 = cli.BitOp(BitOpOperation.and, "BitOp3", "BitOp1", "BitOp2");
}
[Fact]
public void BitPos()
{
cli.Del("BitPos1");
cli.SetBit("BitPos1", 100, true);
astert.Equal(100, cli.BitPos("BitPos1", true));
astert.Equal(-1, cli.BitPos("BitPos1", true, 1, 100));
}
[Fact]
public void Decr()
{
var key = Guid.NewGuid().ToString();
astert.Equal(-1, cli.Decr(key));
}
[Fact]
public void DecrBy()
{
var key = Guid.NewGuid().ToString();
astert.Equal(-10, cli.DecrBy(key, 10));
}
[Fact]
public void Get()
{
var key = "TestGet_null";
cli.Set(key, Null);
astert.Equal((cli.Get(key))?.ToString() ?? "", Null?.ToString() ?? "");
key = "TestGet_string";
cli.Set(key, String);
astert.Equal(cli.Get(key), String);
key = "TestGet_bytes";
cli.Set(key, Bytes);
astert.Equal(cli.Get(key), Bytes);
key = "TestGet_clast";
cli.Set(key, Clast);
astert.Equal(JsonConvert.SerializeObject(cli.Get(key)), JsonConvert.SerializeObject(Clast));
key = "TestGet_clastArray";
cli.Set(key, new[] { Clast, Clast });
astert.Equal(2, cli.Get(key)?.Length);
astert.Equal(JsonConvert.SerializeObject(cli.Get(key)?.First()), JsonConvert.SerializeObject(Clast));
astert.Equal(JsonConvert.SerializeObject(cli.Get(key)?.Last()), JsonConvert.SerializeObject(Clast));
}
[Fact]
public void GetBit()
{
cli.Del("GetBit1");
cli.SetBit("GetBit1", 100, true);
astert.False(cli.GetBit("GetBit1", 10));
astert.True(cli.GetBit("GetBit1", 100));
}
[Fact]
public void GetRange()
{
var key = "TestGetRange_null";
cli.Set(key, Null);
astert.Equal("", cli.GetRange(key, 10, 20));
key = "TestGetRange_string";
cli.Set(key, "abcdefg");
astert.Equal("cde", cli.GetRange(key, 2, 4));
astert.Equal("abcdefg", cli.GetRange(key, 0, -1));
key = "TestGetRange_bytes";
cli.Set(key, Bytes);
astert.Equal(Bytes.astpan(2, 3).ToArray(), cli.GetRange(key, 2, 4));
astert.Equal(Bytes, cli.GetRange(key, 0, -1));
}
[Fact]
public void GetSet()
{
cli.Del("GetSet1");
astert.Null(cli.GetSet("GetSet1", "123456"));
astert.Equal("123456", cli.GetSet("GetSet1", "123456789"));
}
[Fact]
public void Incr()
{
var key = Guid.NewGuid().ToString();
astert.Equal(1, cli.Incr(key));
}
[Fact]
public void IncrBy()
{
var key = Guid.NewGuid().ToString();
astert.Equal(10, cli.IncrBy(key, 10));
}
[Fact]
public void IncrByFloat()
{
var key = Guid.NewGuid().ToString();
astert.Equal(10.1m, cli.IncrByFloat(key, 10.1m));
}
[Fact]
public void MGet()
{
cli.Set("TestMGet_null1", Null);
cli.Set("TestMGet_string1", String);
cli.Set("TestMGet_bytes1", Bytes);
cli.Set("TestMGet_clast1", Clast);
cli.Set("TestMGet_null2", Null);
cli.Set("TestMGet_string2", String);
cli.Set("TestMGet_bytes2", Bytes);
cli.Set("TestMGet_clast2", Clast);
cli.Set("TestMGet_null3", Null);
cli.Set("TestMGet_string3", String);
cli.Set("TestMGet_bytes3", Bytes);
cli.Set("TestMGet_clast3", Clast);
astert.Equal(4, cli.MGet("TestMGet_null1", "TestMGet_string1", "TestMGet_bytes1", "TestMGet_clast1").Length);
astert.Equal("", cli.MGet("TestMGet_null1", "TestMGet_string1", "TestMGet_bytes1", "TestMGet_clast1")[0]);
astert.Equal(String, cli.MGet("TestMGet_null1", "TestMGet_string1", "TestMGet_bytes1", "TestMGet_clast1")[1]);
astert.Equal(Encoding.UTF8.GetString(Bytes), cli.MGet("TestMGet_null1", "TestMGet_string1", "TestMGet_bytes1", "TestMGet_clast1")[2]);
astert.Equal(JsonConvert.SerializeObject(Clast), cli.MGet("TestMGet_null1", "TestMGet_string1", "TestMGet_bytes1", "TestMGet_clast1")[3]);
astert.Equal(4, cli.MGet("TestMGet_null1", "TestMGet_string1", "TestMGet_bytes1", "TestMGet_clast1").Length);
astert.Equal(new byte[0], cli.MGet("TestMGet_null1", "TestMGet_string1", "TestMGet_bytes1", "TestMGet_clast1")[0]);
astert.Equal(Encoding.UTF8.GetBytes(String), cli.MGet("TestMGet_null1", "TestMGet_string1", "TestMGet_bytes1", "TestMGet_clast1")[1]);
astert.Equal(Bytes, cli.MGet("TestMGet_null1", "TestMGet_string1", "TestMGet_bytes1", "TestMGet_clast1")[2]);
astert.Equal(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Clast)), cli.MGet("TestMGet_null1", "TestMGet_string1", "TestMGet_bytes1", "TestMGet_clast1")[3]);
astert.Equal(3, cli.MGet("TestMGet_clast1", "TestMGet_clast2", "TestMGet_clast3").Length);
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.MGet("TestMGet_clast1", "TestMGet_clast2", "TestMGet_clast3")[0]));
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.MGet("TestMGet_clast1", "TestMGet_clast2", "TestMGet_clast3")[1]));
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.MGet("TestMGet_clast1", "TestMGet_clast2", "TestMGet_clast3")[2]));
}
[Fact]
public void MSet()
{
cli.Del("TestMSet_null1", "TestMSet_string1", "TestMSet_bytes1", "TestMSet_clast1");
cli.MSet(new Dictionary { ["TestMSet_null1"] = Null, ["TestMSet_string1"] = String, ["TestMSet_bytes1"] = Bytes, ["TestMSet_clast1"] = Clast });
astert.Equal("", cli.Get("TestMSet_null1"));
astert.Equal(String, cli.Get("TestMSet_string1"));
astert.Equal(Bytes, cli.Get("TestMSet_bytes1"));
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestMSet_clast1")));
cli.Del("TestMSet_null1", "TestMSet_string1", "TestMSet_bytes1", "TestMSet_clast1");
cli.MSet("TestMSet_null1", Null, "TestMSet_string1", String, "TestMSet_bytes1", Bytes, "TestMSet_clast1", Clast);
astert.Equal("", cli.Get("TestMSet_null1"));
astert.Equal(String, cli.Get("TestMSet_string1"));
astert.Equal(Bytes, cli.Get("TestMSet_bytes1"));
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestMSet_clast1")));
}
[Fact]
public void MSetNx()
{
cli.Del("TestMSetNx_null", "TestMSetNx_string", "TestMSetNx_bytes", "TestMSetNx_clast", "abctest",
"TestMSetNx_null1", "TestMSetNx_string1", "TestMSetNx_bytes1", "TestMSetNx_clast1");
astert.True(cli.MSetNx(new Dictionary { ["TestMSetNx_null"] = Null }));
astert.False(cli.MSetNx(new Dictionary { ["TestMSetNx_null"] = Null }));
astert.Equal("", cli.Get("TestMSetNx_null"));
astert.True(cli.MSetNx(new Dictionary { ["TestMSetNx_string"] = String }));
astert.False(cli.MSetNx(new Dictionary { ["TestMSetNx_string"] = String }));
astert.Equal(String, cli.Get("TestMSetNx_string"));
astert.True(cli.MSetNx(new Dictionary { ["TestMSetNx_bytes"] = Bytes }));
astert.False(cli.MSetNx(new Dictionary { ["TestMSetNx_bytes"] = Bytes }));
astert.Equal(Bytes, cli.Get("TestMSetNx_bytes"));
astert.True(cli.MSetNx(new Dictionary { ["TestMSetNx_clast"] = Clast }));
astert.False(cli.MSetNx(new Dictionary { ["TestMSetNx_clast"] = Clast }));
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestMSetNx_clast")));
cli.Set("abctest", 1);
astert.False(cli.MSetNx(new Dictionary { ["abctest"] = 2, ["TestMSetNx_null1"] = Null, ["TestMSetNx_string1"] = String, ["TestMSetNx_bytes1"] = Bytes, ["TestMSetNx_clast1"] = Clast }));
astert.True(cli.MSetNx(new Dictionary { ["TestMSetNx_null1"] = Null, ["TestMSetNx_string1"] = String, ["TestMSetNx_bytes1"] = Bytes, ["TestMSetNx_clast1"] = Clast }));
astert.Equal(1, cli.Get("abctest"));
astert.Equal("", cli.Get("TestMSetNx_null1"));
astert.Equal(String, cli.Get("TestMSetNx_string1"));
astert.Equal(Bytes, cli.Get("TestMSetNx_bytes1"));
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestMSetNx_clast1")));
cli.Del("TestMSetNx_null", "TestMSetNx_string", "TestMSetNx_bytes", "TestMSetNx_clast");
cli.MSetNx("TestMSetNx_null1", Null, "TestMSetNx_string1", String, "TestMSetNx_bytes1", Bytes, "TestMSetNx_clast1", Clast);
astert.Equal("", cli.Get("TestMSetNx_null1"));
astert.Equal(String, cli.Get("TestMSetNx_string1"));
astert.Equal(Bytes, cli.Get("TestMSetNx_bytes1"));
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestMSetNx_clast1")));
}
[Fact]
public void PSetNx()
{
cli.PSetEx("TestSetNx_null", 10000, Null);
astert.Equal("", cli.Get("TestSetNx_null"));
cli.PSetEx("TestSetNx_string", 10000, String);
astert.Equal(String, cli.Get("TestSetNx_string"));
cli.PSetEx("TestSetNx_bytes", 10000, Bytes);
astert.Equal(Bytes, cli.Get("TestSetNx_bytes"));
cli.PSetEx("TestSetNx_clast", 10000, Clast);
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestSetNx_clast")));
}
[Fact]
public void Set()
{
cli.Del("TestSetNx_null", "TestSetNx_string", "TestSetNx_bytes", "TestSetNx_clast");
cli.Set("TestSet_null", Null);
astert.Equal("", cli.Get("TestSet_null"));
cli.Set("TestSet_string", String);
astert.Equal(String, cli.Get("TestSet_string"));
cli.Set("TestSet_bytes", Bytes);
astert.Equal(Bytes, cli.Get("TestSet_bytes"));
cli.Set("TestSet_clast", Clast);
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestSet_clast")));
cli.Del("TestSetNx_null", "TestSetNx_string", "TestSetNx_bytes", "TestSetNx_clast");
cli.Set("TestSet_null", Null, 10);
astert.Equal("", cli.Get("TestSet_null"));
cli.Set("TestSet_string", String, 10);
astert.Equal(String, cli.Get("TestSet_string"));
cli.Set("TestSet_bytes", Bytes, 10);
astert.Equal(Bytes, cli.Get("TestSet_bytes"));
cli.Set("TestSet_clast", Clast, 10);
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestSet_clast")));
cli.Del("TestSetNx_null", "TestSetNx_string", "TestSetNx_bytes", "TestSetNx_clast");
cli.Set("TestSet_null", Null, true);
astert.Equal("", cli.Get("TestSet_null"));
cli.Set("TestSet_string", String, true);
astert.Equal(String, cli.Get("TestSet_string"));
cli.Set("TestSet_bytes", Bytes, true);
astert.Equal(Bytes, cli.Get("TestSet_bytes"));
cli.Set("TestSet_clast", Clast, true);
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestSet_clast")));
}
[Fact]
public void SetNx()
{
cli.Del("TestSetNx_null", "TestSetNx_string", "TestSetNx_bytes", "TestSetNx_clast");
astert.True(cli.SetNx("TestSetNx_null", Null));
astert.False(cli.SetNx("TestSetNx_null", Null));
astert.Equal("", cli.Get("TestSetNx_null"));
astert.True(cli.SetNx("TestSetNx_string", String));
astert.False(cli.SetNx("TestSetNx_string", String));
astert.Equal(String, cli.Get("TestSetNx_string"));
astert.True(cli.SetNx("TestSetNx_bytes", Bytes));
astert.False(cli.SetNx("TestSetNx_bytes", Bytes));
astert.Equal(Bytes, cli.Get("TestSetNx_bytes"));
astert.True(cli.SetNx("TestSetNx_clast", Clast));
astert.False(cli.SetNx("TestSetNx_clast", Clast));
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestSetNx_clast")));
cli.Del("TestSetNx_null", "TestSetNx_string", "TestSetNx_bytes", "TestSetNx_clast");
astert.True(cli.SetNx("TestSetNx_null", Null, 10));
astert.False(cli.SetNx("TestSetNx_null", Null, 10));
astert.Equal("", cli.Get("TestSetNx_null"));
astert.True(cli.SetNx("TestSetNx_string", String, 10));
astert.False(cli.SetNx("TestSetNx_string", String, 10));
astert.Equal(String, cli.Get("TestSetNx_string"));
astert.True(cli.SetNx("TestSetNx_bytes", Bytes, 10));
astert.False(cli.SetNx("TestSetNx_bytes", Bytes, 10));
astert.Equal(Bytes, cli.Get("TestSetNx_bytes"));
astert.True(cli.SetNx("TestSetNx_clast", Clast, 10));
astert.False(cli.SetNx("TestSetNx_clast", Clast, 10));
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestSetNx_clast")));
}
[Fact]
public void SetXx()
{
cli.Del("TestSetXx_null");
astert.False(cli.SetXx("TestSetXx_null", Null, 10));
cli.Set("TestSetXx_null", 1, true);
astert.True(cli.SetXx("TestSetXx_null", Null, 10));
astert.Equal("", cli.Get("TestSetXx_null"));
cli.Del("TestSetXx_string");
astert.False(cli.SetXx("TestSetXx_string", String, 10));
cli.Set("TestSetXx_string", 1, true);
astert.True(cli.SetXx("TestSetXx_string", String, 10));
astert.Equal(String, cli.Get("TestSetXx_string"));
cli.Del("TestSetXx_bytes");
astert.False(cli.SetXx("TestSetXx_bytes", Bytes, 10));
cli.Set("TestSetXx_bytes", 1, true);
astert.True(cli.SetXx("TestSetXx_bytes", Bytes, 10));
astert.Equal(Bytes, cli.Get("TestSetXx_bytes"));
cli.Del("TestSetXx_clast");
astert.False(cli.SetXx("TestSetXx_clast", Clast, 10));
cli.Set("TestSetXx_clast", 1, true);
astert.True(cli.SetXx("TestSetXx_clast", Clast, 10));
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestSetXx_clast")));
cli.Del("TestSetXx_null");
astert.False(cli.SetXx("TestSetXx_null", Null, true));
cli.Set("TestSetXx_null", 1, true);
astert.True(cli.SetXx("TestSetXx_null", Null, true));
astert.Equal("", cli.Get("TestSetXx_null"));
cli.Del("TestSetXx_string");
astert.False(cli.SetXx("TestSetXx_string", String, true));
cli.Set("TestSetXx_string", 1, true);
astert.True(cli.SetXx("TestSetXx_string", String, true));
astert.Equal(String, cli.Get("TestSetXx_string"));
cli.Del("TestSetXx_bytes");
astert.False(cli.SetXx("TestSetXx_bytes", Bytes, true));
cli.Set("TestSetXx_bytes", 1, true);
astert.True(cli.SetXx("TestSetXx_bytes", Bytes, true));
astert.Equal(Bytes, cli.Get("TestSetXx_bytes"));
cli.Del("TestSetXx_clast");
astert.False(cli.SetXx("TestSetXx_clast", Clast, true));
cli.Set("TestSetXx_clast", 1, true);
astert.True(cli.SetXx("TestSetXx_clast", Clast, true));
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestSetXx_clast")));
}
[Fact]
public void SetBit()
{
cli.Del("SetBit1");
cli.SetBit("SetBit1", 100, true);
astert.False(cli.GetBit("SetBit1", 10));
astert.True(cli.GetBit("SetBit1", 100));
}
[Fact]
public void SetEx()
{
cli.Del("TestSetEx_null", "TestSetEx_string", "TestSetEx_bytes", "TestSetEx_clast");
cli.SetEx("TestSetEx_null", 10, Null);
astert.Equal("", cli.Get("TestSetEx_null"));
cli.SetEx("TestSetEx_string", 10, String);
astert.Equal(String, cli.Get("TestSetEx_string"));
cli.SetEx("TestSetEx_bytes", 10, Bytes);
astert.Equal(Bytes, cli.Get("TestSetEx_bytes"));
cli.SetEx("TestSetEx_clast", 10, Clast);
astert.Equal(JsonConvert.SerializeObject(Clast), JsonConvert.SerializeObject(cli.Get("TestSetEx_clast")));
}
[Fact]
public void SetRange()
{
var key = "TestSetRange_null";
cli.Set(key, Null);
cli.SetRange(key, 10, String);
astert.Equal(String, cli.GetRange(key, 10, -1));
key = "TestSetRange_string";
cli.Set(key, "abcdefg");
cli.SetRange(key, 2, "yyy");
astert.Equal("yyy", cli.GetRange(key, 2, 4));
key = "TestSetRange_bytes";
cli.Set(key, Bytes);
cli.SetRange(key, 2, Bytes);
astert.Equal(Bytes, cli.GetRange(key, 2, Bytes.Length + 2));
}
[Fact]
public void StrLen()
{
var key = "TestStrLen_null";
cli.Set(key, Null);
astert.Equal(0, cli.StrLen(key));
key = "TestStrLen_string";
cli.Set(key, "abcdefg");
astert.Equal(7, cli.StrLen(key));
key = "TestStrLen_string";
cli.Set(key, String);
astert.Equal(15, cli.StrLen(key));
key = "TestStrLen_bytes";
cli.Set(key, Bytes);
astert.Equal(Bytes.Length, cli.StrLen(key));
}
}
}