Skip to content

Commit e855cda

Browse files
committed
#283 Adding couple of tests for clearing memory cache by providing entity or entities.
1 parent 3235f86 commit e855cda

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/DataTests/MemoryCacheBuilderTests.cs

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Linq;
56
using Internal.Caching;
67
using Microsoft.AspNetCore.Mvc.ApplicationParts;
78
using Microsoft.Extensions.Caching.Memory;
@@ -183,9 +184,106 @@ public void WithoutMemoryEntryCacheShouldReturnCorrectCacheData()
183184
["third"] = "thirdValue"
184185
}))
185186
.WithoutMemoryCache(cache => cache.WithoutEntry("second"))
186-
.Calling(c => c.GetCount(From.Services<IMemoryCache>()))
187+
.Calling(c => c.GetAllEntities(From.Services<IMemoryCache>()))
188+
.ShouldReturn()
189+
.Ok(ok => ok
190+
.WithModel(new Dictionary<object, object>
191+
{
192+
["first"] = "firstValue",
193+
["third"] = "thirdValue"
194+
}));
195+
}
196+
197+
[Fact]
198+
public void WithoutMemoryCacheByKeyShouldReturnCorrectCacheData()
199+
{
200+
MyController<MemoryCacheController>
201+
.Instance()
202+
.WithMemoryCache(memoryCache => memoryCache
203+
.WithEntries(new Dictionary<object, object>
204+
{
205+
["first"] = "firstValue",
206+
["second"] = "secondValue",
207+
["third"] = "thirdValue"
208+
}))
209+
.WithoutMemoryCache("second")
210+
.Calling(c => c.GetAllEntities(From.Services<IMemoryCache>()))
187211
.ShouldReturn()
188-
.Ok(ok => ok.WithModel(2));
212+
.Ok(ok => ok
213+
.WithModel(new Dictionary<object, object>
214+
{
215+
["first"] = "firstValue",
216+
["third"] = "thirdValue"
217+
}));
218+
}
219+
220+
[Fact]
221+
public void WithoutMemoryCacheByKeysShouldReturnCorrectCacheData()
222+
{
223+
var entities = new Dictionary<object, object>
224+
{
225+
["first"] = "firstValue",
226+
["second"] = "secondValue",
227+
["third"] = "thirdValue"
228+
};
229+
230+
var entriesToDelete = entities.Select(x => x.Key).ToList();
231+
entriesToDelete.RemoveAt(0);
232+
233+
MyController<MemoryCacheController>
234+
.Instance()
235+
.WithMemoryCache(memoryCache => memoryCache.WithEntries(entities))
236+
.WithoutMemoryCache(entriesToDelete)
237+
.Calling(c => c.GetAllEntities(From.Services<IMemoryCache>()))
238+
.ShouldReturn()
239+
.Ok(ok => ok
240+
.WithModel(new Dictionary<object, object>
241+
{
242+
["first"] = "firstValue"
243+
}));
244+
}
245+
246+
[Fact]
247+
public void WithoutMemoryCacheByNonExistingKeysShouldReturnCorrectCacheData()
248+
{
249+
var entities = new Dictionary<object, object>
250+
{
251+
["first"] = "firstValue",
252+
["second"] = "secondValue",
253+
["third"] = "thirdValue"
254+
};
255+
256+
var entitiesToDelete = new List<string> { "key1", "key2" };
257+
MyController<MemoryCacheController>
258+
.Instance()
259+
.WithMemoryCache(memoryCache => memoryCache
260+
.WithEntries(entities))
261+
.WithoutMemoryCache(entitiesToDelete)
262+
.Calling(c => c.GetAllEntities(From.Services<IMemoryCache>()))
263+
.ShouldReturn()
264+
.Ok(ok => ok
265+
.WithModel(entities));
266+
}
267+
268+
[Fact]
269+
public void WithoutMemoryCacheByNonExistingKeyShouldReturnCorrectCacheData()
270+
{
271+
var entities = new Dictionary<object, object>
272+
{
273+
["first"] = "firstValue",
274+
["second"] = "secondValue",
275+
["third"] = "thirdValue"
276+
};
277+
278+
MyController<MemoryCacheController>
279+
.Instance()
280+
.WithMemoryCache(memoryCache => memoryCache
281+
.WithEntries(entities))
282+
.WithoutMemoryCache("firstValue")
283+
.Calling(c => c.GetAllEntities(From.Services<IMemoryCache>()))
284+
.ShouldReturn()
285+
.Ok(ok => ok
286+
.WithModel(entities));
189287
}
190288

191289
public void Dispose() => MyApplication.StartsFrom<DefaultStartup>();

test/MyTested.AspNetCore.Mvc.Caching.Test/Setups/Controllers/MemoryCacheController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@ public IActionResult GetCount([FromServices]IMemoryCache cache)
3838
{
3939
return this.Ok((cache as IMemoryCacheMock).GetCacheAsDictionary().Count);
4040
}
41+
42+
public IActionResult GetAllEntities([FromServices]IMemoryCache cache)
43+
{
44+
return this.Ok((cache as IMemoryCacheMock).GetCacheAsDictionary());
45+
}
4146
}
4247
}

0 commit comments

Comments
 (0)