Add ConfigureAwait(false) to all await expressions in Application and Infrastructure projects (111 files). This follows .NET best practices for library code to avoid potential deadlocks when consumed from synchronization contexts (legacy, unit test runners). Also fix 4 test assertions that check SaveChangesAsync — update to SaveChangesWithRetryAsync to match the handler changes from Fix 4.
58 lines
2.3 KiB
C#
58 lines
2.3 KiB
C#
using Heimdall.Application.Cameras.Commands.CreateCamera;
|
|
using Heimdall.Application.Common;
|
|
using Heimdall.Application.Tests.Common;
|
|
using Heimdall.Domain.Entities;
|
|
using NSubstitute;
|
|
|
|
namespace Heimdall.Application.Tests.Handlers.Cameras;
|
|
|
|
public class CreateCameraHandlerTests
|
|
{
|
|
private readonly Site _existingSite = new("Test Site", "Paris", "Europe/Paris");
|
|
|
|
[Fact]
|
|
public async Task Create_camera_should_return_success()
|
|
{
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([_existingSite]));
|
|
context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>());
|
|
var handler = new CreateCameraCommandHandler(context);
|
|
var command = new CreateCameraCommand(_existingSite.Id, "Front Door", "rtsp://192.168.1.100/stream1", "Hikvision", "DS-2CD2T47G2-L");
|
|
|
|
var result = await handler.Handle(command, CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
result.Data.ShouldNotBeNull();
|
|
result.Data.Name.ShouldBe("Front Door");
|
|
result.Data.SiteId.ShouldBe(_existingSite.Id);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_camera_nonexistent_site_should_return_not_found()
|
|
{
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([_existingSite]));
|
|
var handler = new CreateCameraCommandHandler(context);
|
|
var command = new CreateCameraCommand(Guid.NewGuid(), "Camera", null, null, null);
|
|
|
|
var result = await handler.Handle(command, CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
result.StatusCode.ShouldBe(404);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_camera_should_add_and_save()
|
|
{
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([_existingSite]));
|
|
context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>());
|
|
var handler = new CreateCameraCommandHandler(context);
|
|
var command = new CreateCameraCommand(_existingSite.Id, "Back Yard", null, null, null);
|
|
|
|
await handler.Handle(command, CancellationToken.None);
|
|
|
|
await context.Received(1).SaveChangesWithRetryAsync(ct: Arg.Any<CancellationToken>());
|
|
}
|
|
}
|