Added tests, need to go through them manually to check if they make sense and are correct later

This commit is contained in:
moarten
2026-04-19 20:55:26 +02:00
parent 11bff0de63
commit 3a1071dadc
10 changed files with 862 additions and 3 deletions

View File

@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="FluentAssertions" Version="8.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Moq.EntityFrameworkCore" Version="10.0.0.2" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Lutra.Application\Lutra.Application.csproj" />
<ProjectReference Include="..\Lutra.Domain\Lutra.Domain.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,72 @@
using FluentAssertions;
using Lutra.Application.Interfaces;
using Lutra.Application.Verspakketten;
using Microsoft.EntityFrameworkCore;
using Moq;
using Moq.EntityFrameworkCore;
namespace Lutra.Application.UnitTests.Verspakketten;
public class AddBeoordelingHandlerTests
{
private readonly Mock<ILutraDbContext> _contextMock;
private readonly AddBeoordeling.Handler _handler;
public AddBeoordelingHandlerTests()
{
_contextMock = new Mock<ILutraDbContext>();
_handler = new AddBeoordeling.Handler(_contextMock.Object);
}
[Fact]
public async Task Handle_VerspakketExists_AddsBeoordeling()
{
var verspakketId = Guid.NewGuid();
var verspakketten = new List<Domain.Entities.Verspakket>
{
new() { Id = verspakketId, Naam = "Test", AantalPersonen = 2, SupermarktId = Guid.NewGuid(), CreatedAt = DateTime.UtcNow, ModifiedAt = DateTime.UtcNow }
};
_contextMock.Setup(c => c.Verspaketten).ReturnsDbSet(verspakketten);
_contextMock.Setup(c => c.Beoordelingen).ReturnsDbSet(new List<Domain.Entities.Beoordeling>());
_contextMock.Setup(c => c.SaveChangesAsync(It.IsAny<CancellationToken>())).ReturnsAsync(1);
var command = new AddBeoordeling.Command(verspakketId, 8, 7, true, "Lekker!");
var result = await _handler.Handle(command, CancellationToken.None);
result.Id.Should().NotBeEmpty();
_contextMock.Verify(c => c.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task Handle_VerspakketNotFound_ThrowsInvalidOperationException()
{
_contextMock.Setup(c => c.Verspaketten).ReturnsDbSet(new List<Domain.Entities.Verspakket>());
var command = new AddBeoordeling.Command(Guid.NewGuid(), 8, 7, true, null);
var act = () => _handler.Handle(command, CancellationToken.None);
await act.Should().ThrowAsync<InvalidOperationException>()
.WithMessage("*was not found*");
}
[Fact]
public async Task Handle_VerspakketDeleted_ThrowsInvalidOperationException()
{
var verspakketId = Guid.NewGuid();
var verspakketten = new List<Domain.Entities.Verspakket>
{
new() { Id = verspakketId, Naam = "Deleted", AantalPersonen = 2, SupermarktId = Guid.NewGuid(), CreatedAt = DateTime.UtcNow, ModifiedAt = DateTime.UtcNow, DeletedAt = DateTime.UtcNow }
};
_contextMock.Setup(c => c.Verspaketten).ReturnsDbSet(verspakketten);
var command = new AddBeoordeling.Command(verspakketId, 8, 7, true, null);
var act = () => _handler.Handle(command, CancellationToken.None);
await act.Should().ThrowAsync<InvalidOperationException>();
}
}

View File

@@ -0,0 +1,81 @@
using FluentAssertions;
using Lutra.Application.Interfaces;
using Lutra.Application.Verspakketten;
using Moq;
using Moq.EntityFrameworkCore;
namespace Lutra.Application.UnitTests.Verspakketten;
public class CreateVerspakketHandlerTests
{
private readonly Mock<ILutraDbContext> _contextMock;
private readonly CreateVerspakket.Handler _handler;
public CreateVerspakketHandlerTests()
{
_contextMock = new Mock<ILutraDbContext>();
_handler = new CreateVerspakket.Handler(_contextMock.Object);
}
[Fact]
public async Task Handle_SupermarktExists_CreatesVerspakket()
{
var supermarktId = Guid.NewGuid();
var supermarkten = new List<Domain.Entities.Supermarkt>
{
new() { Id = supermarktId, Naam = "AH", CreatedAt = DateTime.UtcNow, ModifiedAt = DateTime.UtcNow }
};
_contextMock.Setup(c => c.Supermarkten).ReturnsDbSet(supermarkten);
_contextMock.Setup(c => c.Verspaketten).ReturnsDbSet(new List<Domain.Entities.Verspakket>());
_contextMock.Setup(c => c.SaveChangesAsync(It.IsAny<CancellationToken>())).ReturnsAsync(1);
var command = new CreateVerspakket.Command("Lente Pakket", 1299, 2, supermarktId);
var result = await _handler.Handle(command, CancellationToken.None);
result.Id.Should().NotBeEmpty();
_contextMock.Verify(c => c.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task Handle_SupermarktNotFound_ThrowsInvalidOperationException()
{
_contextMock.Setup(c => c.Supermarkten).ReturnsDbSet(new List<Domain.Entities.Supermarkt>());
var command = new CreateVerspakket.Command("Lente Pakket", 1299, 2, Guid.NewGuid());
var act = () => _handler.Handle(command, CancellationToken.None);
await act.Should().ThrowAsync<InvalidOperationException>()
.WithMessage("*was not found*");
}
[Fact]
public async Task Handle_CreatesVerspakketWithCorrectProperties()
{
var supermarktId = Guid.NewGuid();
var supermarkten = new List<Domain.Entities.Supermarkt>
{
new() { Id = supermarktId, Naam = "Jumbo", CreatedAt = DateTime.UtcNow, ModifiedAt = DateTime.UtcNow }
};
Domain.Entities.Verspakket? savedVerspakket = null;
_contextMock.Setup(c => c.Supermarkten).ReturnsDbSet(supermarkten);
_contextMock.Setup(c => c.Verspaketten).ReturnsDbSet(new List<Domain.Entities.Verspakket>());
_contextMock
.Setup(c => c.Verspaketten.AddAsync(It.IsAny<Domain.Entities.Verspakket>(), It.IsAny<CancellationToken>()))
.Callback<Domain.Entities.Verspakket, CancellationToken>((v, _) => savedVerspakket = v);
_contextMock.Setup(c => c.SaveChangesAsync(It.IsAny<CancellationToken>())).ReturnsAsync(1);
var command = new CreateVerspakket.Command("Zomer Pakket", 999, 4, supermarktId);
await _handler.Handle(command, CancellationToken.None);
savedVerspakket.Should().NotBeNull();
savedVerspakket!.Naam.Should().Be("Zomer Pakket");
savedVerspakket.PrijsInCenten.Should().Be(999);
savedVerspakket.AantalPersonen.Should().Be(4);
savedVerspakket.SupermarktId.Should().Be(supermarktId);
}
}