2026-08-11 21:42:46 +02:00
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using Heimdall.Domain.Enums;
|
|
|
|
|
using Heimdall.Infrastructure.Data;
|
|
|
|
|
using Heimdall.Infrastructure.Data.Interceptors;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
using Heimdall.TestCommon;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Infrastructure.Tests;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Validation des entités métier (spec 2.1/2.2) sur PostgreSQL réel :
|
|
|
|
|
/// Vehicle, Recording, LicensePlate, StoragePolicy + relations CameraEvent→Vehicle/Recording,
|
|
|
|
|
/// Camera→StoragePolicy N:1.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Collection("PostgresInfra")]
|
|
|
|
|
public class BusinessEntityPersistenceTests : IAsyncLifetime
|
|
|
|
|
{
|
2026-10-10 13:46:12 +02:00
|
|
|
private readonly TestDbContextFactory _dbFactory;
|
2026-08-11 21:42:46 +02:00
|
|
|
|
|
|
|
|
public BusinessEntityPersistenceTests(PostgreSqlContainerFixture postgres)
|
|
|
|
|
{
|
|
|
|
|
_dbFactory = CreateFactory(postgres.ConnectionString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task InitializeAsync()
|
|
|
|
|
{
|
|
|
|
|
await ResetDatabaseAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task DisposeAsync() => Task.CompletedTask;
|
|
|
|
|
|
|
|
|
|
private async Task ResetDatabaseAsync()
|
|
|
|
|
{
|
|
|
|
|
await using var db = await _dbFactory.CreateDbContextAsync();
|
|
|
|
|
await db.Database.ExecuteSqlRawAsync(
|
|
|
|
|
"TRUNCATE TABLE license_plates, recordings, vehicles, storage_policies, camera_events, cameras, sites RESTART IDENTITY CASCADE");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static TestDbContextFactory CreateFactory(string connectionString)
|
|
|
|
|
{
|
|
|
|
|
var opts = new DbContextOptionsBuilder<AppDbContext>()
|
|
|
|
|
.UseNpgsql(connectionString, npgsql =>
|
|
|
|
|
npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public"))
|
|
|
|
|
.Options;
|
|
|
|
|
|
|
|
|
|
// Ensure tables exist (l'extension pg_trgm est requise pour l'index GIN trigram)
|
|
|
|
|
using var conn = new Npgsql.NpgsqlConnection(connectionString);
|
|
|
|
|
conn.Open();
|
|
|
|
|
using (var cmd = conn.CreateCommand())
|
|
|
|
|
{
|
|
|
|
|
cmd.CommandText = "CREATE EXTENSION IF NOT EXISTS pg_trgm;";
|
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
using var ctx = new AppDbContext(opts, new AuditInterceptor());
|
|
|
|
|
ctx.Database.EnsureCreated();
|
|
|
|
|
|
|
|
|
|
return new TestDbContextFactory(opts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StoragePolicy_can_be_created_and_shared_by_cameras()
|
|
|
|
|
{
|
|
|
|
|
await using var db = await _dbFactory.CreateDbContextAsync();
|
|
|
|
|
var site = new Site("Biz Site", "test", "UTC");
|
|
|
|
|
db.Sites.Add(site);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var policy = new StoragePolicy("30 jours", 30, StorageProvider.S3);
|
|
|
|
|
db.StoragePolicies.Add(policy);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var cam1 = new Camera(site.Id, "Cam-1");
|
|
|
|
|
cam1.AssignStoragePolicy(policy.Id);
|
|
|
|
|
var cam2 = new Camera(site.Id, "Cam-2");
|
|
|
|
|
cam2.AssignStoragePolicy(policy.Id);
|
|
|
|
|
db.Cameras.AddRange(cam1, cam2);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
// Recharger : les 2 caméras partagent la même politique (N:1)
|
|
|
|
|
var reloaded = await db.Cameras
|
|
|
|
|
.Include(c => c.StoragePolicy)
|
|
|
|
|
.Where(c => c.SiteId == site.Id)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
reloaded.Count.ShouldBe(2);
|
|
|
|
|
reloaded.ShouldAllBe(c => c.StoragePolicyId == policy.Id);
|
|
|
|
|
reloaded.ShouldAllBe(c => c.StoragePolicy!.Name == "30 jours");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task CameraEvent_can_link_vehicle_and_recording()
|
|
|
|
|
{
|
|
|
|
|
await using var db = await _dbFactory.CreateDbContextAsync();
|
|
|
|
|
var site = new Site("Biz Site 2", "test", "UTC");
|
|
|
|
|
db.Sites.Add(site);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var camera = new Camera(site.Id, "Cam-Event");
|
|
|
|
|
db.Cameras.Add(camera);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var vehicle = new Vehicle("AB-123-CD", null, "Renault", "Clio", "Noir");
|
|
|
|
|
db.Vehicles.Add(vehicle);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
2026-10-22 19:58:05 +02:00
|
|
|
var recording = new Recording(camera.Id, DateTime.UtcNow, 60, "rec/1.mp4", 1000, StorageProvider.S3,
|
|
|
|
|
new StorageLocation(StorageProvider.S3, S3Endpoint: "https://storage.test", S3Bucket: "heimdall"));
|
2026-08-11 21:42:46 +02:00
|
|
|
db.Recordings.Add(recording);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var evt = new CameraEvent(camera.Id, EventType.VehicleDetected, 0.92);
|
|
|
|
|
evt.AssignVehicle(vehicle.Id);
|
|
|
|
|
evt.AssignRecording(recording.Id);
|
|
|
|
|
evt.SetTrackId("track-1");
|
|
|
|
|
evt.SetDetectionHash("hash-1");
|
|
|
|
|
db.CameraEvents.Add(evt);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
2026-08-27 00:31:12 +02:00
|
|
|
// FK IDs are stored correctly (no navigation — partitioned table can't have FKs)
|
2026-08-11 21:42:46 +02:00
|
|
|
var reloaded = await db.CameraEvents
|
|
|
|
|
.SingleAsync(e => e.Id == evt.Id);
|
|
|
|
|
reloaded.VehicleId.ShouldBe(vehicle.Id);
|
|
|
|
|
reloaded.RecordingId.ShouldBe(recording.Id);
|
|
|
|
|
reloaded.TrackId.ShouldBe("track-1");
|
|
|
|
|
reloaded.DetectionHash.ShouldBe("hash-1");
|
2026-08-27 00:31:12 +02:00
|
|
|
|
|
|
|
|
// Verify linked entities exist separately
|
|
|
|
|
var linkedVehicle = await db.Vehicles.SingleAsync(v => v.Id == vehicle.Id);
|
|
|
|
|
linkedVehicle.LicensePlate.ShouldBe("AB-123-CD");
|
|
|
|
|
|
|
|
|
|
var linkedRecording = await db.Recordings.SingleAsync(r => r.Id == recording.Id);
|
|
|
|
|
linkedRecording.StoragePath.ShouldBe("rec/1.mp4");
|
2026-08-11 21:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task LicensePlate_can_link_vehicle_and_person()
|
|
|
|
|
{
|
|
|
|
|
await using var db = await _dbFactory.CreateDbContextAsync();
|
|
|
|
|
var person = new Person("Jean Dupont");
|
|
|
|
|
db.Persons.Add(person);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var vehicle = new Vehicle("AB-123-CD");
|
|
|
|
|
vehicle.AssignPerson(person.Id);
|
|
|
|
|
db.Vehicles.Add(vehicle);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var plate = new LicensePlate("AB-123-CD", vehicle.Id, person.Id, 0.98);
|
|
|
|
|
db.LicensePlates.Add(plate);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var reloaded = await db.LicensePlates
|
|
|
|
|
.Include(p => p.Vehicle)
|
|
|
|
|
.Include(p => p.Person)
|
|
|
|
|
.SingleAsync(p => p.Id == plate.Id);
|
|
|
|
|
reloaded.PlateText.ShouldBe("AB-123-CD");
|
|
|
|
|
reloaded.VehicleId.ShouldBe(vehicle.Id);
|
|
|
|
|
reloaded.PersonId.ShouldBe(person.Id);
|
|
|
|
|
reloaded.Vehicle!.LicensePlate.ShouldBe("AB-123-CD");
|
|
|
|
|
reloaded.Person!.DisplayName.ShouldBe("Jean Dupont");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task CameraEvent_parent_child_hierarchy_works()
|
|
|
|
|
{
|
|
|
|
|
await using var db = await _dbFactory.CreateDbContextAsync();
|
|
|
|
|
var site = new Site("Biz Site 3", "test", "UTC");
|
|
|
|
|
db.Sites.Add(site);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var camera = new Camera(site.Id, "Cam-Hier");
|
|
|
|
|
db.Cameras.Add(camera);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var parent = new CameraEvent(camera.Id, EventType.MotionDetected, 0.8);
|
|
|
|
|
db.CameraEvents.Add(parent);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var child = new CameraEvent(camera.Id, EventType.PersonDetected, 0.95);
|
|
|
|
|
child.AssignParentEvent(parent.Id);
|
|
|
|
|
db.CameraEvents.Add(child);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
|
2026-08-27 00:31:12 +02:00
|
|
|
// FK ID is stored correctly (no navigation — partitioned table can't have self-FK)
|
2026-08-11 21:42:46 +02:00
|
|
|
var reloaded = await db.CameraEvents
|
|
|
|
|
.SingleAsync(e => e.Id == child.Id);
|
|
|
|
|
reloaded.ParentEventId.ShouldBe(parent.Id);
|
2026-08-27 00:31:12 +02:00
|
|
|
|
|
|
|
|
// Verify parent event exists separately
|
|
|
|
|
var parentEvent = await db.CameraEvents.SingleAsync(e => e.Id == parent.Id);
|
|
|
|
|
parentEvent.Type.ShouldBe(EventType.MotionDetected);
|
2026-08-11 21:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private sealed class TestDbContextFactory(DbContextOptions<AppDbContext> opts)
|
|
|
|
|
: IDbContextFactory<AppDbContext>
|
|
|
|
|
{
|
|
|
|
|
private static readonly AuditInterceptor Audit = new();
|
|
|
|
|
|
|
|
|
|
public AppDbContext CreateDbContext() => new(opts, Audit);
|
|
|
|
|
public Task<AppDbContext> CreateDbContextAsync(CancellationToken ct = default)
|
|
|
|
|
=> Task.FromResult(new AppDbContext(opts, Audit));
|
|
|
|
|
}
|
|
|
|
|
}
|