Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(unit-tests): increase code coverage #252

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/EFCoreSecondLevelCacheInterceptor/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("EFCoreSecondLevelCacheInterceptor.UnitTests")]

namespace EFCoreSecondLevelCacheInterceptor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,39 @@ public class DbCommandInterceptorProcessorTests
{
private readonly IDbCommandInterceptorProcessor _processor;
private readonly Mock<IEFDebugLogger> _loggerMock;
private readonly Mock<ILogger<DbCommandInterceptorProcessor>> _interceptorProcessorLoggerMock;
private readonly Mock<IEFCacheServiceProvider> _cacheServiceMock;
private readonly Mock<IEFCacheDependenciesProcessor> _cacheDependenciesProcessorMock;
private readonly Mock<IEFCacheKeyProvider> _cacheKeyProviderMock;
private readonly Mock<IEFCachePolicyParser> _cachePolicyParserMock;
private readonly Mock<IEFSqlCommandsProcessor> _sqlCommandsProcessorMock;
private readonly Mock<IOptions<EFCoreSecondLevelCacheSettings>> _cacheSettingsMock;
private readonly Mock<IEFCacheServiceCheck> _cacheServiceCheckMock;
private readonly EFCoreSecondLevelCacheSettings _cacheSettings;

public DbCommandInterceptorProcessorTests()
{
var interceptorProcessorLoggerMock = new Mock<ILogger<DbCommandInterceptorProcessor>>();
var cacheSettingsMock = new Mock<IOptions<EFCoreSecondLevelCacheSettings>>();

_loggerMock = new Mock<IEFDebugLogger>();
_interceptorProcessorLoggerMock = new Mock<ILogger<DbCommandInterceptorProcessor>>();
_cacheServiceMock = new Mock<IEFCacheServiceProvider>();
_cacheDependenciesProcessorMock = new Mock<IEFCacheDependenciesProcessor>();
_cacheKeyProviderMock = new Mock<IEFCacheKeyProvider>();
_cachePolicyParserMock = new Mock<IEFCachePolicyParser>();
_sqlCommandsProcessorMock = new Mock<IEFSqlCommandsProcessor>();
_cacheSettingsMock = new Mock<IOptions<EFCoreSecondLevelCacheSettings>>();
_cacheServiceCheckMock = new Mock<IEFCacheServiceCheck>();
_cacheSettings = new EFCoreSecondLevelCacheSettings();

_cacheSettingsMock.SetupGet(x => x.Value).Returns(_cacheSettings);
cacheSettingsMock.SetupGet(x => x.Value).Returns(_cacheSettings);

_processor = new DbCommandInterceptorProcessor(
_loggerMock.Object,
_interceptorProcessorLoggerMock.Object,
interceptorProcessorLoggerMock.Object,
_cacheServiceMock.Object,
_cacheDependenciesProcessorMock.Object,
_cacheKeyProviderMock.Object,
_cachePolicyParserMock.Object,
_sqlCommandsProcessorMock.Object,
_cacheSettingsMock.Object,
cacheSettingsMock.Object,
_cacheServiceCheckMock.Object);
}

Expand All @@ -71,6 +70,7 @@ public void Constructor_ThrowsArgumentNullException_WhenCacheSettingsIsNull()
cacheKeyProvider,
cachePolicyParser,
sqlCommandsProcessor,
// ReSharper disable once AssignNullToNotNullAttribute
null,
cacheServiceCheck));
}
Expand Down Expand Up @@ -112,6 +112,8 @@ public void ProcessExecutedCommands_ReturnsExpectedResultWithoutPrecessing_WhenD
DbContext context = null;

// Act
// ReSharper disable once ExpressionIsAlwaysNull
// ReSharper disable once AssignNullToNotNullAttribute
var actual = _processor.ProcessExecutedCommands<object>(null, context, null);

// Assert
Expand All @@ -125,6 +127,7 @@ public void ProcessExecutedCommands_ReturnsExpectedResultWithoutPrecessing_WhenC
var context = Mock.Of<DbContext>();

// Act
// ReSharper disable once AssignNullToNotNullAttribute
var actual = _processor.ProcessExecutedCommands<object>(null, context, null);

// Assert
Expand Down Expand Up @@ -222,7 +225,7 @@ public void
_cacheSettings.SkipCachingDbContexts = new List<Type> { context.GetType() };

// Act
var actual = _processor.ProcessExecutedCommands(command, context, result);
_processor.ProcessExecutedCommands(command, context, result);

