Add tests for batch duplicate guard in rec action invoke
Added InvokeBatchDuplicateGuardTests to verify that invoking batch rec actions with an existing BatchId throws BadRequestException, while using a new BatchId does not trigger the duplicate guard. Tests use MediatR ISender and real database data for integration coverage. Added necessary using directives.
This commit is contained in:
@@ -0,0 +1,87 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DigitalData.Core.Exceptions;
|
||||||
|
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_BadRequestException()
|
||||||
|
{
|
||||||
|
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,
|
||||||
|
Last = 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
|
||||||
|
var ex = Assert.ThrowsAsync<BadRequestException>(async () =>
|
||||||
|
await sender.Send(new InvokeBatchRecActionViewsCommand
|
||||||
|
{
|
||||||
|
ProfileId = ProfileId,
|
||||||
|
References = new InvokeReferences
|
||||||
|
{
|
||||||
|
BatchId = existingBatchId!
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
Assert.That(ex!.Message, Does.Contain(existingBatchId!));
|
||||||
|
}
|
||||||
|
|
||||||
|
[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 BadRequestException 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 (BadRequestException badReq) when (badReq.Message.Contains("already results associated"))
|
||||||
|
{
|
||||||
|
Assert.Fail("Duplicate guard should not trigger for a unique BatchId.");
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Other exceptions (endpoint errors, etc.) are acceptable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user