Files
ReC/tests/ReC.Tests/Application/RecActions/InvokeBatchDuplicateGuardTests.cs
TekH f5b2db0296 Update tests to expect ValidationException for BatchId dupes
Refactored InvokeBatchDuplicateGuardTests to expect and assert
FluentValidation's ValidationException instead of the custom
BadRequestException when a duplicate BatchId is submitted.
Assertions and comments were updated accordingly, and the
DigitalData.Core.Exceptions import was removed. Test logic
remains unchanged.
2026-04-16 14:15:21 +02:00

88 lines
2.9 KiB
C#

using System.Linq;
using System.Threading.Tasks;
using FluentValidation;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using ReC.Application.RecActions.Commands;
using ReC.Application.Results.Queries;
namespace ReC.Tests.Application.RecActions;
[TestFixture]
public class InvokeBatchDuplicateGuardTests : RecApplicationTestBase
{
private const long ProfileId = 3;
private (ISender Sender, IServiceScope Scope) CreateScopedSender()
{
var scope = ServiceProvider.CreateScope();
var sender = scope.ServiceProvider.GetRequiredService<ISender>();
return (sender, scope);
}
[Test]
public async Task Invoke_with_existing_batchId_throws_ValidationException()
{
var (sender, scope) = CreateScopedSender();
using var _ = scope;
// Arrange: read an existing result to get a real BatchId from the database
var results = await sender.Send(new ReadResultViewQuery
{
ProfileId = ProfileId,
IncludeAction = false,
LastBatch = true
});
var existingBatchId = results.FirstOrDefault()?.BatchId;
Assert.That(existingBatchId, Is.Not.Null.And.Not.Empty,
$"No results with a BatchId found for ProfileId {ProfileId}. Ensure test data exists in the database.");
// Act & Assert: invoking with the same BatchId should throw ValidationException
var ex = Assert.ThrowsAsync<ValidationException>(async () =>
await sender.Send(new InvokeBatchRecActionViewsCommand
{
ProfileId = ProfileId,
References = new InvokeReferences
{
BatchId = existingBatchId!
}
}));
Assert.That(ex!.Errors.Any(e => e.PropertyName.Contains("BatchId")));
}
[Test]
public void Invoke_with_new_batchId_does_not_throw_duplicate_guard()
{
var (sender, scope) = CreateScopedSender();
using var _ = scope;
var uniqueBatchId = $"test-{System.Guid.NewGuid():N}";
// This should NOT throw ValidationException for duplicate BatchId.
// It may throw other exceptions (e.g., no actions found, endpoint errors),
// but the duplicate guard should pass.
try
{
sender.Send(new InvokeBatchRecActionViewsCommand
{
ProfileId = ProfileId,
References = new InvokeReferences
{
BatchId = uniqueBatchId
}
}).GetAwaiter().GetResult();
}
catch (ValidationException valEx) when (valEx.Errors.Any(e => e.PropertyName.Contains("BatchId")))
{
Assert.Fail("Duplicate guard should not trigger for a unique BatchId.");
}
catch
{
// Other exceptions (endpoint errors, etc.) are acceptable
}
}
}