// Assert
_loggerMock.Verify(x => x.NotifyCacheableEvent(
Expand Down Expand Up @@ -861,7 +864,7 @@ public void ProcessExecutedCommands_ReturnsExpectedResult_WhenObjectDataAddedToC
public void ProcessExecutedCommands_ReturnsNull_WhenResultIsNull()
{
// Arrange
object expected = null;
object result = null;

var commandMock = new Mock<DbCommand>();
var transaction = Mock.Of<DbTransaction>();
Expand All @@ -879,7 +882,8 @@ public void ProcessExecutedCommands_ReturnsNull_WhenResultIsNull()
_cacheSettings.AllowCachingWithExplicitTransactions = true;

// Act
var actual = _processor.ProcessExecutedCommands(commandMock.Object, context, expected);
// ReSharper disable once ExpressionIsAlwaysNull
var actual = _processor.ProcessExecutedCommands(commandMock.Object, context, result);

// Assert
Assert.Null(actual);
Expand All @@ -892,6 +896,8 @@ public void ProcessExecutingCommands_ReturnsExpectedResultWithoutPrecessing_When
DbContext context = null;

// Act
// ReSharper disable once AssignNullToNotNullAttribute
// ReSharper disable once ExpressionIsAlwaysNull
var actual = _processor.ProcessExecutingCommands<object>(null, context, null);

// Assert
Expand All @@ -905,6 +911,7 @@ public void ProcessExecutingCommands_ReturnsExpectedResultWithoutPrecessing_When
var context = Mock.Of<DbContext>();

// Act
// ReSharper disable once AssignNullToNotNullAttribute
var actual = _processor.ProcessExecutingCommands<object>(null, context, null);

// Assert
Expand Down Expand Up @@ -1367,6 +1374,7 @@ public void ProcessExecutingCommands_NotifiesCachingSkippedEvent_WhenResultIsNul
_cacheSettings.AllowCachingWithExplicitTransactions = true;

// Act
// ReSharper disable once ExpressionIsAlwaysNull
_processor.ProcessExecutingCommands(commandMock.Object, context, result);

// Assert
Expand Down Expand Up @@ -1397,6 +1405,7 @@ public void ProcessExecutingCommands_ReturnsNull_WhenResultIsNull()
_cacheSettings.AllowCachingWithExplicitTransactions = true;

// Act
// ReSharper disable once ExpressionIsAlwaysNull
var actual = _processor.ProcessExecutingCommands(commandMock.Object, context, result);

// Assert
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Microsoft.Extensions.Options;
using Moq;

namespace EFCoreSecondLevelCacheInterceptor.UnitTests;

// ReSharper disable once InconsistentNaming
public class EFCacheKeyPrefixProviderTests
{
[Fact]
public void Constructor_ThrowsArgumentNullException_WhenCacheSettingsIsNull()
{
// Arrange
var serviceProvider = new Mock<IServiceProvider>().Object;

// ReSharper disable once ObjectCreationAsStatement
void Act() => new EFCacheKeyPrefixProvider(serviceProvider, null!);

// Act && Assert
Assert.Throws<ArgumentNullException>("cacheSettings", Act);
}

[Fact]
public void GetCacheKeyPrefix_ReturnsCacheKeyPrefixSelectorResult_WhenSelectorIsNotNull()
{
// Arrange
var serviceProvider = new Mock<IServiceProvider>().Object;
var cacheSettings = Options.Create(new EFCoreSecondLevelCacheSettings
{
CacheKeyPrefixSelector = _ => "CustomPrefix"
});

var provider = new EFCacheKeyPrefixProvider(serviceProvider, cacheSettings);

// Act
var actual = provider.GetCacheKeyPrefix();

// Assert
Assert.Equal("CustomPrefix", actual);
}

[Fact]
public void GetCacheKeyPrefix_ReturnsCacheKeyPrefix_WhenSelectorIsNull()
{
// Arrange
var serviceProvider = new Mock<IServiceProvider>().Object;
var cacheSettings = Options.Create(new EFCoreSecondLevelCacheSettings
{
CacheKeyPrefix = "DefaultPrefix",
CacheKeyPrefixSelector = null
});

var provider = new EFCacheKeyPrefixProvider(serviceProvider, cacheSettings);

// Act
var actual = provider.GetCacheKeyPrefix();

// Assert
Assert.Equal("DefaultPrefix", actual);
}
}
Loading
Loading