using System.Data.Common; using Microsoft.EntityFrameworkCore.Diagnostics; namespace Govor.Application.Tests.Support; // Inject a provider-independent command failure after schema/seed initialization. internal sealed class FailDatabaseCommand(Func? matches = null, Func? beforeFailure = null) : DbCommandInterceptor { private readonly Func _matches = matches ?? (_ => true); public override async ValueTask> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) { if (_matches(command.CommandText)) { if (beforeFailure is not null) await beforeFailure(); throw new InvalidOperationException("Simulated database failure."); } return result; } public override async ValueTask> NonQueryExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) { if (_matches(command.CommandText)) { if (beforeFailure is not null) await beforeFailure(); throw new InvalidOperationException("Simulated database failure."); } return result; } }