Skip to content

Commit

Permalink
feat(unit-tests): increase code coverage
Browse files Browse the repository at this point in the history
This commit adds:
- Improvements to existing unit test code
- Improvements to existing unit test naming
- Unit tests for `EFCacheKeyPrefixProvider` class
- Unit tests for `EFCacheKeyProvider` class
- Unit tests for `EFCacheKey` class
- Unit tests for `EFCacheServiceCheck` class
- Unit tests for `EFDataReaderLoader` class
- Unit tests for `EFDebugLogger` class
- Unit tests for `EFServiceCollectionExtensions` class
- Unit tests for `LockProvider` class
- Unit tests for `StringExtensions` class
- Unit tests for `TableEntityInfo` class
- Unit tests for `XxHash64Unsafe` class
  • Loading branch information
dmitrii-kiselev committed Oct 1, 2024
1 parent f08a7e2 commit ea968f5
Show file tree
Hide file tree
Showing 19 changed files with 2,900 additions and 54 deletions.
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

0 comments on commit ea968f5

Please sign in to comment.