Skip to content

Commit e78a315

Browse files
committed
#283 Renaming several interfaces in order to match their corresponsing implementations.
Added tests for without distributed cache logic. Added extension methods for the distributed cache component builder.
1 parent 7695c50 commit e78a315

8 files changed

Lines changed: 283 additions & 22 deletions

File tree

src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithoutDistributedCache/IAndWithoutDistributedCache.cs renamed to src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithoutDistributedCache/IAndWithoutDistributedCacheBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
/// <summary>
44
/// Used for adding AndAlso() method to the <see cref="Microsoft.Extensions.Caching.Distributed.IDistributedCache"/> builder.
55
/// </summary>
6-
public interface IAndWithoutDistributedCache : IWithoutDistributedCache
6+
public interface IAndWithoutDistributedCacheBuilder : IWithoutDistributedCacheBuilder
77
{
88
/// <summary>
99
/// AndAlso method for better readability when building <see cref="Microsoft.Extensions.Caching.Distributed.IDistributedCache"/>.
1010
/// </summary>
1111
/// <returns></returns>
12-
IWithoutDistributedCache AndAlso();
12+
IWithoutDistributedCacheBuilder AndAlso();
1313
}
1414
}

src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithoutDistributedCache/IWithoutDistributedCache.cs renamed to src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/DistributedCache/WithoutDistributedCache/IWithoutDistributedCacheBuilder.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@
66
/// <summary>
77
/// Used for building mocked <see cref="IDistributedCache"/>.
88
/// </summary>
9-
public interface IWithoutDistributedCache
9+
public interface IWithoutDistributedCacheBuilder
1010
{
1111
/// <summary>
1212
/// Remove cache entry to the mocked <see cref="IDistributedCache"/>.
1313
/// </summary>
1414
/// <param name="key">Key of the cache entry.</param>
15-
/// <returns>The same <see cref="IAndWithoutDistributedCache"/>.</returns>
16-
IAndWithoutDistributedCache WithoutEntry(object key);
15+
/// <returns>The same <see cref="IAndWithoutDistributedCacheBuilder"/>.</returns>
16+
IAndWithoutDistributedCacheBuilder WithoutEntry(string key);
1717

1818
/// <summary>
1919
/// Remove cache entries to the mocked <see cref="IDistributedCache"/>.
2020
/// </summary>
2121
/// <param name="keys">Keys of the cache entries.</param>
22-
/// <returns>The same <see cref="IAndWithoutDistributedCache"/>.</returns>
23-
IAndWithoutDistributedCache WithoutEntries(IEnumerable<object> keys);
22+
/// <returns>The same <see cref="IAndWithoutDistributedCacheBuilder"/>.</returns>
23+
IAndWithoutDistributedCacheBuilder WithoutEntries(IEnumerable<string> keys);
2424

2525
/// <summary>
2626
/// Remove cache params to the mocked <see cref="IDistributedCache"/>.
2727
/// </summary>
2828
/// <param name="keys">Keys of the cache entries.</param>
29-
/// <returns>The same <see cref="IAndWithoutDistributedCache"/>.</returns>
30-
IAndWithoutDistributedCache WithoutEntries(params object[] keys);
29+
/// <returns>The same <see cref="IAndWithoutDistributedCacheBuilder"/>.</returns>
30+
IAndWithoutDistributedCacheBuilder WithoutEntries(params string[] keys);
3131

3232
/// <summary>
3333
/// Clear all entries persisted into the <see cref="IDistributedCache"/>.
3434
/// </summary>
35-
/// <returns>The same <see cref="IAndWithoutDistributedCache"/>.</returns>
36-
IAndWithoutDistributedCache WithoutAllEntries();
35+
/// <returns>The same <see cref="IAndWithoutDistributedCacheBuilder"/>.</returns>
36+
IAndWithoutDistributedCacheBuilder WithoutAllEntries();
3737
}
3838
}

src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/WithoutDistributedCache/WithoutDistributedCacheBuilder.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
using System.Collections.Generic;
55
using Microsoft.Extensions.Caching.Distributed;
66
using MyTested.AspNetCore.Mvc.Builders.Contracts.Data;
7+
using MyTested.AspNetCore.Mvc.Utilities.Extensions;
78

