forked from mkrypka/dotnet-caching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRawCacheTests.cs
139 lines (109 loc) · 3.92 KB
/
RawCacheTests.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
using System;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Shouldly;
using Vtex.Caching.Interfaces;
namespace Vtex.Caching.Tests
{
[TestFixture("redis", Category = "Redis")]
[TestFixture("inProcess")]
public class RawCacheTests
{
private readonly IRawCache _cacheBackend;
public RawCacheTests(string backendName)
{
switch (backendName)
{
case "redis":
this._cacheBackend = ResourceFactory.GetRedisCache();
break;
case "inProcess":
this._cacheBackend = ResourceFactory.GetInProcessCache();
break;
}
}
[Test]
public async Task Should_set_and_get_value()
{
//Arrange
var testKey = ResourceFactory.GenerateKey();
await this._cacheBackend.SetAsync(testKey, "testValue", TimeSpan.FromSeconds(1));
//Act
var result = await this._cacheBackend.GetAsync<string>(testKey);
//Assert
result.ShouldBe("testValue");
}
[Test]
public async Task Should_exist_when_set()
{
//Arrange
var testKey = ResourceFactory.GenerateKey();
await this._cacheBackend.SetAsync(testKey, "testValue", TimeSpan.FromSeconds(1));
//Act
var result = await this._cacheBackend.ExistsAsync(testKey);
//Assert
result.ShouldBe(true);
}
[Test]
public async Task Should_delete_value()
{
//Arrange
var testKey = ResourceFactory.GenerateKey();
await this._cacheBackend.SetAsync(testKey, "testValue", null);
//Act
await this._cacheBackend.DeleteAsync(testKey);
//Assert
(await this._cacheBackend.GetAsync<string>(testKey)).ShouldBe(null);
}
[Test]
public async Task Should_update_time_to_live()
{
//Arrange
var testKey = ResourceFactory.GenerateKey();
await this._cacheBackend.SetAsync(testKey, "testValue", null);
//Act
await this._cacheBackend.ExpireInAsync(testKey, TimeSpan.FromSeconds(1));
//Assert
(await this._cacheBackend.GetTimeToLiveAsync(testKey)).ShouldNotBe(null);
}
[Test]
public async Task Should_return_null_time_to_live()
{
//Arrange
var testKey = ResourceFactory.GenerateKey();
await this._cacheBackend.SetAsync(testKey, "testValue", null);
//Act
var timeToLive = await this._cacheBackend.GetTimeToLiveAsync(testKey);
//Assert
timeToLive.ShouldBe(null);
}
[Test]
public async Task Should_store_null()
{
//Arrange
var testKey = ResourceFactory.GenerateKey();
//Act
await this._cacheBackend.SetAsync<object>(testKey, null, TimeSpan.FromSeconds(1));
//Assert
(await this._cacheBackend.GetAsync<object>(testKey)).ShouldBe(null);
}
[Test]
public async Task Should_create_value_once()
{
//Arrange
var testKey = ResourceFactory.GenerateKey();
var fakeValueCreator = new Mock<IFakeValueCreator>();
fakeValueCreator.Setup(it => it.CreateString()).ReturnsAsync("testValue");
await
this._cacheBackend.GetOrSetAsync(testKey, TimeSpan.FromSeconds(1),
() => fakeValueCreator.Object.CreateString());
//Act
await
this._cacheBackend.GetOrSetAsync(testKey, TimeSpan.FromSeconds(1),
() => fakeValueCreator.Object.CreateString());
//Assert
fakeValueCreator.Verify(it => it.CreateString(), Times.Once);
}
}
}