This repository was archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 870
/
Copy pathRedisClientsManagerExtensionsTests.cs
156 lines (140 loc) · 4.56 KB
/
RedisClientsManagerExtensionsTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System.Collections.Generic;
using NUnit.Framework;
using ServiceStack.Common;
using ServiceStack.Common.Tests.Models;
using ServiceStack.Redis.Generic;
namespace ServiceStack.Redis.Tests
{
[TestFixture]
public class RedisClientsManagerExtensionsTests
{
private IRedisClientsManager redisManager;
[SetUp]
public void OnBeforeEachTest()
{
if (redisManager != null) redisManager.Dispose();
redisManager = TestConfig.BasicClientManger;
redisManager.Exec(r => r.FlushAll());
}
[Test]
public void Can_Exec_Action()
{
redisManager.Exec(r =>
{
r.Increment("key", 1);
Assert.That(r.Get<int>("key"), Is.EqualTo(1));
});
}
[Test]
public void Can_Exec_Func_string()
{
string value = redisManager.Exec(r =>
{
r.SetValue("key", "value");
return r.GetValue("key");
});
Assert.That(value, Is.EqualTo("value"));
}
[Test]
public void Can_Exec_Func_long()
{
long value = redisManager.Exec(r => r.Increment("key", 1));
Assert.That(value, Is.EqualTo(1));
}
[Test]
public void Can_Exec_Func_int()
{
long value = redisManager.Exec(r =>
{
r.AddItemToList("list", "value");
return r.GetListCount("list");
});
Assert.That(value, Is.EqualTo(1));
}
[Test]
public void Can_Exec_Func_double()
{
double value = redisManager.Exec(r =>
{
r.AddItemToSortedSet("zset", "value", 1.1d);
return r.GetItemScoreInSortedSet("zset", "value");
});
Assert.That(value, Is.EqualTo(1.1d));
}
[Test]
public void Can_Exec_Func_bool()
{
bool value = redisManager.Exec(r =>
{
r.AddItemToSet("set", "item");
return r.SetContainsItem("set", "item");
});
Assert.That(value, Is.True);
}
[Test]
public void Can_Exec_Transaction_Action()
{
var value = false;
redisManager.ExecTrans(trans =>
{
trans.QueueCommand(r => r.AddItemToSet("set", "item"));
trans.QueueCommand(r => r.SetContainsItem("set", "item"), x => value = x);
});
Assert.That(value, Is.True);
}
[Test]
public void Can_ExecAs_ModelWithIdAndName_Action()
{
var expected = ModelWithIdAndName.Create(1);
redisManager.ExecAs<ModelWithIdAndName>(m =>
{
m.Store(expected);
var actual = m.GetById(expected.Id);
Assert.That(actual, Is.EqualTo(expected));
});
}
[Test]
public void Can_ExecAs_ModelWithIdAndName_Func()
{
var expected = ModelWithIdAndName.Create(1);
ModelWithIdAndName actual = redisManager.ExecAs<ModelWithIdAndName>(m =>
{
m.Store(expected);
return m.GetById(expected.Id);
});
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void Can_ExecAs_ModelWithIdAndName_Func_IList()
{
var expected = new[] {
ModelWithIdAndName.Create(1),
ModelWithIdAndName.Create(2),
ModelWithIdAndName.Create(3),
};
IList<ModelWithIdAndName> actual = redisManager.ExecAs<ModelWithIdAndName>(m =>
{
var list = m.Lists["typed-list"];
list.AddRange(expected);
return (IList<ModelWithIdAndName>)list.GetAll();
});
Assert.That(actual.EquivalentTo(expected));
}
[Test]
public void Can_ExecAs_ModelWithIdAndName_Func_List()
{
var expected = new[] {
ModelWithIdAndName.Create(1),
ModelWithIdAndName.Create(2),
ModelWithIdAndName.Create(3),
};
List<ModelWithIdAndName> actual = redisManager.ExecAs<ModelWithIdAndName>(m =>
{
var list = m.Lists["typed-list"];
list.AddRange(expected);
return list.GetAll();
});
Assert.That(actual.EquivalentTo(expected));
}
}
}