8-
public class WithoutDistributedCacheBuilder : BaseDistributedCacheBuilder, IAndWithoutDistributedCache
9+
public class WithoutDistributedCacheBuilder : BaseDistributedCacheBuilder, IAndWithoutDistributedCacheBuilder
910
{
1011
/// <summary>
1112
/// Initializes a new instance of the <see cref="WithoutDistributedCacheBuilder"/> class.
@@ -17,30 +18,34 @@ public WithoutDistributedCacheBuilder(IServiceProvider services)
1718
}
1819

1920
/// <inheritdoc />
20-
public IAndWithoutDistributedCache WithoutAllEntries()
21+
public IAndWithoutDistributedCacheBuilder WithoutAllEntries()
2122
{
22-
throw new NotImplementedException();
23+
this.DistributedCache.AsDistributedCacheMock().ClearCache();
24+
return this;
2325
}
2426

2527
/// <inheritdoc />
26-
public IAndWithoutDistributedCache WithoutEntries(IEnumerable<object> keys)
28+
public IAndWithoutDistributedCacheBuilder WithoutEntries(IEnumerable<string> keys)
2729
{
28-
throw new NotImplementedException();
30+
this.DistributedCache.AsDistributedCacheMock().RemoveKeys(keys);
31+
return this;
2932
}
3033

3134
/// <inheritdoc />
32-
public IAndWithoutDistributedCache WithoutEntries(params object[] keys)
35+
public IAndWithoutDistributedCacheBuilder WithoutEntries(params string[] keys)
3336
{
34-
throw new NotImplementedException();
37+
this.DistributedCache.AsDistributedCacheMock().RemoveKeys(keys);
38+
return this;
3539
}
3640

3741
/// <inheritdoc />
38-
public IAndWithoutDistributedCache WithoutEntry(object key)
42+
public IAndWithoutDistributedCacheBuilder WithoutEntry(string key)
3943
{
40-
throw new NotImplementedException();
44+
this.DistributedCache.Remove(key);
45+
return this;
4146
}
4247

4348
/// <inheritdoc />
44-
public IWithoutDistributedCache AndAlso() => this;
49+
public IWithoutDistributedCacheBuilder AndAlso() => this;
4550
}
4651
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace MyTested.AspNetCore.Mvc
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using MyTested.AspNetCore.Mvc.Builders.Base;
6+
using MyTested.AspNetCore.Mvc.Builders.Contracts.Base;
7+
using MyTested.AspNetCore.Mvc.Builders.Contracts.Data;
8+
using MyTested.AspNetCore.Mvc.Builders.Data.DistributedCache.WithoutDistributedCache;
9+
10+
public static class ComponentBuilderDistributedCacheWithoutExtensions
11+
{
12+
public static TBuilder WithoutDistributedCache<TBuilder>(
13+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder)
14+
where TBuilder : IBaseTestBuilder
15+
=> builder.WithoutDistributedCache(cache => cache.WithoutAllEntries());
16+
17+
public static TBuilder WithoutDistributedCache<TBuilder>(
18+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
19+
string key)
20+
where TBuilder : IBaseTestBuilder
21+
=> builder.WithoutDistributedCache(cache => cache.WithoutEntry(key));
22+
23+
public static TBuilder WithoutDistributedCache<TBuilder>(
24+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
25+
IEnumerable<string> keys)
26+
where TBuilder : IBaseTestBuilder
27+
=> builder.WithoutDistributedCache(cache => cache.WithoutEntries(keys));
28+
29+
public static TBuilder WithoutDistributedCache<TBuilder>(
30+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
31+
params string[] keys)
32+
where TBuilder : IBaseTestBuilder
33+
=> builder.WithoutDistributedCache(cache => cache.WithoutEntries(keys));
34+
35+
public static TBuilder WithoutDistributedCache<TBuilder>(
36+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
37+
Action<IWithoutDistributedCacheBuilder> distributedCacheBuilder)
38+
where TBuilder : IBaseTestBuilder
39+
{
40+
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
41+
42+
distributedCacheBuilder(new WithoutDistributedCacheBuilder(actualBuilder.TestContext.HttpContext.RequestServices));
43+
44+
return actualBuilder.Builder;
45+
}
46+
}
47+
}

