Files
scadalink-design/tests/ScadaLink.ConfigurationDatabase.Tests/ServiceCollectionExtensionsTests.cs
Joseph Doherty de839627ed feat(configdb): AuditLogRepository (EF) + DI registration, append-only with M6-deferred SwitchOutPartition (#23)
EF Core implementation of IAuditLogRepository:
- InsertIfNotExistsAsync: single IF NOT EXISTS ... INSERT via
  ExecuteSqlInterpolatedAsync, bypasses the change tracker. Enum
  values converted to string in C# (columns are varchar(32) via
  HasConversion<string>).
- QueryAsync: AsNoTracking, predicate-per-non-null-filter, keyset
  paging on (OccurredAtUtc DESC, EventId DESC) — EF Core 10
  translates Guid.CompareTo to a uniqueidentifier < comparison
  natively (verified against MSSQL 2022).
- SwitchOutPartitionAsync: throws NotSupportedException naming M6;
  the non-aligned UX_AuditLog_EventId unique index blocks
  ALTER TABLE SWITCH PARTITION until the drop-and-rebuild dance
  ships with the purge actor.

DI: AddScoped<IAuditLogRepository, AuditLogRepository>() added after
the NotificationOutboxRepository registration; existing DI smoke test
extended with an IAuditLogRepository assertion.

Integration tests (8 new) use the Bundle C MsSqlMigrationFixture and
scope by a per-test SourceSiteId guid so they neither collide nor
require cleanup.

Bundle D of the Audit Log #23 M1 Foundation plan.
2026-05-20 11:05:18 -04:00

59 lines
2.5 KiB
C#

using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using ScadaLink.Commons.Interfaces.Repositories;
using ScadaLink.Commons.Interfaces.Services;
using ScadaLink.ConfigurationDatabase;
namespace ScadaLink.ConfigurationDatabase.Tests;
public class ServiceCollectionExtensionsTests
{
[Fact]
public void AddConfigurationDatabase_WithConnectionString_RegistersRepositoriesAndServices()
{
var services = new ServiceCollection();
services.AddConfigurationDatabase("DataSource=:memory:");
Assert.Contains(services, d => d.ServiceType == typeof(ITemplateEngineRepository));
Assert.Contains(services, d => d.ServiceType == typeof(IAuditService));
Assert.Contains(services, d => d.ServiceType == typeof(IInstanceLocator));
Assert.Contains(services, d => d.ServiceType == typeof(IAuditLogRepository));
}
// The no-arg overload is [Obsolete(error: true)], so it cannot be referenced directly
// from source — that is the compile-time guard. Invoke it via reflection to verify the
// runtime defence-in-depth behaviour.
private static MethodInfo NoArgOverload =>
typeof(ServiceCollectionExtensions).GetMethod(
nameof(ServiceCollectionExtensions.AddConfigurationDatabase),
BindingFlags.Public | BindingFlags.Static,
binder: null,
types: new[] { typeof(IServiceCollection) },
modifiers: null)!;
[Fact]
public void AddConfigurationDatabase_NoArgOverload_FailsFastWithClearMessage()
{
// Regression guard for ConfigurationDatabase-003: the parameterless overload must not
// silently register nothing. Misuse must surface immediately at wire-up time with an
// actionable message — not later as an opaque DI resolution failure.
var services = new ServiceCollection();
var invocation = Assert.Throws<TargetInvocationException>(
() => NoArgOverload.Invoke(null, new object[] { services }));
var ex = Assert.IsType<InvalidOperationException>(invocation.InnerException);
Assert.Contains("connection string", ex.Message, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void AddConfigurationDatabase_NoArgOverload_IsMarkedObsoleteAsError()
{
// The no-op overload must be flagged so misuse is caught at compile time.
var obsolete = NoArgOverload.GetCustomAttribute<ObsoleteAttribute>();
Assert.NotNull(obsolete);
Assert.True(obsolete!.IsError);
}
}