Files
Govor/tests/Govor.Application.Tests/Support/FailDatabaseCommand.cs
Artemy 2d99c57a3b Expand server tests and enforce coverage
Consolidate server tests under tests/, upgrade them to .NET 10, and add CI coverage thresholds and reports. Expand API and application boundary coverage while fixing related issues in media handling, friend blocking, session crypto, presence, username validation, and local storage.
2026-10-24 15:30:32 +07:00

33 lines
1.3 KiB
C#

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<string, bool>? matches = null, Func<Task>? beforeFailure = null) : DbCommandInterceptor
{
private readonly Func<string, bool> _matches = matches ?? (_ => true);
public override async ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command,
CommandEventData eventData, InterceptionResult<DbDataReader> 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<InterceptionResult<int>> NonQueryExecutingAsync(DbCommand command,
CommandEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)
{
if (_matches(command.CommandText))
{
if (beforeFailure is not null) await beforeFailure();
throw new InvalidOperationException("Simulated database failure.");
}
return result;
}
}