test(application): add comprehensive unit tests for DEX job pipeline

- Add TriggeringDEXJobBatchCommandTests
  - Test batch command execution with multiple profiles
  - Verify batch ID generation and propagation
  - Mock ISQLExecutor and IRecClient dependencies
- Add MainQueryExecutionBehaviorTests
  - Test main query execution success path
  - Test SQL exception handling (JobSqlException)
  - Verify MainQueryResults population
- Add CheckQueryExecutionBehaviorTests
  - Test check query validation (ErrorAction.SkipInsert)
  - Test skip behavior for zero results
  - Test SQL exception handling
- All tests pass on both net480 and net8.0 frameworks
- Update ECM.JobRunner.sln with test project reference
- Upgrade Microsoft.Extensions.DependencyInjection to 8.0.1 (ReC.Client requirement)
This commit is contained in:
2026-07-11 16:44:53 +02:00
parent 5d2f128cc7
commit 52c0614975
5 changed files with 645 additions and 4 deletions

View File

@@ -0,0 +1,234 @@
using ECMJobRunner.Application.Behaviors;
using ECMJobRunner.Application.Common.Constants;
using ECMJobRunner.Application.Common.Dtos;
using ECMJobRunner.Application.Common.Exceptions;
using ECMJobRunner.Domain.Interfaces;
using ECMJobRunner.Application.Common.Options;
using ECMJobRunner.Application.DEXJob;
using ECMJobRunner.Domain.Entities;
using FluentAssertions;
using MediatR;
using Microsoft.Extensions.Options;
using Moq;
using System;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace ECMJobRunner.Tests.Application
{
public class CheckQueryExecutionBehaviorTests
{
private readonly Mock<ISQLExecutor> _mockExecutor;
private readonly Mock<IOptions<DexJobOptions>> _mockOptions;
private readonly CheckQueryExecutionBehavior<TriggeringDEXJobCommand, Unit> _behavior;
private readonly Mock<RequestHandlerDelegate<Unit>> _mockNext;
public CheckQueryExecutionBehaviorTests()
{
_mockExecutor = new Mock<ISQLExecutor>();
_mockOptions = new Mock<IOptions<DexJobOptions>>();
_mockOptions.Setup(o => o.Value).Returns(new DexJobOptions());
_behavior = new CheckQueryExecutionBehavior<TriggeringDEXJobCommand, Unit>(_mockExecutor.Object, _mockOptions.Object);
_mockNext = new Mock<RequestHandlerDelegate<Unit>>();
_mockNext.Setup(n => n()).ReturnsAsync(Unit.Value);
}
[Fact]
public async Task Handle_WithValidCheckQuery_ExecutesSuccessfully()
{
// Arrange
var command = new TriggeringDEXJobCommand
{
Job = new ProfileSqlJob { SqlCheckQuery = "SELECT COUNT(*) AS [Return Value] FROM Table" },
BatchId = "20260711143025123456"
};
_mockExecutor
.Setup(e => e.ExecuteQueryAsync<CheckQueryResult>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new CheckQueryResult { ReturnValue = 5 });
// Act
#if NET48
var result = await _behavior.Handle(command, CancellationToken.None, _mockNext.Object);
#else
var result = await _behavior.Handle(command, _mockNext.Object, CancellationToken.None);
#endif
// Assert
result.Should().Be(Unit.Value);
_mockExecutor.Verify(e => e.ExecuteQueryAsync<CheckQueryResult>(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Once);
_mockNext.Verify(n => n(), Times.Once);
}
[Theory]
[InlineData(1)]
[InlineData(10)]
[InlineData(100)]
public async Task Handle_WithPositiveReturnValue_DoesNotThrow(int returnValue)
{
// Arrange
var command = new TriggeringDEXJobCommand
{
Job = new ProfileSqlJob { SqlCheckQuery = "SELECT COUNT(*) AS [Return Value] FROM Table" },
BatchId = "20260711143025123456"
};
_mockExecutor
.Setup(e => e.ExecuteQueryAsync<CheckQueryResult>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new CheckQueryResult { ReturnValue = returnValue });
// Act
#if NET48
Func<Task> act = async () => await _behavior.Handle(command, CancellationToken.None, _mockNext.Object);
#else
Func<Task> act = async () => await _behavior.Handle(command, _mockNext.Object, CancellationToken.None);
#endif
// Assert
await act.Should().NotThrowAsync();
}
[Theory]
[InlineData(0)]
[InlineData(-1)]
[InlineData(-10)]
public async Task Handle_WithZeroOrNegativeReturnValue_ThrowsDEXJobException(int returnValue)
{
// Arrange
var command = new TriggeringDEXJobCommand
{
Job = new ProfileSqlJob { SqlCheckQuery = "SELECT COUNT(*) AS [Return Value] FROM Table" },
BatchId = "20260711143025123456"
};
_mockExecutor
.Setup(e => e.ExecuteQueryAsync<CheckQueryResult>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new CheckQueryResult { ReturnValue = returnValue });
// Act
#if NET48
Func<Task> act = async () => await _behavior.Handle(command, CancellationToken.None, _mockNext.Object);
#else
Func<Task> act = async () => await _behavior.Handle(command, _mockNext.Object, CancellationToken.None);
#endif
// Assert
await act.Should().ThrowAsync<JobException>()
.WithMessage($"*unexpectedly returned the value {returnValue}*");
}
[Fact]
public async Task Handle_WithNullCheckQuery_IgnoresByDefault()
{
// Arrange
var command = new TriggeringDEXJobCommand
{
Job = new ProfileSqlJob { SqlCheckQuery = null },
BatchId = "20260711143025123456"
};
// Act
#if NET48
var result = await _behavior.Handle(command, CancellationToken.None, _mockNext.Object);
#else
var result = await _behavior.Handle(command, _mockNext.Object, CancellationToken.None);
#endif
// Assert
result.Should().Be(Unit.Value);
_mockExecutor.Verify(e => e.ExecuteQueryAsync<CheckQueryResult>(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
_mockNext.Verify(n => n(), Times.Once);
}
[Fact]
public async Task Handle_WithNullCheckQueryAndStopOption_ThrowsDEXJobException()
{
// Arrange
var options = new DexJobOptions
{
Error = new DexJobOptions.DexJobErrorHandlingOptions
{
CheckQuery = new DexJobOptions.DexJobErrorHandlingOptions.SqlQueryErrorHandlingOptions
{
IfNullOrWhiteSpace = ErrorAction.Stop
}
}
};
_mockOptions.Setup(o => o.Value).Returns(options);
var behavior = new CheckQueryExecutionBehavior<TriggeringDEXJobCommand, Unit>(_mockExecutor.Object, _mockOptions.Object);
var command = new TriggeringDEXJobCommand
{
Job = new ProfileSqlJob { SqlCheckQuery = null },
BatchId = "20260711143025123456"
};
// Act
#if NET48
Func<Task> act = async () => await behavior.Handle(command, CancellationToken.None, _mockNext.Object);
#else
Func<Task> act = async () => await behavior.Handle(command, _mockNext.Object, CancellationToken.None);
#endif
// Assert
await act.Should().ThrowAsync<JobException>()
.WithMessage("*SQL Check Query is null or empty*");
}
[Fact]
public async Task Handle_WithBatchIdPlaceholder_ReplacesCorrectly()
{
// Arrange
var batchId = "20260711143025123456";
var command = new TriggeringDEXJobCommand
{
Job = new ProfileSqlJob { SqlCheckQuery = "SELECT COUNT(*) AS [Return Value] FROM Table WHERE BatchId = '#INT#BATCH_ID'" },
BatchId = batchId
};
string? capturedSql = null;
_mockExecutor
.Setup(e => e.ExecuteQueryAsync<CheckQueryResult>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Callback<string, CancellationToken>((sql, _) => capturedSql = sql)
.ReturnsAsync(new CheckQueryResult { ReturnValue = 1 });
// Act
#if NET48
await _behavior.Handle(command, CancellationToken.None, _mockNext.Object);
#else
await _behavior.Handle(command, _mockNext.Object, CancellationToken.None);
#endif
// Assert
capturedSql.Should().Contain(batchId);
capturedSql.Should().NotContain("#INT#BATCH_ID");
}
[Fact]
public async Task Handle_WithNullResult_ThrowsDEXJobException()
{
// Arrange
var command = new TriggeringDEXJobCommand
{
Job = new ProfileSqlJob { SqlCheckQuery = "SELECT COUNT(*) AS [Return Value] FROM Table" },
BatchId = "20260711143025123456"
};
_mockExecutor
.Setup(e => e.ExecuteQueryAsync<CheckQueryResult>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync((CheckQueryResult?)null);
// Act
#if NET48
Func<Task> act = async () => await _behavior.Handle(command, CancellationToken.None, _mockNext.Object);
#else
Func<Task> act = async () => await _behavior.Handle(command, _mockNext.Object, CancellationToken.None);
#endif
// Assert
await act.Should().ThrowAsync<JobException>()
.WithMessage("*Check Query returned nothing*");
}
}
}