Premiere commit - Omika
This commit is contained in:
@@ -0,0 +1,711 @@
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Omika.Core.Compatibility;
|
||||
using Omika.Core.Contracts.Common;
|
||||
using Omika.Core.Contracts.Interventions;
|
||||
using Omika.Core.Domain.Commands;
|
||||
using Omika.Core.Domain.Entities.Interventions;
|
||||
using Omika.Core.Interfaces;
|
||||
using Omika.Core.Runtime;
|
||||
using Omika.Data;
|
||||
using Omika.Data.Configuration;
|
||||
using Omika.Data.Connection;
|
||||
using Omika.Data.Repositories;
|
||||
using Omika.Data.Repositories.Batik;
|
||||
using Omika.Data.Repositories.Omika;
|
||||
|
||||
namespace Omika.Tests.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// Tests unitaires de <see cref="AdaptiveInterventionRepository"/> : délégation,
|
||||
/// comportement Unknown, propagation d'erreurs, architecture, et DI.
|
||||
/// </summary>
|
||||
[Collection("ApplicationRuntimeContext")]
|
||||
public class AdaptiveInterventionRepositoryTests : IDisposable
|
||||
{
|
||||
public AdaptiveInterventionRepositoryTests()
|
||||
{
|
||||
ApplicationRuntimeContext.Reset();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ApplicationRuntimeContext.Reset();
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// DELEGATION SEARCH
|
||||
// =========================================================================
|
||||
|
||||
[Fact]
|
||||
public async Task SearchAsync_BatikDetected_DelegatesToBatikRepository()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Batik);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
batikConnection.InterventionRows = [BatikInterventionDict(1, "Test", "INT-001")];
|
||||
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
await adaptive.SearchAsync(new InterventionSearchCriteria { Titre = "Test" });
|
||||
|
||||
Assert.True(batikConnection.QueryCount > 0, "Batik aurait dû exécuter des requêtes.");
|
||||
Assert.Equal(0, omikaConnection.QueryCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SearchAsync_OmikaDetected_DelegatesToOmikaRepository()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Omika);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
omikaConnection.InterventionRows = [OmikaInterventionDict(1, "Test", "INT-001")];
|
||||
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
await adaptive.SearchAsync(new InterventionSearchCriteria { Titre = "Test" });
|
||||
|
||||
Assert.Equal(0, batikConnection.QueryCount);
|
||||
Assert.True(omikaConnection.QueryCount > 0, "Omika aurait dû exécuter des requêtes.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SearchAsync_UnknownDetected_ThrowsInvalidOperationException()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => adaptive.SearchAsync(new InterventionSearchCriteria()));
|
||||
|
||||
Assert.Contains("not been detected", ex.Message);
|
||||
Assert.Equal(0, batikConnection.QueryCount);
|
||||
Assert.Equal(0, omikaConnection.QueryCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SearchAsync_EmptyCriteria_DoesNotCrash()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Batik);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
batikConnection.InterventionRows = [];
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
PagedResult<Intervention> result = await adaptive.SearchAsync(new InterventionSearchCriteria());
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Empty(result.Items);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// DELEGATION DETAIL
|
||||
// =========================================================================
|
||||
|
||||
[Fact]
|
||||
public async Task GetDetailsAsync_BatikDetected_DelegatesToBatik()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Batik);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
batikConnection.InterventionRows = [BatikInterventionDict(42, "Dupont", "INT-001")];
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
Intervention? result = await adaptive.GetDetailsAsync("42");
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.True(batikConnection.QueryCount > 0, "Batik aurait dû exécuter des requêtes.");
|
||||
Assert.Equal(0, omikaConnection.QueryCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetDetailsAsync_OmikaDetected_DelegatesToOmika()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Omika);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
omikaConnection.InterventionRows = [OmikaInterventionDict(42, "Dupont", "INT-001")];
|
||||
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
Intervention? result = await adaptive.GetDetailsAsync("42");
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(0, batikConnection.QueryCount);
|
||||
Assert.True(omikaConnection.QueryCount > 0, "Omika aurait dû exécuter des requêtes.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetDetailsAsync_UnknownDetected_ThrowsInvalidOperationException()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => adaptive.GetDetailsAsync("42"));
|
||||
|
||||
Assert.Contains("not been detected", ex.Message);
|
||||
Assert.Equal(0, batikConnection.QueryCount);
|
||||
Assert.Equal(0, omikaConnection.QueryCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetDetailsAsync_NullFromRepository_ReturnsNull()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Batik);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
batikConnection.InterventionRows = [];
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
Intervention? result = await adaptive.GetDetailsAsync("999");
|
||||
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// PROPAGATION D'ERREURS
|
||||
// =========================================================================
|
||||
|
||||
[Fact]
|
||||
public async Task SearchAsync_BatikInfrastructureError_Propagated()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Batik);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection { ThrowOnQuery = true };
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => adaptive.SearchAsync(new InterventionSearchCriteria()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetDetailsAsync_OmikaInfrastructureError_Propagated()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Omika);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
var omikaConnection = new SpyingHfsqlConnection { ThrowOnQuery = true };
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => adaptive.GetDetailsAsync("42"));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// DI TESTS
|
||||
// =========================================================================
|
||||
|
||||
[Fact]
|
||||
public void AddOmikaData_RegistersBatikInterventionRepositoryAsScoped()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddOmikaData();
|
||||
|
||||
var descriptor = services.Single(d => d.ServiceType == typeof(BatikInterventionRepository));
|
||||
Assert.Equal(ServiceLifetime.Scoped, descriptor.Lifetime);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddOmikaData_RegistersOmikaInterventionRepositoryAsScoped()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddOmikaData();
|
||||
|
||||
var descriptor = services.Single(d => d.ServiceType == typeof(OmikaInterventionRepository));
|
||||
Assert.Equal(ServiceLifetime.Scoped, descriptor.Lifetime);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddOmikaData_RegistersIInterventionRepositoryAsScoped()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddOmikaData();
|
||||
|
||||
var descriptor = services.Single(d => d.ServiceType == typeof(IInterventionRepository));
|
||||
Assert.Equal(ServiceLifetime.Scoped, descriptor.Lifetime);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddOmikaData_IInterventionRepositoryPointsToAdaptiveInterventionRepository()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddOmikaData();
|
||||
|
||||
var descriptor = services.Single(d => d.ServiceType == typeof(IInterventionRepository));
|
||||
Assert.Equal(typeof(AdaptiveInterventionRepository), descriptor.ImplementationType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddOmikaData_OnlyOneIInterventionRepositoryRegistered()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddOmikaData();
|
||||
|
||||
var descriptors = services.Where(d => d.ServiceType == typeof(IInterventionRepository)).ToList();
|
||||
Assert.Single(descriptors);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddOmikaData_ResolvesAllInterventionRepositories_WithFakeConnection()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
|
||||
services.AddOmikaData();
|
||||
|
||||
var oldDescriptor = services.Single(d => d.ServiceType == typeof(IHfsqlConnection));
|
||||
services.Remove(oldDescriptor);
|
||||
services.AddScoped<IHfsqlConnection, MinimalFakeHfsqlConnection>();
|
||||
|
||||
var provider = services.BuildServiceProvider();
|
||||
await using var scope = provider.CreateAsyncScope();
|
||||
|
||||
var interventionRepo = scope.ServiceProvider.GetRequiredService<IInterventionRepository>();
|
||||
Assert.IsType<AdaptiveInterventionRepository>(interventionRepo);
|
||||
|
||||
var batikRepo = scope.ServiceProvider.GetRequiredService<BatikInterventionRepository>();
|
||||
Assert.NotNull(batikRepo);
|
||||
|
||||
var omikaRepo = scope.ServiceProvider.GetRequiredService<OmikaInterventionRepository>();
|
||||
Assert.NotNull(omikaRepo);
|
||||
}
|
||||
|
||||
|
||||
// =========================================================================
|
||||
// DELEGATION UPDATE
|
||||
// =========================================================================
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_BatikDetected_DelegatesToBatik()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Batik);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
batikConnection.InterventionRows = [BatikInterventionDict(42, "Dupont", "INT-001")];
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
await adaptive.UpdateAsync("42", new InterventionUpdateCommand { Titre = "Updated" },
|
||||
CancellationToken.None);
|
||||
|
||||
Assert.True(batikConnection.QueryCount > 0, "Batik aurait dû exécuter des requêtes.");
|
||||
Assert.Equal(0, omikaConnection.QueryCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_OmikaDetected_DelegatesToOmika()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Omika);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
omikaConnection.InterventionRows = [OmikaInterventionDict(42, "Dupont", "INT-001")];
|
||||
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
await adaptive.UpdateAsync("42", new InterventionUpdateCommand { Titre = "Updated" },
|
||||
CancellationToken.None);
|
||||
|
||||
Assert.Equal(0, batikConnection.QueryCount);
|
||||
Assert.True(omikaConnection.QueryCount > 0, "Omika aurait dû exécuter des requêtes.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_UnknownDetected_ThrowsInvalidOperationException()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => adaptive.UpdateAsync("42", new InterventionUpdateCommand()));
|
||||
|
||||
Assert.Contains("not been detected", ex.Message);
|
||||
Assert.Equal(0, batikConnection.QueryCount);
|
||||
Assert.Equal(0, omikaConnection.QueryCount);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// DELEGATION ARCHIVE
|
||||
// =========================================================================
|
||||
|
||||
[Fact]
|
||||
public async Task ArchiveAsync_BatikDetected_Existing_ReturnsTrue()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Batik);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
batikConnection.InterventionRows = [BatikInterventionDict(42, "Dupont", "INT-001")];
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
bool result = await adaptive.ArchiveAsync("42");
|
||||
|
||||
Assert.True(result);
|
||||
Assert.True(batikConnection.QueryCount > 0, "Batik aurait dû exécuter des requêtes.");
|
||||
Assert.Equal(0, omikaConnection.QueryCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ArchiveAsync_OmikaDetected_Existing_ReturnsTrue()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Omika);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
omikaConnection.InterventionRows = [OmikaInterventionDict(42, "Dupont", "INT-001")];
|
||||
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
bool result = await adaptive.ArchiveAsync("42");
|
||||
|
||||
Assert.True(result);
|
||||
Assert.Equal(0, batikConnection.QueryCount);
|
||||
Assert.True(omikaConnection.QueryCount > 0, "Omika aurait dû exécuter des requêtes.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ArchiveAsync_UnknownDetected_ThrowsInvalidOperationException()
|
||||
{
|
||||
ApplicationRuntimeContext.Configure(DatabaseSchemaSelection.Auto);
|
||||
|
||||
var batikConnection = new SpyingHfsqlConnection();
|
||||
var omikaConnection = new SpyingHfsqlConnection();
|
||||
var batikRepo = new BatikInterventionRepository(batikConnection);
|
||||
var omikaRepo = new OmikaInterventionRepository(omikaConnection);
|
||||
var adaptive = new AdaptiveInterventionRepository(batikRepo, omikaRepo);
|
||||
|
||||
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => adaptive.ArchiveAsync("42"));
|
||||
|
||||
Assert.Contains("not been detected", ex.Message);
|
||||
Assert.Equal(0, batikConnection.QueryCount);
|
||||
Assert.Equal(0, omikaConnection.QueryCount);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// ARCHITECTURE
|
||||
// =========================================================================
|
||||
|
||||
[Fact]
|
||||
public void Repository_ImplementsIInterventionRepository()
|
||||
{
|
||||
Assert.True(typeof(IInterventionRepository).IsAssignableFrom(typeof(AdaptiveInterventionRepository)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Repository_ContainsNoSql()
|
||||
{
|
||||
string source = ReadSourceFile(typeof(AdaptiveInterventionRepository));
|
||||
|
||||
Assert.DoesNotContain("SELECT ", source, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("FROM ", source, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("WHERE ", source, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Repository_ContainsNoOdbcTypes()
|
||||
{
|
||||
var type = typeof(AdaptiveInterventionRepository);
|
||||
var referenced = GetPublicSurfaceTypes(type);
|
||||
|
||||
Assert.DoesNotContain(referenced, t =>
|
||||
t.Namespace is not null && t.Namespace.StartsWith("System.Data.Odbc", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Repository_ContainsNoHfsqlRowReferences()
|
||||
{
|
||||
var type = typeof(AdaptiveInterventionRepository);
|
||||
var referenced = GetPublicSurfaceTypes(type);
|
||||
|
||||
Assert.DoesNotContain(referenced, t =>
|
||||
t.Name.Contains("Row", StringComparison.OrdinalIgnoreCase)
|
||||
&& t.Namespace is not null
|
||||
&& t.Namespace.Contains("HfSql", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Repository_DoesNotDependOnHfsqlConnection()
|
||||
{
|
||||
var type = typeof(AdaptiveInterventionRepository);
|
||||
|
||||
bool hasHfsqlConnectionCtor = type.GetConstructors()
|
||||
.Any(c => c.GetParameters()
|
||||
.Any(p => typeof(IHfsqlConnection).IsAssignableFrom(p.ParameterType)));
|
||||
|
||||
Assert.False(hasHfsqlConnectionCtor,
|
||||
"AdaptiveInterventionRepository ne doit pas dépendre directement de IHfsqlConnection.");
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// HELPERS
|
||||
// =========================================================================
|
||||
|
||||
private static IEnumerable<Type> GetPublicSurfaceTypes(Type type)
|
||||
{
|
||||
var members = type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
|
||||
.Select(m => m.DeclaringType)
|
||||
.Where(t => t is not null)
|
||||
.Cast<Type>();
|
||||
|
||||
var parameters = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
|
||||
.SelectMany(m => m.GetParameters())
|
||||
.Select(p => p.ParameterType);
|
||||
|
||||
var returnTypes = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
|
||||
.Select(m => m.ReturnType);
|
||||
|
||||
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
|
||||
.Select(p => p.PropertyType);
|
||||
|
||||
var ctorParams = type.GetConstructors()
|
||||
.SelectMany(c => c.GetParameters())
|
||||
.Select(p => p.ParameterType);
|
||||
|
||||
return members.Concat(parameters).Concat(returnTypes).Concat(properties).Concat(ctorParams);
|
||||
}
|
||||
|
||||
private static string ReadSourceFile(Type type)
|
||||
{
|
||||
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
|
||||
var dir = new DirectoryInfo(baseDir);
|
||||
while (dir != null && !File.Exists(Path.Combine(dir.FullName, "Omika.slnx")))
|
||||
dir = dir.Parent;
|
||||
|
||||
string root = dir?.FullName ?? baseDir;
|
||||
return File.ReadAllText(Path.Combine(root, "src", "Omika.Data", "Repositories", type.Name + ".cs"));
|
||||
}
|
||||
|
||||
// --- Dictionnaires de lignes physiques ----------------------------------
|
||||
|
||||
private static Dictionary<string, object?> BatikInterventionDict(
|
||||
long pkid, string? titre, string? reference)
|
||||
=> new()
|
||||
{
|
||||
["IN_PKID"] = pkid,
|
||||
["IN_Titre"] = titre,
|
||||
["IN_Reference"] = reference,
|
||||
["Archive"] = false
|
||||
};
|
||||
|
||||
private static Dictionary<string, object?> OmikaInterventionDict(
|
||||
long pkid, string? titre, string? reference)
|
||||
=> new()
|
||||
{
|
||||
["IN_PKID"] = pkid,
|
||||
["IN_Titre"] = titre,
|
||||
["IN_Reference"] = reference,
|
||||
["Archive"] = false,
|
||||
["GUID"] = null,
|
||||
["ModifDate"] = DateTime.MinValue,
|
||||
["CreationDate"] = DateTime.MinValue
|
||||
};
|
||||
|
||||
// =========================================================================
|
||||
// SpyingHfsqlConnection
|
||||
// =========================================================================
|
||||
|
||||
private sealed class SpyingHfsqlConnection : IHfsqlConnection
|
||||
{
|
||||
public HfsqlOptions Options => new();
|
||||
public HfsqlConnectionState State { get; private set; } = HfsqlConnectionState.Closed;
|
||||
|
||||
public int QueryCount { get; private set; }
|
||||
public bool ThrowOnQuery { get; set; }
|
||||
public string? LastSql { get; private set; }
|
||||
public IReadOnlyList<object?>? LastParameters { get; private set; }
|
||||
public CancellationToken LastCancellationToken { get; private set; }
|
||||
public IReadOnlyList<string> AllSql => _allSql;
|
||||
private readonly List<string> _allSql = [];
|
||||
|
||||
public IReadOnlyList<IReadOnlyDictionary<string, object?>> InterventionRows { get; set; } = [];
|
||||
|
||||
private void Record(string sql, IReadOnlyList<object?>? parameters, CancellationToken cancellationToken)
|
||||
{
|
||||
LastSql = sql;
|
||||
LastParameters = parameters;
|
||||
LastCancellationToken = cancellationToken;
|
||||
QueryCount++;
|
||||
_allSql.Add(sql);
|
||||
}
|
||||
|
||||
public Task OpenAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
State = HfsqlConnectionState.Open;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task CloseAsync()
|
||||
{
|
||||
State = HfsqlConnectionState.Closed;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<object?> ExecuteScalarAsync(string sql, IReadOnlyList<object?>? parameters = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Record(sql, parameters, cancellationToken);
|
||||
|
||||
if (ThrowOnQuery)
|
||||
throw new InvalidOperationException("Panne d'infrastructure simulée.");
|
||||
|
||||
return Task.FromResult<object?>(null);
|
||||
}
|
||||
|
||||
public Task<int> ExecuteNonQueryAsync(string sql, IReadOnlyList<object?>? parameters = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Record(sql, parameters, cancellationToken);
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<IReadOnlyDictionary<string, object?>>> QueryAsync(
|
||||
string sql, IReadOnlyList<object?>? parameters = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Record(sql, parameters, cancellationToken);
|
||||
|
||||
if (ThrowOnQuery)
|
||||
throw new InvalidOperationException("Panne d'infrastructure simulée.");
|
||||
|
||||
return Task.FromResult(ResolveRows(sql));
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<HfsqlColumnMetadata>> GetColumnsAsync(
|
||||
string tableName, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return Task.FromResult<IReadOnlyList<HfsqlColumnMetadata>>([]);
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
State = HfsqlConnectionState.Closed;
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
private IReadOnlyList<IReadOnlyDictionary<string, object?>> ResolveRows(string sql)
|
||||
{
|
||||
if (ContainsFrom(sql, "Intervention")) return InterventionRows;
|
||||
return [];
|
||||
}
|
||||
|
||||
private static bool ContainsFrom(string sql, string table)
|
||||
{
|
||||
string marker = $"FROM {table}";
|
||||
int index = sql.IndexOf(marker, StringComparison.OrdinalIgnoreCase);
|
||||
while (index >= 0)
|
||||
{
|
||||
int next = index + marker.Length;
|
||||
if (next >= sql.Length || (!char.IsLetterOrDigit(sql[next]) && sql[next] != '_'))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
index = sql.IndexOf(marker, index + 1, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// MinimalFakeHfsqlConnection (DI)
|
||||
// =========================================================================
|
||||
|
||||
private sealed class MinimalFakeHfsqlConnection : IHfsqlConnection
|
||||
{
|
||||
public HfsqlOptions Options => new();
|
||||
public HfsqlConnectionState State { get; private set; } = HfsqlConnectionState.Closed;
|
||||
|
||||
public Task OpenAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
State = HfsqlConnectionState.Open;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task CloseAsync()
|
||||
{
|
||||
State = HfsqlConnectionState.Closed;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<object?> ExecuteScalarAsync(string sql, IReadOnlyList<object?>? parameters = null, CancellationToken cancellationToken = default)
|
||||
=> Task.FromResult<object?>(null);
|
||||
|
||||
public Task<int> ExecuteNonQueryAsync(string sql, IReadOnlyList<object?>? parameters = null, CancellationToken cancellationToken = default)
|
||||
=> Task.FromResult(0);
|
||||
|
||||
public Task<IReadOnlyList<IReadOnlyDictionary<string, object?>>> QueryAsync(
|
||||
string sql, IReadOnlyList<object?>? parameters = null, CancellationToken cancellationToken = default)
|
||||
=> Task.FromResult<IReadOnlyList<IReadOnlyDictionary<string, object?>>>([]);
|
||||
|
||||
public Task<IReadOnlyList<HfsqlColumnMetadata>> GetColumnsAsync(
|
||||
string tableName, CancellationToken cancellationToken = default)
|
||||
=> Task.FromResult<IReadOnlyList<HfsqlColumnMetadata>>([]);
|
||||
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
State = HfsqlConnectionState.Closed;
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user