2026-10-10 17:49:26 +02:00
|
|
|
using System.Linq;
|
2026-07-23 19:19:05 +00:00
|
|
|
using Heimdall.Application.Common;
|
|
|
|
|
using Heimdall.Application.Persons.Commands.CreatePerson;
|
|
|
|
|
using Heimdall.Application.Tests.Common;
|
|
|
|
|
using Heimdall.Domain.Entities;
|
2026-10-10 17:49:26 +02:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-07-23 19:19:05 +00:00
|
|
|
using NSubstitute;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Tests.Handlers.Persons;
|
|
|
|
|
|
|
|
|
|
public class CreatePersonHandlerTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Create_person_should_return_success()
|
|
|
|
|
{
|
|
|
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
|
|
|
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>());
|
2026-10-10 17:49:26 +02:00
|
|
|
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
2026-07-23 19:19:05 +00:00
|
|
|
var handler = new CreatePersonCommandHandler(context);
|
|
|
|
|
|
|
|
|
|
var result = await handler.Handle(
|
|
|
|
|
new CreatePersonCommand("New Person", "family", null, false), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
result.Data.ShouldNotBeNull();
|
|
|
|
|
result.Data.DisplayName.ShouldBe("New Person");
|
|
|
|
|
result.Data.Category.ShouldBe("family");
|
|
|
|
|
await context.Received(1).SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Create_person_default_category_should_be_null()
|
|
|
|
|
{
|
|
|
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
|
|
|
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>());
|
2026-10-10 17:49:26 +02:00
|
|
|
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
2026-07-23 19:19:05 +00:00
|
|
|
var handler = new CreatePersonCommandHandler(context);
|
|
|
|
|
|
|
|
|
|
var result = await handler.Handle(
|
|
|
|
|
new CreatePersonCommand("Minimal Person", null, null), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
result.Data!.DisplayName.ShouldBe("Minimal Person");
|
|
|
|
|
}
|
2026-10-10 17:49:26 +02:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Create_person_should_record_audit_log()
|
|
|
|
|
{
|
|
|
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
|
|
|
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>());
|
|
|
|
|
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
|
|
|
|
var handler = new CreatePersonCommandHandler(context);
|
|
|
|
|
|
|
|
|
|
var result = await handler.Handle(
|
|
|
|
|
new CreatePersonCommand("Audited Person", "family", null, false), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
context.AuditLogs.Received(1).Add(Arg.Any<AuditLog>());
|
|
|
|
|
}
|
2026-07-23 19:19:05 +00:00
|
|
|
}
|