Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 7cef98a

Browse files
committed
Add tests for saving via string / typed APIs?
1 parent 611d85e commit 7cef98a

File tree

1 file changed

+98
-74
lines changed

1 file changed

+98
-74
lines changed
Lines changed: 98 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,92 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using NUnit.Framework;
4+
using ServiceStack.Text;
45

56
namespace ServiceStack.Redis.Tests.Generic
67
{
7-
[TestFixture, Category("Integration")]
8+
[TestFixture, Category("Integration")]
89
public class RedisClientTests : RedisClientTestsBase
9-
{
10-
[TestFixtureSetUp]
11-
public void TestFixture()
12-
{
13-
}
10+
{
11+
[TestFixtureSetUp]
12+
public void TestFixture()
13+
{
14+
}
1415

1516
public override void OnBeforeEachTest()
1617
{
1718
base.OnBeforeEachTest();
1819
Redis.NamespacePrefix = "GenericRedisClientTests";
1920
}
20-
21-
[Test]
22-
public void Can_Set_and_Get_string()
23-
{
24-
const string value = "value";
25-
Redis.SetEntry("key", value);
26-
var valueString = Redis.GetValue("key");
27-
28-
Assert.That(valueString, Is.EqualTo(value));
29-
}
30-
31-
[Test]
32-
public void Can_Set_and_Get_key_with_all_byte_values()
33-
{
34-
const string key = "bytesKey";
35-
36-
var value = new byte[256];
37-
for (var i = 0; i < value.Length; i++)
38-
{
39-
value[i] = (byte) i;
40-
}
41-
42-
var redis = Redis.As<byte[]>();
43-
44-
redis.SetEntry(key, value);
45-
var resultValue = redis.GetValue(key);
46-
47-
Assert.That(resultValue, Is.EquivalentTo(value));
48-
}
49-
50-
public List<T> Sort<T>(IEnumerable<T> list)
51-
{
52-
var sortedList = list.ToList();
53-
sortedList.Sort((x, y) =>
54-
x.GetId().ToString().CompareTo(y.GetId().ToString()));
55-
56-
return sortedList;
57-
}
58-
59-
public void AssertUnorderedListsAreEqual<T>(IList<T> actualList, IList<T> expectedList)
60-
{
61-
Assert.That(actualList, Has.Count.EqualTo(expectedList.Count));
62-
63-
var actualMap = Sort(actualList.Select(x => x.GetId()));
64-
var expectedMap = Sort(expectedList.Select(x => x.GetId()));
65-
66-
Assert.That(actualMap, Is.EquivalentTo(expectedMap));
67-
}
68-
21+
22+
[Test]
23+
public void Can_Set_and_Get_string()
24+
{
25+
const string value = "value";
26+
Redis.SetValue("key", value);
27+
var valueString = Redis.GetValue("key");
28+
29+
Assert.That(valueString, Is.EqualTo(value));
30+
}
31+
32+
[Test]
33+
public void Can_Set_and_Get_key_with_all_byte_values()
34+
{
35+
const string key = "bytesKey";
36+
37+
var value = new byte[256];
38+
for (var i = 0; i < value.Length; i++)
39+
{
40+
value[i] = (byte)i;
41+
}
42+
43+
var redis = Redis.As<byte[]>();
44+
45+
redis.SetValue(key, value);
46+
var resultValue = redis.GetValue(key);
47+
48+
Assert.That(resultValue, Is.EquivalentTo(value));
49+
}
50+
51+
public List<T> Sort<T>(IEnumerable<T> list)
52+
{
53+
var sortedList = list.ToList();
54+
sortedList.Sort((x, y) =>
55+
x.GetId().ToString().CompareTo(y.GetId().ToString()));
56+
57+
return sortedList;
58+
}
59+
60+
public void AssertUnorderedListsAreEqual<T>(IList<T> actualList, IList<T> expectedList)
61+
{
62+
Assert.That(actualList, Has.Count.EqualTo(expectedList.Count));
63+
64+
var actualMap = Sort(actualList.Select(x => x.GetId()));
65+
var expectedMap = Sort(expectedList.Select(x => x.GetId()));
66+
67+
Assert.That(actualMap, Is.EquivalentTo(expectedMap));
68+
}
69+
6970
[Test]
7071
public void Can_SetBit_And_GetBit_And_BitCount()
7172
{
7273
const string key = "BitKey";
7374
const int offset = 100;
7475
Redis.SetBit(key, offset, 1);
75-
Assert.AreEqual(1, Redis.GetBit(key,offset));
76+
Assert.AreEqual(1, Redis.GetBit(key, offset));
7677
Assert.AreEqual(1, Redis.BitCount(key));
7778
}
78-
79-
public class Dummy
80-
{
81-
public int Id { get; set; }
82-
public string Name { get; set; }
83-
}
84-
85-
[Test]
86-
public void Can_Delete()
87-
{
88-
var dto = new Dummy { Id = 1, Name = "Name" };
79+
80+
public class Dummy
81+
{
82+
public int Id { get; set; }
83+
public string Name { get; set; }
84+
}
85+
86+
[Test]
87+
public void Can_Delete()
88+
{
89+
var dto = new Dummy { Id = 1, Name = "Name" };
8990

9091
Redis.Store(dto);
9192

@@ -96,12 +97,12 @@ public void Can_Delete()
9697

9798
Assert.That(Redis.GetAllItemsFromSet(Redis.NamespacePrefix + "ids:Dummy").Count, Is.EqualTo(0));
9899
Assert.That(Redis.GetById<Dummy>(1), Is.Null);
99-
}
100+
}
100101

101-
[Test]
102-
public void Can_DeleteById()
103-
{
104-
var dto = new Dummy { Id = 1, Name = "Name" };
102+
[Test]
103+
public void Can_DeleteById()
104+
{
105+
var dto = new Dummy { Id = 1, Name = "Name" };
105106
Redis.Store(dto);
106107

107108
Assert.That(Redis.GetAllItemsFromSet(Redis.NamespacePrefix + "ids:Dummy").ToArray()[0], Is.EqualTo("1"));
@@ -111,7 +112,30 @@ public void Can_DeleteById()
111112

112113
Assert.That(Redis.GetAllItemsFromSet(Redis.NamespacePrefix + "ids:Dummy").Count, Is.EqualTo(0));
113114
Assert.That(Redis.GetById<Dummy>(1), Is.Null);
114-
}
115+
}
116+
117+
[Test]
118+
public void Can_save_via_string()
119+
{
120+
var dtos = 10.Times(i => new Dummy { Id = i, Name = "Name" + i });
121+
122+
Redis.SetValue("dummy:strings", dtos.ToJson());
123+
124+
var fromDtos = Redis.GetValue("dummy:strings").FromJson<List<Dummy>>();
125+
126+
Assert.That(fromDtos.Count, Is.EqualTo(10));
127+
}
115128

116-
}
129+
[Test]
130+
public void Can_save_via_types()
131+
{
132+
var dtos = 10.Times(i => new Dummy { Id = i, Name = "Name" + i });
133+
134+
Redis.Set("dummy:strings", dtos);
135+
136+
var fromDtos = Redis.Get<List<Dummy>>("dummy:strings");
137+
138+
Assert.That(fromDtos.Count, Is.EqualTo(10));
139+
}
140+
}
117141
}

0 commit comments

Comments
 (0)