Skip to content

Commit 748ded9

Browse files
committed
Implemented overloads with string values (#247)
1 parent 4aea38a commit 748ded9

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/DistributedCacheBuilder.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,25 @@ public IAndDistributedCacheBuilder WithEntry(string key, byte[] value)
3131
return this;
3232
}
3333

34+
public IAndDistributedCacheBuilder WithEntry(string key, string value)
35+
{
36+
this.DistributedCache.SetString(key, value);
37+
return this;
38+
}
39+
3440
/// <inheritdoc />
3541
public IAndDistributedCacheBuilder WithEntry(string key, byte[] value, DistributedCacheEntryOptions options)
3642
{
3743
this.DistributedCache.Set(key, value, options);
3844
return this;
3945
}
4046

47+
public IAndDistributedCacheBuilder WithEntry(string key, string value, DistributedCacheEntryOptions options)
48+
{
49+
this.DistributedCache.SetString(key, value, options);
50+
return this;
51+
}
52+
4153
public IAndDistributedCacheBuilder WithEntry(Action<IDistributedCacheEntryKeyBuilder> distributedCacheEntryBuilder)
4254
{
4355
var newDistributedCacheEntryBuilder = new DistributedCacheEntryBuilder();
@@ -61,6 +73,12 @@ public IAndDistributedCacheBuilder WithEntries(IDictionary<string, byte[]> entri
6173
return this;
6274
}
6375

76+
public IAndDistributedCacheBuilder WithEntries(IDictionary<string, string> entries)
77+
{
78+
entries.ForEach(e => this.WithEntry(e.Key, e.Value));
79+
return this;
80+
}
81+
6482
/// <inheritdoc />
6583
public IDistributedCacheBuilder AndAlso() => this;
6684
}

src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/DistributedCacheEntryBuilder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using Contracts.Data.DistributedCache;
55
using Internal.Caching;
6+
using Utilities;
67

78
public class DistributedCacheEntryBuilder : IDistributedCacheEntryKeyBuilder, IAndDistributedCacheEntryBuilder
89
{
@@ -25,6 +26,12 @@ public IAndDistributedCacheEntryBuilder WithValue(byte[] value)
2526
return this;
2627
}
2728

29+
public IAndDistributedCacheEntryBuilder WithValue(string value)
30+
{
31+
this.WithValue(BytesHelper.GetBytes(value));
32+
return this;
33+
}
34+
2835
public IAndDistributedCacheEntryBuilder WithAbsoluteExpiration(DateTimeOffset? absoluteExpiration)
2936
{
3037
this.DistributedCacheEntry.Options.AbsoluteExpiration = absoluteExpiration;

src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/DistributedCacheEntryTestBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public DistributedCacheEntryTestBuilder(ComponentTestContext testContext)
4848
return this;
4949
}
5050

51+
public new IAndDistributedCacheEntryTestBuilder WithValue(string value)
52+
{
53+
this.WithValue(BytesHelper.GetBytes(value));
54+
return this;
55+
}
56+
5157
public new IAndDistributedCacheEntryTestBuilder WithAbsoluteExpiration(DateTimeOffset? absoluteExpiration)
5258
{
5359
this.validations.Add((expected, actual) =>

src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/DistributedCache/DistributedCacheTestBuilder.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,34 @@ public DistributedCacheTestBuilder(ComponentTestContext testContext)
3939
this.distributedCacheDictionary = this.GetDistributedCacheDictionary();
4040
}
4141

42+
public IAndDistributedCacheTestBuilder ContainingEntryWithValue(string value)
43+
{
44+
this.ContainingEntryWithValue(BytesHelper.GetBytes(value));
45+
return this;
46+
}
47+
4248
/// <inheritdoc />
4349
public IAndDistributedCacheTestBuilder ContainingEntries(IDictionary<string, byte[]> entries)
4450
{
4551
var expectedItems = entries.Count;
46-
var actualItems = ((IDistributedCacheMock)distributedCache).Count; //TODO: check where count is needed and either copy mem cache implementation or provide way to get count.
52+
var actualItems = this.GetDistributedCacheMock().Count;
53+
54+
if (expectedItems != actualItems)
55+
{
56+
this.ThrowNewDataProviderAssertionException(
57+
DistributedCacheName,
58+
$"to have {expectedItems} {(expectedItems != 1 ? "entries" : "entry")}",
59+
$"in fact found {actualItems}");
60+
}
61+
62+
entries.ForEach(e => this.ContainingEntry(e.Key, e.Value));
63+
return this;
64+
}
65+
66+
public IAndDistributedCacheTestBuilder ContainingEntries(IDictionary<string, string> entries)
67+
{
68+
var expectedItems = entries.Count;
69+
var actualItems = this.GetDistributedCacheMock().Count;
4770

4871
if (expectedItems != actualItems)
4972
{
@@ -72,6 +95,12 @@ public IAndDistributedCacheTestBuilder ContainingEntry(string key, byte[] value)
7295
return this;
7396
}
7497

98+
public IAndDistributedCacheTestBuilder ContainingEntry(string key, string value)
99+
{
100+
this.ContainingEntry(key, BytesHelper.GetBytes(value));
101+
return this;
102+
}
103+
75104
/// <inheritdoc />
76105
public IAndDistributedCacheTestBuilder ContainingEntry(string key, byte[] value, DistributedCacheEntryOptions options)
77106
{
@@ -90,6 +119,12 @@ public IAndDistributedCacheTestBuilder ContainingEntry(string key, byte[] value,
90119
return this;
91120
}
92121

122+
public IAndDistributedCacheTestBuilder ContainingEntry(string key, string value, DistributedCacheEntryOptions options)
123+
{
124+
this.ContainingEntry(key, BytesHelper.GetBytes(value), options);
125+
return this;
126+
}
127+
93128
public IAndDistributedCacheTestBuilder ContainingEntry(Action<IDistributedCacheEntryKeyTestBuilder> distributedCacheEntryTestBuilder)
94129
{
95130
var mockedDistributedCache = this.GetDistributedCacheMock();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace MyTested.AspNetCore.Mvc.Utilities
2+
{
3+
using System.Text;
4+
5+
/// <summary>
6+
/// Contains helper methods that provide byte representations of types.
7+
/// </summary>
8+
public static class BytesHelper
9+
{
10+
/// <summary>
11+
/// Returns the byte representation of an UTF8 string.
12+
/// </summary>
13+
/// <param name="value">The string to get the byte representation of.</param>
14+
/// <returns></returns>
15+
public static byte[] GetBytes(string value) => Encoding.UTF8.GetBytes(value);
16+
}
17+
}

0 commit comments

Comments
 (0)