Files
Heimdall/tests/Heimdall.Application.Tests/Handlers/Sites/DeleteSiteHandlerTests.cs
T

55 lines
1.9 KiB
C#

using Heimdall.Application.Common;
using Heimdall.Application.Sites.Commands.DeleteSite;
using Heimdall.Application.Tests.Common;
using Heimdall.Domain.Entities;
using NSubstitute;
namespace Heimdall.Application.Tests.Handlers.Sites;
public class DeleteSiteHandlerTests
{
private readonly Site _existingSite = new("Test Site", "Paris", "Europe/Paris");
[Fact]
public async Task Delete_site_should_return_success()
{
var context = Substitute.For<IApplicationDbContext>();
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([_existingSite]));
var handler = new DeleteSiteHandler(context);
var command = new DeleteSiteCommand(_existingSite.Id);
var result = await handler.Handle(command, CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
}
[Fact]
public async Task Delete_nonexistent_site_should_return_not_found()
{
var context = Substitute.For<IApplicationDbContext>();
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([_existingSite]));
var handler = new DeleteSiteHandler(context);
var command = new DeleteSiteCommand(Guid.NewGuid());
var result = await handler.Handle(command, CancellationToken.None);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(404);
}
[Fact]
public async Task Delete_site_should_soft_delete_and_save()
{
var context = Substitute.For<IApplicationDbContext>();
context.Sites.Returns(MockDbSetHelper.CreateFor<Site>([_existingSite]));
var handler = new DeleteSiteHandler(context);
var command = new DeleteSiteCommand(_existingSite.Id);
await handler.Handle(command, CancellationToken.None);
_existingSite.IsDeleted.ShouldBeTrue();
_existingSite.DeletedAt.ShouldNotBeNull();
await context.Received(1).SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>());
}
}