101 lines
4.4 KiB
C#
101 lines
4.4 KiB
C#
using Heimdall.Application.Common;
|
|||
|
|
using Heimdall.Domain.Entities;
|
||
|
|
using Heimdall.Domain.Enums;
|
||
|
|
using Heimdall.Infrastructure.Data;
|
||
|
|
using Heimdall.Infrastructure.Data.Interceptors;
|
||
|
|
using Heimdall.Infrastructure.Jobs;
|
||
|
|
using Heimdall.TestCommon;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
||
|
|
using Npgsql;
|
||
|
|
using NSubstitute;
|
||
|
|
using Shouldly;
|
||
|
|
|
||
|
|
namespace Heimdall.Infrastructure.Tests;
|
||
|
|
|
||
|
|
[Collection("PostgresInfra")]
|
||
|
|
public sealed class PlateEventRetentionPostgresTests(PostgreSqlContainerFixture postgres) : IAsyncLifetime
|
||
|
|
{
|
||
|
|
private readonly string _database = "plate_retention_" + Guid.NewGuid().ToString("N");
|
||
|
|
private NpgsqlConnectionStringBuilder _connection = null!;
|
||
|
|
private Factory _factory = null!;
|
||
|
|
private Guid _cameraId;
|
||
|
|
|
||
|
|
public async Task InitializeAsync()
|
||
|
|
{
|
||
|
|
await using var admin = new NpgsqlConnection(postgres.ConnectionString);
|
||
|
|
await admin.OpenAsync();
|
||
|
|
await using (var create = new NpgsqlCommand($"CREATE DATABASE {_database}", admin))
|
||
|
|
await create.ExecuteNonQueryAsync();
|
||
|
|
|
||
|
|
_connection = new NpgsqlConnectionStringBuilder(postgres.ConnectionString)
|
||
|
|
{
|
||
|
|
Database = _database,
|
||
|
|
Pooling = false
|
||
|
|
};
|
||
|
|
var options = new DbContextOptionsBuilder<AppDbContext>().UseNpgsql(_connection.ConnectionString).Options;
|
||
|
|
_factory = new Factory(options);
|
||
|
|
await using (var extensionConnection = new NpgsqlConnection(_connection.ConnectionString))
|
||
|
|
{
|
||
|
|
await extensionConnection.OpenAsync();
|
||
|
|
await using var extension = new NpgsqlCommand("CREATE EXTENSION IF NOT EXISTS pg_trgm", extensionConnection);
|
||
|
|
await extension.ExecuteNonQueryAsync();
|
||
|
|
}
|
||
|
|
await using var db = await _factory.CreateDbContextAsync();
|
||
|
|
await db.Database.EnsureCreatedAsync();
|
||
|
|
var site = new Site("Plate retention site");
|
||
|
|
db.Sites.Add(site);
|
||
|
|
await db.SaveChangesAsync();
|
||
|
|
var camera = new Camera(site.Id, "Plate retention camera");
|
||
|
|
db.Cameras.Add(camera);
|
||
|
|
await db.SaveChangesAsync();
|
||
|
|
_cameraId = camera.Id;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task DisposeAsync()
|
||
|
|
{
|
||
|
|
await using var admin = new NpgsqlConnection(postgres.ConnectionString);
|
||
|
|
await admin.OpenAsync();
|
||
|
|
await using var drop = new NpgsqlCommand($"DROP DATABASE IF EXISTS {_database} WITH (FORCE)", admin);
|
||
|
|
await drop.ExecuteNonQueryAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task ExecuteAsync_DeletesExpiredPlateEventsUsingPostgresEfTranslation()
|
||
|
|
{
|
||
|
|
var expiredId = Guid.NewGuid();
|
||
|
|
var recentId = Guid.NewGuid();
|
||
|
|
var motionId = Guid.NewGuid();
|
||
|
|
await using (var db = await _factory.CreateDbContextAsync())
|
||
|
|
{
|
||
|
|
db.CameraEvents.AddRange(
|
||
|
|
CreateEvent(EventType.LicensePlateDetected, DateTime.UtcNow.AddDays(-31), "AB-123-CD", expiredId),
|
||
|
|
CreateEvent(EventType.LicensePlateDetected, DateTime.UtcNow.AddDays(-2), "XY-456-ZZ", recentId),
|
||
|
|
CreateEvent(EventType.MotionDetected, DateTime.UtcNow.AddDays(-31), "movement", motionId));
|
||
|
|
await db.SaveChangesAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
await new CleanupLicensePlatesJob(_factory, NullLogger<CleanupLicensePlatesJob>.Instance,
|
||
|
|
Substitute.For<IStorageObjectDeletion>()).ExecuteAsync(CancellationToken.None);
|
||
|
|
|
||
|
|
await using var verify = await _factory.CreateDbContextAsync();
|
||
|
|
var remaining = await verify.CameraEvents.IgnoreQueryFilters().AsNoTracking().ToListAsync();
|
||
|
|
remaining.Select(e => e.Id).ShouldBe(new[] { recentId, motionId }, ignoreOrder: true);
|
||
|
|
remaining.ShouldContain(e => e.Id == recentId && e.Label == "XY-456-ZZ");
|
||
|
|
remaining.ShouldContain(e => e.Id == motionId && e.Label == "movement");
|
||
|
|
}
|
||
|
|
|
||
|
|
private CameraEvent CreateEvent(EventType type, DateTime occurredAt, string label, Guid id)
|
||
|
|
{
|
||
|
|
var ev = new CameraEvent(_cameraId, type, 0.9, occurredAt, id);
|
||
|
|
ev.SetMetadata(label, "{\"plate\":\"AB-123-CD\"}");
|
||
|
|
return ev;
|
||
|
|
}
|
||
|
|
|
||
|
|
private sealed class Factory(DbContextOptions<AppDbContext> options) : IDbContextFactory<AppDbContext>
|
||
|
|
{
|
||
|
|
public AppDbContext CreateDbContext() => new(options, new AuditInterceptor());
|
||
|
|
public Task<AppDbContext> CreateDbContextAsync(CancellationToken cancellationToken = default)
|
||
|
|
=> Task.FromResult(CreateDbContext());
|
||
|
|
}
|
||
|
|
}
|