-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#release-note: Added Tests to the Caching package #)
- Loading branch information
Farshad DASHTI
authored and
Farshad DASHTI
committed
Oct 9, 2024
1 parent
8c8c02b
commit 68bc138
Showing
6 changed files
with
212 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Tests/DfE.CoreLibs.Caching.Tests/DfE.CoreLibs.Caching.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\DfE.CoreLibs.Caching\DfE.CoreLibs.Caching.csproj" /> | ||
<ProjectReference Include="..\..\DfE.CoreLibs.Testing\DfE.CoreLibs.Testing.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
66 changes: 66 additions & 0 deletions
66
src/Tests/DfE.CoreLibs.Caching.Tests/Helpers/CacheKeyHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using DfE.CoreLibs.Caching.Helpers; | ||
|
||
namespace DfE.CoreLibs.Caching.Tests.Helpers | ||
{ | ||
public class CacheKeyHelperTests | ||
{ | ||
[Fact] | ||
public void GenerateHashedCacheKey_ShouldThrowArgumentException_WhenInputIsNullOrEmpty() | ||
{ | ||
// Arrange, Act & Assert | ||
Assert.Throws<ArgumentException>(() => CacheKeyHelper.GenerateHashedCacheKey(string.Empty)); | ||
Assert.Throws<ArgumentException>(() => CacheKeyHelper.GenerateHashedCacheKey((string)null!)); | ||
} | ||
|
||
[Fact] | ||
public void GenerateHashedCacheKey_ShouldReturnConsistentHash_WhenGivenSameInput() | ||
{ | ||
// Arrange | ||
var input = "test-input"; | ||
|
||
// Act | ||
var result1 = CacheKeyHelper.GenerateHashedCacheKey(input); | ||
var result2 = CacheKeyHelper.GenerateHashedCacheKey(input); | ||
|
||
// Assert | ||
Assert.Equal(result1, result2); | ||
} | ||
|
||
[Fact] | ||
public void GenerateHashedCacheKey_ShouldThrowArgumentException_WhenInputCollectionIsNullOrEmpty() | ||
{ | ||
// Arrange, Act & Assert | ||
Assert.Throws<ArgumentException>(() => CacheKeyHelper.GenerateHashedCacheKey((IEnumerable<string>)null!)); | ||
Assert.Throws<ArgumentException>(() => CacheKeyHelper.GenerateHashedCacheKey(new List<string>())); | ||
} | ||
|
||
[Fact] | ||
public void GenerateHashedCacheKey_ShouldReturnDifferentHashes_ForDifferentInputs() | ||
{ | ||
// Arrange | ||
var input1 = "input-1"; | ||
var input2 = "input-2"; | ||
|
||
// Act | ||
var result1 = CacheKeyHelper.GenerateHashedCacheKey(input1); | ||
var result2 = CacheKeyHelper.GenerateHashedCacheKey(input2); | ||
|
||
// Assert | ||
Assert.NotEqual(result1, result2); | ||
} | ||
|
||
[Fact] | ||
public void GenerateHashedCacheKey_ForCollection_ShouldReturnConsistentHash_WhenGivenSameInputs() | ||
{ | ||
// Arrange | ||
var inputs = new List<string> { "input-1", "input-2", "input-3" }; | ||
|
||
// Act | ||
var result1 = CacheKeyHelper.GenerateHashedCacheKey(inputs); | ||
var result2 = CacheKeyHelper.GenerateHashedCacheKey(inputs); | ||
|
||
// Assert | ||
Assert.Equal(result1, result2); | ||
} | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
src/Tests/DfE.CoreLibs.Caching.Tests/Services/MemoryCacheServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using AutoFixture; | ||
using DfE.CoreLibs.Caching.Services; | ||
using DfE.CoreLibs.Caching.Settings; | ||
using DfE.CoreLibs.Testing.AutoFixture.Attributes; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using NSubstitute; | ||
|
||
namespace DfE.CoreLibs.Caching.Tests.Services | ||
{ | ||
public class MemoryCacheServiceTests | ||
{ | ||
private readonly IFixture _fixture; | ||
private readonly IMemoryCache _memoryCache; | ||
private readonly ILogger<MemoryCacheService> _logger; | ||
private readonly IOptions<CacheSettings> _options; | ||
private readonly MemoryCacheService _cacheService; | ||
private readonly MemoryCacheSettings _cacheSettings; | ||
|
||
public MemoryCacheServiceTests() | ||
{ | ||
_fixture = new Fixture(); | ||
_memoryCache = Substitute.For<IMemoryCache>(); | ||
_logger = Substitute.For<ILogger<MemoryCacheService>>(); | ||
|
||
_cacheSettings = new MemoryCacheSettings { DefaultDurationInSeconds = 5, Durations = new Dictionary<string, int> { { "TestMethod", 10 } } }; | ||
var settings = new CacheSettings { Memory = _cacheSettings }; | ||
_options = Options.Create(settings); | ||
|
||
_cacheService = new MemoryCacheService(_memoryCache, _logger, _options); | ||
} | ||
|
||
[Theory] | ||
[CustomAutoData()] | ||
public async Task GetOrAddAsync_ShouldReturnCachedValue_WhenCacheKeyExists(string cacheKey, string methodName) | ||
{ | ||
// Arrange | ||
var cachedValue = _fixture.Create<string>(); | ||
_memoryCache.TryGetValue(cacheKey, out Arg.Any<object>()!).Returns(x => | ||
{ | ||
x[1] = cachedValue; | ||
return true; | ||
}); | ||
|
||
// Act | ||
var result = await _cacheService.GetOrAddAsync(cacheKey, () => Task.FromResult(cachedValue), methodName); | ||
|
||
// Assert | ||
Assert.Equal(cachedValue, result); | ||
_logger.Received(1).Log( | ||
LogLevel.Information, | ||
Arg.Any<EventId>(), | ||
Arg.Is<object>(v => v.ToString()!.Contains($"Cache hit for key: {cacheKey}")), | ||
Arg.Any<Exception>(), | ||
Arg.Any<Func<object, Exception, string>>()!); | ||
} | ||
|
||
[Theory] | ||
[CustomAutoData()] | ||
public async Task GetOrAddAsync_ShouldFetchAndCacheValue_WhenCacheKeyDoesNotExist(string cacheKey, string methodName) | ||
{ | ||
// Arrange | ||
var expectedValue = _fixture.Create<string>(); | ||
_memoryCache.TryGetValue(cacheKey, out Arg.Any<object>()).Returns(false); | ||
|
||
// Act | ||
var result = await _cacheService.GetOrAddAsync(cacheKey, () => Task.FromResult(expectedValue), methodName); | ||
|
||
// Assert | ||
Assert.Equal(expectedValue, result); | ||
_memoryCache.Received(1).Set(cacheKey, expectedValue, TimeSpan.FromSeconds(_cacheSettings.DefaultDurationInSeconds)); | ||
_logger.Received(1).Log( | ||
LogLevel.Information, | ||
Arg.Any<EventId>(), | ||
Arg.Is<object>(v => v.ToString()!.Contains($"Cache miss for key: {cacheKey}")), | ||
Arg.Any<Exception>(), | ||
Arg.Any<Func<object, Exception, string>>()!); | ||
_logger.Received(1).Log( | ||
LogLevel.Information, | ||
Arg.Any<EventId>(), | ||
Arg.Is<object>(v => v.ToString()!.Contains($"Cached result for key: {cacheKey}")), | ||
Arg.Any<Exception>(), | ||
Arg.Any<Func<object, Exception, string>>()!); | ||
} | ||
|
||
[Theory] | ||
[CustomAutoData()] | ||
public void Remove_ShouldRemoveValueFromCache_WhenCalled(string cacheKey) | ||
{ | ||
// Act | ||
_cacheService.Remove(cacheKey); | ||
|
||
// Assert | ||
_memoryCache.Received(1).Remove(cacheKey); | ||
_logger.Received(1).Log( | ||
LogLevel.Information, | ||
Arg.Any<EventId>(), | ||
Arg.Is<object>(v => v.ToString()!.Contains($"Cache removed for key: {cacheKey}")), | ||
Arg.Any<Exception>(), | ||
Arg.Any<Func<object, Exception, string>>()!); | ||
} | ||
} | ||
} |