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.
This commit is contained in:
2026-04-16 14:15:21 +02:00
parent 7a22024624
commit f5b2db0296

View File

@@ -1,6 +1,6 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using DigitalData.Core.Exceptions; using FluentValidation;
using MediatR; using MediatR;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework; using NUnit.Framework;
@@ -22,7 +22,7 @@ public class InvokeBatchDuplicateGuardTests : RecApplicationTestBase
} }
[Test] [Test]
public async Task Invoke_with_existing_batchId_throws_BadRequestException() public async Task Invoke_with_existing_batchId_throws_ValidationException()
{ {
var (sender, scope) = CreateScopedSender(); var (sender, scope) = CreateScopedSender();
using var _ = scope; using var _ = scope;
@@ -39,8 +39,8 @@ public class InvokeBatchDuplicateGuardTests : RecApplicationTestBase
Assert.That(existingBatchId, Is.Not.Null.And.Not.Empty, 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."); $"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 // Act & Assert: invoking with the same BatchId should throw ValidationException
var ex = Assert.ThrowsAsync<BadRequestException>(async () => var ex = Assert.ThrowsAsync<ValidationException>(async () =>
await sender.Send(new InvokeBatchRecActionViewsCommand await sender.Send(new InvokeBatchRecActionViewsCommand
{ {
ProfileId = ProfileId, ProfileId = ProfileId,
@@ -50,7 +50,7 @@ public class InvokeBatchDuplicateGuardTests : RecApplicationTestBase
} }
})); }));
Assert.That(ex!.Message, Does.Contain(existingBatchId!)); Assert.That(ex!.Errors.Any(e => e.PropertyName.Contains("BatchId")));
} }
[Test] [Test]
@@ -61,7 +61,7 @@ public class InvokeBatchDuplicateGuardTests : RecApplicationTestBase
var uniqueBatchId = $"test-{System.Guid.NewGuid():N}"; var uniqueBatchId = $"test-{System.Guid.NewGuid():N}";
// This should NOT throw BadRequestException for duplicate BatchId. // This should NOT throw ValidationException for duplicate BatchId.
// It may throw other exceptions (e.g., no actions found, endpoint errors), // It may throw other exceptions (e.g., no actions found, endpoint errors),
// but the duplicate guard should pass. // but the duplicate guard should pass.
try try
@@ -75,7 +75,7 @@ public class InvokeBatchDuplicateGuardTests : RecApplicationTestBase
} }
}).GetAwaiter().GetResult(); }).GetAwaiter().GetResult();
} }
catch (BadRequestException badReq) when (badReq.Message.Contains("already results associated")) catch (ValidationException valEx) when (valEx.Errors.Any(e => e.PropertyName.Contains("BatchId")))
{ {
Assert.Fail("Duplicate guard should not trigger for a unique BatchId."); Assert.Fail("Duplicate guard should not trigger for a unique BatchId.");
} }