Files
Heimdall/tests/Heimdall.Application.Tests/Handlers/Vehicles/GetVehiclesHandlerTests.cs
T

88 lines
3.1 KiB
C#
Raw Normal View History

using Heimdall.Application.Common;
using Heimdall.Application.Vehicles.Queries.GetVehicles;
using Heimdall.Application.Tests.Common;
using Heimdall.Domain.Entities;
using NSubstitute;
namespace Heimdall.Application.Tests.Handlers.Vehicles;
public class GetVehiclesHandlerTests
{
[Fact]
public async Task Get_vehicles_should_return_all()
{
var vehicles = new List<Vehicle>
{
new("AA-111-AA"),
new("BB-222-BB"),
new("CC-333-CC"),
};
var context = Substitute.For<IApplicationDbContext>();
context.Vehicles.Returns(MockDbSetHelper.CreateFor<Vehicle>(vehicles));
context.CameraEvents.Returns(MockDbSetHelper.CreateFor<CameraEvent>());
var handler = new GetVehiclesQueryHandler(context);
var result = await handler.Handle(new GetVehiclesQuery(), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Count.ShouldBe(3);
}
[Fact]
public async Task Get_vehicles_search_should_filter_by_plate()
{
var vehicles = new List<Vehicle>
{
new("AB-123-CD"),
new("XY-999-ZZ"),
new("AB-456-EF"),
};
var context = Substitute.For<IApplicationDbContext>();
context.Vehicles.Returns(MockDbSetHelper.CreateFor<Vehicle>(vehicles));
context.CameraEvents.Returns(MockDbSetHelper.CreateFor<CameraEvent>());
var handler = new GetVehiclesQueryHandler(context);
var result = await handler.Handle(new GetVehiclesQuery(Search: "AB"), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Count.ShouldBe(2);
result.Data.ShouldAllBe(v => v.LicensePlate!.Contains("AB"));
}
[Fact]
public async Task Get_vehicles_search_should_be_case_insensitive()
{
var vehicles = new List<Vehicle>
{
new("ab-123-cd"),
new("XY-999-ZZ"),
};
var context = Substitute.For<IApplicationDbContext>();
context.Vehicles.Returns(MockDbSetHelper.CreateFor<Vehicle>(vehicles));
context.CameraEvents.Returns(MockDbSetHelper.CreateFor<CameraEvent>());
var handler = new GetVehiclesQueryHandler(context);
var result = await handler.Handle(new GetVehiclesQuery(Search: "AB"), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Count.ShouldBe(1);
result.Data[0].LicensePlate.ShouldBe("ab-123-cd");
}
[Fact]
public async Task Get_vehicles_empty_should_return_empty_list()
{
var context = Substitute.For<IApplicationDbContext>();
context.Vehicles.Returns(MockDbSetHelper.CreateFor<Vehicle>());
context.CameraEvents.Returns(MockDbSetHelper.CreateFor<CameraEvent>());
var handler = new GetVehiclesQueryHandler(context);
var result = await handler.Handle(new GetVehiclesQuery(), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldBeEmpty();
}
}