-
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.
- Loading branch information
Showing
6 changed files
with
204 additions
and
134 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
19 changes: 19 additions & 0 deletions
19
src/KeyValuePush.Redis.Tests/KeyValuePush.Redis.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,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" /> | ||
<PackageReference Include="StackExchange.Redis" Version="2.7.17" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" /> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0" /> | ||
<PackageReference Include="Moq" Version="4.20.2" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\KeyValuePush.Redis\AutoGuru.KeyValuePush.Redis.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,29 @@ | ||
using Moq; | ||
using StackExchange.Redis; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace AutoGuru.KeyValuePush.Redis.Tests | ||
{ | ||
public class RedisPusherTests | ||
{ | ||
private readonly Mock<IDatabase> _mockDatabase; | ||
private readonly RedisPusher _pusher; | ||
|
||
public RedisPusherTests() | ||
{ | ||
_mockDatabase = new Mock<IDatabase>(); | ||
_pusher = new RedisPusher(); | ||
|
||
// Usamos reflexión para inyectar el mock en el campo privado | ||
var field = typeof(RedisPusher).GetField("_db", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | ||
field?.SetValue(_pusher, _mockDatabase.Object); | ||
} | ||
|
||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AutoGuru.KeyValuePush; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace KeyValuePush.Tests | ||
{ | ||
public class DefaultExecutorTests | ||
{ | ||
private readonly Mock<IDictionaryBuilder> _mockDictionaryBuilder; | ||
private readonly Mock<IPusher> _mockPusher; | ||
private readonly DefaultExecutor _executor; | ||
|
||
public DefaultExecutorTests() | ||
{ | ||
_mockDictionaryBuilder = new Mock<IDictionaryBuilder>(); | ||
_mockPusher = new Mock<IPusher>(); | ||
_executor = new DefaultExecutor(_mockDictionaryBuilder.Object, _mockPusher.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteAsync_ShouldReturnZero_WhenSuccessful() | ||
{ | ||
// Arrange | ||
var dict = new Dictionary<string, string> { { "key1", "value1" } }; | ||
_mockDictionaryBuilder | ||
.Setup(builder => builder.BuildAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<SearchOption>(), It.IsAny<bool>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(dict); | ||
_mockPusher | ||
.Setup(pusher => pusher.PushAsync(It.IsAny<IDictionary<string, string>>(), It.IsAny<CancellationToken>())) | ||
.Returns(Task.CompletedTask); | ||
|
||
// Act | ||
var result = await _executor.ExecuteAsync("path", "*.txt", SearchOption.TopDirectoryOnly, false); | ||
|
||
// Assert | ||
Assert.Equal(0, result); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteAsync_ShouldReturnBuilderError_WhenBuildAsyncFails() | ||
{ | ||
// Arrange | ||
_mockDictionaryBuilder | ||
.Setup(builder => builder.BuildAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<SearchOption>(), It.IsAny<bool>(), It.IsAny<CancellationToken>())) | ||
.ThrowsAsync(new Exception("Build error")); | ||
|
||
// Act | ||
var result = await _executor.ExecuteAsync("path", "*.txt", SearchOption.TopDirectoryOnly, false); | ||
|
||
// Assert | ||
Assert.Equal((int)DefaultExecutor.ErrorCode.BuilderError, result); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteAsync_ShouldReturnPusherError_WhenPushAsyncFails() | ||
{ | ||
// Arrange | ||
var dict = new Dictionary<string, string> { { "key1", "value1" } }; | ||
_mockDictionaryBuilder | ||
.Setup(builder => builder.BuildAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<SearchOption>(), It.IsAny<bool>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(dict); | ||
_mockPusher | ||
.Setup(pusher => pusher.PushAsync(It.IsAny<IDictionary<string, string>>(), It.IsAny<CancellationToken>())) | ||
.ThrowsAsync(new Exception("Push error")); | ||
|
||
// Act | ||
var result = await _executor.ExecuteAsync("path", "*.txt", SearchOption.TopDirectoryOnly, false); | ||
|
||
// Assert | ||
Assert.Equal((int)DefaultExecutor.ErrorCode.PusherError, result); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteAsync_ShouldLogErrorMessage_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var dict = new Dictionary<string, string> { { "key1", "value1" } }; | ||
_mockDictionaryBuilder | ||
.Setup(builder => builder.BuildAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<SearchOption>(), It.IsAny<bool>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(dict); | ||
_mockPusher | ||
.Setup(pusher => pusher.PushAsync(It.IsAny<IDictionary<string, string>>(), It.IsAny<CancellationToken>())) | ||
.ThrowsAsync(new Exception("Push error")); | ||
|
||
using var consoleOutput = new StringWriter(); | ||
Console.SetError(consoleOutput); | ||
|
||
// Act | ||
await _executor.ExecuteAsync("path", "*.txt", SearchOption.TopDirectoryOnly, false); | ||
|
||
// Assert | ||
var output = consoleOutput.ToString(); | ||
Assert.Contains("Unexpected error!", output); | ||
Assert.Contains("Push error", output); | ||
} | ||
} | ||
} |
Oops, something went wrong.