Files
Omika/tests/Omika.Diagnostics.Tests/DiagnosticDeduplicationTests.cs
T
2026-10-23 18:46:50 +02:00

47 lines
1.7 KiB
C#

using Microsoft.Extensions.Options;
using Omika.Diagnostics.Import;
using Omika.Diagnostics.Models;
using Omika.Diagnostics.Tests.Fakes;
namespace Omika.Diagnostics.Tests;
/// <summary>§53 — Déduplication : deux événements avec le même EventId → 1 événement PostgreSQL, sans exception fatale.</summary>
public class DiagnosticDeduplicationTests
{
[Fact]
public async Task Importer_DuplicateEventIds_ProducesSingleInserted()
{
Guid id = Guid.NewGuid();
var source = new FakeDiagnosticJsonlSource(new[]
{
DiagnosticTestData.Record(id, "A"),
DiagnosticTestData.Record(id, "A") // doublon (at-least-once)
});
var repository = new FakeDiagnosticEventRepository();
var importer = new DiagnosticImporter(source, repository, Options.Create(new DiagnosticImportOptions { BatchSize = 100 }));
DiagnosticImportResult result = await importer.ImportOnceAsync();
Assert.True(result.Succeeded);
Assert.Equal(2, result.ReadCount);
Assert.Equal(1, result.ImportedCount); // 1 seul événement persisté
Assert.Equal(1, repository.InsertedCount);
}
[Fact]
public async Task Repository_DuplicateEventIds_IsIdempotent()
{
Guid id = Guid.NewGuid();
var repository = new FakeDiagnosticEventRepository();
DiagnosticEventRecord record = DiagnosticTestData.Event(id);
int first = await repository.InsertBatchAsync(new[] { record });
int second = await repository.InsertBatchAsync(new[] { DiagnosticTestData.Event(id) });
Assert.Equal(1, first);
Assert.Equal(0, second); // ON CONFLICT DO NOTHING → 0 nouvelle ligne
Assert.Equal(1, repository.InsertedCount);
}
}