61 lines
2.6 KiB
C#
61 lines
2.6 KiB
C#
using Heimdall.Application.Common;
|
|
using Heimdall.Application.Events.Commands.DeleteEvent;
|
|
using Heimdall.Application.Tests.Common;
|
|
using Heimdall.Domain.Entities;
|
|
using Heimdall.Domain.Enums;
|
|
using NSubstitute;
|
|
|
|
namespace Heimdall.Application.Tests.Handlers.Events;
|
|
|
|
public class DeleteEventHandlerTests
|
|
{
|
|
[Fact]
|
|
public async Task Delete_event_should_return_success()
|
|
{
|
|
var ev = new CameraEvent(Guid.NewGuid(), EventType.MotionDetected, 0.9);
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
var storage = Substitute.For<IStorageObjectDeletion>();
|
|
context.CameraEvents.Returns(MockDbSetHelper.CreateFor<CameraEvent>([ev]));
|
|
context.SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(Task.CompletedTask);
|
|
var handler = new DeleteEventHandler(context, storage);
|
|
|
|
var result = await handler.Handle(new DeleteEventCommand(ev.Id), CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Delete_unknown_event_should_return_not_found()
|
|
{
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
var storage = Substitute.For<IStorageObjectDeletion>();
|
|
context.CameraEvents.Returns(MockDbSetHelper.CreateFor<CameraEvent>());
|
|
var handler = new DeleteEventHandler(context, storage);
|
|
|
|
var result = await handler.Handle(new DeleteEventCommand(Guid.NewGuid()), CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
result.StatusCode.ShouldBe(404);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Delete_stored_event_removes_object_and_database_row()
|
|
{
|
|
var ev = new CameraEvent(Guid.NewGuid(), EventType.PersonDetected, 0.9);
|
|
var location = new StorageLocation(StorageProvider.Local, LocalRoot: "/var/lib/heimdall/media");
|
|
ev.SetStoredSnapshot($"snapshots/{ev.Id:D}.jpg", location);
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
var storage = Substitute.For<IStorageObjectDeletion>();
|
|
context.CameraEvents.Returns(MockDbSetHelper.CreateFor<CameraEvent>([ev]));
|
|
context.SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(Task.CompletedTask);
|
|
var handler = new DeleteEventHandler(context, storage);
|
|
|
|
var result = await handler.Handle(new DeleteEventCommand(ev.Id), CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
await storage.Received(1).DeleteBatchAsync(
|
|
Arg.Is<IReadOnlyCollection<string>>(keys => keys != null && keys.Count == 1 && keys.Contains(ev.SnapshotObjectKey!)),
|
|
location, Arg.Any<CancellationToken>());
|
|
}
|
|
}
|