src/MyTested.AspNetCore.Mvc.Caching/Internal/Caching/DistributedCacheMock.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,19 @@ public bool TryGetCacheEntryOptions(string key, out DistributedCacheEntryOptions
8080
public Dictionary<string, byte[]> GetCacheAsDictionary()
8181
=> this.cache.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Value);
8282

83-
public void Dispose()
83+
public void RemoveKeys(IEnumerable<string> keys)
84+
{
85+
foreach (var key in keys)
86+
{
87+
this.Remove(key);
88+
}
89+
}
90+
91+
public void ClearCache()
8492
=> this.cache.Clear();
93+
94+
public void Dispose()
95+
=> this.ClearCache();
96+
8597
}
8698
}

src/MyTested.AspNetCore.Mvc.Caching/Internal/Contracts/IDistributedCacheMock.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@ public interface IDistributedCacheMock : IDistributedCache, IDisposable
1111
bool TryGetCacheEntryOptions(string key, out DistributedCacheEntryOptions cacheEntryOptions);
1212

1313
Dictionary<string, byte[]> GetCacheAsDictionary();
14+
15+
void RemoveKeys(IEnumerable<string> keys);
16+
17+
void ClearCache();
1418
}
1519
}

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

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
57
using Microsoft.AspNetCore.Mvc.ApplicationParts;
68
using Microsoft.Extensions.Caching.Distributed;
79
using Microsoft.Extensions.DependencyInjection;
@@ -268,6 +270,185 @@ public void WithCacheBuilderWithKeyBuilderAndAlsoShouldSetCorrectValues()
268270
.Ok();
269271
}
270272

