-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachingServiceTests.cs
158 lines (130 loc) · 5.09 KB
/
CachingServiceTests.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
157
158
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Moq;
using NUnit.Framework;
namespace CachingServiceTests.Tests
{
[TestFixture]
public class CachingServiceTests
{
private CachingService _cachingService;
private Mock<ICacheProvider> _cacheProviderMock;
[SetUp]
public void Setup()
{
_cacheProviderMock = new Mock<ICacheProvider>();
_cachingService = new CachingService(_cacheProviderMock.Object);
}
[Test]
public void Add_ShouldAddItemToCache()
{
// Arrange
var key = "test_key";
var item = "test_item";
var options = new MemoryCacheEntryOptions();
// Act
_cachingService.Add(key, item, options);
// Assert
_cacheProviderMock.Verify(p => p.Set(key, item, options), Times.Once);
}
[Test]
public void Get_ShouldReturnCachedItem()
{
// Arrange
var key = "test_key";
var cachedItem = "test_item";
_cacheProviderMock.Setup(p => p.Get(key)).Returns(cachedItem);
// Act
var result = _cachingService.Get<string>(key);
// Assert
Assert.AreEqual(cachedItem, result);
_cacheProviderMock.Verify(p => p.Get(key), Times.Once);
}
[Test]
public void Get_ShouldThrowException_WhenKeyIsNull()
{
// Arrange & Act & Assert
Assert.Throws<ArgumentNullException>(() => _cachingService.Get<string>(null));
}
[Test]
public void GetOrAdd_ShouldAddNewItemToCache_WhenNotPresent()
{
// Arrange
var key = "test_key";
var item = "test_item";
_cacheProviderMock.Setup(p => p.GetOrCreate<object>(It.IsAny<string>(), It.IsAny<MemoryCacheEntryOptions>(), It.IsAny<Func<ICacheEntry, object>>()))
.Returns(new Lazy<string>(() => item));
// Act
var result = _cachingService.GetOrAdd(key, entry => item);
// Assert
Assert.AreEqual(item, result);
_cacheProviderMock.Verify(p => p.GetOrCreate<object>(It.IsAny<string>(), It.IsAny<MemoryCacheEntryOptions>(), It.IsAny<Func<ICacheEntry, object>>()), Times.Once);
}
[Test]
public void Remove_ShouldRemoveItemFromCache()
{
// Arrange
var key = "test_key";
// Act
_cachingService.Remove(key);
// Assert
_cacheProviderMock.Verify(p => p.Remove(key), Times.Once);
}
[Test]
public async Task GetAsync_ShouldReturnCachedItem()
{
// Arrange
var key = "test_key";
var cachedItem = Task.FromResult("test_item");
_cacheProviderMock.Setup(p => p.Get(key)).Returns(cachedItem);
// Act
var result = await _cachingService.GetAsync<string>(key);
// Assert
Assert.AreEqual("test_item", result);
_cacheProviderMock.Verify(p => p.Get(key), Times.Once);
}
[Test]
public async Task GetOrAddAsync_ShouldAddNewItemToCache_WhenNotPresent()
{
// Arrange
var key = "test_key";
var item = "test_item";
_cacheProviderMock.Setup(p => p.GetOrCreate<object>(It.IsAny<string>(), It.IsAny<MemoryCacheEntryOptions>(), It.IsAny<Func<ICacheEntry, object>>()))
.Returns(new AsyncLazy<string>(async () => await Task.FromResult(item)));
// Act
var result = await _cachingService.GetOrAddAsync(key, async entry => await Task.FromResult(item));
// Assert
Assert.AreEqual(item, result);
_cacheProviderMock.Verify(p => p.GetOrCreate<object>(It.IsAny<string>(), It.IsAny<MemoryCacheEntryOptions>(), It.IsAny<Func<ICacheEntry, object>>()), Times.Once);
}
[Test]
public void TryGetValue_ShouldReturnTrue_WhenItemIsPresentInCache()
{
// Arrange
var key = "test_key";
var item = "test_item";
_cacheProviderMock.Setup(p => p.TryGetValue(key, out item)).Returns(true);
// Act
var result = _cachingService.TryGetValue(key, out string cachedItem);
// Assert
Assert.IsTrue(result);
Assert.AreEqual(item, cachedItem);
_cacheProviderMock.Verify(p => p.TryGetValue(key, out item), Times.Once);
}
[Test]
public void TryGetValue_ShouldReturnFalse_WhenItemIsNotInCache()
{
// Arrange
var key = "test_key";
string item = null;
_cacheProviderMock.Setup(p => p.TryGetValue(key, out item)).Returns(false);
// Act
var result = _cachingService.TryGetValue(key, out string cachedItem);
// Assert
Assert.IsFalse(result);
Assert.IsNull(cachedItem);
_cacheProviderMock.Verify(p => p.TryGetValue(key, out item), Times.Once);
}
}
}