273+
[Fact]
274+
public void WithoutDistributedCacheShouldReturnEmptyCache()
275+
{
276+
MyController<DistributedCacheController>
277+
.Instance()
278+
.WithDistributedCache(distributedCache => distributedCache
279+
.WithEntries(new Dictionary<string, string>
280+
{
281+
["first"] = "firstValue",
282+
["second"] = "secondValue",
283+
["third"] = "thirdValue"
284+
}))
285+
.WithoutDistributedCache()
286+
.Calling(c => c.GetCount(From.Services<IDistributedCache>()))
287+
.ShouldReturn()
288+
.Ok(ok => ok.WithModel(0));
289+
}
290+
291+
[Fact]
292+
public void WithoutDistributedCacheShouldReturnEmptyCacheWhenClearingAlreadyEmptyCache()
293+
{
294+
MyController<DistributedCacheController>
295+
.Instance()
296+
.WithDistributedCache(distributedCache => distributedCache
297+
.WithEntries(new Dictionary<string, string>()))
298+
.WithoutDistributedCache()
299+
.Calling(c => c.GetCount(From.Services<IDistributedCache>()))
300+
.ShouldReturn()
301+
.Ok(ok => ok.WithModel(0));
302+
}
303+
304+
[Fact]
305+
public void WithoutDistributedEntryCacheShouldReturnCorrectCacheData()
306+
{
307+
MyController<DistributedCacheController>
308+
.Instance()
309+
.WithDistributedCache(cache => cache
310+
.WithEntries(new Dictionary<string, string>
311+
{
312+
["first"] = "firstValue",
313+
["second"] = "secondValue",
314+
["third"] = "thirdValue"
315+
}))
316+
.WithoutDistributedCache(cache => cache.WithoutEntry("second"))
317+
.Calling(c => c.GetAllEntities(From.Services<IDistributedCache>()))
318+
.ShouldReturn()
319+
.Ok(ok => ok
320+
.WithModel(new SortedDictionary<string, byte[]>
321+
{
322+
["first"] = GetByteArray("firstValue"),
323+
["third"] = GetByteArray("thirdValue")
324+
}));
325+
}
326+
327+
[Fact]
328+
public void WithoutDistributedCacheByKeyShouldReturnCorrectCacheData()
329+
{
330+
MyController<DistributedCacheController>
331+
.Instance()
332+
.WithDistributedCache(cache => cache
333+
.WithEntries(new Dictionary<string, string>
334+
{
335+
["first"] = "firstValue",
336+
["second"] = "secondValue",
337+
["third"] = "thirdValue"
338+
}))
339+
.WithoutDistributedCache("second")
340+
.Calling(c => c.GetAllEntities(From.Services<IDistributedCache>()))
341+
.ShouldReturn()
342+
.Ok(ok => ok
343+
.WithModel(new SortedDictionary<string, byte[]>
344+
{
345+
["third"] = GetByteArray("thirdValue"),
346+
["first"] = GetByteArray("firstValue")
347+
}));
348+
}
349+
350+
[Fact]
351+
public void WithoutDistributedCacheByKeysShouldReturnCorrectCacheData()
352+
{
353+
var entities = new Dictionary<string, string>
354+
{
355+
["first"] = "firstValue",
356+
["second"] = "secondValue",
357+
["third"] = "thirdValue"
358+
};
359+
360+
var entriesToDelete = entities.Select(x => x.Key).ToList();
361+
entriesToDelete.RemoveAt(0);
362+
363+
MyController<DistributedCacheController>
364+
.Instance()
365+
.WithDistributedCache(cache => cache.WithEntries(entities))
366+
.WithoutDistributedCache(entriesToDelete)
367+
.Calling(c => c.GetAllEntities(From.Services<IDistributedCache>()))
368+
.ShouldReturn()
369+
.Ok(ok => ok
370+
.WithModel(new SortedDictionary<string, byte[]>
371+
{
372+
["first"] = GetByteArray("firstValue")
373+
}));
374+
}
375+
376+
[Fact]
377+
public void WithoutDistributedCacheByNonExistingKeysShouldReturnCorrectCacheData()
378+
{
379+
var entities = new Dictionary<string, byte[]>
380+
{
381+
["first"] = GetByteArray("firstValue"),
382+
["second"] = GetByteArray("secondValue"),
383+
["third"] = GetByteArray("thirdValue")
384+
};
385+
386+
var entitiesToDelete = new List<string> { "key1", "key2" };
387+
MyController<DistributedCacheController>
388+
.Instance()
389+
.WithDistributedCache(cache => cache
390+
.WithEntries(entities))
391+
.WithoutDistributedCache(entitiesToDelete)
392+
.Calling(c => c.GetAllEntities(From.Services<IDistributedCache>()))
393+
.ShouldReturn()
394+
.Ok(ok => ok
395+
.WithModel(new SortedDictionary<string, byte[]>(entities)));
396+
}
397+
398+
[Fact]
399+
public void WithoutDistributedCacheByNonExistingKeyShouldReturnCorrectCacheData()
400+
{
401+
var entities = new Dictionary<string, byte[]>
402+
{
403+
["first"] = GetByteArray("firstValue"),
404+
["second"] = GetByteArray("secondValue"),
405+
["third"] = GetByteArray("thirdValue")
406+
};
407+
408+
MyController<DistributedCacheController>
409+
.Instance()
410+
.WithDistributedCache(cache => cache
411+
.WithEntries(entities))
412+
.WithoutDistributedCache("firstValue")
413+
.Calling(c => c.GetAllEntities(From.Services<IDistributedCache>()))
414+
.ShouldReturn()
415+
.Ok(ok => ok
416+
.WithModel(new SortedDictionary<string, byte[]>(entities)));
417+
}
418+
419+
[Fact]
420+
public void WithoutDistributedCacheByParamKeysShouldReturnCorrectCacheData()
421+
{
422+
var entities = new Dictionary<string, string>
423+
{
424+
["first"] = "firstValue",
425+
["second"] = "secondValue",
426+
["third"] = "thirdValue"
427+
};
428+
429+
var entriesToDelete = entities.Select(x => x.Key).ToList();
430+
entriesToDelete.RemoveAt(0);
431+
432+
var encodedValue = GetByteArray("firstValue");
433+
434+
MyController<DistributedCacheController>
435+
.Instance()
436+
.WithDistributedCache(cache => cache.WithEntries(entities))
437+
.WithoutDistributedCache(entriesToDelete[0], entriesToDelete[1])
438+
.Calling(c => c.GetAllEntities(From.Services<IDistributedCache>()))
439+
.ShouldReturn()
440+
.Ok(ok => ok
441+
.WithModel(new SortedDictionary<string, byte[]>
442+
{
443+
["first"] = encodedValue
444+
})); ;
445+
}
446+
271447
public void Dispose() => MyApplication.StartsFrom<DefaultStartup>();
448+
449+
private static byte[] GetByteArray(string str)
450+
{
451+
return Encoding.ASCII.GetBytes(str);
452+
}
272453
}
273454
}

0 commit comments

Comments
 (0)